Mercurial > hg-old > index.cgi
annotate src/expr.c @ 17:df0c4a46af8f
Started adding expression handling infrastructure
author | lost |
---|---|
date | Thu, 01 Jan 2009 02:26:26 +0000 |
parents | 1f598d89b9b0 |
children | 218aabbc3b1a |
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 |
17 | 28 #include <stdio.h> |
15 | 29 #include <stdlib.h> |
30 | |
14 | 31 #include "expr.h" |
17 | 32 #include "util.h" |
33 | |
34 lwasm_expr_stack_t *lwasm_expr_stack_create(void) | |
35 { | |
36 lwasm_expr_stack_t *s; | |
37 | |
38 s = lwasm_alloc(sizeof(lwasm_expr_stack_t)); | |
39 s -> head = NULL; | |
40 s -> tail = NULL; | |
41 return s; | |
42 } | |
15 | 43 |
17 | 44 void lwasm_expr_stack_free(lwasm_expr_stack_t *s) |
45 { | |
46 while (s -> head) | |
47 { | |
48 s -> tail = s -> head; | |
49 s -> head = s -> head -> next; | |
50 lwasm_expr_term_free(s -> tail -> term); | |
51 lwasm_free(s -> tail); | |
52 } | |
53 lwasm_free(s); | |
54 } | |
14 | 55 |
17 | 56 void lwasm_expr_term_free(lwasm_expr_term_t *t) |
57 { | |
58 if (t) | |
59 { | |
60 if (t -> symbol) | |
61 lwasm_free(t -> symbol); | |
62 lwasm_free(t); | |
63 } | |
64 } | |
65 | |
66 lwasm_expr_term_t *lwasm_expr_term_create_oper(int oper) | |
67 { | |
68 lwasm_expr_term_t *t; | |
69 | |
70 t = lwasm_alloc(sizeof(lwasm_expr_term_t)); | |
71 t -> term_type = LWASM_TERM_OPER; | |
72 t -> value = oper; | |
73 return t; | |
74 } | |
15 | 75 |
17 | 76 lwasm_expr_term_t *lwasm_expr_term_create_int(int val) |
14 | 77 { |
17 | 78 lwasm_expr_term_t *t; |
79 | |
80 t = lwasm_alloc(sizeof(lwasm_expr_term_t)); | |
81 t -> term_type = LWASM_TERM_INT; | |
82 t -> value = val; | |
83 return t; | |
84 } | |
85 | |
86 lwasm_expr_term_t *lwasm_expr_term_create_sym(char *sym) | |
87 { | |
88 lwasm_expr_term_t *t; | |
14 | 89 |
17 | 90 t = lwasm_alloc(sizeof(lwasm_expr_term_t)); |
91 t -> term_type = LWASM_TERM_SYM; | |
92 t -> symbol = lwasm_strdup(sym); | |
93 return t; | |
94 } | |
15 | 95 |
17 | 96 lwasm_expr_term_t *lwasm_expr_term_dup(lwasm_expr_term_t *t) |
97 { | |
98 switch (t -> term_type) | |
15 | 99 { |
17 | 100 case LWASM_TERM_INT: |
101 return lwasm_expr_term_create_int(t -> value); | |
102 | |
103 case LWASM_TERM_OPER: | |
104 return lwasm_expr_term_create_oper(t -> value); | |
105 | |
106 case LWASM_TERM_SYM: | |
107 return lwasm_expr_term_create_sym(t -> symbol); | |
108 | |
109 default: | |
110 fprintf(stderr, "lwasm_expr_term_dup(): invalid term type %d\n", t -> term_type); | |
111 exit(1); | |
112 } | |
113 // can't get here | |
114 } | |
115 | |
116 void lwasm_expr_stack_push(lwasm_expr_stack_t *s, lwasm_expr_term_t *t) | |
117 { | |
118 lwasm_expr_stack_node_t *n; | |
119 | |
120 if (!s) | |
121 { | |
122 fprintf(stderr, "lwasm_expr_stack_push(): invalid stack pointer\n"); | |
123 exit(1); | |
15 | 124 } |
125 | |
17 | 126 n = lwasm_alloc(sizeof(lwasm_expr_stack_node_t)); |
127 n -> next = NULL; | |
128 n -> prev = s -> tail; | |
129 n -> term = lwasm_expr_term_dup(t); | |
130 | |
131 if (s -> head) | |
132 { | |
133 s -> tail -> next = n; | |
134 s -> tail = n; | |
135 } | |
136 else | |
15 | 137 { |
17 | 138 s -> head = n; |
139 s -> tail = n; | |
140 } | |
141 } | |
142 | |
143 lwasm_expr_term_t *lwasm_expr_stack_pop(lwasm_expr_stack_t *s) | |
144 { | |
145 lwasm_expr_term_t *t; | |
146 lwasm_expr_stack_node_t *n; | |
147 | |
148 if (!(s -> tail)) | |
149 return NULL; | |
150 | |
151 n = s -> tail; | |
152 s -> tail = n -> prev; | |
153 if (!(n -> prev)) | |
154 { | |
155 s -> head = NULL; | |
15 | 156 } |
14 | 157 |
17 | 158 t = n -> term; |
159 n -> term = NULL; | |
160 | |
161 lwasm_free(n); | |
162 | |
163 return t; | |
14 | 164 } |