about summary refs log tree commit diff
path: root/pkg/lang/vm/vm_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/vm/vm_test.go')
-rw-r--r--pkg/lang/vm/vm_test.go99
1 files changed, 98 insertions, 1 deletions
diff --git a/pkg/lang/vm/vm_test.go b/pkg/lang/vm/vm_test.go
index 325f200..d63523f 100644
--- a/pkg/lang/vm/vm_test.go
+++ b/pkg/lang/vm/vm_test.go
@@ -236,7 +236,7 @@ func TestTypeConstruct(t *testing.T) {
 				this.name = name
 				this.age = age
 			}
-		
+
 			fn meow(this) {
 				return this.name + " says Meow!"
 			}
@@ -306,6 +306,103 @@ func TestTypeConstruct(t *testing.T) {
 	test(t, src, "\"Kitty says Meow!\"")
 }
 
+func TestPrimes(t *testing.T) {
+	/*
+		var i = 0
+		var primes = []
+
+		for i <= 10 {
+			var factors = []
+
+			var j = 1
+			for j <= i {
+				if i % j <= 0 {
+					factors.push(j)
+				}
+				j = j + 1
+			}
+
+			if factors.length() <= 2 {
+				primes.push(i)
+			}
+
+			i = i + 1
+		}
+
+		print(primes)
+	*/
+
+	// TODO: This test uses OpLte instead of OpEq, which works,
+	// but once OpEq is implemented, this test should be updated.
+
+	src := `
+	push_int 2
+	push_array
+	@main_loop:
+		push_int 10
+		get_local 0
+		lte
+		jf @end
+
+		push_array
+		push_int 1
+
+	@factor_loop:
+		get_local 0
+		get_local 3
+		lte
+		jf @prime_check
+
+		push_int 0
+
+		get_local 3
+		get_local 0
+		mod
+
+		lte
+		jf @factor_loop_inc
+
+		get_local 3
+		get_local 2
+		temp_arr_push
+
+	@factor_loop_inc:
+		push_int 1
+		get_local 3
+		add
+		set_local 3
+		jmp @factor_loop
+
+	@prime_check:
+		push_int 2
+		get_local 2
+		get_member "length"
+		call 0
+		lte
+
+		jf @main_loop_inc
+
+		get_local 0
+		get_local 1
+		temp_arr_push
+
+	@main_loop_inc:
+		push_int 1
+		get_local 0
+		add
+		set_local 0
+
+		drop
+		drop
+
+		jmp @main_loop
+	@end:
+		halt
+	`
+
+	test(t, src, "[2, 3, 5, 7]")
+}
+
 func test(t *testing.T, src string, expected string) {
 	bc := compile(t, src)
 	vm := vm.New(&bc)