Example #1
0
        public int Evaluate(SymbolTableInterface <string, int> symbolTable)
        {
            int leftResult  = leftExpression.Evaluate(symbolTable);
            int rightResult = rightExpression.Evaluate(symbolTable);

            switch (oper)
            {
            case '+': return(leftResult + rightResult);

            case '-': return(leftResult - rightResult);

            case '*': return(leftResult * rightResult);

            case '/':
                if (rightResult != 0)
                {
                    return(leftResult / rightResult);
                }
                else
                {
                    throw new DivisionByZeroException();
                }

            default:
                throw new UnknownOperationException(oper);
            }
        }
Example #2
0
        private Statement statement;                   // as opposed to the java version, i have added an initial state here
                                                       // so there is no need for a manual push of the statement to the stack

        public ProgramState(
            ExecutionStackInterface <Statement> execStack,
            SymbolTableInterface <string, int> symTable,
            OutputListInterface <int> outList,
            FileTableInterface <int, FileDescriptor> fTable,
            Statement initialStatement
            )
        {
            executionStack = execStack;
            symbolTable    = symTable;
            outputList     = outList;
            fileTable      = fTable;
            statement      = initialStatement;
            execStack.push(initialStatement);   // push the initial statement onto the stack
        }
Example #3
0
        public void LogProgramState()
        {
            using (StreamWriter logFile = new StreamWriter(File.Open(fileName, FileMode.Append)))
            {
                ProgramState programState = Current;
                ExecutionStackInterface <Statement>      executionStack = programState.ExecutionStack;
                SymbolTableInterface <string, int>       symbolTable    = programState.SymbolTable;
                OutputListInterface <int>                cout           = programState.OutputList;
                FileTableInterface <int, FileDescriptor> fileTable      = programState.FileTable;

                logFile.WriteLine("ExecStack:");
                foreach (Statement stm in executionStack.Content)
                {
                    logFile.WriteLine(stm);
                }

                logFile.WriteLine("SymTable:");
                foreach (KeyValuePair <string, int> entry in symbolTable.Content)
                {
                    logFile.WriteLine(entry.Key + " -> " + entry.Value);
                }

                logFile.WriteLine("Output:");
                foreach (int k in cout.Content)
                {
                    logFile.WriteLine(k);
                }

                logFile.WriteLine("FileTable:");
                foreach (KeyValuePair <int, FileDescriptor> entry in fileTable.Content)
                {
                    logFile.WriteLine(entry.Key + " -> " + entry.Value);
                }

                logFile.WriteLine();
            }
        }
Example #4
0
 public int Evaluate(SymbolTableInterface <string, int> symbolTable)
 {
     return(value);
 }
Example #5
0
 public int Evaluate(SymbolTableInterface <string, int> symbolTable)
 {
     return(symbolTable[variableName]);
 }