implemented default arguments

This commit is contained in:
cozis
2022-08-15 19:33:46 +02:00
parent 4a326bff7e
commit d9fd452b8b
11 changed files with 205 additions and 47 deletions
+26
View File
@@ -157,6 +157,11 @@ static void emitInstr_PUSHTYPTYP(CodegenContext *ctx, int off, int len)
CodegenContext_EmitInstr(ctx, OPCODE_PUSHTYPTYP, NULL, 0, off, len);
}
static void emitInstr_PUSHNNETYP(CodegenContext *ctx, int off, int len)
{
CodegenContext_EmitInstr(ctx, OPCODE_PUSHNNETYP, NULL, 0, off, len);
}
static void emitInstrForNode(CodegenContext *ctx, Node *node, Label *label_break);
static Opcode exprkind_to_opcode(ExprKind kind)
@@ -208,6 +213,14 @@ static void emitInstrForFuncCallNode(CodegenContext *ctx, CallExprNode *expr,
static void emitInstrForArgumentNode(CodegenContext *ctx, ArgumentNode *arg, int argidx)
{
/*
* // If a default value for the argument was specified
* PUSHTYP;
* PUSHNNETYP;
* EQL;
* JUMPIFNOTANDPOP default_handled;
* POP 1;
* <default-value>
* default_handled:
* // Push the type of the argument
* PUSHTYP;
*
@@ -255,6 +268,19 @@ static void emitInstrForArgumentNode(CodegenContext *ctx, ArgumentNode *arg, int
* POP 1;
*/
if(arg->value != NULL) {
/* Emit bytecode for the default argument */
Label *label_default_handled = Label_New(ctx);
emitInstr_PUSHTYP(ctx, arg->value->offset, arg->value->length);
emitInstr_PUSHNNETYP(ctx, arg->value->offset, arg->value->length);
emitInstr_EQL(ctx, arg->value->offset, arg->value->length);
emitInstr_JUMPIFNOTANDPOP(ctx, label_default_handled, arg->value->offset, arg->value->length);
emitInstr_POP1(ctx, arg->value->offset, arg->value->length);
emitInstrForNode(ctx, arg->value, NULL);
Label_SetHere(label_default_handled, ctx);
Label_Free(label_default_handled);
}
if(arg->typev != NULL) {
/* Emit checks for the argument type */