static ast.Expression parseHashLiteral() { ast.HashLiteral hash = new ast.HashLiteral { Token = curToken }; hash.Pairs = new Dictionary <ast.Expression, ast.Expression>(); while (!peekTokenIs(token.RBRACE)) { nextToken(); ast.Expression key = parseExpression(LOWEST); if (!expectPeek(token.COLON)) { return(null); } nextToken(); ast.Expression value = parseExpression(LOWEST); hash.Pairs.Add(key, value); if (!peekTokenIs(token.RBRACE) && !expectPeek(token.COMMA)) { return(null); } } if (!expectPeek(token.RBRACE)) { return(null); } return(hash); }
static ast.Expression parseExpression(int precedence) { prefixParseFn prefix; if (curToken.Type == null || !prefixParseFns.TryGetValue(curToken.Type, out prefix)) { noPrefixFnError(curToken.Type); return(null); } // stuff made static since saved tokens disappear from the scope of prefix() ast.Expression leftExp = prefix(); while (!peekTokenIs(token.SEMICOLON) && precedence < peekPrecedence()) { infixParseFn infix; if (peekToken.Type == null || !infixParseFns.TryGetValue(peekToken.Type, out infix)) { return(leftExp); } nextToken(); leftExp = infix(leftExp); } return(leftExp); }
static ast.Expression parseCallExpression(ast.Expression function) { ast.CallExpression exp = new ast.CallExpression { Token = curToken, Function = function }; exp.Arguments = parseExpressionList(token.RPAREN); return(exp); }
static ast.Expression parseGroupedExpression() { nextToken(); ast.Expression exp = parseExpression(LOWEST); if (!expectPeek(token.RPAREN)) { return(null); } return(exp); }
static ast.Expression parseIndexExpression(ast.Expression left) { ast.IndexExpression exp = new ast.IndexExpression { Token = curToken, Left = left }; nextToken(); exp.Index = parseExpression(LOWEST); if (!expectPeek(token.RBRACKET)) { return(null); } return(exp); }
static ast.Expression parseInfixExpression(ast.Expression left) { ast.InfixExpression expression = new ast.InfixExpression { Token = curToken, Operator = curToken.Literal, Left = left }; int precedence = curPrecedence(); nextToken(); expression.Right = parseExpression(precedence); return(expression); }