From e06aeb7fa2fcb9046b8861ed3c23417555e823f5 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 12 Jul 2022 01:31:27 +0200 Subject: Compile continue and break statements --- pkg/lang/compiler/compiler_test.go | 71 +++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'pkg/lang/compiler/compiler_test.go') 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 -- cgit 1.4.1