private ExcelExpression Term() { var res = Factor(); var next = Peek(); if (next != null) { switch (next.Kind) { case TokenKind.Star: Read(); res = ExcelExpression.Multiply(res, Expr()); break; case TokenKind.Slash: Read(); res = ExcelExpression.Divide(res, Expr()); break; case TokenKind.Percent: Read(); res = ExcelExpression.Modulo(res, Expr()); break; } } return(res); }
public static LambdaExpression Compile(ExcelExpression expression) { var comp = new Impl(); var res = comp.Visit(expression); return(Expression.Lambda(res, comp._cellReferences.Values)); }
private ExcelExpression Factor() { var token = Read(); if (token == null) { throw new InvalidOperationException(); } switch (token.Kind) { case TokenKind.Number: return(ExcelExpression.Number(double.Parse(token.Value, CultureInfo.CurrentCulture /* UI */))); case TokenKind.Identifier: var next = Peek(); if (next != null && next.Kind == TokenKind.OpenParen) { return(ExcelExpression.Formula(token.Value, ArgList())); } else if (next != null && next.Kind == TokenKind.Colon) { Read(); var endRange = Peek(); if (endRange == null || endRange.Kind != TokenKind.Identifier) { throw new InvalidOperationException("Incomplete range expression."); } endRange = Read(); return(ExcelExpression.Range(token.Value, endRange.Value)); } else { return(ExcelExpression.Cell(token.Value)); } case TokenKind.OpenParen: var res = Expr(); var closeParen = Read(); if (closeParen == null || closeParen.Kind != TokenKind.CloseParen) { throw new InvalidOperationException(); } return(res); default: throw new InvalidOperationException("Unexpected token: " + token.Kind); } }
private ExcelExpression Expr() { var res = Term(); var next = Peek(); if (next != null) { switch (next.Kind) { case TokenKind.Plus: Read(); res = ExcelExpression.Add(res, Expr()); break; case TokenKind.Dash: Read(); res = ExcelExpression.Subtract(res, Expr()); break; } } return(res); }