diff --git a/src/Makefile b/src/Makefile index b0c5f71..954b848 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ # ----------------------- # Native build (gcc) CC_NATIVE = gcc -CFLAGS_NATIVE = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I. +CFLAGS_NATIVE = -g -O2 -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I. LDFLAGS_NATIVE = LDLIBS_NATIVE = -lSDL2 diff --git a/src/arch/linux/main.c b/src/arch/linux/main.c index 5d935c7..6d270c6 100644 --- a/src/arch/linux/main.c +++ b/src/arch/linux/main.c @@ -13,8 +13,12 @@ int main(int argc, char **argv) { /* test_add_compile(&vm); */ /* test_add_function_compile(&vm); */ /* test_loop_compile(&vm); */ - test_hello_world_compile(&vm); - /* test_recursive_function_compile(&vm); */ + /* test_hello_world_compile(&vm); */ + test_recursive_function_compile(&vm); + + while(step_vm(&vm)); + + return 0; uint32_t buffer_size = 640 * 480 * sizeof(uint32_t); @@ -58,6 +62,7 @@ int main(int argc, char **argv) { bool running = true; while (running) { step_vm(&vm); + SDL_PumpEvents(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { diff --git a/src/test.c b/src/test.c index 0956563..e7b9a6e 100644 --- a/src/test.c +++ b/src/test.c @@ -34,6 +34,23 @@ bool test_add_compile(VM *vm) { } bool test_loop_compile(VM *vm) { + vm->memory[vm->mp++].u = 17; + vm->memory[vm->mp].c[0] = 'E'; + vm->memory[vm->mp].c[1] = 'n'; + vm->memory[vm->mp].c[2] = 't'; + vm->memory[vm->mp++].c[3] = 'e'; + vm->memory[vm->mp].c[0] = 'r'; + vm->memory[vm->mp].c[1] = ' '; + vm->memory[vm->mp].c[2] = 'a'; + vm->memory[vm->mp++].c[3] = ' '; + vm->memory[vm->mp].c[0] = 's'; + vm->memory[vm->mp].c[1] = 't'; + vm->memory[vm->mp].c[2] = 'r'; + vm->memory[vm->mp++].c[3] = 'i'; + vm->memory[vm->mp].c[0] = 'n'; + vm->memory[vm->mp].c[1] = 'g'; + vm->memory[vm->mp].c[2] = ':'; + vm->memory[vm->mp++].c[3] = '\0'; vm->code[vm->cp++].u = OP(OP_LOADF, 0, 0, 0); /* let a = 5.0 */ vm->code[vm->cp++].f = 5.0f; vm->code[vm->cp++].u = OP(OP_LOADI, 1, 0, 0); /* do (i = 50000, 0, -1) { */ @@ -51,6 +68,9 @@ bool test_loop_compile(VM *vm) { vm->code[vm->cp++].u = OP(OP_ADD_INT, 1, 1, 3); /* (implied by loop) i = i + (-1) */ vm->code[vm->cp++].u = OP(OP_JGE_INT, 4, 1, 2); /* } */ vm->code[vm->cp++].u = OP(OP_REAL_TO_UINT, 1, 0, 0); /* let b = a as nat; */ + vm->code[vm->cp++].u = OP(OP_LOADU, 5, 0, 0); + vm->code[vm->cp++].u = 0; + vm->code[vm->cp++].u = OP(OP_PRINT_STRING, 0, 5, 0); /* print("Enter a string: "); */ vm->code[vm->cp++].u = OP(OP_READ_STRING, 2, 0, 0); /* let user_string = gets(); */ vm->code[vm->cp++].u = OP(OP_UINT_TO_STRING, 3, 1, 0); vm->code[vm->cp++].u = OP(OP_PRINT_STRING, 0, 3, 0); /* print(a.toS()); */ diff --git a/test/add.zl b/test/add.zl new file mode 100644 index 0000000..8b90e5f --- /dev/null +++ b/test/add.zl @@ -0,0 +1,5 @@ +fn add(a, b) { + return a + b; +} +let sum = add(1, 1); +print sum; diff --git a/test/add.zre b/test/add.zre index 248e445..fd2bcea 100644 --- a/test/add.zre +++ b/test/add.zre @@ -1,6 +1,6 @@ -int add(int a, int b) { +fn add(int a, int b) { return a + b; } -int sum = add(1, 1); +let sum = add(1, 1); print(sum.toS()); diff --git a/test/fib.zl b/test/fib.zl new file mode 100644 index 0000000..1b95c65 --- /dev/null +++ b/test/fib.zl @@ -0,0 +1,7 @@ +fn fib(n) { + if (n < 2) return n; + return fib(n - 2) + fib(n - 1); +} + +let result = fib(35); +print result; diff --git a/test/loop.asm b/test/loop.asm index bfb52c0..1367b61 100644 --- a/test/loop.asm +++ b/test/loop.asm @@ -1,6 +1,10 @@ +.memory +.ascii Enter a string: + +.code main: loadf $0 5.0 - loadi $1 5 + loadi $1 50000 loadi $2 0 loadi $3 -1 loop: @@ -10,6 +14,8 @@ loop: loadu $4 &loop jgei $4 $1 $2 rtou $1 $0 + loadu $5 0 + puts $5 gets $2 utos $3 $1 puts $3 diff --git a/test/loop.lua b/test/loop.lua index 2b5aa57..b1a8854 100644 --- a/test/loop.lua +++ b/test/loop.lua @@ -3,7 +3,7 @@ for i = 50000, 0, -1 do a = a + 5.0 end local b = math.floor(a) -io.write("Enter a string: ") +io.write("Enter a string:") local user_string = io.read("*l") print(tostring(a)) print(tostring(b)) diff --git a/test/loop.pl b/test/loop.pl index 5183ae9..e2a2492 100644 --- a/test/loop.pl +++ b/test/loop.pl @@ -3,7 +3,7 @@ for (my $i = 50000; $i >= 0; $i--) { $a += 5.0; } my $b = int($a); -print "Enter a string: "; +print "Enter a string:"; my $user_string = ; chomp($user_string); print($a . "\n"); diff --git a/test/loop.py b/test/loop.py index 16295c8..11cc9ba 100644 --- a/test/loop.py +++ b/test/loop.py @@ -2,7 +2,7 @@ a = 5.0 for i in range(50000, -1, -1): a += 5.0 b = int(a) -user_string = input("Enter a string: ") +user_string = input("Enter a string:") print(str(a)) print(str(b)) print(user_string) diff --git a/test/loop.zl b/test/loop.zl new file mode 100644 index 0000000..8ccc384 --- /dev/null +++ b/test/loop.zl @@ -0,0 +1,10 @@ +let a = 5.0; +for (let i = 50000; i > 0; i = i - 1) { + a = a + 5.0; +} +let b = a; +print "Enter a string:"; +let user_string = read(); +print(a); +print(b); +print(user_string); diff --git a/test/run.sh b/test/run.sh index 3be0756..de4ffdb 100755 --- a/test/run.sh +++ b/test/run.sh @@ -32,6 +32,10 @@ print_section "Perl ($FILENAME.pl)" print_section "Python ($FILENAME.py)" echo "test input" | time python3 "$FILENAME.py" +# ZLC Implementation +print_section "zlc ($FILENAME.zl)" + echo "test input" | time zlc "$FILENAME.zl" + # ZRE Implementation print_section "zre ($FILENAME.zre)" echo "test input" | time ../src/zre "$FILENAME.zre"