Exemplo n.º 1
0
        private Ast WhileLoop()
        {
            if (!_tokenStream.IsMatch <WhileToken>())
            {
                return(null);
            }

            WhileToken token = _tokenStream.Take <WhileToken>();

            _tokenStream.Take <LeftParensToken>();

            Ast condition = Expression();

            if (condition == null)
            {
                throw new InvalidSyntaxException("Condition of while-loop must be an expression", _tokenStream.Current.Position, null);
            }

            _tokenStream.Take <RightParensToken>();

            Ast body = Statement();

            if (body == null)
            {
                throw new InvalidSyntaxException("While-loop must have a body statement", _tokenStream.Current.Position, null);
            }

            return(new WhileLoop(token, condition, body.ToScope()));
        }
Exemplo n.º 2
0
        public Token GetToken()
        {
            State = LexicAnalyserState.Initial;
            Value = HasNext ? Character.ToString() : "";
            Token token = null;

            while (HasNext && token == null)
            {
                switch (State)
                {
                case LexicAnalyserState.Initial:
                    HandleInitial();
                    break;

                case LexicAnalyserState.NonTerminal:
                    token = new NonTerminalToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Space:
                    token = new SpaceToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Terminal:
                    token = new TerminalToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Empty:
                    token = new EmptyToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.NewLine:
                    token = new NewLineToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Identifier:
                    token = new IdentifierToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Number:
                    token = new NumberToken(Value);
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Var:
                    token = new VarToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Write:
                    token = new WriteToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Read:
                    token = new ReadToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.If:
                    token = new IfToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.End:
                    token = new EndToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Then:
                    token = new ThenToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Begin:
                    token = new BeginToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.While:
                    token = new WhileToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Do:
                    token = new DoToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.SemiColon:
                    token = new SemiColonToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.OpenParentheses:
                    token = new OpenParenthesesToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.CloseParentheses:
                    token = new CloseParenthesesToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Plus:
                    token = new PlusToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Sub:
                    token = new SubToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Great:
                    token = new GreatToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Less:
                    token = new LessToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Equal:
                    token = new EqualToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Repeat:
                    token = new RepeatToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Until:
                    token = new UntilToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.Attribution:
                    token = new AttributionToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.NotEqual:
                    token = new NotEqualToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.GreatOrEqual:
                    token = new GreatOrEqualToken();
                    CurrentIndex++;
                    break;

                case LexicAnalyserState.LessOrEqual:
                    token = new LessOrEqualToken();
                    CurrentIndex++;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(token);
        }