Compare commits

..

2 commits

6 changed files with 55 additions and 10 deletions

View file

@ -5,4 +5,5 @@ gcc `find src/ -name '*.c'` \
submodules/utilitiec/build/lib/liballocator-interface.a \
submodules/utilitiec/build/lib/libStringView.a \
submodules/utilitiec/build/lib/libScratchpad.a \
-Isubmodules/utilitiec/src \
-lm -ggdb -Wall -Wextra -pedantic -o bin/flup

View file

@ -23,6 +23,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
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);

View file

@ -26,6 +26,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_PrintLine(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);
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;

View file

@ -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;
}

View file

@ -0,0 +1,6 @@
factorial: int n -> int
| n 0 == -> 1
| 1 -> n 1 - factorial n * duplicate println
;
25 factorial