Commit | Line | Data |
---|---|---|
b0c08430 | 1 | #ifndef lint |
0180c2d9 | 2 | static char *sccsid = "@(#)parser1.c 3.12 84/04/05"; |
b0c08430 EW |
3 | #endif |
4 | ||
a7b2e18a | 5 | #include "parser.h" |
b0c08430 EW |
6 | |
7 | p_start() | |
8 | { | |
9 | char flag = 1; | |
10 | ||
11 | (void) s_gettok(); | |
12 | for (;;) { | |
13 | p_statementlist(flag); | |
14 | if (token == T_EOF || p_abort()) | |
15 | break; | |
16 | flag = 0; | |
17 | p_synerror(); | |
18 | while (token != T_EOL && token != T_EOF) { | |
19 | if (token == T_STR) | |
20 | str_free(token_str); | |
21 | (void) s_gettok(); | |
22 | } | |
23 | if (token == T_EOL) | |
24 | (void) s_gettok(); | |
25 | p_clearerr(); | |
26 | } | |
27 | } | |
28 | ||
29 | p_statementlist(flag) | |
30 | char flag; | |
31 | { | |
63f7da95 EW |
32 | for (; p_statement(flag) >= 0; p_clearerr()) |
33 | ; | |
b0c08430 EW |
34 | } |
35 | ||
36 | p_statement(flag) | |
37 | char flag; | |
38 | { | |
39 | #ifdef DEBUG | |
40 | error("statement: %d.", flag); | |
41 | #endif | |
42 | switch (token) { | |
43 | case T_EOL: | |
44 | #ifdef DEBUG | |
45 | error("statement: EOL.", flag); | |
46 | #endif | |
47 | (void) s_gettok(); | |
48 | return 0; | |
49 | case T_IF: | |
50 | #ifdef DEBUG | |
51 | error("statement: IF.", flag); | |
52 | #endif | |
53 | return p_if(flag); | |
54 | default: | |
55 | #ifdef DEBUG | |
56 | error("statement: command.", flag); | |
57 | #endif | |
58 | return p_command(flag); | |
59 | } | |
60 | } | |
61 | ||
62 | p_if(flag) | |
63 | char flag; | |
64 | { | |
65 | struct value t; | |
66 | char true = 0; | |
67 | ||
68 | top: | |
69 | (void) s_gettok(); | |
70 | ||
71 | if (p_expr(&t, flag) < 0) { | |
72 | p_synerror(); | |
73 | return -1; | |
74 | } | |
75 | switch (t.v_type) { | |
76 | case V_NUM: | |
77 | true = !true && t.v_num != 0; | |
78 | break; | |
79 | case V_STR: | |
80 | p_error("Numeric value required for if."); | |
81 | str_free(t.v_str); | |
82 | case V_ERR: | |
83 | flag = 0; | |
84 | break; | |
85 | } | |
86 | ||
87 | if (token != T_THEN) { | |
88 | p_synerror(); | |
89 | return -1; | |
90 | } | |
91 | ||
92 | (void) s_gettok(); | |
93 | p_statementlist(flag && true); | |
94 | if (p_erred()) | |
95 | return -1; | |
96 | ||
97 | if (token == T_ELSIF) | |
98 | goto top; | |
99 | ||
100 | if (token == T_ELSE) { | |
101 | (void) s_gettok(); | |
102 | p_statementlist(flag && !true); | |
103 | if (p_erred()) | |
104 | return -1; | |
105 | } | |
106 | ||
107 | if (token == T_ENDIF) { | |
108 | (void) s_gettok(); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | p_synerror(); | |
113 | return -1; | |
114 | } | |
115 | ||
116 | p_command(flag) | |
117 | char flag; | |
118 | { | |
119 | struct value t; | |
120 | char *cmd; | |
121 | ||
122 | #ifdef DEBUG | |
123 | error("command: %d.", flag); | |
124 | #endif | |
125 | switch (token) { | |
126 | case T_MOD: | |
127 | t.v_type = V_STR; | |
803a666e | 128 | t.v_str = str_cpy("%"); |
b0c08430 EW |
129 | (void) s_gettok(); |
130 | break; | |
131 | case T_NUM: | |
132 | t.v_type = V_NUM; | |
133 | t.v_num = token_num; | |
134 | (void) s_gettok(); | |
135 | break; | |
136 | case T_STR: | |
137 | t.v_type = V_STR; | |
138 | t.v_str = token_str; | |
139 | (void) s_gettok(); | |
140 | break; | |
141 | default: | |
142 | if (p_expr(&t, flag) < 0) | |
143 | return -1; | |
144 | if (token == T_EOF) { | |
145 | #ifdef DEBUG | |
146 | error("command: expression."); | |
147 | #endif | |
474b372d | 148 | val_free(t); |
b0c08430 EW |
149 | return 0; |
150 | } | |
151 | } | |
152 | switch (t.v_type) { | |
153 | case V_ERR: | |
154 | cmd = 0; | |
155 | break; | |
156 | case V_STR: | |
157 | cmd = t.v_str; | |
158 | break; | |
159 | case V_NUM: | |
160 | if ((cmd = str_itoa(t.v_num)) == 0) { | |
161 | p_memerror(); | |
162 | return -1; | |
163 | } | |
164 | } | |
165 | if (token == T_ASSIGN) { | |
166 | #ifdef DEBUG | |
167 | error("command: assignment %s.", cmd == 0 ? "ERR" : cmd); | |
168 | #endif | |
169 | if (p_assign(cmd, &t, flag) < 0) { | |
170 | str_free(cmd); | |
171 | return -1; | |
172 | } | |
173 | } else { | |
174 | #ifdef DEBUG | |
175 | error("command: function %s.", cmd == 0 ? "ERR" : cmd); | |
176 | #endif | |
177 | if (p_function(cmd, &t, flag) < 0) { | |
178 | str_free(cmd); | |
179 | return -1; | |
180 | } | |
181 | } | |
182 | str_free(cmd); | |
474b372d | 183 | val_free(t); |
b0c08430 EW |
184 | if (token == T_EOL) |
185 | (void) s_gettok(); | |
186 | else if (token != T_EOF) { | |
187 | p_synerror(); | |
188 | return -1; | |
189 | } | |
190 | return 0; | |
191 | } | |
192 | ||
0180c2d9 EW |
193 | p_convstr(v) |
194 | register struct value *v; | |
195 | { | |
196 | if (v->v_type != V_NUM) | |
197 | return 0; | |
198 | if ((v->v_str = str_itoa(v->v_num)) == 0) { | |
199 | p_memerror(); | |
200 | v->v_type = V_ERR; | |
201 | return -1; | |
202 | } | |
203 | v->v_type = V_STR; | |
204 | return 0; | |
205 | } | |
206 | ||
b0c08430 EW |
207 | p_synerror() |
208 | { | |
209 | if (!cx.x_synerred) { | |
210 | cx.x_synerred = cx.x_erred = 1; | |
211 | error("Syntax error."); | |
212 | } | |
213 | } | |
214 | ||
215 | /*VARARGS1*/ | |
216 | p_error(msg, a, b, c) | |
217 | char *msg; | |
218 | { | |
219 | if (!cx.x_erred) { | |
220 | cx.x_erred = 1; | |
221 | error(msg, a, b, c); | |
222 | } | |
223 | } | |
224 | ||
225 | p_memerror() | |
226 | { | |
227 | cx.x_erred = cx.x_abort = 1; | |
228 | error("Out of memory."); | |
229 | } |