Skip to content

Commit

Permalink
added closure.hk example
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosvm committed Aug 18, 2023
1 parent aa4c8b1 commit 7f816fa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/opcodes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions examples/closure.hk
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions include/hook/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];
Expand Down

0 comments on commit 7f816fa

Please sign in to comment.