Skip to content

Commit ab6f5bd

Browse files
committed
chore: finish super calls and optimize them
1 parent dd7c5ca commit ab6f5bd

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/chunk.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef enum
3535
OP_LOOP,
3636
OP_CALL,
3737
OP_INVOKE,
38+
OP_SUPER_INVOKE,
3839
OP_CLOSURE,
3940
OP_CLOSE_UPVALUE,
4041
OP_RETURN,

src/compiler.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,18 @@ static void super_(bool can_assign)
663663
u8 name = identifier_constant(&parser.previous);
664664

665665
named_variable(synthetic_token("this"), false);
666-
named_variable(synthetic_token("super"), false);
667-
emit_bytes(OP_GET_SUPER, name);
666+
if (match(TOKEN_LEFT_PAREN))
667+
{
668+
u8 arg_count = arguments_list();
669+
named_variable(synthetic_token("super"), false);
670+
emit_bytes(OP_SUPER_INVOKE, name);
671+
emit_byte(arg_count);
672+
}
673+
else
674+
{
675+
named_variable(synthetic_token("super"), false);
676+
emit_bytes(OP_GET_SUPER, name);
677+
}
668678
}
669679

670680
static void this_(bool can_assign)

src/debug.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ int disassemble_instruction(Chunk* chunk, int offset)
6262
return constant_instruction("OP_GET_PROPERTY", chunk, offset);
6363
case OP_SET_PROPERTY:
6464
return constant_instruction("OP_SET_PROPERTY", chunk, offset);
65+
case OP_GET_SUPER:
66+
return constant_instruction("OP_GET_SUPER", chunk, offset);
6567
case OP_EQUAL:
6668
return simple_instruction("OP_EQUAL", offset);
6769
case OP_GREATER:
@@ -92,6 +94,8 @@ int disassemble_instruction(Chunk* chunk, int offset)
9294
return byte_instruction("OP_CALL", chunk, offset);
9395
case OP_INVOKE:
9496
return invoke_instruction("OP_INVOKE", chunk, offset);
97+
case OP_SUPER_INVOKE:
98+
return invoke_instruction("OP_SUPER_INVOKE", chunk, offset);
9599
case OP_CLOSURE:
96100
{
97101
u8 constant = chunk->code[++offset];

src/vm.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,14 @@ static InterpretResult run()
436436
push(value);
437437
break;
438438
}
439+
case OP_GET_SUPER:
440+
{
441+
ObjString* name = READ_STRING();
442+
ObjClass* super_class = AS_CLASS(pop());
443+
if (!bind_method(super_class, name))
444+
return INTERPRET_RUNTIME_ERROR;
445+
break;
446+
}
439447
case OP_EQUAL:
440448
{
441449
Value v2 = pop();
@@ -533,6 +541,18 @@ static InterpretResult run()
533541
frame = &vm.frames[vm.frame_count - 1];
534542
break;
535543
}
544+
case OP_SUPER_INVOKE:
545+
{
546+
ObjString* method = READ_STRING();
547+
int arg_count = READ_BYTE();
548+
ObjClass* super_class = AS_CLASS(pop());
549+
550+
if (!invoke_from_class(super_class, method, arg_count))
551+
return INTERPRET_RUNTIME_ERROR;
552+
553+
frame = &vm.frames[vm.frame_count - 1];
554+
break;
555+
}
536556
case OP_CLOSURE:
537557
{
538558
ObjFunction* function = AS_FUNCTION(READ_CONSTANT());
@@ -577,6 +597,7 @@ static InterpretResult run()
577597
push(OBJ_VAL(new_class(READ_STRING())));
578598
break;
579599
case OP_INHERIT:
600+
{
580601
Value super_class = peek(1);
581602

582603
if (!IS_CLASS(super_class))
@@ -588,6 +609,7 @@ static InterpretResult run()
588609
table_add_all(&AS_CLASS(super_class)->methods, &sub_class->methods);
589610
pop(); // sub_class
590611
break;
612+
}
591613
case OP_METHOD:
592614
define_method(READ_STRING());
593615
break;

0 commit comments

Comments
 (0)