Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]feature/parse_underscore #730

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (l *Lexer) NextToken() token.Token {
}
case ';':
tok = newToken(token.Semicolon, l.ch, l.line)
case '_':
tok = newToken(token.UnderScore, l.ch, l.line)
case '(':
tok = newToken(token.LParen, l.ch, l.line)
case ')':
Expand Down
39 changes: 38 additions & 1 deletion compiler/parser/data_type_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,49 @@ func (p *Parser) parseIntegerLiteral() ast.Expression {
return lit
}

func (p *Parser) parseUnderScoreLiteral(integerPart ast.Expression) ast.Expression {
p.nextToken()

fmt.Println("!!!")

lit := &ast.IntegerLiteral{BaseNode: &ast.BaseNode{Token: p.curToken}}

firstValue, err := strconv.ParseInt(integerPart.String(), 0, 64)

len := len(p.curToken.Literal)

for ; len > 0; len-- {
firstValue *= 10
}

secondValue, err := strconv.ParseInt(lit.TokenLiteral(), 0, 64)

if err != nil {
p.error = errors.NewTypeParsingError(lit.TokenLiteral(), "integer", p.curToken.Line)
return nil
}

lit.Value = int(firstValue + secondValue)

lit.Token.Literal = strconv.Itoa(lit.Value)

return lit
}

func (p *Parser) parseFloatLiteral(integerPart ast.Expression) ast.Expression {
// Get the fractional part of the token
p.nextToken()
lit2 := p.parseIntegerLiteral()

for p.peekTokenIs(token.UnderScore) {
p.nextToken()

lit2 = p.parseUnderScoreLiteral(lit2)
}

floatTok := token.Token{
Type: token.Float,
Literal: fmt.Sprintf("%s.%s", integerPart.String(), p.curToken.Literal),
Literal: fmt.Sprintf("%s.%s", integerPart.String(), lit2.String()),
Line: p.curToken.Line,
}
lit := &ast.FloatLiteral{BaseNode: &ast.BaseNode{Token: floatTok}}
Expand All @@ -39,6 +75,7 @@ func (p *Parser) parseFloatLiteral(integerPart ast.Expression) ast.Expression {
return nil
}
lit.Value = float64(value)

return lit
}

Expand Down
6 changes: 6 additions & 0 deletions compiler/parser/expression_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ func (p *Parser) parseExpression(precedence int) ast.Expression {
}

leftExp = infixFn(leftExp)

for p.peekTokenIs(token.UnderScore) {
p.nextToken()
leftExp = infixFn(leftExp)
}

}

if p.peekTokenIs(token.Semicolon) {
Expand Down
3 changes: 3 additions & 0 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfix(token.Assign, p.parseAssignExpression)
p.registerInfix(token.Range, p.parseRangeExpression)
p.registerInfix(token.Dot, p.parseCallExpressionWithReceiver)
p.registerInfix(token.UnderScore, p.parseUnderScoreLiteral)
p.registerInfix(token.LParen, p.parseCallExpressionWithoutReceiver)
p.registerInfix(token.LBracket, p.parseIndexExpression)
p.registerInfix(token.Colon, p.parseArgumentPairExpression)
Expand All @@ -113,6 +114,8 @@ func New(l *lexer.Lexer) *Parser {
// ParseProgram update program statements and return program
func (p *Parser) ParseProgram() (program *ast.Program, err *errors.Error) {

fmt.Println("aa")

defer func() {
if recover() != nil {
err = p.error
Expand Down
1 change: 1 addition & 0 deletions compiler/parser/precedence/precedence.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (

// LookupTable maps token to its corresponding precedence
var LookupTable = map[token.Type]int{
token.UnderScore: Equals,
token.Eq: Equals,
token.NotEq: Equals,
token.Match: Compare,
Expand Down
29 changes: 15 additions & 14 deletions compiler/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ const (
String = "STRING"
Comment = "COMMENT"

Assign = "="
Plus = "+"
PlusEq = "+="
Minus = "-"
MinusEq = "-="
Bang = "!"
Asterisk = "*"
Pow = "**"
Slash = "/"
Dot = "."
And = "&&"
Or = "||"
OrEq = "||="
Modulo = "%"
UnderScore = "_"
Assign = "="
Plus = "+"
PlusEq = "+="
Minus = "-"
MinusEq = "-="
Bang = "!"
Asterisk = "*"
Pow = "**"
Slash = "/"
Dot = "."
And = "&&"
Or = "||"
OrEq = "||="
Modulo = "%"

Match = "=~"
LT = "<"
Expand Down