Пример #1
0
        public override void Execute(Interpreter interpreter, TokenMarker tokenMarker)
        {
            var             variableNameToken = tokenMarker.Token as VariableNameToken;
            ValueTokenArray indicies          = null;

            if (variableNameToken == null)
            {
                throw new Exceptions.SyntaxErrorException();
            }

            if (tokenMarker.PeekNext() is OpenParenToken)
            {
                indicies = interpreter.ExpressionEvaluator.EvaluateVariableArrayAssignment(tokenMarker);
            }
            else
            {
                tokenMarker.MoveNext();
            }

            var equalsToken = tokenMarker.Token;

            if (!(equalsToken is Operators.EqualToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            if (tokenMarker.GetNextToken() == null)
            {
                throw new Exceptions.SyntaxErrorException();
            }

            var result = interpreter.ExpressionEvaluator.Evaluate(tokenMarker);

            interpreter.VariablesEnvironment.SetVariableValue(variableNameToken, indicies, result);
        }
Пример #2
0
        public override void Execute(Interpreter interpreter, TokenMarker tokenMarker)
        {
            var variableNameTokens = new List <VariableNameToken>();

            while (tokenMarker.Token != null)
            {
                if (!(tokenMarker.Token is VariableNameToken variableNameToken))
                {
                    throw new Exceptions.SyntaxErrorException();
                }

                if (!((variableNameToken.VariableType == VariableValueType.IntegerNumber) ||
                      (variableNameToken.VariableType == VariableValueType.RealNumber)))
                {
                    throw new Exceptions.TypeMismatchException();
                }

                variableNameTokens.Add(variableNameToken);

                // Maybe add more variable names to the list
                if (tokenMarker.GetNextToken() is CommaToken)
                {
                    tokenMarker.MoveNext();
                    continue;
                }
            }

            if (interpreter.ForNextLoops.Count == 0)
            {
                throw new Exceptions.NextWithoutForException();
            }

            if (variableNameTokens.Count == 0)
            {
                ProcessForNextState(interpreter, interpreter.ForNextLoops.Peek());
                return;
            }

            int variableNameIndex = 0;

            while (variableNameIndex < variableNameTokens.Count)
            {
                if (interpreter.ForNextLoops.TryPeek(out ForNextLoopState forNextState) == false)
                {
                    throw new Exceptions.NextWithoutForException();
                }

                bool forNextLoopDone = ProcessForNextState(interpreter, forNextState, variableNameTokens[variableNameIndex]);
                if (!forNextLoopDone)
                {
                    return;
                }

                variableNameIndex += 1;
            }
        }
Пример #3
0
        public override void Execute(Interpreter interpreter, TokenMarker tokenMarker)
        {
            if (!(tokenMarker.Token is ToToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            if (!(tokenMarker.GetNextToken() is NumericValueToken numberToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            var gotoMarker = interpreter.CreateStatementMarker();

            if (gotoMarker.MoveToLine(numberToken.IntValue) == false)
            {
                throw new Exceptions.UndefinedStatementException();
            }

            interpreter.NextStatementMarker = gotoMarker;
        }
Пример #4
0
        public override void Execute(Interpreter interpreter, TokenMarker tokenMarker)
        {
            // Check to see if in direct mode

            if (!(tokenMarker.Token is FnToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }


            var userDefinedFunctionNameToken = tokenMarker.GetNextToken();

            if (!(userDefinedFunctionNameToken is VariableNameToken userDefinedNameToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            if (userDefinedNameToken.VariableType != VariableValueType.RealNumber)
            {
                throw new Exceptions.TypeMismatchException();
            }

            var openParenToken = tokenMarker.GetNextToken();

            if (!(openParenToken is OpenParenToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            var parameterToken = tokenMarker.GetNextToken();

            if (!(parameterToken is VariableNameToken functionParameterToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            var closeParenToken = tokenMarker.GetNextToken();

            if (!(closeParenToken is CloseParenToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            var equalsParenToken = tokenMarker.GetNextToken();

            if (!(equalsParenToken is Operators.EqualToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            List <Token> userDefinedFunctionTokens = new List <Token>();

            while (tokenMarker.GetNextToken() != null)
            {
                userDefinedFunctionTokens.Add(tokenMarker.Token);
            }

            var userDefinedStatement = new Statement(userDefinedFunctionTokens);

            var userDefinedFunction = new Functions.UserDefinedFunction(functionParameterToken, userDefinedStatement);

            interpreter.VariablesEnvironment.CreateUserDefinedFunction(userDefinedNameToken, userDefinedFunction);
        }
Пример #5
0
        public override void Execute(Interpreter interpreter, TokenMarker tokenMarker)
        {
            if (!(tokenMarker.Token is VariableNameToken variableNameToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            if (variableNameToken.VariableType != VariableValueType.RealNumber)
            {
                throw new Exceptions.SyntaxErrorException();
            }

            if (!(tokenMarker.GetNextToken() is EqualToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            tokenMarker.MoveNext();

            NumericValueToken startValue;

            try
            {
                startValue = (NumericValueToken)interpreter.ExpressionEvaluator.Evaluate(tokenMarker);
            }
            catch (InvalidCastException)
            {
                throw new Exceptions.TypeMismatchException();
            }

            if (!(tokenMarker.Token is ToToken))
            {
                throw new Exceptions.SyntaxErrorException();
            }

            tokenMarker.MoveNext();

            NumericValueToken endValue;

            try
            {
                endValue = (NumericValueToken)interpreter.ExpressionEvaluator.Evaluate(tokenMarker);
            }
            catch (InvalidCastException)
            {
                throw new Exceptions.TypeMismatchException();
            }

            NumericValueToken stepValueToken = null;

            if (tokenMarker.Token != null)
            {
                if (!(tokenMarker.Token is StepToken))
                {
                    throw new Exceptions.SyntaxErrorException();
                }

                tokenMarker.MoveNext();
                var result = interpreter.ExpressionEvaluator.Evaluate(tokenMarker);
                if (!(result is NumericValueToken))
                {
                    throw new Exceptions.TypeMismatchException();
                }
                stepValueToken = (NumericValueToken)result;
            }
            else
            {
                // Default step is 1 or -1
//                if (startValue.RealValue <= endValue.RealValue)
                stepValueToken = interpreter.TokensProvider.CreateRealValueToken(1);
//                else
//                    stepValueToken = interpreter.TokensProvider.CreateRealValueToken(-1);
            }

            ForNextLoopState forNextLoopState;

            if (interpreter.InterpreterMode == InterpreterMode.Running)
            {
                var forNextMarker = interpreter.CopyCurrentStatementMarker();

                // In immediate mode, FOR may not neccessarily be followed by a statement.
                if (forNextMarker.Valid)
                {
                    forNextMarker.MoveToNextStatement();
                }

                forNextLoopState = new ForNextLoopState(forNextMarker, variableNameToken, startValue, endValue, stepValueToken);
            }
            else
            {
                var index = interpreter.CurrentImmediateModeStatementMarker.StatementIndex;
                forNextLoopState = new ForNextLoopState(index, variableNameToken, startValue, endValue, stepValueToken);
            }

            interpreter.ForNextLoops.Push(forNextLoopState);
            interpreter.VariablesEnvironment.SetVariableValue(variableNameToken, startValue);
        }
Пример #6
0
        public override void Execute(Interpreter interpreter, TokenMarker tokenMarker)
        {
            // Short circuit in case of no parameters
            if (tokenMarker.Token == null)
            {
                interpreter.List();
                return;
            }

            int startIndex = -1;
            int endIndex   = int.MaxValue;

            Token token;
            bool  haveMinus    = false;
            bool  haveEndIndex = false;

            // Look for a starting line or a '-'
            token = tokenMarker.Token;
            switch (token)
            {
            case NumericValueToken startValueToken:
                startIndex = startValueToken.IntValue;
                break;

            case MinusToken minusStartToken:
                haveMinus = true;
                break;

            default:
                throw new Exceptions.SyntaxErrorException();
            }

            token = tokenMarker.GetNextToken();
            if (token == null)
            {
                interpreter.List(startIndex, startIndex);
                return;
            }

            //Look for a '-' or an ending number
            switch (token)
            {
            case MinusToken minusEndToken:
                if (haveMinus)
                {
                    throw new Exceptions.SyntaxErrorException();
                }
                haveMinus = true;
                endIndex  = int.MaxValue;
                break;

            case NumericValueToken endValueToken1:
                endIndex     = endValueToken1.IntValue;
                haveEndIndex = true;
                break;

            default:
                throw new Exceptions.SyntaxErrorException();
            }

            token = tokenMarker.GetNextToken();
            if (token == null)
            {
                interpreter.List(startIndex, endIndex);
                return;
            }

            // Look for an ending line number
            switch (token)
            {
            case NumericValueToken endValueToken2:
                if (haveEndIndex)
                {
                    throw new Exceptions.SyntaxErrorException();
                }
                endIndex = endValueToken2.IntValue;
                break;

            default:
                throw new Exceptions.SyntaxErrorException();
            }

            interpreter.List(startIndex, endIndex);
        }