From 4fae5d63a6a62b8abbcf7ff027e849447cf5dc69 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Thu, 10 Oct 2024 16:36:02 +0200 Subject: [PATCH] Datatype byte and print function --- src/builtin-functions.c | 48 ++++++++++++++++++++++++++++------------- src/builtin-functions.h | 1 + src/interpreter.c | 2 ++ src/value-content.h | 1 + src/value.c | 4 ++++ src/value.h | 1 + 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/builtin-functions.c b/src/builtin-functions.c index 6194465..2ede45d 100644 --- a/src/builtin-functions.c +++ b/src/builtin-functions.c @@ -74,6 +74,8 @@ int BuiltinFunction_Plus(CallFrame* top_frame) break; case VALUETYPE_OBJECT: return EXIT_FAILURE; + case VALUETYPE_BYTE: + return v1.get.byte + v2.get.byte; } top_frame->instruction_pointer++; @@ -109,6 +111,8 @@ int BuiltinFunction_Multiply(CallFrame* top_frame) break; case VALUETYPE_OBJECT: return EXIT_FAILURE; + case VALUETYPE_BYTE: + return v1.get.byte * v2.get.byte; } top_frame->instruction_pointer++; @@ -144,14 +148,15 @@ int BuiltinFunction_Minus(CallFrame* top_frame) break; case VALUETYPE_OBJECT: return EXIT_FAILURE; + case VALUETYPE_BYTE: + return v1.get.byte - v2.get.byte; } top_frame->instruction_pointer++; return CallFrame_StackPush(top_frame, &result); } - -int BuiltinFunction_PrintLine(CallFrame* top_frame) +int BuiltinFunction_Print(CallFrame* top_frame) { Value v1; @@ -161,34 +166,47 @@ int BuiltinFunction_PrintLine(CallFrame* top_frame) switch (v1.type) { case VALUETYPE_INT64: - printf("%" PRIi64 "\n", v1.get.i64); + printf("%" PRIi64, v1.get.i64); break; case VALUETYPE_DOUBLE: - printf("%f\n", v1.get.f64); + printf("%f", v1.get.f64); break; case VALUETYPE_BOOLEAN: if (v1.get.boolean) { - printf("true\n"); + printf("true"); } else { - printf("false\n"); + printf("false"); } break; case VALUETYPE_OBJECT: - if (v1.get.object != NULL) { - StringView type_name_view = v1.get.object->type->name; - char type_name[type_name_view.length]; - type_name[type_name_view.length] = '\0'; - StringView_Paste(type_name, type_name_view); - printf("object (%s) at %p\n", type_name, (void*) v1.get.object); + if (v1.get.object != NULL) { + StringView type_name_view = v1.get.object->type->name; + char type_name[type_name_view.length]; + type_name[type_name_view.length] = '\0'; + StringView_Paste(type_name, type_name_view); + printf("object (%s) at %p\n", type_name, (void*) v1.get.object); + } else { + puts("null"); + } + break; + case VALUETYPE_BYTE: + printf("%c", v1.get.byte); break; - } else { - puts("null"); - } } return EXIT_SUCCESS; } +int BuiltinFunction_PrintLine(CallFrame* top_frame) +{ + if (BuiltinFunction_Print(top_frame)) { + return EXIT_FAILURE; + } else { + puts(""); + return EXIT_SUCCESS; + } +} + int BuiltinFunction_Duplicate(CallFrame* top_frame) { Value v1; diff --git a/src/builtin-functions.h b/src/builtin-functions.h index fb919a0..fc62b55 100644 --- a/src/builtin-functions.h +++ b/src/builtin-functions.h @@ -28,6 +28,7 @@ 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_Print(CallFrame* top_frame); int BuiltinFunction_PrintLine(CallFrame* top_frame); int BuiltinFunction_Duplicate(CallFrame* top_frame); diff --git a/src/interpreter.c b/src/interpreter.c index f8fc08f..936bd41 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -20,12 +20,14 @@ static FlupFunctionAlternative* FlupFunctionAlternative_Malloc(size_t condition_ const char* standard_builtin_names[] = { "println", "duplicate", + "print", NULL }; const BuiltinFunction standard_builtin_functions[] = { BuiltinFunction_PrintLine, BuiltinFunction_Duplicate, + BuiltinFunction_Print, }; int Interpreter_Create(Interpreter* self, DynamicArray* tokens) diff --git a/src/value-content.h b/src/value-content.h index 8a71268..20cadf9 100644 --- a/src/value-content.h +++ b/src/value-content.h @@ -28,6 +28,7 @@ union ValueContent { int64_t i64; double f64; bool boolean; + uint8_t byte; Object* object; }; diff --git a/src/value.c b/src/value.c index 94b4eda..f0edbca 100644 --- a/src/value.c +++ b/src/value.c @@ -31,6 +31,8 @@ bool Value_IsTruthy(Value* v) return v->get.boolean; case VALUETYPE_OBJECT: return v->get.object != NULL; + case VALUETYPE_BYTE: + return v->get.byte != '\0'; } return false; @@ -48,6 +50,8 @@ bool Value_Equal(Value* v1, Value* v2) return v1->get.boolean == v2->get.boolean; case VALUETYPE_OBJECT: return v1->get.object == v2->get.object; + case VALUETYPE_BYTE: + return v1->get.byte == v2->get.byte; } return false; diff --git a/src/value.h b/src/value.h index e6a37fd..855facb 100644 --- a/src/value.h +++ b/src/value.h @@ -26,6 +26,7 @@ #include "../submodules/utilitiec/src/StringView/StringView.h" enum ValueType { + VALUETYPE_BYTE, VALUETYPE_INT64, VALUETYPE_DOUBLE, VALUETYPE_OBJECT,