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