Mercurial > hg-old > index.cgi
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 |
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 | 22 This file contains the actual expression evaluator which uses the LWVAL |
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 | 25 |
26 #define __expr_c_seen__ | |
15 | 27 |
28 #include <ctype.h> | |
29 #include <stdlib.h> | |
30 | |
14 | 31 #include "expr.h" |
15 | 32 #include "lwval.h" |
33 | |
34 // parse a single term out of the expression; return NULL if | |
35 // end of expression; return LWVAL_TYPE_ERR if error | |
36 /* | |
37 The following is handled by lwasm_parse_term: | |
14 | 38 |
15 | 39 - constants |
40 - parsing a symbol | |
41 - unary - | |
42 - unary + | |
43 - () | |
44 | |
45 */ | |
46 LWVAL *lwasm_parse_term(char **ptr) | |
14 | 47 { |
15 | 48 int sign = 1; |
49 int s = 0; | |
50 LWVAL *rval; | |
14 | 51 |
15 | 52 start_term: |
53 if (!**ptr || isspace(**ptr) || **ptr == ')') | |
54 return s ? lwval_construct_err(1) : NULL; | |
55 | |
56 s = 1; | |
57 // unary + - NOOP | |
58 if (**ptr == '+') | |
59 { | |
60 (*ptr)++; | |
61 goto start_term; | |
62 } | |
63 | |
64 // unary - - applied once the rest of the term is worked out | |
65 if (**ptr == '-') | |
66 { | |
67 (*ptr)++; | |
68 sign = -sign; | |
69 goto start_term; | |
70 } | |
14 | 71 |
15 | 72 // parens |
73 if (**ptr == '(') | |
74 { | |
75 LWVAL *v; | |
76 (*ptr)++; | |
77 rval = lwasm_parse_expr(ptr); | |
78 if (**ptr != ')') | |
79 { | |
80 lwval_destroy(rval); | |
81 return lwval_construct_err(1); | |
82 } | |
83 (*ptr)++; | |
84 goto ret; | |
85 } | |
86 | |
87 // parse an actual term here; no more futzing with expressions | |
88 | |
89 ret: | |
90 // apply negation if appropriate | |
91 if (sign < 0) | |
92 lwval_neg(rval); | |
93 return rval; | |
14 | 94 } |
95 | |
15 | 96 // parse an expression |
97 LWVAL *lwasm_parse_expr(char **ptr) | |
14 | 98 { |
99 } | |
100 | |
15 | 101 // attempt to evaluate/simplify expression |
102 int lwasm_eval_expr(LWVAL *expr) | |
14 | 103 { |
104 } | |
105 | |
106 |