private static Expression ParseFactor(ExpressionLexer lexer)
        {
            ExprTokenType tokenType = lexer.PeekNextToken();

            if (tokenType == ExprTokenType.Number)
            {
                return(new PrimitiveExpression((int)lexer.GetNextToken(ExprTokenType.Number)));
            }
            if (tokenType == ExprTokenType.String)
            {
                return(new PrimitiveExpression((string)lexer.GetNextToken(ExprTokenType.String)));
            }
            if (tokenType == ExprTokenType.Symbol)
            {
                string symbol = (string)lexer.GetNextToken(ExprTokenType.Symbol);
                if (lexer.PeekNextToken() == ExprTokenType.Dot)
                {
                    lexer.GetNextToken(ExprTokenType.Dot);
                    Expression exprInContext = ParseFactor(lexer);
                    return(new ContextExpression(symbol, exprInContext, new Expression[0]));
                }

                if (lexer.PeekNextToken() == ExprTokenType.Open)
                {
                    lexer.GetNextToken(ExprTokenType.Open);
                    var parameters = new List <Expression>();
                    while (lexer.PeekNextToken() != ExprTokenType.Close)
                    {
                        if (parameters.Count > 0)
                        {
                            lexer.GetNextToken(ExprTokenType.Comma);
                        }
                        parameters.Add(ParseCondCombo(lexer));
                    }
                    lexer.GetNextToken(ExprTokenType.Close);
                    if (lexer.PeekNextToken() == ExprTokenType.Dot)
                    {
                        lexer.GetNextToken(ExprTokenType.Dot);
                        Expression exprInContext = ParseFactor(lexer);
                        return(new ContextExpression(symbol, exprInContext, parameters.ToArray()));
                    }
                    return(new FunctionExpression(symbol, parameters.ToArray()));
                }
                return(new SymbolExpression(symbol));
            }
            if (tokenType == ExprTokenType.Open)
            {
                lexer.GetNextToken(ExprTokenType.Open);
                Expression result = ParseCondCombo(lexer);
                lexer.GetNextToken(ExprTokenType.Close);
                return(result);
            }
            if (tokenType == ExprTokenType.Minus || tokenType == ExprTokenType.NOT)
            {
                lexer.GetNextToken(tokenType);
                return(new UnaryExpression(ParseFactor(lexer), tokenType));
            }
            throw new ParseException("Unexpected token " + tokenType, lexer.CurrentPosition);
        }
Beispiel #2
0
 public ExprToken(ExprTokenType tokenType, byte value)
 {
     if (tokenType == ExprTokenType.VARIABLE)
     {
         throw new Exception("Token type cannot be VARIABLE with a byte value.");
     }
     TokenType         = tokenType;
     Value             = value;
     AssemblerVariable = null;
 }
Beispiel #3
0
 private static Expression RecursiveDescentStep(ExpressionLexer lexer, ExprTokenType[] tokens,
     StepDownDelegate stepDown, CreateBinaryDelegate createBinary)
 {
     Expression expr = stepDown(lexer);
     while (true)
     {
         ExprTokenType? token = lexer.CheckNextToken(tokens);
         if (token.HasValue)
             expr = createBinary(token.Value, expr, stepDown(lexer));
         else
             break;
     }
     return expr;
 }
Beispiel #4
0
 public UnaryExpression(Expression operand, ExprTokenType operation)
 {
     _operand = operand;
     _operation = operation;
 }
Beispiel #5
0
 public LogicalExpression(ExprTokenType operation, Expression lhs, Expression rhs)
     : base(operation, lhs, rhs)
 {
 }
Beispiel #6
0
 public CompareExpression(ExprTokenType operation, Expression lhs, Expression rhs)
     : base(operation, lhs, rhs)
 {
 }
Beispiel #7
0
 public BinaryExpression(ExprTokenType operation, Expression lhs, Expression rhs)
 {
     _operation = operation;
     _lhs = lhs;
     _rhs = rhs;
 }
Beispiel #8
0
 public UnaryExpression(Expression operand, ExprTokenType operation)
 {
     _operand   = operand;
     _operation = operation;
 }
Beispiel #9
0
 public LogicalExpression(ExprTokenType operation, Expression lhs, Expression rhs)
     : base(operation, lhs, rhs)
 {
 }
Beispiel #10
0
 public CompareExpression(ExprTokenType operation, Expression lhs, Expression rhs)
     : base(operation, lhs, rhs)
 {
 }
Beispiel #11
0
 public BinaryExpression(ExprTokenType operation, Expression lhs, Expression rhs)
 {
     _operation = operation;
     _lhs       = lhs;
     _rhs       = rhs;
 }