Portable print function using inttypes, multiplication

This commit is contained in:
VegOwOtenks 2024-10-08 23:24:25 +02:00
parent a663ce2044
commit aec860a6b8
4 changed files with 48 additions and 10 deletions

View file

@ -23,6 +23,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <inttypes.h>
int BuiltinFunction_Equality(CallFrame* top_frame) int BuiltinFunction_Equality(CallFrame* top_frame)
{ {
@ -77,6 +78,39 @@ int BuiltinFunction_Plus(CallFrame* top_frame)
return CallFrame_StackPush(top_frame, &result); 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) int BuiltinFunction_Minus(CallFrame* top_frame)
{ {
Value v1; Value v1;
@ -121,7 +155,7 @@ int BuiltinFunction_PrintLine(CallFrame* top_frame)
switch (v1.type) { switch (v1.type) {
case VALUETYPE_INT64: case VALUETYPE_INT64:
printf("%i\n", v1.get.i64); printf("%" PRIi64 "\n", v1.get.i64);
break; break;
case VALUETYPE_DOUBLE: case VALUETYPE_DOUBLE:
printf("%f\n", v1.get.f64); printf("%f\n", v1.get.f64);

View file

@ -26,6 +26,7 @@
int BuiltinFunction_Minus(CallFrame* top_frame); int BuiltinFunction_Minus(CallFrame* top_frame);
int BuiltinFunction_Plus(CallFrame* top_frame); int BuiltinFunction_Plus(CallFrame* top_frame);
int BuiltinFunction_Multiply(CallFrame* top_frame);
int BuiltinFunction_Equality(CallFrame* top_frame); int BuiltinFunction_Equality(CallFrame* top_frame);
int BuiltinFunction_PrintLine(CallFrame* top_frame); int BuiltinFunction_PrintLine(CallFrame* top_frame);
int BuiltinFunction_Duplicate(CallFrame* top_frame); int BuiltinFunction_Duplicate(CallFrame* top_frame);

View file

@ -360,6 +360,11 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token)
BuiltinFunction_Minus(top_frame); BuiltinFunction_Minus(top_frame);
break; break;
} }
case TOKENTYPE_MULTIPLY:
{
BuiltinFunction_Multiply(top_frame);
break;
}
case TOKENTYPE_BIND: case TOKENTYPE_BIND:
{ {
@ -478,7 +483,6 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token)
case TOKENTYPE_LEFT_BRACE: case TOKENTYPE_LEFT_BRACE:
case TOKENTYPE_RIGHT_BRACE: case TOKENTYPE_RIGHT_BRACE:
case TOKENTYPE_AMPERSAND: case TOKENTYPE_AMPERSAND:
case TOKENTYPE_MULTIPLY:
case TOKENTYPE_DIVIDE: case TOKENTYPE_DIVIDE:
case TOKENTYPE_ARROW: case TOKENTYPE_ARROW:
case TOKENTYPE_COLON: case TOKENTYPE_COLON:
@ -490,7 +494,7 @@ int Interpreter_ExecuteNext(Interpreter* self, size_t stop_token)
case TOKENTYPE_COMMA: case TOKENTYPE_COMMA:
case TOKENTYPE_LEFT_PAREN: case TOKENTYPE_LEFT_PAREN:
case TOKENTYPE_RIGHT_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++; top_frame->instruction_pointer++;
break; break;

View file

@ -56,10 +56,10 @@ char* load_file_string(StringView path)
return NULL; return NULL;
} }
if (fseek(stream, 0, SEEK_SET)) { if (fseek(stream, 0, SEEK_SET)) {
perror("fseek"); perror("fseek");
return NULL; return NULL;
} }
char* buffer = malloc(length + 1); char* buffer = malloc(length + 1);
if (buffer == NULL) { if (buffer == NULL) {
@ -68,9 +68,8 @@ char* load_file_string(StringView path)
} }
size_t objects_read = fread(buffer, 1, length, stream); 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); fprintf(stderr, "Fatal Error: Failed read %li bytes from script file, got only %li\n", length, objects_read);
;
free(buffer); free(buffer);
return NULL; return NULL;
} }