private ExpressionValue EvaluateMacro(ExpressionNode node)
        {
            Contract.Requires(node.Kind == ExpressionKind.Leaf);
            Contract.Requires(node.Token.TokenType == TokenType.Word);

            ExpressionValue value = default(ExpressionValue);
            Macro           m     = null;

            if (_macroMap.TryGetValue(node.Token.Value, out m))
            {
                Number numValue;
                if (TokenHelper.TryConvertToNumber(m.Value, out numValue))
                {
                    value = ExpressionValue.Create(numValue);
                }
                else
                {
                    value = ExpressionValue.Create(1);
                }
            }
            else
            {
                value = ExpressionValue.Create(0);
            }

            return(value);
        }
Exemple #2
0
        public void VerifyParse(string str, object value)
        {
            Number converted;

            Assert.True(Helper.TryConvertToNumber(str, out converted));
            Assert.Equal(value, converted.Value);
        }
Exemple #3
0
        public void Unsigned3()
        {
            Number val;

            Assert.True(Helper.TryConvertToNumber("-42", out val));
            Assert.Equal(-42, val.Integer);
        }
Exemple #4
0
        public void Unsigned2()
        {
            Number val;

            Assert.True(Helper.TryConvertToNumber("6000000000U", out val));
            Assert.Equal(6000000000L, val.Long);
        }
Exemple #5
0
        public void Exponent3()
        {
            Number val;

            Assert.True(Helper.TryConvertToNumber("6.5e2L", out val));
            Assert.Equal(650f, val.Single);
        }
Exemple #6
0
        public void Exponent1()
        {
            Number val;

            Assert.True(Helper.TryConvertToNumber("6e2", out val));
            Assert.Equal(600f, val.Single);
        }
Exemple #7
0
        public void Float1()
        {
            Number val;

            Assert.True(Helper.TryConvertToNumber("6.5F", out val));
            Assert.Equal(6.5f, val.Single);
        }
Exemple #8
0
        public void Signed1()
        {
            Number val;

            Assert.True(Helper.TryConvertToNumber("42", out val));
            Assert.Equal(42, val.Integer);
            Assert.True(Helper.TryConvertToNumber("400", out val));
            Assert.Equal(400, val.Integer);
            Assert.True(Helper.TryConvertToNumber("-1", out val));
            Assert.Equal(-1, val.Integer);
        }
        private ExpressionValue EvaluateLeaf(ExpressionNode node)
        {
            Token token = node.Token;

            if (token.IsNumber)
            {
                Number value;
                if (!TokenHelper.TryConvertToNumber(node.Token, out value))
                {
                    throw new Exception($"Can't convert token to number {node.Token}");
                }

                return(ExpressionValue.Create(value));
            }
            else if (token.TokenType == TokenType.TrueKeyword)
            {
                return(ExpressionValue.Create(true));
            }
            else if (token.TokenType == TokenType.FalseKeyword)
            {
                return(ExpressionValue.Create(false));
            }
            else if (token.TokenType == TokenType.Word)
            {
                return(EvaluateMacro(node));
            }
            else if (token.IsCharacter)
            {
                char cValue = '0';
                if (!TokenHelper.TryConvertToChar(node.Token, out cValue))
                {
                    throw new Exception($"Can't convert token to char {node.Token}");
                }
                return(ExpressionValue.Create(cValue));
            }
            else if (token.IsQuotedString)
            {
                string sValue = null;
                if (!TokenHelper.TryConvertToString(token, out sValue))
                {
                    throw new Exception($"Can't convert token to string {node.Token}");
                }
                return(ExpressionValue.Create(sValue));
            }
            else if (TokenHelper.IsKeyword(node.Token.TokenType))
            {
                return(ExpressionValue.Create(1));
            }
            else
            {
                throw new Exception($"Unexpected leaf token {node.Token}");
            }
        }
Exemple #10
0
        public void Invalid1()
        {
            Number val;

            Assert.False(Helper.TryConvertToNumber("aoo", out val));
        }