diff --git a/src/main.c b/src/main.c index a38fe34..cc02b04 100644 --- a/src/main.c +++ b/src/main.c @@ -30,7 +30,9 @@ int main(int argc, char **argv) { vm.return_stack_size = STACK_SIZE; vm.stack_size = STACK_SIZE; vm.memory_size = MEMORY_SIZE; - test_recursive_function_compile(vm.memory); + test_loop_compile(vm.memory); + /* test_add_function_compile(vm.memory); */ + /* test_recursive_function_compile(vm.memory); */ #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(mainloop, 0, 1); diff --git a/src/test.c b/src/test.c index 17aecb2..c2dd4b3 100644 --- a/src/test.c +++ b/src/test.c @@ -30,7 +30,7 @@ uint32_t test_loop_compile(Value *memory) { memory[i++].f = 5.0f; memory[i++].u = OP(OP_ADD_REAL, 0, 0, 5); /* a += 5.0; */ memory[i++].u = OP(OP_ADD_INT, 1, 1, 3); /* (implied by loop) i = i + (-1) */ - memory[i++].u = OP(OP_JGT_INT, 4, 1, 2); /* } */ + memory[i++].u = OP(OP_JGE_INT, 4, 1, 2); /* } */ memory[i++].u = OP(OP_REAL_TO_UINT, 1, 0, 0); /* let b = a as nat; */ memory[i++].u = OP(OP_READ_STRING, 2, 0, 0); /* let user_string = gets(); */ memory[i++].u = OP(OP_UINT_TO_STRING, 3, 1, 0); diff --git a/src/vm.c b/src/vm.c index d3f4fe6..da3da81 100644 --- a/src/vm.c +++ b/src/vm.c @@ -81,9 +81,7 @@ bool step_vm(VM *vm) { return true; case OP_RETURN: vm->pc = vm->return_stack[--vm->rp].u; /* set pc to return address */ - Slice s = vm->frames[vm->fp].allocated; /* get allocated slice */ - vm->fp--; /* pop the frame */ - vm->mp = s.start; /* reset memory pointer to start of old slice */ + vm->mp = vm->frames[vm->fp--].allocated.start; /* reset memory pointer to start of old slice, pop the frame */ return true; case OP_LOADI: vm->frames[vm->fp].registers[dest].i = vm->memory[vm->pc++].i; diff --git a/test/bench/add.lua b/test/bench/add.lua new file mode 100644 index 0000000..e9977da --- /dev/null +++ b/test/bench/add.lua @@ -0,0 +1,6 @@ +function add(a, b) + return a + b +end + +local sum = add(1, 1) +print(tostring(sum)) diff --git a/test/bench/add.pl b/test/bench/add.pl new file mode 100644 index 0000000..65985eb --- /dev/null +++ b/test/bench/add.pl @@ -0,0 +1,7 @@ +sub add { + my ($a, $b) = @_; + return $a + $b; +} + +my $sum = add(1, 1); +print($sum . "\n"); diff --git a/test/bench/add.py b/test/bench/add.py new file mode 100644 index 0000000..d8f7edb --- /dev/null +++ b/test/bench/add.py @@ -0,0 +1,5 @@ +def add(a, b): + return a + b + +sum = add(1, 1) +print(str(sum)) diff --git a/test/bench/add.wren b/test/bench/add.wren new file mode 100644 index 0000000..fbaca81 --- /dev/null +++ b/test/bench/add.wren @@ -0,0 +1,6 @@ +var add = Fn.new { |a, b| + return a + b +} + +var sum = add.call(1, 1) +System.print(sum.toString) diff --git a/test/bench/loop.lua b/test/bench/loop.lua new file mode 100644 index 0000000..e73282f --- /dev/null +++ b/test/bench/loop.lua @@ -0,0 +1,10 @@ +local a = 5.0 +for i = 5, 0, -1 do + a = a + 5.0 +end +local b = math.floor(a) +io.write("Enter a string: ") +local user_string = io.read("*l") +print(tostring(a)) +print(tostring(b)) +print(user_string) diff --git a/test/bench/loop.pl b/test/bench/loop.pl new file mode 100644 index 0000000..9427079 --- /dev/null +++ b/test/bench/loop.pl @@ -0,0 +1,11 @@ +my $a = 5.0; +for (my $i = 5; $i >= 0; $i--) { + $a += 5.0; +} +my $b = int($a); +print "Enter a string: "; +my $user_string = ; +chomp($user_string); +print($a . "\n"); +print($b . "\n"); +print($user_string . "\n"); diff --git a/test/bench/loop.py b/test/bench/loop.py new file mode 100644 index 0000000..00a0d6a --- /dev/null +++ b/test/bench/loop.py @@ -0,0 +1,8 @@ +a = 5.0 +for i in range(5, -1, -1): + a += 5.0 +b = int(a) +user_string = input("Enter a string: ") +print(str(a)) +print(str(b)) +print(user_string) diff --git a/test/bench/loop.wren b/test/bench/loop.wren new file mode 100644 index 0000000..beca453 --- /dev/null +++ b/test/bench/loop.wren @@ -0,0 +1,10 @@ +var a = 5.0 +var i = 5 +while (i >= 0) { + a = a + 5.0 + i = i - 1 +} +var b = a +System.write("Enter a string: ") +System.print(a.toString) +System.print(b.toString) diff --git a/test/bench/run.sh b/test/bench/run.sh index cc6d61b..f71b44d 100755 --- a/test/bench/run.sh +++ b/test/bench/run.sh @@ -3,27 +3,35 @@ # Exit on error set -e +# Check if a filename was provided +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +FILENAME=$1 + # Function to print section headers print_section() { printf '\n\e[1;34m%s\e[0m\n' "$1" } +# Wren Implementation +print_section "Wren ($FILENAME.wren)" +time wren "$FILENAME.wren" + # Lua Implementation -print_section "Lua (fib.lua)" -time lua fib.lua +print_section "Lua ($FILENAME.lua)" +time lua "$FILENAME.lua" # Perl Implementation -print_section "Perl (fib.pl)" -time perl fib.pl +print_section "Perl ($FILENAME.pl)" +time perl "$FILENAME.pl" # Python Implementation -print_section "Python (fib.py)" -time python3 fib.py - -# Wren Implementation -print_section "Wren (fib.wren)" -time wren fib.wren +print_section "Python ($FILENAME.py)" +time python3 "$FILENAME.py" # ZRE Implementation -print_section "zre (fib.zre)" -time ../../src/zre #currently hardcoded +print_section "zre ($FILENAME.zre)" +time ../../src/zre "$FILENAME.zre" diff --git a/test/fib.s b/test/fib.s index d0895bf..335d7a9 100644 --- a/test/fib.s +++ b/test/fib.s @@ -1,5 +1,5 @@ main: - loadi $0 23 + loadi $0 35 pushi $0 call &fib popi $0 diff --git a/test/loop.zre b/test/loop.zre index 7647722..d1979e3 100644 --- a/test/loop.zre +++ b/test/loop.zre @@ -1,9 +1,9 @@ -real a = 5.0; -do (int i = 5, 0, -1) { +let a = 5.0; +do (let i = 5, 0, -1) { a += 5.0; } -nat b = a as nat; -str user_string = readln(); +let b = a as nat; +let user_string = gets(); print(a.toS()); print(b.toS()); print(user_string);