diff --git a/src/builtin-functions.c b/src/builtin-functions.c index 119e75b..adb3f0e 100644 --- a/src/builtin-functions.c +++ b/src/builtin-functions.c @@ -23,6 +23,7 @@ #include #include +#include int BuiltinFunction_Equality(CallFrame* top_frame) { @@ -77,6 +78,39 @@ int BuiltinFunction_Plus(CallFrame* top_frame) return CallFrame_StackPush(top_frame, &result); } +int BuiltinFunction_Multiply(CallFrame* top_frame) +{ + Value v1; + Value v2; + + CallFrame_StackPop(top_frame, &v1); + if (CallFrame_StackPop(top_frame, &v2) != EXIT_SUCCESS) { + // TODO: Error message + return EXIT_FAILURE; + } + if (v1.type != v2.type) { + // TODO: Error message + return EXIT_FAILURE; + } + + Value result; + result.type = v1.type; + switch (v1.type) { + case VALUETYPE_INT64: + result.get.i64 = v2.get.i64 * v1.get.i64; + break; + case VALUETYPE_DOUBLE: + result.get.f64 = v2.get.f64 * v1.get.f64; + break; + case VALUETYPE_BOOLEAN: + result.get.boolean = v2.get.boolean * v1.get.boolean; + break; + } + + top_frame->instruction_pointer++; + return CallFrame_StackPush(top_frame, &result); +} + int BuiltinFunction_Minus(CallFrame* top_frame) { Value v1; @@ -121,7 +155,7 @@ int BuiltinFunction_PrintLine(CallFrame* top_frame) switch (v1.type) { case VALUETYPE_INT64: - printf("%i\n", v1.get.i64); + printf("%" PRIi64 "\n", v1.get.i64); break; case VALUETYPE_DOUBLE: printf("%f\n", v1.get.f64); diff --git a/src/builtin-functions.h b/src/builtin-functions.h index cd690a3..fb919a0 100644 --- a/src/builtin-functions.h +++ b/src/builtin-functions.h @@ -26,9 +26,10 @@ int BuiltinFunction_Minus(CallFrame* top_frame); int BuiltinFunction_Plus(CallFrame* top_frame); +int BuiltinFunction_Multiply(CallFrame* top_frame); int BuiltinFunction_Equality(CallFrame* top_frame); int BuiltinFunction_PrintLine(CallFrame* top_frame); int BuiltinFunction_Duplicate(CallFrame* top_frame); -#endif \ No newline at end of file +#endif diff --git a/src/interpreter.c b/src/interpreter.c index 1630364..d8c9d01 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -360,6 +360,11 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token) BuiltinFunction_Minus(top_frame); break; } + case TOKENTYPE_MULTIPLY: + { + BuiltinFunction_Multiply(top_frame); + break; + } case TOKENTYPE_BIND: { @@ -478,7 +483,6 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token) case TOKENTYPE_LEFT_BRACE: case TOKENTYPE_RIGHT_BRACE: case TOKENTYPE_AMPERSAND: - case TOKENTYPE_MULTIPLY: case TOKENTYPE_DIVIDE: case TOKENTYPE_ARROW: case TOKENTYPE_COLON: @@ -490,7 +494,7 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token) case TOKENTYPE_COMMA: case TOKENTYPE_LEFT_PAREN: case TOKENTYPE_RIGHT_PAREN: - fprintf(stderr, "Unexpected token with type: %s\n, continuing with next token...\n", TokenType_ToString(t->type)); + fprintf(stderr, "Unexpected token with type: %s, continuing with next token...\n", TokenType_ToString(t->type)); top_frame->instruction_pointer++; break; diff --git a/src/main.c b/src/main.c index 4e9bba1..3d83bea 100644 --- a/src/main.c +++ b/src/main.c @@ -56,10 +56,10 @@ char* load_file_string(StringView path) return NULL; } - if (fseek(stream, 0, SEEK_SET)) { - perror("fseek"); - return NULL; - } + if (fseek(stream, 0, SEEK_SET)) { + perror("fseek"); + return NULL; + } char* buffer = malloc(length + 1); if (buffer == NULL) { @@ -68,9 +68,8 @@ char* load_file_string(StringView path) } size_t objects_read = fread(buffer, 1, length, stream); - if (objects_read != length) { + if (objects_read != (size_t) length) { fprintf(stderr, "Fatal Error: Failed read %li bytes from script file, got only %li\n", length, objects_read); - ; free(buffer); return NULL; }