|
@@ -5,6 +5,7 @@ import (
|
|
|
"github/runnignwater/monkey/ast"
|
|
"github/runnignwater/monkey/ast"
|
|
|
"github/runnignwater/monkey/lexer"
|
|
"github/runnignwater/monkey/lexer"
|
|
|
"github/runnignwater/monkey/token"
|
|
"github/runnignwater/monkey/token"
|
|
|
|
|
+ "strconv"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -50,6 +51,7 @@ func New(l *lexer.Lexer) *Parser {
|
|
|
|
|
|
|
|
p.prefixParseFns = make(map[token.TypeToken]prefixParseFn)
|
|
p.prefixParseFns = make(map[token.TypeToken]prefixParseFn)
|
|
|
p.registerPrefix(token.IDENT, p.parseIdentifier)
|
|
p.registerPrefix(token.IDENT, p.parseIdentifier)
|
|
|
|
|
+ p.registerPrefix(token.INT, p.parseIntegerLiteral)
|
|
|
|
|
|
|
|
// Read two tokens, so curToken and peekToken are both set
|
|
// Read two tokens, so curToken and peekToken are both set
|
|
|
p.nextToken()
|
|
p.nextToken()
|
|
@@ -165,6 +167,19 @@ func (p *Parser) parseExpression(precedence int) ast.Expression {
|
|
|
|
|
|
|
|
return leftExp
|
|
return leftExp
|
|
|
}
|
|
}
|
|
|
|
|
+func (p *Parser) parseIntegerLiteral() ast.Expression {
|
|
|
|
|
+ lit := &ast.IntegerLiteral{Token: p.curToken}
|
|
|
|
|
+
|
|
|
|
|
+ value, err := strconv.ParseInt(p.curToken.Literal, 0, 64)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ msg := fmt.Sprintf("could not parse %q as integer", p.curToken.Literal)
|
|
|
|
|
+ p.errors = append(p.errors, msg)
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ lit.Value = value
|
|
|
|
|
+
|
|
|
|
|
+ return lit
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
func (p *Parser) curTokenIs(t token.TypeToken) bool {
|
|
func (p *Parser) curTokenIs(t token.TypeToken) bool {
|
|
|
return p.curToken.Type == t
|
|
return p.curToken.Type == t
|