selectors support for hash
Some checks failed
Build / build (push) Successful in 10m20s
Test / build (push) Failing after 16m7s

This commit is contained in:
Chuck Smith
2024-03-21 16:39:31 -04:00
parent d3471af03d
commit 66d5453ecc
8 changed files with 98 additions and 2 deletions

View File

@@ -35,6 +35,7 @@ var precedences = map[token.TokenType]int{
token.ASTERISK: PRODUCT,
token.LPAREN: CALL,
token.LBRACKET: INDEX,
token.DOT: INDEX,
}
type (
@@ -89,6 +90,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfix(token.LPAREN, p.parseCallExpression)
p.registerInfix(token.LBRACKET, p.parseIndexExpression)
p.registerInfix(token.ASSIGN, p.parseAssignmentExpression)
p.registerInfix(token.DOT, p.parseSelectorExpression)
// Read two tokens, so curToken and peekToken are both set
p.nextToken()
@@ -570,3 +572,9 @@ func (p *Parser) parseComment() ast.Statement {
func (p *Parser) parseNull() ast.Expression {
return &ast.Null{Token: p.curToken}
}
func (p *Parser) parseSelectorExpression(expression ast.Expression) ast.Expression {
p.expectPeek(token.IDENT)
index := &ast.StringLiteral{Token: p.curToken, Value: p.curToken.Literal}
return &ast.IndexExpression{Left: expression, Index: index}
}