Beispiel #1
0
        public static void Less()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 < val1 ? 1 : 0);
        }
Beispiel #2
0
        public static void Greater()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 > val1 ? 1 : 0);
        }
Beispiel #3
0
        public static void Or()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 | val1);
        }
Beispiel #4
0
        public static void Equal()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 == val1 ? 1 : 0);
        }
Beispiel #5
0
        public static void Modulo()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 % val1);
        }
Beispiel #6
0
        public static void Divide()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 / val1);
        }
Beispiel #7
0
        public static void Multiply()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 * val1);
        }
Beispiel #8
0
        public static void Subtract()
        {
            int val1 = (int)StackManipulation.Pop();
            int val2 = (int)StackManipulation.Pop();

            StackManipulation.Push(val2 - val1);
        }
Beispiel #9
0
        public static void Execute(KeyValuePair <string, string> instruction)
        {
            switch (instruction.Key)
            {
                #region StackManipulation
            case "push":
                StackManipulation.Push(int.Parse(instruction.Value));
                break;

            case "rvalue":
                StackManipulation.RValue(instruction.Value);
                break;

            case "lvalue":
                StackManipulation.LValue(instruction.Value);
                break;

            case "pop":
                StackManipulation.Pop();
                break;

            case ":=":
                StackManipulation.Set();
                break;

            case "copy":
                StackManipulation.Copy();
                break;

                #endregion
                #region ControlFlow
            case "goto":
                ControlFlow.Goto(instruction.Value);
                break;

            case "gofalse":
                ControlFlow.GoFalse(instruction.Value);
                break;

            case "gotrue":
                ControlFlow.GoTrue(instruction.Value);
                break;

            case "halt":
                ControlFlow.Halt();
                break;

                #endregion
                #region ArithmeticOps
            case "+":
                ArithmeticOps.Add();
                break;

            case "-":
                ArithmeticOps.Subtract();
                break;

            case "*":
                ArithmeticOps.Multiply();
                break;

            case "/":
                ArithmeticOps.Divide();
                break;

            case "div":
                ArithmeticOps.Modulo();
                break;

                #endregion
                #region LogicalOps
            case "&":
                LogicalOps.And();
                break;

            case "|":
                LogicalOps.Or();
                break;

            case "!":
                LogicalOps.Not();
                break;

                #endregion
                #region RelationalOps
            case "<>":
                RelationalOps.NotEqual();
                break;

            case "=":
                RelationalOps.Equal();
                break;

            case ">=":
                RelationalOps.GreaterOrEqual();
                break;

            case "<=":
                RelationalOps.LessOrEqual();
                break;

            case ">":
                RelationalOps.Greater();
                break;

            case "<":
                RelationalOps.Less();
                break;

                #endregion
                #region Output
            case "print":
                Output.Print();
                break;

            case "show":
                Output.Show(instruction.Value);
                break;

                #endregion
                #region Subprogram Control
            case "begin":
                SubprogramControl.Begin();
                break;

            case "end":
                SubprogramControl.End();
                break;

            case "return":
                SubprogramControl.Return();
                break;

            case "call":
                SubprogramControl.Call(instruction.Value);
                break;
                #endregion
            }
        }
