示例#1
0
        object EvalAttribute(AttributeExpression expr, EvalOption option)
        {
            var value = Eval(expr.Value, option);

            if (value is Attribute)
            {
                value = (value as Attribute).Value;
            }

            var table = value as SymbolTable;

            if (table == null)
            {
                throw new UnknownValueException(expr.Attribute, expr.Line);
            }

            if (!table.Tables.ContainsKey(expr.Attribute))
            {
                throw new UnknownValueException(expr.Attribute, expr.Line);
            }

            return(table.Tables[expr.Attribute]);
        }
示例#2
0
        Expression Group()
        {
            var        token = Peek();
            Expression value = null;

            if (token != null)
            {
                if (token.Type == TokenType.LeftParentheses)
                {
                    Next();
                    var center = Expr();
                    var right  = Peek();

                    if (right != null && right.Type == TokenType.RightParentheses)
                    {
                        Next();
                        value = center;
                    }
                    else
                    {
                        if (token == null)
                        {
                            throw new InvalidEOFException(tokens.LastOrDefault().Line);
                        }

                        throw new CompileException(string.Format("Invalid token {0}", token), token.Line);
                    }
                }
                else if (token.Type == TokenType.Operator && ((string)token.Value == "+" || (string)token.Value == "-"))
                {
                    Next();
                    var rhs = Group();

                    var ret = new UnaryOpExpression();
                    ret.Line     = token.Line;
                    ret.Expr     = rhs;
                    ret.Operator = (string)token.Value;
                    value        = ret;
                }
                else if (token.Type == TokenType.Label)
                {
                    var ret = Value();
                    ret.Line = token.Line;
                    value    = ret;
                }
                else if (token.Type == TokenType.Digit)
                {
                    Next();
                    var ret = new NumberExpression((float)token.Value);
                    ret.Line = token.Line;
                    value    = ret;
                }
                else
                {
                    throw new InvalidTokenException(token.ToString(), token.Line);
                }
            }
            else
            {
                throw new InvalidEOFException(tokens.LastOrDefault().Line);
            }

            while (true)
            {
                var dotToken = Peek();

                if (dotToken != null && dotToken.Type == TokenType.Dot)
                {
                    var next = Next();
                    if (next == null)
                    {
                        throw new InvalidEOFException(tokens.LastOrDefault().Line);
                    }
                    else if (next.Type != TokenType.Label)
                    {
                        throw new InvalidTokenException(token.ToString(), token.Line);
                    }

                    var v_ = new AttributeExpression();
                    v_.Value     = value;
                    v_.Attribute = (string)next.Value;
                    v_.Line      = next.Line;
                    value        = v_;

                    Next();
                }
                else
                {
                    break;
                }
            }

            return(value);
        }