about summary refs log tree commit diff
path: root/pkg/lang/compiler/compiler_test.go
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-12 01:31:27 +0200
committerMel <einebeere@gmail.com>2022-07-12 01:31:27 +0200
commite06aeb7fa2fcb9046b8861ed3c23417555e823f5 (patch)
tree3edbaa3a329e0c545c0e8334aeefc77f2bb994d6 /pkg/lang/compiler/compiler_test.go
parentc61e995b316ba3382798492b03ab7d5a60002237 (diff)
downloadjinx-e06aeb7fa2fcb9046b8861ed3c23417555e823f5.tar.zst
jinx-e06aeb7fa2fcb9046b8861ed3c23417555e823f5.zip
Compile continue and break statements
Diffstat (limited to 'pkg/lang/compiler/compiler_test.go')
-rw-r--r--pkg/lang/compiler/compiler_test.go71
1 files changed, 70 insertions, 1 deletions
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go
index bdbc375..7741ca9 100644
--- a/pkg/lang/compiler/compiler_test.go
+++ b/pkg/lang/compiler/compiler_test.go
@@ -291,9 +291,65 @@ func TestForCond(t *testing.T) {
 	mustCompileTo(t, src, expected)
 }
 
+func TestForCondBreakContinue(t *testing.T) {
+	src := `
+	var sum = 0
+	var x = 0
+	for {
+		if x % 2 == 0 {
+			continue
+		}
+
+		sum = sum + x
+
+		if x == 100 {
+			break
+		}
+	}
+	`
+
+	expected := `
+	push_int 0
+	push_int 0
+
+	@continue:
+	get_local 1
+	push_int 2
+	mod
+	push_int 0
+	eq
+	jf @sum
+	jmp @continue
+
+	@sum:
+	get_local 0
+	get_local 1
+	add
+	set_local 0
+
+	get_local 1
+	push_int 100
+	eq
+	jf @repeat
+	jmp @break
+
+	@repeat:
+	jmp @continue
+
+	@break:
+	halt
+	`
+
+	mustCompileTo(t, src, expected)
+}
+
 func TestForIn(t *testing.T) {
 	src := `
-	for x in [1, 2, 3] {
+	for x in [1, 2, 3, "oops"] {
+		if x == "oops" {
+			break
+		}
+
 		"say"(x)
 	}
 	`
@@ -316,6 +372,11 @@ func TestForIn(t *testing.T) {
 	push_int 3
 	call 1
 
+	get_local 0
+	get_member "push"
+	push_string "oops"
+	call 1
+
 	push_int 0
 	push_null
 
@@ -338,6 +399,14 @@ func TestForIn(t *testing.T) {
 	add
 	set_local 1
 
+	get_local 2
+	push_string "oops"
+	eq
+	jf @say
+
+	jmp @end
+
+	@say:
 	push_string "say"
 	get_local 2
 	call 1