Beispiel #1
0
        private void RunInstruction(T1Instruction instruction)
        {
            switch (instruction.Type)
            {
            // Variable table should contain scope pointer as well as variable id
            case T1InstructionType.Assignment:
                EvaluateExpression(((T1InstructionAssignment)instruction).Expression);
                if (((T1InstructionAssignment)instruction).Variable.VariableType != ExpressionType)
                {
                    throw new Exception("Runtime error: Variable type mismatch in expression");
                }

                ((T1InstructionAssignment)instruction).Variable.Scope.VariableTable[((T1InstructionAssignment)instruction).Variable.VariableId] = new T1Variable(((T1InstructionAssignment)instruction).Variable.VariableType, ExpressionResult);

                break;

            case T1InstructionType.ConditionalJump:
                EvaluateExpression(((T1InstructionConditionalJump)instruction).Expression);

                if ((int)ExpressionResult != 0)
                {
                    instructionPointer = LabelTable[((T1InstructionConditionalJump)instruction).LabelId] + 1;
                }

                break;

            case T1InstructionType.ExpressionEvaluation:
                EvaluateExpression(((T1InstructionEvaluateExpression)instruction).Expression);
                break;

            case T1InstructionType.FunctionCall:
                T1Scope newScope = (T1Scope)SubScopes[((T1InstructionFunctionCall)instruction).FunctionId];     // .Clone();
                newScope.ParentScope = this;

                // Variable assignments for arguments
                for (int i = 0; i < ((T1InstructionFunctionCall)instruction).Arguments.Count; i++)
                {
                    newScope.VariableTable[i + 1] = new T1Variable(((T1InstructionFunctionCall)instruction).Arguments[i].VariableType, ((T1InstructionFunctionCall)instruction).Arguments[i].Operand);
                }

                newScope.Run();
                break;

            case T1InstructionType.FunctionDeclaration:
                T1Scope subScope = ((T1InstructionFunctionDeclaration)instruction).FunctionScope;

                // Return value
                T1Variable returnValue = null;
                switch (((T1InstructionFunctionDeclaration)instruction).ReturnType)
                {
                case T1VariableType.Void:
                    returnValue = new T1Variable(T1VariableType.Void);
                    break;

                case T1VariableType.Byte:
                    returnValue = new T1Variable(T1VariableType.Byte);
                    break;

                case T1VariableType.Double:
                    returnValue = new T1Variable(T1VariableType.Double);
                    break;

                case T1VariableType.Int:
                    returnValue = new T1Variable(T1VariableType.Int);
                    break;

                case T1VariableType.String:
                    returnValue = new T1Variable(T1VariableType.String);
                    break;
                }

                subScope.VariableTable.Add(returnValue);

                // Arguments
                for (int i = 0; i < ((T1InstructionFunctionDeclaration)instruction).Arguments.Count; i++)
                {
                    T1Variable newArgument = null;
                    switch (((T1InstructionFunctionDeclaration)instruction).Arguments[i].VariableType)
                    {
                    case T1VariableType.Void:
                        newArgument = new T1Variable(T1VariableType.Void);
                        break;

                    case T1VariableType.Byte:
                        newArgument = new T1Variable(T1VariableType.Byte);
                        break;

                    case T1VariableType.Double:
                        newArgument = new T1Variable(T1VariableType.Double);
                        break;

                    case T1VariableType.Int:
                        newArgument = new T1Variable(T1VariableType.Int);
                        break;

                    case T1VariableType.String:
                        newArgument = new T1Variable(T1VariableType.String);
                        break;
                    }

                    subScope.VariableTable.Add(newArgument);
                }

                SubScopes.Add(subScope);
                break;

            case T1InstructionType.Import:
                break;

            case T1InstructionType.Jump:
                instructionPointer = LabelTable[((T1InstructionJump)instruction).LabelId] + 1;
                break;

            case T1InstructionType.LabelSet:
                LabelTable.Add(instructionPointer);
                break;

            case T1InstructionType.VariableDeclaration:
                T1Variable newVariable = null;

                switch (((T1InstructionVariableDeclaration)instruction).VariableType)
                {
                case T1VariableType.Void:
                    newVariable = new T1Variable(T1VariableType.Void, VariableTable.Count);
                    break;

                case T1VariableType.Byte:
                    newVariable = new T1Variable(T1VariableType.Byte, VariableTable.Count);
                    break;

                case T1VariableType.Double:
                    newVariable = new T1Variable(T1VariableType.Double, VariableTable.Count);
                    break;

                case T1VariableType.Int:
                    newVariable = new T1Variable(T1VariableType.Int, VariableTable.Count);
                    break;

                case T1VariableType.String:
                    newVariable = new T1Variable(T1VariableType.String, VariableTable.Count);
                    break;
                }

                VariableTable.Add(newVariable);
                break;

            case T1InstructionType.Return:
                instructionPointer = Instructions.Count;

                break;
            }
        }
Beispiel #2
0
 public void RunInstructions()
 {
     MainScope.Run();
 }