diff --git a/docs/opcodes.txt b/docs/opcodes.txt index c1f1e4c..f7a7826 100644 --- a/docs/opcodes.txt +++ b/docs/opcodes.txt @@ -16,8 +16,8 @@ OP_UNPACK_STRUCT OP_POP OP_GLOBAL OP_NONLOCAL -OP_LOAD -OP_STORE +OP_GET_LOCAL +OP_SET_LOCAL OP_ADD_ELEMENT OP_GET_ELEMENT OP_FETCH_ELEMENT diff --git a/examples/closure.hk b/examples/closure.hk new file mode 100644 index 0000000..076a708 --- /dev/null +++ b/examples/closure.hk @@ -0,0 +1,17 @@ +// +// closure.hk +// + +let x = 1; + +fn f(y) { + let z = 2; + fn g() { + return x + y + z; + } + return g; +} + +let g = f(3); +let result = g(); +println(result); diff --git a/include/hook/chunk.h b/include/hook/chunk.h index 24d8f08..65b93d1 100644 --- a/include/hook/chunk.h +++ b/include/hook/chunk.h @@ -15,8 +15,8 @@ typedef enum HK_OP_ARRAY, HK_OP_STRUCT, HK_OP_INSTANCE, HK_OP_CONSTRUCT, HK_OP_ITERATOR, HK_OP_CLOSURE, HK_OP_UNPACK_ARRAY, HK_OP_UNPACK_STRUCT, HK_OP_POP, - HK_OP_GLOBAL, HK_OP_NONLOCAL, HK_OP_LOAD, - HK_OP_STORE, HK_OP_ADD_ELEMENT, HK_OP_GET_ELEMENT, + HK_OP_GLOBAL, HK_OP_NONLOCAL, HK_OP_GET_LOCAL, + HK_OP_SET_LOCAL, HK_OP_ADD_ELEMENT, HK_OP_GET_ELEMENT, HK_OP_FETCH_ELEMENT, HK_OP_SET_ELEMENT, HK_OP_PUT_ELEMENT, HK_OP_DELETE_ELEMENT, HK_OP_INPLACE_ADD_ELEMENT, HK_OP_INPLACE_PUT_ELEMENT, HK_OP_INPLACE_DELETE_ELEMENT, HK_OP_GET_FIELD, HK_OP_FETCH_FIELD, diff --git a/src/compiler.c b/src/compiler.c index 90c7528..beee142 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -788,7 +788,7 @@ static void compile_assign_statement(Compiler *comp, Token *tk) if (!var.isMutable) syntax_error(fn->name, scan->file->chars, tk->line, tk->col, "cannot assign to immutable variable `%.*s`", tk->length, tk->start); - hk_chunk_emit_opcode(chunk, HK_OP_STORE); + hk_chunk_emit_opcode(chunk, HK_OP_SET_LOCAL); hk_chunk_emit_byte(chunk, var.index); } @@ -1217,10 +1217,10 @@ static void compile_del_statement(Compiler *comp) if (!var.isMutable) syntax_error(fn->name, scan->file->chars, tk.line, tk.col, "cannot delete element from immutable variable `%.*s`", tk.length, tk.start); - hk_chunk_emit_opcode(chunk, HK_OP_LOAD); + hk_chunk_emit_opcode(chunk, HK_OP_GET_LOCAL); hk_chunk_emit_byte(chunk, var.index); compile_delete(comp, true); - hk_chunk_emit_opcode(chunk, HK_OP_STORE); + hk_chunk_emit_opcode(chunk, HK_OP_SET_LOCAL); hk_chunk_emit_byte(chunk, var.index); } @@ -2187,7 +2187,7 @@ static Variable compile_variable(Compiler *comp, Token *tk, bool emit) { if (!emit) return *var; - hk_chunk_emit_opcode(chunk, var->isLocal ? HK_OP_LOAD : HK_OP_NONLOCAL); + hk_chunk_emit_opcode(chunk, var->isLocal ? HK_OP_GET_LOCAL : HK_OP_NONLOCAL); hk_chunk_emit_byte(chunk, var->index); return *var; } @@ -2224,7 +2224,7 @@ static Variable *compile_nonlocal(Compiler *comp, Token *tk) if (var->isMutable) syntax_error(fn->name, comp->scan->file->chars, tk->line, tk->col, "cannot capture mutable variable `%.*s`", tk->length, tk->start); - op = HK_OP_LOAD; + op = HK_OP_GET_LOCAL; } hk_chunk_emit_opcode(chunk, op); hk_chunk_emit_byte(chunk, var->index); diff --git a/src/dump.c b/src/dump.c index 7dd2fa8..3f0a9de 100644 --- a/src/dump.c +++ b/src/dump.c @@ -105,11 +105,11 @@ void hk_dump(HkFunction *fn, FILE *stream) case HK_OP_NONLOCAL: fprintf(stream, "NonLocal %5d\n", code[i++]); break; - case HK_OP_LOAD: - fprintf(stream, "Load %5d\n", code[i++]); + case HK_OP_GET_LOCAL: + fprintf(stream, "GetLocal %5d\n", code[i++]); break; - case HK_OP_STORE: - fprintf(stream, "Store %5d\n", code[i++]); + case HK_OP_SET_LOCAL: + fprintf(stream, "SetLocal %5d\n", code[i++]); break; case HK_OP_ADD_ELEMENT: fprintf(stream, "AddElement\n"); diff --git a/src/state.c b/src/state.c index b452590..27cd047 100644 --- a/src/state.c +++ b/src/state.c @@ -1504,7 +1504,7 @@ static inline void call_function(HkState *state, HkValue *locals, HkClosure *cl, hk_value_incr_ref(val); } break; - case HK_OP_LOAD: + case HK_OP_GET_LOCAL: { HkValue val = locals[read_byte(&pc)]; push(state, val); @@ -1513,7 +1513,7 @@ static inline void call_function(HkState *state, HkValue *locals, HkClosure *cl, hk_value_incr_ref(val); } break; - case HK_OP_STORE: + case HK_OP_SET_LOCAL: { int index = read_byte(&pc); HkValue val = slots[state->stackTop];