comparison lwbasic/parser.c @ 34:bfea77812e64

Start of assignment code
author Lost Wizard (lost@starbug3)
date Fri, 04 Feb 2011 21:27:03 -0700
parents 890a8f688889
children cdb0175e1063
comparison
equal deleted inserted replaced
33:890a8f688889 34:bfea77812e64
28 #include <lw_alloc.h> 28 #include <lw_alloc.h>
29 #include <lw_string.h> 29 #include <lw_string.h>
30 30
31 #include "lwbasic.h" 31 #include "lwbasic.h"
32 #include "symtab.h" 32 #include "symtab.h"
33
34 static void expect(cstate *state, int tt)
35 {
36 if (state -> lexer_token != tt)
37 lwb_error("Expecting %s, got %s\n", lexer_token_name(tt), lexer_return_token(state));
38 lexer(state);
39 }
33 40
34 41
35 /* size of a type */ 42 /* size of a type */
36 static int sizeof_type(int type) 43 static int sizeof_type(int type)
37 { 44 {
96 state -> framesize += sizeof_type(vt); 103 state -> framesize += sizeof_type(vt);
97 symtab_register(state -> local_syms, vn, -(state -> framesize), symtype_var, NULL); 104 symtab_register(state -> local_syms, vn, -(state -> framesize), symtype_var, NULL);
98 105
99 lw_free(vn); 106 lw_free(vn);
100 break; 107 break;
108
109 /* blank lines allowed */
110 case token_eol:
111 break;
112
101 default: 113 default:
102 return; 114 return;
103 } 115 }
104 if (state -> lexer_token != token_eol) 116 if (state -> lexer_token != token_eol)
105 lwb_error("Expecting end of line; got %s", lexer_return_token(state)); 117 lwb_error("Expecting end of line; got %s\n", lexer_return_token(state));
118 lexer(state);
119 }
120 }
121
122 static void parse_statements(cstate *state)
123 {
124 symtab_entry_t *se;
125
126 for (;;)
127 {
128 switch (state -> lexer_token)
129 {
130 /* blank lines allowed */
131 case token_eol:
132 break;
133
134 /* variable assignment */
135 case token_identifier:
136 se = symtab_find(state -> local_syms, state -> lexer_token_string);
137 if (!se)
138 {
139 se = symtab_find(state -> global_syms, state -> lexer_token_string);
140 }
141 if (!se)
142 lwb_error("Unknown variable %s\n", state -> lexer_token_string);
143 lexer(state);
144 expect(state, token_op_assignment);
145
146 /* parse the expression */
147 /* parse_expression(state); */
148
149 /* actually do the assignment */
150
151 break;
152
153 /* anything we don't recognize as a statement token breaks out */
154 default:
155 return;
156 }
157 if (state -> lexer_token != token_eol)
158 lwb_error("Expecting end of line; got %s\n", lexer_return_token(state));
106 lexer(state); 159 lexer(state);
107 } 160 }
108 } 161 }
109 162
110 163
227 280
228 /* output function/sub prolog */ 281 /* output function/sub prolog */
229 emit_prolog(state, vis); 282 emit_prolog(state, vis);
230 283
231 /* parse statement block */ 284 /* parse statement block */
232 /* parse_statements(state); */ 285 parse_statements(state);
233 286
234 if (issub) 287 if (issub)
235 { 288 {
236 if (state -> lexer_token != token_kw_endsub) 289 if (state -> lexer_token != token_kw_endsub)
237 { 290 {