aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/compiler.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-27 10:43:30 +0200
committerBad Diode <bd@badd10de.dev>2021-10-27 10:43:30 +0200
commitf0cd7a3cab56a6f8d7b4520aaa168a271a94d6d4 (patch)
tree70a5e81b14f0fe65c959d675909ee3aa471f8354 /src/bytecode/compiler.h
parent6eed0aa02c657ae97e18280a6dd9d74c84fd91d4 (diff)
downloadbdl-f0cd7a3cab56a6f8d7b4520aaa168a271a94d6d4.tar.gz
bdl-f0cd7a3cab56a6f8d7b4520aaa168a271a94d6d4.zip
Add initial implementation of locals
Diffstat (limited to 'src/bytecode/compiler.h')
-rwxr-xr-xsrc/bytecode/compiler.h48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h
index 6a17beb..7497ea7 100755
--- a/src/bytecode/compiler.h
+++ b/src/bytecode/compiler.h
@@ -34,8 +34,8 @@ has_next_token(const Compiler *compiler) {
34ssize_t 34ssize_t
35find_local_index(Chunk *chunk, Token tok) { 35find_local_index(Chunk *chunk, Token tok) {
36 // NOTE: This is dumb and potentially slow. 36 // NOTE: This is dumb and potentially slow.
37 for (size_t i = 0; i < array_size(chunk->params); i++) { 37 for (size_t i = 0; i < array_size(chunk->locals); i++) {
38 if (sv_equal(&tok.value, &chunk->params[i])) { 38 if (sv_equal(&tok.value, &chunk->locals[i])) {
39 return i; 39 return i;
40 } 40 }
41 } 41 }
@@ -203,9 +203,11 @@ compile_list_simple_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) {
203 }); 203 });
204} 204}
205 205
206#define STR_ARRAY(ARR) (StringView){(ARR), array_size(ARR)}
207
206void 208void
207compile_declare_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) { 209compile_declare_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) {
208 Token name = peek_token(compiler); 210 Token name = next_token(compiler);
209 if (name.type != TOKEN_SYMBOL) { 211 if (name.type != TOKEN_SYMBOL) {
210 error_push((Error){ 212 error_push((Error){
211 .type = ERR_TYPE_COMPILER, 213 .type = ERR_TYPE_COMPILER,
@@ -215,9 +217,29 @@ compile_declare_op(Chunk *chunk, Compiler *compiler, Token start, Ops op) {
215 }); 217 });
216 return; 218 return;
217 } 219 }
218 Object obj = make_symbol(name.value); 220 // TODO: If we are inside a function and we are using OP_DEF, we just
219 emit_constant(chunk, name, obj); 221 // declare the local variable directly in the stack. No need for symbols!
220 next_token(compiler); 222 // TODO: If we are inside a function and we are using OP_SET, check if the
223 // local variable has been defined before, if not try to read from the
224 // globals for setting.
225 if (sv_equal(&STRING(""), &STR_ARRAY(chunk->name))) {
226 Object obj = make_symbol(name.value);
227 emit_constant(chunk, name, obj);
228 } else {
229 // TODO: only do this if we are defining! not setting.
230 // Check if we already have the local
231 ssize_t idx = find_local_index(chunk, name);
232 if (idx < 0) {
233 array_push(chunk->locals, name.value);
234 idx = array_size(chunk->locals) - 1;
235 }
236 if (op == OP_DEF) {
237 op = OP_DEF_LOCAL;
238 } else if (op == OP_SET) {
239 op = OP_SET_LOCAL;
240 }
241 emit_constant(chunk, name, FIXNUM_VAL(idx));
242 }
221 243
222 Token tok = peek_token(compiler); 244 Token tok = peek_token(compiler);
223 if (name.type == TOKEN_EOF || tok.type == TOKEN_EOF) { 245 if (name.type == TOKEN_EOF || tok.type == TOKEN_EOF) {
@@ -282,7 +304,19 @@ compile_lambda(Chunk *chunk, Compiler *compiler, Token start, StringView name) {
282 }); 304 });
283 return; 305 return;
284 } 306 }
307 // Check if parameters name already exists.
308 ssize_t idx = find_local_index(fun.chunk, tok);
309 if (idx >= 0) {
310 error_push((Error){
311 .type = ERR_TYPE_COMPILER,
312 .value = ERR_AMBIGUOUS_PARAMS,
313 .line = tok.line,
314 .col = tok.column,
315 });
316 return;
317 }
285 array_push(fun.chunk->params, tok.value); 318 array_push(fun.chunk->params, tok.value);
319 array_push(fun.chunk->locals, tok.value);
286 } 320 }
287 } else if (tok.type != TOKEN_NIL) { 321 } else if (tok.type != TOKEN_NIL) {
288 error_push((Error){ 322 error_push((Error){
@@ -549,7 +583,7 @@ parse_tree(Chunk *chunk, Compiler *compiler) {
549Chunk * 583Chunk *
550compile(Token *tokens) { 584compile(Token *tokens) {
551 Chunk *chunk = NULL; 585 Chunk *chunk = NULL;
552 chunk = NEW_CHUNK("main"); 586 chunk = NEW_CHUNK("");
553 Compiler compiler = (Compiler){ 587 Compiler compiler = (Compiler){
554 .tokens = tokens, 588 .tokens = tokens,
555 .current = 0, 589 .current = 0,