annotate src/expr.c @ 15:1f598d89b9b0

Started creating expression parser
author lost
date Thu, 23 Oct 2008 03:37:48 +0000
parents b28d7cb60779
children df0c4a46af8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
1 /*
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
2 expr.c
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
4
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
5 This file is part of LWASM.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
6
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
10 version.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
11
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
15 more details.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
16
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
19 */
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
20
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
21 /*
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
22 This file contains the actual expression evaluator which uses the LWVAL
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
23 mechanism to store results.
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
24 */
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
25
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
26 #define __expr_c_seen__
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
27
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
28 #include <ctype.h>
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
29 #include <stdlib.h>
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
30
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
31 #include "expr.h"
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
32 #include "lwval.h"
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
33
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
34 // parse a single term out of the expression; return NULL if
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
35 // end of expression; return LWVAL_TYPE_ERR if error
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
36 /*
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
37 The following is handled by lwasm_parse_term:
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
38
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
39 - constants
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
40 - parsing a symbol
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
41 - unary -
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
42 - unary +
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
43 - ()
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
44
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
45 */
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
46 LWVAL *lwasm_parse_term(char **ptr)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
47 {
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
48 int sign = 1;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
49 int s = 0;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
50 LWVAL *rval;
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
51
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
52 start_term:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
53 if (!**ptr || isspace(**ptr) || **ptr == ')')
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
54 return s ? lwval_construct_err(1) : NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
55
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
56 s = 1;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
57 // unary + - NOOP
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
58 if (**ptr == '+')
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
59 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
60 (*ptr)++;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
61 goto start_term;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
62 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
63
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
64 // unary - - applied once the rest of the term is worked out
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
65 if (**ptr == '-')
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
66 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
67 (*ptr)++;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
68 sign = -sign;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
69 goto start_term;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
70 }
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
71
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
72 // parens
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
73 if (**ptr == '(')
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
74 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
75 LWVAL *v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
76 (*ptr)++;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
77 rval = lwasm_parse_expr(ptr);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
78 if (**ptr != ')')
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
79 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
80 lwval_destroy(rval);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
81 return lwval_construct_err(1);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
82 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
83 (*ptr)++;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
84 goto ret;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
85 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
86
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
87 // parse an actual term here; no more futzing with expressions
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
88
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
89 ret:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
90 // apply negation if appropriate
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
91 if (sign < 0)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
92 lwval_neg(rval);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
93 return rval;
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
94 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
95
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
96 // parse an expression
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
97 LWVAL *lwasm_parse_expr(char **ptr)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
98 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
99 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
100
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
101 // attempt to evaluate/simplify expression
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
102 int lwasm_eval_expr(LWVAL *expr)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
103 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
104 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
105
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
106