Mercurial > hg > index.cgi
comparison lwbasic/compiler.c @ 26:26aa76da75ad
Additional parsing in function/sub; emission of prolog/epilog code
author | lost@l-w.ca |
---|---|
date | Thu, 27 Jan 2011 20:44:57 -0700 |
parents | 87590f43e76d |
children | 77626fc37af2 |
comparison
equal
deleted
inserted
replaced
25:87590f43e76d | 26:26aa76da75ad |
---|---|
22 /* | 22 /* |
23 This is the actual compiler bit; it drives the parser and code generation | 23 This is the actual compiler bit; it drives the parser and code generation |
24 */ | 24 */ |
25 | 25 |
26 #include <stdio.h> | 26 #include <stdio.h> |
27 | |
28 #include <lw_alloc.h> | |
29 #include <lw_string.h> | |
27 | 30 |
28 #include "lwbasic.h" | 31 #include "lwbasic.h" |
29 | 32 |
30 /* parse a type; the next token will be acquired as a result */ | 33 /* parse a type; the next token will be acquired as a result */ |
31 /* the token advancement is to provide consistency */ | 34 /* the token advancement is to provide consistency */ |
50 | 53 |
51 /* issub means RETURNS is not allowed; !issub means RETURNS is required */ | 54 /* issub means RETURNS is not allowed; !issub means RETURNS is required */ |
52 static void parse_subfunc(cstate *state, int issub) | 55 static void parse_subfunc(cstate *state, int issub) |
53 { | 56 { |
54 int pt; | 57 int pt; |
55 | 58 char *subname; |
59 int vis = 0; | |
60 | |
56 lexer(state); | 61 lexer(state); |
57 if (state -> lexer_token != token_identifier) | 62 if (state -> lexer_token != token_identifier) |
58 { | 63 { |
59 lwb_error("Invalid sub name '%s'", state -> lexer_token_string); | 64 lwb_error("Invalid sub name '%s'", state -> lexer_token_string); |
60 } | 65 } |
61 | 66 |
62 printf("<name> = %s\n", state -> lexer_token_string); | 67 subname = lw_strdup(state -> lexer_token_string); |
63 | 68 |
64 lexer(state); | 69 lexer(state); |
65 if (state -> lexer_token == token_kw_public || state -> lexer_token == token_kw_private) | 70 if (state -> lexer_token == token_kw_public || state -> lexer_token == token_kw_private) |
66 { | 71 { |
67 printf("<type> = %s\n", state -> lexer_token_string); | 72 if (state -> lexer_token == token_kw_public) |
73 vis = 1; | |
68 lexer(state); | 74 lexer(state); |
69 } | 75 } |
70 | 76 |
71 /* ignore the "PARAMS" keyword if present */ | 77 /* ignore the "PARAMS" keyword if present */ |
72 if (state -> lexer_token == token_kw_params) | 78 if (state -> lexer_token == token_kw_params) |
73 lexer(state); | 79 lexer(state); |
74 | 80 |
75 if (state -> lexer_token == token_eol) | 81 if (state -> lexer_token == token_eol || state -> lexer_token == token_kw_returns) |
76 goto noparms; | 82 goto noparms; |
77 | 83 |
78 paramagain: | 84 paramagain: |
79 if (state -> lexer_token != token_identifier) | 85 if (state -> lexer_token != token_identifier) |
80 { | 86 { |
81 lwb_error("Parameter name expected, get %d, %s\n", state -> lexer_token, state -> lexer_token_string); | 87 lwb_error("Parameter name expected, got %d, %s\n", state -> lexer_token, state -> lexer_token_string); |
82 } | 88 } |
83 printf("Got <param> = %s\n", state -> lexer_token_string); | 89 printf("Got <param> = %s\n", state -> lexer_token_string); |
84 lexer(state); | 90 lexer(state); |
85 | 91 |
86 if (state -> lexer_token != token_kw_as) | 92 if (state -> lexer_token != token_kw_as) |
128 | 134 |
129 if (state -> lexer_token != token_eol) | 135 if (state -> lexer_token != token_eol) |
130 { | 136 { |
131 lwb_error("EOL expected; found %d, %s\n", state -> lexer_token, state -> lexer_token_string); | 137 lwb_error("EOL expected; found %d, %s\n", state -> lexer_token, state -> lexer_token_string); |
132 } | 138 } |
139 | |
140 | |
141 printf("Sub/Func %s, vis %s\n", subname, vis ? "public" : "private"); | |
142 | |
143 state -> currentsub = subname; | |
144 | |
145 /* consume EOL */ | |
146 lexer(state); | |
147 | |
148 /* variable declarations */ | |
149 /* parse_decls(state); */ | |
150 | |
151 /* output function/sub prolog */ | |
152 emit_prolog(state, vis, 0); | |
153 | |
154 /* parse statement block */ | |
155 /* parse_statemetns(state); */ | |
156 | |
157 if (issub) | |
158 { | |
159 if (state -> lexer_token != token_kw_endsub) | |
160 { | |
161 lwb_error("Expecting ENDSUB, got %d (%s)\n", state -> lexer_token, state -> lexer_token_string); | |
162 } | |
163 } | |
164 else | |
165 { | |
166 if (state -> lexer_token != token_kw_endfunction) | |
167 { | |
168 lwb_error("Expecting ENDFUNCTION, got %d (%s)\n", state -> lexer_token, state -> lexer_token_string); | |
169 } | |
170 } | |
171 /* output function/sub epilog */ | |
172 emit_epilog(state); | |
173 | |
174 lw_free(state -> currentsub); | |
175 state -> currentsub = NULL; | |
176 | |
133 } | 177 } |
134 | 178 |
135 void compiler(cstate *state) | 179 void compiler(cstate *state) |
136 { | 180 { |
137 state -> lexer_curchar = -1; | 181 state -> lexer_curchar = -1; |