diff --git a/include/ground.h b/include/ground.h index 98d1092..abbdd6d 100644 --- a/include/ground.h +++ b/include/ground.h @@ -300,6 +300,9 @@ struct GroundBytecodeFunction { GroundBytecodeHeap* closure; GroundSize _requiredSize; + + // Only for native function + GroundType* returnType; }; struct GroundBytecodeStruct {}; struct GroundBytecodeObject {}; diff --git a/src/Bytecode/Instruction/execute.c b/src/Bytecode/Instruction/execute.c index 1166cd8..9c643be 100644 --- a/src/Bytecode/Instruction/execute.c +++ b/src/Bytecode/Instruction/execute.c @@ -82,14 +82,12 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns PRINT: { for (GroundSize i = 0; i < instruction->args.len; i++) { printValue(HEAP_GET(heap, instruction->args.at[i])); - printf(" "); } return CONTINUE; } PRINTLN: { for (GroundSize i = 0; i < instruction->args.len; i++) { printValue(HEAP_GET(heap, instruction->args.at[i])); - printf(" "); } printf("\n"); return CONTINUE; @@ -835,21 +833,64 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns GroundBytecodeFunction bf = { .isNativeFunction = true, .closure = NULL, - .program.native = handle, + .program.native = function, .args = { .at = malloc(sizeof(GroundFunctionArg) * argsSize), .capacity = argsSize, .count = argsSize }, + .returnType = malloc(sizeof(GroundType)) }; - if (bf.args.at == NULL) { + if (bf.args.at == NULL || bf.returnType == NULL) { Ground.Log.Error("malloc failed in Ground.Bytecode.Instruction.execute"); Ground.Flags.error = true; return CONTINUE; } - for (size_t i = 0; i < argsSize; i++) { + enum GroundTypeType returnTypeType = HEAP_GET(heap, instruction->args.at[3])->type.type; + + switch (returnTypeType) { + case GroundType_Struct: { + bf.returnType->type = GroundType_Object; + // TODO implement structs + break; + } + case GroundType_CoreType: { + bf.returnType->type = HEAP_GET(heap, instruction->args.at[3])->as.CoreType; + break; + } + default: { + // should have been caught elsewhere, not really our problem + break; + } } + + for (size_t i = 0; i < argsSize; i++) { + enum GroundTypeType argTypeType = HEAP_GET(heap, instruction->args.at[i + 4])->as.CoreType; + switch (argTypeType) { + case GroundType_Struct: { + bf.args.at[i].as.type.type = GroundType_Object; + // TODO implement structs + break; + } + case GroundType_CoreType: { + bf.args.at[i].as.type.type = HEAP_GET(heap, instruction->args.at[3])->as.CoreType; + break; + } + default: { + // should have been caught elsewhere, not really our problem + break; + } + } + } + + GroundBytecodeValue val = { + .type = {GroundType_Function}, + .as.Function = bf + }; + + HEAP_SET(heap, instruction->args.at[3], val); + return CONTINUE; } CREATELABEL: {