Beispiel #10
0
        // The main method.  Here we set-up the entire file and execute it.

        public static void Main(string[] args)
        {
            try
            {
                StackManipulation   stack = new StackManipulation();
                ArithmeticOperators arOp  = new ArithmeticOperators(stack);
                LogicalOperators    loOp  = new LogicalOperators(stack);
                RelationalOperators relOp = new RelationalOperators(stack);
                Output output             = new Output(stack);

                // Get the filename, parse the code, and analyze it
                string filename = (new JazInterpreter()).getFileName(args);
                string[,] array = (new CodeParser()).Parse(filename);
                (new Analyzer()).Analyze(array);

                // instruction pointer is used to decide where we are in the code
                int instructionPointer = 0;

                // defines current variable scope
                int currentLevel = 0;

                // flags for scoping
                bool isAfterBeginButBeforeCall = false;
                bool isAfterCallButBeforeEnd   = false;

                while (true)
                {
                    switch (array[instructionPointer, 0])
                    {
                    case "push":
                        stack.Push(Int32.Parse(array[instructionPointer, 1]));
                        break;

                    case "rvalue":
                        if (isAfterCallButBeforeEnd)
                        {
                            Identifier identifier = SymbolsTable.VariableTable[currentLevel + 1].Find(x => x.Name == array[instructionPointer, 1]);
                            stack.RValue(identifier);
                        }
                        else
                        {
                            Identifier identifier = SymbolsTable.VariableTable[currentLevel].Find(x => x.Name == array[instructionPointer, 1]);
                            stack.RValue(identifier);
                        }
                        break;

                    case "lvalue":
                        if (isAfterBeginButBeforeCall)
                        {
                            Identifier identifier2 = SymbolsTable.VariableTable[currentLevel + 1].Find(x => x.Name == array[instructionPointer, 1]);
                            stack.LValue(identifier2);
                        }
                        else
                        {
                            Identifier identifier2 = SymbolsTable.VariableTable[currentLevel].Find(x => x.Name == array[instructionPointer, 1]);
                            stack.LValue(identifier2);
                        }
                        break;

                    case "pop":
                        stack.Pop();
                        break;

                    case ":=":
                        stack.ColonEquals();
                        break;

                    case "copy":
                        stack.Copy();
                        break;

                    case "label":
                        break;

                    case "goto":
                        instructionPointer = ControlFlow.Goto(array[instructionPointer, 1]);
                        break;

                    case "gofalse":
                        if (!Convert.ToBoolean(stack.Peek()))
                        {
                            instructionPointer = ControlFlow.Gofalse(array[instructionPointer, 1]);
                        }
                        stack.Pop();
                        break;

                    case "gotrue":
                        if (Convert.ToBoolean(stack.Peek()))
                        {
                            instructionPointer = ControlFlow.Gotrue(array[instructionPointer, 1]);
                        }
                        stack.Pop();
                        break;

                    case "halt":
                        return;

                    case "+":
                        arOp.Add();
                        break;

                    case "-":
                        arOp.Subtract();
                        break;

                    case "*":
                        arOp.Multiply();
                        break;

                    case "/":
                        arOp.Divide();
                        break;

                    case "div":
                        arOp.Mod();
                        break;

                    case "&":
                        loOp.And();
                        break;

                    case "!":
                        loOp.Not();
                        break;

                    case "|":
                        loOp.Or();
                        break;

                    case "<>":
                        relOp.NotEqual();
                        break;

                    case "<=":
                        relOp.LessThanOrEqualTo();
                        break;

                    case ">=":
                        relOp.GreaterThanOrEqualTo();
                        break;

                    case "<":
                        relOp.LessThan();
                        break;

                    case ">":
                        relOp.GreaterThan();
                        break;

                    case "=":
                        relOp.Equal();
                        break;

                    case "print":
                        output.Print();
                        break;

                    case "show":
                        output.Show(array[instructionPointer, 1]);
                        break;

                    case "begin":
                        isAfterBeginButBeforeCall = true;
                        SymbolsTable.AddLevel(currentLevel);

                        break;

                    case "end":
                        //need to remove level
                        SymbolsTable.VariableTable.RemoveAt(currentLevel + 1);
                        isAfterBeginButBeforeCall = false;
                        isAfterCallButBeforeEnd   = false;
                        break;

                    case "return":
                        //return needs to decrement the leve and restore everything
                        currentLevel--;
                        instructionPointer        = stack.PeekAndPop();
                        isAfterBeginButBeforeCall = false;
                        isAfterCallButBeforeEnd   = true;
                        break;

                    case "call":
                        //call needs to save the current instruction pointer, set the instruction pointer to the next level,
                        //increment the level, reset the boolean flags
                        stack.Push(instructionPointer);
                        isAfterBeginButBeforeCall = false;
                        isAfterCallButBeforeEnd   = false;
                        currentLevel++;
                        instructionPointer = SymbolsTable.LabelTable [array [instructionPointer, 1]];

                        break;

                    case "":
                        break;

                    default:
                        Console.WriteLine("YOU DID SOMETHING VERY BAD");
                        break;
                    }
                    instructionPointer++;
                }
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Beispiel #11
0
        public static void Not()
        {
            int val1 = (int)StackManipulation.Pop();

            StackManipulation.Push(val1 == 0 ? 1 : 0);
        }