conditionals
This commit is contained in:
@@ -174,6 +174,55 @@ func TestBooleanExpressions(t *testing.T) {
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestConditionals(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: `
|
||||
if (true) { 10 }; 3333;
|
||||
`,
|
||||
expectedConstants: []interface{}{10, 3333},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpTrue),
|
||||
// 0001
|
||||
code.Make(code.OpJumpNotTruthy, 7),
|
||||
// 0004
|
||||
code.Make(code.OpConstant, 0),
|
||||
// 0007
|
||||
code.Make(code.OpPop),
|
||||
// 0008
|
||||
code.Make(code.OpConstant, 1),
|
||||
// 0011
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
}, {
|
||||
input: `
|
||||
if (true) { 10 } else { 20 }; 3333;
|
||||
`,
|
||||
expectedConstants: []interface{}{10, 20, 3333},
|
||||
expectedInstructions: []code.Instructions{
|
||||
// 0000
|
||||
code.Make(code.OpTrue),
|
||||
// 0001
|
||||
code.Make(code.OpJumpNotTruthy, 10),
|
||||
// 0004
|
||||
code.Make(code.OpConstant, 0),
|
||||
// 0007
|
||||
code.Make(code.OpJump, 13),
|
||||
// 0010
|
||||
code.Make(code.OpConstant, 1),
|
||||
// 0013
|
||||
code.Make(code.OpPop),
|
||||
// 0014
|
||||
code.Make(code.OpConstant, 2),
|
||||
// 0017
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user