コード例 #1
0
        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}");
            }
        }
コード例 #2
0
        public void VerifyChar(char c)
        {
            Token token     = new Token(TokenType.CharacterAnsi, "'" + c + "'");
            char  converted = '0';

            Assert.True(Helper.TryConvertToChar(token, out converted));
            Assert.Equal(c, converted);

            token = new Token(TokenType.CharacterUnicode, "L'" + c + "'");
            Assert.True(Helper.TryConvertToChar(token, out converted));
            Assert.Equal(c, converted);
        }