Skip to content

Commit

Permalink
Support PutFloat instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexius-Huang committed Oct 24, 2017
1 parent 1edac9c commit d968d3f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
2 changes: 2 additions & 0 deletions compiler/bytecode/expression_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (g *Generator) compileExpression(is *InstructionSet, exp ast.Expression, sc
is.define(GetInstanceVariable, sourceLine, exp.Value)
case *ast.IntegerLiteral:
is.define(PutObject, sourceLine, fmt.Sprint(exp.Value))
case *ast.FloatLiteral:
is.define(PutFloat, sourceLine, fmt.Sprint(exp.Value))
case *ast.StringLiteral:
is.define(PutString, sourceLine, exp.Value)
case *ast.BooleanExpression:
Expand Down
1 change: 1 addition & 0 deletions compiler/bytecode/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
SetInstanceVariable = "setinstancevariable"
PutBoolean = "putboolean"
PutString = "putstring"
PutFloat = "putfloat"
PutSelf = "putself"
PutObject = "putobject"
PutNull = "putnil"
Expand Down
1 change: 0 additions & 1 deletion compiler/parser/data_type_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (p *Parser) parseFloatLiteral(receiver ast.Expression) ast.Expression {
return nil
}
lit.Value = float64(value)

return lit
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/parser/expression_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,13 @@ func (p *Parser) parseMultiVariables(left ast.Expression) ast.Expression {
func (p *Parser) parseDotExpression(receiver ast.Expression) ast.Expression {
_, ok := receiver.(*ast.IntegerLiteral)

// When both receiver & caller are integer => Float
if ok && p.peekTokenIs(token.Int) {
// When both receiver & caller are integer => Float
return p.parseFloatLiteral(receiver)
} else {
// Normal call method expression with receiver
return p.parseCallExpressionWithReceiver(receiver)
}

// Normal call method expression with receiver
return p.parseCallExpressionWithReceiver(receiver)
}

func (p *Parser) expandAssignmentValue(value ast.Expression) ast.Expression {
Expand Down
12 changes: 6 additions & 6 deletions vm/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestFloatArithmeticOperationWithInteger(t *testing.T) {
input string
expected interface{}
}{
{`13.5.to_f + 3`, 16.5},
{`13.5.to_f - 3`, 10.5},
{`13.5.to_f * 3`, 40.5},
{`13.5.to_f % 3`, 1.5},
{`13.5.to_f / 3`, 4.5},
{`13.5.to_f ** 3`, 2460.375},
{`13.5 + 3`, 16.5},
{`13.5 - 3`, 10.5},
{`13.5 * 3`, 40.5},
{`13.5 % 3`, 1.5},
{`13.5 / 3`, 4.5},
{`13.5 ** 3`, 2460.375},
}

for i, tt := range tests {
Expand Down
17 changes: 16 additions & 1 deletion vm/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
"strings"
"strconv"
)

type operation func(t *thread, i *instruction, cf *normalCallFrame, args ...interface{})
Expand Down Expand Up @@ -343,6 +344,20 @@ var builtinActions = map[operationType]*action{
t.stack.push(&Pointer{Target: object})
},
},
bytecode.PutFloat: {
name: bytecode.PutFloat,
operation: func(t *thread, i *instruction, cf *normalCallFrame, args ...interface{}) {
var value float64;
switch argValue := args[0].(type) {
case string:
value, _ = strconv.ParseFloat(argValue,64)
case int:
value = float64(argValue)
}
object := t.vm.initFloatObject(value)
t.stack.push(&Pointer{Target: object})
},
},
bytecode.PutNull: {
name: bytecode.PutNull,
operation: func(t *thread, i *instruction, cf *normalCallFrame, args ...interface{}) {
Expand Down Expand Up @@ -567,7 +582,7 @@ func (vm *VM) initObjectFromGoType(value interface{}) Object {
case int32:
return vm.initIntegerObject(int(v))
case float64:
return vm.initIntegerObject(int(v))
return vm.initFloatObject(v)
case []uint8:
bytes := []byte{}

Expand Down

0 comments on commit d968d3f

Please sign in to comment.