diff options
| author | Marc Vertes <mvertes@free.fr> | 2023-10-21 11:20:52 -0400 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2023-10-21 11:20:52 -0400 |
| commit | 20d331813cf05f42961f0f6df531c2880f753a07 (patch) | |
| tree | 409e2b6b358579dd7199947f162d490b7c9ffe08 /parser/interpreter_test.go | |
| parent | e7814f9d8c1d062852f5c9a652a1c4eb3335cf04 (diff) | |
parser: implement operator precedence rules in expressions
Diffstat (limited to 'parser/interpreter_test.go')
| -rw-r--r-- | parser/interpreter_test.go | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/parser/interpreter_test.go b/parser/interpreter_test.go index efaaedb..a49eb96 100644 --- a/parser/interpreter_test.go +++ b/parser/interpreter_test.go @@ -55,6 +55,10 @@ func TestExpr(t *testing.T) { {src: "(6+(1+2)+3)+5", res: "17"}, {src: "(6+(1+2+3)+5", err: "1:1: block not terminated"}, {src: "a := 2; a = 3; a", res: "3"}, + {src: "2 * 3 + 1 == 7", res: "true"}, + {src: "7 == 2 * 3 + 1", res: "true"}, + {src: "1 + 3 * 2 == 2 * 3 + 1", res: "true"}, + {src: "a := 1 + 3 * 2 == 2 * 3 + 1; a", res: "true"}, }) } @@ -77,6 +81,8 @@ func TestIf(t *testing.T) { {src: "a := 0; if a == 1 { a = 2 } else { a = 1 }; a", res: "1"}, {src: "a := 0; if a == 1 { a = 2 } else if a == 0 { a = 3 } else { a = 1 }; a", res: "3"}, {src: "a := 0; if a == 1 { a = 2 } else if a == 2 { a = 3 } else { a = 1 }; a", res: "1"}, + //{src: "a := 1; if a > 0 && a < 2 { a = 3 }; a", res: "3"}, + //{src: "a := 1; if a < 0 || a < 2 { a = 3 }; a", res: "3"}, }) } @@ -91,7 +97,8 @@ func TestFor(t *testing.T) { } func TestGoto(t *testing.T) { - gen(etest{src: ` + run(t, []etest{{ + src: ` func f(a int) int { a = a+1 goto end @@ -99,6 +106,26 @@ func f(a int) int { end: return a } -f(3)`, - res: "4"})(t) +f(3)`, res: "4"}, + }) +} + +/* +func TestSwitch(t *testing.T) { + run(t, []etest{{ + src: ` +func f(a int) int { + switch a { + default: + a = 0 + case 1,2: + a = a+1 + case 3: + a = a+2 + } + return a +} +f(3)`, res: "5"}, + }) } +*/ |
