Skip to content

Commit

Permalink
Fix errors and refactor the float expression parsing part
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexius-Huang committed Oct 27, 2017
1 parent 5991cbe commit e0c381c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
28 changes: 28 additions & 0 deletions compiler/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ type IntegerLiteral struct {
}

func (il *IntegerLiteral) expressionNode() {}

// Get the literal of the Integer type token
func (il *IntegerLiteral) TokenLiteral() string {
return il.Token.Literal
}

// Get the string format of the Integer type token
func (il *IntegerLiteral) String() string {
return il.Token.Literal
}
Expand All @@ -25,9 +29,13 @@ type FloatLiteral struct {
}

func (il *FloatLiteral) expressionNode() {}

// Get the literal of the Float type token
func (il *FloatLiteral) TokenLiteral() string {
return il.Token.Literal
}

// Get the string format of the Float type token
func (il *FloatLiteral) String() string {
return il.Token.Literal
}
Expand All @@ -38,9 +46,13 @@ type StringLiteral struct {
}

func (sl *StringLiteral) expressionNode() {}

// Get the literal of the String type token
func (sl *StringLiteral) TokenLiteral() string {
return sl.Token.Literal
}

// Get the string format of the String type token
func (sl *StringLiteral) String() string {
var out bytes.Buffer

Expand All @@ -56,9 +68,13 @@ type ArrayExpression struct {
}

func (ae *ArrayExpression) expressionNode() {}

// Get the literal of the Array type token
func (ae *ArrayExpression) TokenLiteral() string {
return ae.Token.Literal
}

// Get the string format of the Array type token
func (ae *ArrayExpression) String() string {
var out bytes.Buffer

Expand Down Expand Up @@ -109,9 +125,13 @@ type HashExpression struct {
}

func (he *HashExpression) expressionNode() {}

// Get the literal of the Hash type token
func (he *HashExpression) TokenLiteral() string {
return he.Token.Literal
}

// Get the string format of the Hash type token
func (he *HashExpression) String() string {
var out bytes.Buffer
var pairs []string
Expand Down Expand Up @@ -209,9 +229,13 @@ type BooleanExpression struct {
}

func (b *BooleanExpression) expressionNode() {}

// Get the literal of the Boolean type token
func (b *BooleanExpression) TokenLiteral() string {
return b.Token.Literal
}

// Get the string format of the Boolean type token
func (b *BooleanExpression) String() string {
return b.Token.Literal
}
Expand Down Expand Up @@ -387,9 +411,13 @@ type RangeExpression struct {
}

func (re *RangeExpression) expressionNode() {}

// Get the literal of the Range type token
func (re *RangeExpression) TokenLiteral() string {
return re.Token.Literal
}

// Get the string format of the Range type token
func (re *RangeExpression) String() string {
var out bytes.Buffer

Expand Down
11 changes: 8 additions & 3 deletions compiler/parser/data_type_parsing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"fmt"
"github.com/goby-lang/goby/compiler/ast"
"github.com/goby-lang/goby/compiler/token"
"strconv"
Expand All @@ -20,11 +21,15 @@ func (p *Parser) parseIntegerLiteral() ast.Expression {
return lit
}

func (p *Parser) parseFloatLiteral(receiver ast.Expression) ast.Expression {
func (p *Parser) parseFloatLiteral(integerPart ast.Expression) ast.Expression {
// Get the fractional part of the token
p.nextToken()
floatLiteral := receiver.String() + "." + p.curToken.Literal

floatTok := token.Token{Type: token.Float, Literal: floatLiteral, Line: p.curToken.Line}
floatTok := token.Token{
Type: token.Float,
Literal: fmt.Sprintf("%s.%s", integerPart.String(), p.curToken.Literal),
Line: p.curToken.Line,
}
lit := &ast.FloatLiteral{BaseNode: &ast.BaseNode{Token: floatTok}}
value, err := strconv.ParseFloat(lit.TokenLiteral(), 64)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions vm/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func TestFloatConversions(t *testing.T) {
}{
{`(100.3).to_i`, 100},
{`(100.3).to_s`, "100.3"},
{`100.3.to_i`, 100},
{`100.3.to_s`, "100.3"},
}

for i, tt := range tests {
Expand Down

0 comments on commit e0c381c

Please sign in to comment.