Beispiel #1
0
        private Expression ParseTerm()
        {
            //Console.WriteLine(_enumerator.Current.Kind.ToString() + "Term");
            var first = ParseFactor();

            while (_enumerator.MoveNext() &&
                   (MatchCurrentBool(TokenKind.STAR) ||
                    MatchCurrentBool(TokenKind.SLASH) ||
                    MatchCurrentBool(TokenKind.PERCENT)))
            {
                var op     = _enumerator.Current.Kind;
                var errRow = _enumerator.Current.row;
                var errCol = _enumerator.Current.column;
                if (_enumerator.MoveNext())
                {
                    var second = ParseFactor();
                    first = new BinOp(first.Row,
                                      first.Column,
                                      op,
                                      first,
                                      second
                                      );
                }
                else
                {
                    throw new SyntaxException($"Expected token at {errRow}:{errCol}", errRow, errCol);
                }
            }

            //Console.WriteLine(first.GetType().ToString());

            return(first);
        }
Beispiel #2
0
        private Expression ParseExpr()
        {
            //Console.WriteLine(_enumerator.Current.Kind.ToString() + "Expr");
            var first = ParseTerm();

            while (
                MatchCurrentBool(TokenKind.PLUS) ||
                MatchCurrentBool(TokenKind.MINUS))
            {
                var op     = _enumerator.Current.Kind;
                var ErrRow = _enumerator.Current.row;
                var ErrCol = _enumerator.Current.column;
                if (_enumerator.MoveNext())
                {
                    var second = ParseTerm();
                    first = new BinOp(first.Row,
                                      first.Column,
                                      op,
                                      first,
                                      second
                                      );
                }
            }

            if (MatchCurrentBool(TokenKind.IF) && _enumerator.MoveNext())
            {
                var condition = ParseExpr();

                MatchCurrent(TokenKind.ELSE);
                _enumerator.MoveNext();
                var elseExpression = ParseExpr();
                return(new ConditionalExpression(first.Row,
                                                 first.Column,
                                                 first,
                                                 condition,
                                                 elseExpression));
            }

            if (MatchCurrentBool(TokenKind.LESS) ||
                MatchCurrentBool(TokenKind.GREATER) ||
                MatchCurrentBool(TokenKind.EQEQUAL) ||
                MatchCurrentBool(TokenKind.GREATEREQUAL) ||
                MatchCurrentBool(TokenKind.LESSEQUAL))
            {
                var op = _enumerator.Current.Kind;
                if (_enumerator.MoveNext())
                {
                    var third = ParseExpr();
                    first = new BinOp(first.Row,
                                      first.Column,
                                      op,
                                      first,
                                      third
                                      );
                }
            }

            if (MatchCurrentBool(TokenKind.CIRCUMFLEX))
            {
                var op = _enumerator.Current.Kind;
                if (_enumerator.MoveNext())
                {
                    var fourth = ParseExpr();
                    first = new BinOp(first.Row,
                                      first.Column,
                                      op,
                                      first,
                                      fourth
                                      );
                }
            }

            //first.PrintOp(0);

            return(first);
        }