コード例 #1
0
        /// <summary>
        /// Visit a value expression
        /// </summary>
        /// <param name="expression">The expression</param>
        /// <exception cref="ArgumentNullException">Thrown when the input parameter is null</exception>
        public void Visit(ValueExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            switch (expression.Type)
            {
            case ValueExpressionType.Integer:
                result = int.Parse(expression.Text, CultureInfo.InvariantCulture);
                break;

            case ValueExpressionType.Variable:
                int value;
                int dereferencedValue;
                int pointerValue;
                if (context.TryResolveSymbol(expression.Text, out value))
                {
                    result = context.Variables[expression.Text];
                }
                else if (context.TryResolveDereference(expression.Text, out dereferencedValue, out pointerValue))
                {
                    // give a dummy pointer address for the pointer.
                    result = pointerValue;
                }
                else
                {
                    int intValue;
                    if (Int32.TryParse(expression.Text, out intValue))
                    {
                        result = intValue;
                    }
                    else
                    {
                        result = expression.Text;
                    }
                }
                break;

            case ValueExpressionType.Null:
                result = expression.Text;
                break;

            default:
                break;
            }
        }
コード例 #2
0
        /// <summary>
        /// The Literal operation
        /// </summary>
        /// <returns>An expression node created based on the operation</returns>
        protected ExpressionNode Literal()
        {
            if (currentToken.Text.Length > 0)
            {
                if (currentToken.Type == TokenType.Integer)
                {
                    int res;
                    if (TryConvertNumber(currentToken.Text, out res))
                    {
                        ExpressionNode integer = new ExpressionNode(
                            new Token(TokenType.Integer, res.ToString()));
                        GetNextToken();
                        return(integer);
                    }
                    else
                    {
                        throw new ExpressionEvaluatorException(
                                  String.Format("invalid number '{0}'", currentToken.Text));
                    }
                }
                else if (currentToken.Type == TokenType.String)
                {
                    int x;

                    if (!context.TryResolveSymbol(currentToken.Text, out x))
                    {
                        string tokenText = currentToken.Text;
                        while (true)
                        {
                            if (context.Variables != null &&
                                !context.Variables.ContainsKey(currentToken.Text) &&
                                !DatatypeInfoProvider.IsPredefinedDatatype(currentToken.Text) &&
                                !DatatypeInfoProvider.isPredefinedModifier(currentToken.Text))
                            {
                                string typeInfo;
                                if (context.TryResolveCustomType(currentToken.Text, out typeInfo))
                                {
                                    tokenText = typeInfo;
                                    break;
                                }
                                else
                                {
                                    context.ReportError(
                                        String.Format("cannot resolve symbol '{0}'", currentToken.Text));
                                    break;
                                }
                            }
                            if (context.Variables != null &&
                                context.Variables.ContainsKey(currentToken.Text))
                            {
                                tokenText = currentToken.Text;
                                break;
                            }

                            // treat predefinedModifier as empty node
                            if (DatatypeInfoProvider.isPredefinedModifier(currentToken.Text))
                            {
                                GetNextToken();
                            }
                            else if (DatatypeInfoProvider.IsPredefinedDatatype(currentToken.Text))
                            {
                                tokenText = currentToken.Text;
                                break;
                            }
                        }

                        // in order to get pointer type size and avoid related issue
                        bool isPointerType = false;
                        GetNextToken();
                        if (isInSizeOf)
                        {
                            while (currentToken.Text == "*")
                            {
                                GetNextToken();
                                isPointerType = true;
                            }
                        }

                        if (isPointerType)
                        {
                            ExpressionNode integer = new ExpressionNode(
                                new Token(TokenType.Integer, "4"));
                            return(integer);
                        }
                        ExpressionNode variable = new ExpressionNode(
                            new Token(TokenType.Variable, tokenText));

                        return(variable);
                    }
                    else if (isInSizeOf)
                    {
                        string tokenText = currentToken.Text;

                        GetNextToken();

                        //ignoring other expression
                        while (currentToken.Text != ")")
                        {
                            GetNextToken();
                        }

                        return(new ExpressionNode(
                                   new Token(TokenType.Variable, tokenText)));
                    }
                    else
                    {
                        GetNextToken();
                        return(new ExpressionNode(
                                   new Token(TokenType.Integer, x.ToString())));
                    }
                }
                else
                {
                    throw new ExpressionEvaluatorException(
                              String.Format("unexpected token '{0}'", currentToken));
                }
            }
            else
            {
                throw new ExpressionEvaluatorException("unexpected end of input");
            }
        }