|
@@ -38,6 +38,48 @@ func TestIntegerArithmetic(t *testing.T) {
|
|
|
code.Make(code.OpPop),
|
|
code.Make(code.OpPop),
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
|
|
+ {
|
|
|
|
|
+ input: "1 - 2",
|
|
|
|
|
+ expectedConstants: []interface{}{1, 2},
|
|
|
|
|
+ expectedInstructions: []code.Instructions{
|
|
|
|
|
+ code.Make(code.OpConstant, 0),
|
|
|
|
|
+ code.Make(code.OpConstant, 1),
|
|
|
|
|
+ code.Make(code.OpSub),
|
|
|
|
|
+ code.Make(code.OpPop),
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ input: "1 * 2",
|
|
|
|
|
+ expectedConstants: []interface{}{1, 2},
|
|
|
|
|
+ expectedInstructions: []code.Instructions{
|
|
|
|
|
+ code.Make(code.OpConstant, 0),
|
|
|
|
|
+ code.Make(code.OpConstant, 1),
|
|
|
|
|
+ code.Make(code.OpMul),
|
|
|
|
|
+ code.Make(code.OpPop),
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ input: "2 / 1",
|
|
|
|
|
+ expectedConstants: []interface{}{2, 1},
|
|
|
|
|
+ expectedInstructions: []code.Instructions{
|
|
|
|
|
+ code.Make(code.OpConstant, 0),
|
|
|
|
|
+ code.Make(code.OpConstant, 1),
|
|
|
|
|
+ code.Make(code.OpDiv),
|
|
|
|
|
+ code.Make(code.OpPop),
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ input: "5 * (2 + 10)",
|
|
|
|
|
+ expectedConstants: []interface{}{5, 2, 10},
|
|
|
|
|
+ expectedInstructions: []code.Instructions{
|
|
|
|
|
+ code.Make(code.OpConstant, 0),
|
|
|
|
|
+ code.Make(code.OpConstant, 1),
|
|
|
|
|
+ code.Make(code.OpConstant, 2),
|
|
|
|
|
+ code.Make(code.OpAdd),
|
|
|
|
|
+ code.Make(code.OpMul),
|
|
|
|
|
+ code.Make(code.OpPop),
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
runCompilerTests(t, tests)
|
|
runCompilerTests(t, tests)
|