예제 #1
0
        public Tokenizer(TokensProvider _tokensProvider)
        {
            tokensProvider   = _tokensProvider;
            currentChar      = null;
            tokenNameBuilder = new StringBuilder();
//            statements = new List<Statement>();
        }
예제 #2
0
        public ExpressionEvaluator(Interpreter _interpreter)
        {
            interpreter            = _interpreter;
            interpreterEnvironment = _interpreter.VariablesEnvironment;
            tokensProvider         = _interpreter.TokensProvider;

            valueTokens     = new Stack <ValueToken>();
            operatorTokens  = new Stack <Token>();
            operatorActions = new Stack <Func <Token, ValueToken> >();
            arrayCount      = new Stack <int>();
        }
예제 #3
0
        public void InterpretLine(List <Statement> statement)
        {
            if (InterpreterMode == InterpreterMode.Running)
            {
                return;
            }

            ContinueRunning = true;
            CurrentImmediateModeStatementMarker = new ImmediateModeStatementMarker(statement);

            try
            {
                while (CurrentImmediateModeStatementMarker.Valid && CurrentImmediateModeStatementMarker.StatementIndex < statement.Count)
                {
                    var currentStatement     = CurrentImmediateModeStatementMarker.Statement;
                    var commandToken         = currentStatement.Tokens[0] as Commands.CommandToken;
                    var isVariableAssignment = currentStatement.Tokens[0] is VariableNameToken;

                    if (commandToken == null && !isVariableAssignment)
                    {
                        throw new SyntaxErrorException();
                    }

                    CurrentCommandToken = commandToken;

                    TokenMarker marker = new TokenMarker(currentStatement.Tokens);
                    if (isVariableAssignment)
                    {
                        commandToken = (CommandToken)TokensProvider.GetBuiltinToken("LET");
                    }
                    else
                    {
                        marker.MoveNext();
                    }

                    commandToken.Execute(this, marker);

                    CurrentImmediateModeStatementMarker.MoveToNextStatement();
                }
            }
            catch (RetroBASIC.Exceptions.RetroBASICException basicException)
            {
                ExpressionEvaluator.Clear();
                string outputMessage = $"{ErrorMessages.errorStart} {basicException.Message}{ErrorMessages.error}";
                Console.OutputTextWriter.WriteLine();
                Console.OutputTextWriter.WriteLine(outputMessage);
            }
            finally
            {
                ContinueRunning = false;
                CurrentImmediateModeStatementMarker = null;
                InterpreterMode = InterpreterMode.Immmediate;
            }
        }
예제 #4
0
        public Interpreter(IConsole console = null)
        {
            TokensProvider = new TokensProvider();
            TokensProvider.RegisterTokens();
            tokenizer            = new Tokenizer(TokensProvider);
            Lines                = new SortedList <int, Line>();
            VariablesEnvironment = new InterpreterVariables(this);
            ExpressionEvaluator  = new ExpressionEvaluator(this);

            CurrentStatementMarker = new StatementMarker(Lines);
            CurrentImmediateModeStatementMarker = null;

            NextStatementMarker = null;
            DataStatementMarker = new StatementMarker(Lines);
            DataTokenMarker     = null;

            GosubMarkers = new Stack <StatementMarker>();
            ForNextLoops = new Stack <ForNextLoopState>();

            ContinueRunning = false;
            InterpreterMode = InterpreterMode.Immmediate;

            Console = console;
        }
예제 #5
0
        protected void InterpetLines()
        {
            InterpreterMode = InterpreterMode.Running;
            ContinueRunning = true;

            try
            {
                while (ContinueRunning)
                {
                    var currentStatement = CurrentStatementMarker.Statement;
                    if (currentStatement == null)
                    {
                        break;
                    }

                    var commandToken         = currentStatement.Tokens[0] as Commands.CommandToken;
                    var isVariableAssignment = (currentStatement.Tokens[0] as VariableNameToken) != null;

                    if (commandToken == null && !isVariableAssignment)
                    {
                        throw new SyntaxErrorException();
                    }

                    CurrentCommandToken = commandToken;

                    TokenMarker marker = new TokenMarker(currentStatement.Tokens);
                    if (isVariableAssignment)
                    {
                        commandToken = TokensProvider.GetBuiltinToken("LET") as Commands.CommandToken;
                    }
                    else
                    {
                        marker.MoveNext();
                    }
#if DEBUG
                    if (CurrentStatementMarker.LineNumber == 630)
                    {
                        CurrentStatementMarker = CurrentStatementMarker;
                    }

                    if (CurrentStatementMarker.LineNumber == 770)
                    {
                        CurrentStatementMarker = CurrentStatementMarker;
                    }
#endif

                    commandToken.Execute(this, marker);

                    if (!ContinueRunning)
                    {
                        break;
                    }

                    CurrentCommandToken = null;

                    if (NextStatementMarker != null)
                    {
                        CurrentStatementMarker.MoveTo(NextStatementMarker);
                        NextStatementMarker = null;
                    }
                    else
                    {
                        CurrentStatementMarker.MoveToNextStatement();
                    }

                    if (CurrentStatementMarker.Valid == false)
                    {
                        ContinueRunning = false;
                    }
                }
            }
            catch (RetroBASIC.Exceptions.RetroBASICException basicException)
            {
                string outputMessage = $"{ErrorMessages.errorStart} {basicException.Message}{ErrorMessages.error}{ErrorMessages.inMsg}{CurrentStatementMarker.LineNumber}";
                Console.OutputTextWriter.WriteLine();
                Console.OutputTextWriter.WriteLine(outputMessage);
            }
            finally
            {
                ContinueRunning = false;
                InterpreterMode = InterpreterMode.Immmediate;
            }
        }