Datatype byte and print function

This commit is contained in:
VegOwOtenks 2024-10-10 16:36:02 +02:00
parent 3f05fa5141
commit 4fae5d63a6
6 changed files with 42 additions and 15 deletions

View file

@ -74,6 +74,8 @@ int BuiltinFunction_Plus(CallFrame* top_frame)
break; break;
case VALUETYPE_OBJECT: case VALUETYPE_OBJECT:
return EXIT_FAILURE; return EXIT_FAILURE;
case VALUETYPE_BYTE:
return v1.get.byte + v2.get.byte;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
@ -109,6 +111,8 @@ int BuiltinFunction_Multiply(CallFrame* top_frame)
break; break;
case VALUETYPE_OBJECT: case VALUETYPE_OBJECT:
return EXIT_FAILURE; return EXIT_FAILURE;
case VALUETYPE_BYTE:
return v1.get.byte * v2.get.byte;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
@ -144,14 +148,15 @@ int BuiltinFunction_Minus(CallFrame* top_frame)
break; break;
case VALUETYPE_OBJECT: case VALUETYPE_OBJECT:
return EXIT_FAILURE; return EXIT_FAILURE;
case VALUETYPE_BYTE:
return v1.get.byte - v2.get.byte;
} }
top_frame->instruction_pointer++; top_frame->instruction_pointer++;
return CallFrame_StackPush(top_frame, &result); return CallFrame_StackPush(top_frame, &result);
} }
int BuiltinFunction_Print(CallFrame* top_frame)
int BuiltinFunction_PrintLine(CallFrame* top_frame)
{ {
Value v1; Value v1;
@ -161,34 +166,47 @@ int BuiltinFunction_PrintLine(CallFrame* top_frame)
switch (v1.type) { switch (v1.type) {
case VALUETYPE_INT64: case VALUETYPE_INT64:
printf("%" PRIi64 "\n", v1.get.i64); printf("%" PRIi64, v1.get.i64);
break; break;
case VALUETYPE_DOUBLE: case VALUETYPE_DOUBLE:
printf("%f\n", v1.get.f64); printf("%f", v1.get.f64);
break; break;
case VALUETYPE_BOOLEAN: case VALUETYPE_BOOLEAN:
if (v1.get.boolean) { if (v1.get.boolean) {
printf("true\n"); printf("true");
} else { } else {
printf("false\n"); printf("false");
} }
break; break;
case VALUETYPE_OBJECT: case VALUETYPE_OBJECT:
if (v1.get.object != NULL) { if (v1.get.object != NULL) {
StringView type_name_view = v1.get.object->type->name; StringView type_name_view = v1.get.object->type->name;
char type_name[type_name_view.length]; char type_name[type_name_view.length];
type_name[type_name_view.length] = '\0'; type_name[type_name_view.length] = '\0';
StringView_Paste(type_name, type_name_view); StringView_Paste(type_name, type_name_view);
printf("object (%s) at %p\n", type_name, (void*) v1.get.object); 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; break;
} else {
puts("null");
}
} }
return EXIT_SUCCESS; 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) int BuiltinFunction_Duplicate(CallFrame* top_frame)
{ {
Value v1; Value v1;

View file

@ -28,6 +28,7 @@ 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_Multiply(CallFrame* top_frame);
int BuiltinFunction_Equality(CallFrame* top_frame); int BuiltinFunction_Equality(CallFrame* top_frame);
int BuiltinFunction_Print(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

@ -20,12 +20,14 @@ static FlupFunctionAlternative* FlupFunctionAlternative_Malloc(size_t condition_
const char* standard_builtin_names[] = { const char* standard_builtin_names[] = {
"println", "println",
"duplicate", "duplicate",
"print",
NULL NULL
}; };
const BuiltinFunction standard_builtin_functions[] = { const BuiltinFunction standard_builtin_functions[] = {
BuiltinFunction_PrintLine, BuiltinFunction_PrintLine,
BuiltinFunction_Duplicate, BuiltinFunction_Duplicate,
BuiltinFunction_Print,
}; };
int Interpreter_Create(Interpreter* self, DynamicArray* tokens) int Interpreter_Create(Interpreter* self, DynamicArray* tokens)

View file

@ -28,6 +28,7 @@ union ValueContent {
int64_t i64; int64_t i64;
double f64; double f64;
bool boolean; bool boolean;
uint8_t byte;
Object* object; Object* object;
}; };

View file

@ -31,6 +31,8 @@ bool Value_IsTruthy(Value* v)
return v->get.boolean; return v->get.boolean;
case VALUETYPE_OBJECT: case VALUETYPE_OBJECT:
return v->get.object != NULL; return v->get.object != NULL;
case VALUETYPE_BYTE:
return v->get.byte != '\0';
} }
return false; return false;
@ -48,6 +50,8 @@ bool Value_Equal(Value* v1, Value* v2)
return v1->get.boolean == v2->get.boolean; return v1->get.boolean == v2->get.boolean;
case VALUETYPE_OBJECT: case VALUETYPE_OBJECT:
return v1->get.object == v2->get.object; return v1->get.object == v2->get.object;
case VALUETYPE_BYTE:
return v1->get.byte == v2->get.byte;
} }
return false; return false;

View file

@ -26,6 +26,7 @@
#include "../submodules/utilitiec/src/StringView/StringView.h" #include "../submodules/utilitiec/src/StringView/StringView.h"
enum ValueType { enum ValueType {
VALUETYPE_BYTE,
VALUETYPE_INT64, VALUETYPE_INT64,
VALUETYPE_DOUBLE, VALUETYPE_DOUBLE,
VALUETYPE_OBJECT, VALUETYPE_OBJECT,