Compare commits
2 commits
a663ce2044
...
5313cc5603
Author | SHA1 | Date | |
---|---|---|---|
5313cc5603 | |||
aec860a6b8 |
6 changed files with 55 additions and 10 deletions
1
make.sh
1
make.sh
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
11
src/main.c
11
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;
|
||||
}
|
||||
|
|
6
test-inputs/factorial.flup
Normal file
6
test-inputs/factorial.flup
Normal file
|
@ -0,0 +1,6 @@
|
|||
factorial: int n -> int
|
||||
| n 0 == -> 1
|
||||
| 1 -> n 1 - factorial n * duplicate println
|
||||
;
|
||||
|
||||
25 factorial
|
Loading…
Reference in a new issue