optimizations
This commit is contained in:
@@ -173,8 +173,12 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
case *ast.IntegerLiteral:
|
||||
integer := object.Integer{Value: node.Value}
|
||||
c.emit(code.OpConstant, c.addConstant(integer))
|
||||
i := object.Integer{Value: node.Value}
|
||||
c.emit(code.OpConstant, c.addConstant(i))
|
||||
|
||||
case *ast.FloatLiteral:
|
||||
f := object.Float{Value: node.Value}
|
||||
c.emit(code.OpConstant, c.addConstant(f))
|
||||
|
||||
case *ast.Null:
|
||||
c.emit(code.OpNull)
|
||||
|
||||
@@ -165,6 +165,78 @@ func TestIntegerArithmetic(t *testing.T) {
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestFloatingPointArithmetic(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
input: "1.2 + 2.3",
|
||||
expectedConstants: []interface{}{1.2, 2.3},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpAdd),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpHalt),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "1.2; 2.3",
|
||||
expectedConstants: []interface{}{1.2, 2.3},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpHalt),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "2.5 - 0.5",
|
||||
expectedConstants: []interface{}{2.5, 0.5},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpSub),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpHalt),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "1.2 * 2.3",
|
||||
expectedConstants: []interface{}{1.2, 2.3},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpMul),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpHalt),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "5.0 / 2.0",
|
||||
expectedConstants: []interface{}{5.0, 2.0},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpDiv),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpHalt),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "-2.5",
|
||||
expectedConstants: []interface{}{2.5},
|
||||
expectedInstructions: []code.Instructions{
|
||||
code.Make(code.OpConstant, 0),
|
||||
code.Make(code.OpMinus),
|
||||
code.Make(code.OpPop),
|
||||
code.Make(code.OpHalt),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runCompilerTests(t, tests)
|
||||
}
|
||||
|
||||
func TestBooleanExpressions(t *testing.T) {
|
||||
tests := []compilerTestCase{
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user