Exemplo n.º 1
0
        private static YCodeNode DigitNode(char c)
        {
            YCodeNode node = new YCodeNode();

            node.type = TokenType.CONST;

            while (char.IsDigit(c) || c == '.')
            {
                tokenBuffer += c;
                c            = GetChar();
            }

            BackSpaceChar();

            node.lexicon = tokenBuffer;

            float v;

            if (!float.TryParse(node.lexicon, out v))
            {
                ///The Number is invalid. Set the value to null and generate error message.
                node.value = null;

                AppendErrorMessage(node, ErrorType.InvalidNumber);
            }
            else
            {
                node.value = v;
            }
            return(node);
        }
Exemplo n.º 2
0
        private static YCodeNode LetterNode(char c)
        {
            YCodeNode node = new YCodeNode();

            while (char.IsLetterOrDigit(c))
            {
                tokenBuffer += c;
                c            = GetChar();
            }

            BackSpaceChar();

            if (c == '(')
            {
                ///This is a Function.
                node.type = TokenType.FUNC;
            }
            else
            {
                ///This is a Variable.
                node.type = TokenType.VAR;
            }

            node.lexicon = tokenBuffer;
            node.value   = YCodeVariableBuffer.GetVariable(node.lexicon);

            return(node);
        }
Exemplo n.º 3
0
        public static YCodeNode CompareNode(char c)
        {
            YCodeNode node = new YCodeNode();

            tokenBuffer += c;

            switch (c)
            {
            case '>':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.MOREEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.MORE;
                    BackSpaceChar();
                }
                break;

            case '<':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.LESSEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.LESS;
                    BackSpaceChar();
                }
                break;

            case '=':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.EQUALEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.EQUAL;
                    BackSpaceChar();
                }
                break;
            }

            node.lexicon = tokenBuffer;

            return(node);
        }
Exemplo n.º 4
0
        public static YCodeNode ExprNode(char c)
        {
            YCodeNode node = new YCodeNode();

            node.type = TokenType.EXPR;

            tokenBuffer += c;

            c = GetChar();

            int lBracket = 1, rBracket = 0;

            while (lBracket != rBracket && c != '\0')
            {
                if (c == '(')
                {
                    lBracket++;
                }
                else if (c == ')')
                {
                    rBracket++;
                }

                tokenBuffer += c;

                c = GetChar();
            }

            if (c != '\0')
            {
                BackSpaceChar();
            }

            if (lBracket != rBracket)
            {
                node.value = null;
            }
            else
            {
                node.value = (float)0f;
            }

            node.lexicon = tokenBuffer;

            return(node);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get the nearest node according to the currentPointer.
        /// Meanwhile generates error messages.
        /// </summary>
        /// <returns>The node.</returns>
        private static YCodeNode GetNode()
        {
            YCodeNode node;

            tokenBuffer = "";

            char c = GetChar();

            if (char.IsLetter(c))
            {
                node = LetterNode(c);
            }
            else if (char.IsDigit(c))
            {
                node = DigitNode(c);
            }
            else if (c == '&' || c == '|' || c == '!')
            {
                node = LogicNode(c);
            }
            else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
            {
                node = OperatorNode(c);
            }
            else if (c == '>' || c == '=' || c == '<')
            {
                node = CompareNode(c);
            }
            else if (c == '(')
            {
                node = ExprNode(c);
            }
            else
            {
                node = new YCodeNode(TokenType.ERRTOKEN, ")", null);
                ///Call for generating Error.
            }
            return(node);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Appends the Error Message to the List.
 /// </summary>
 /// <param name="node">Node.</param>
 /// <param name="errType">Error type.</param>
 public static void AppendErrorMessage(YCodeNode node, ErrorType errType)
 {
 }
Exemplo n.º 7
0
        public static YCodeNode OperatorNode(char c)
        {
            YCodeNode node = new YCodeNode();

            tokenBuffer += c;
            node.value   = (float)0f;

            switch (c)
            {
            case '+':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.PLUSEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.PLUS;
                    BackSpaceChar();
                }
                break;

            case '-':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.MINUSEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.MINUS;
                    BackSpaceChar();
                }
                break;

            case '*':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.MULEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.MUL;
                    BackSpaceChar();
                }
                break;

            case '/':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.DIVEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.DIV;
                    BackSpaceChar();
                }
                break;

            case '^':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.POWEQ;
                    tokenBuffer += '=';
                }
                else
                {
                    node.type = TokenType.POW;
                    BackSpaceChar();
                }
                break;
            }

            node.lexicon = tokenBuffer;

            return(node);
        }
Exemplo n.º 8
0
        public static YCodeNode LogicNode(char c)
        {
            YCodeNode node = new YCodeNode();

            node.value = (float)0f;

            tokenBuffer += c;

            switch (c)
            {
            case '&':
                node.type = TokenType.AND;
                c         = GetChar();
                if (c != '&')
                {
                    node.value = null;
                    BackSpaceChar();
                    ///Error Message.
                }
                else
                {
                    tokenBuffer += c;
                }
                break;

            case '|':
                node.type = TokenType.OR;
                c         = GetChar();
                if (c != '|')
                {
                    node.value = null;
                    BackSpaceChar();
                    ///Error Message.
                }
                else
                {
                    tokenBuffer += c;
                }
                break;

            case '!':
                c = GetChar();
                if (c == '=')
                {
                    node.type    = TokenType.NOTEQUAL;
                    tokenBuffer += c;
                }
                else
                {
                    node.type = TokenType.NOT;
                    BackSpaceChar();
                }
                break;

            default:
                ///Actually, this is impossible ...
                break;
            }

            node.lexicon = tokenBuffer;

            return(node);
        }