package Models.Program; import java.io.BufferedReader; import java.util.Set; import Exceptions.BaseException; import Models.Statements.IStatement; import Models.Types.IType; import Models.Values.IValue; import Models.Values.StringValue;; public class ProgramState{ //region Fields private IStack stack; private IDictionary symbolTable; private IQueue output; private IDictionary fileTable; private IStatement originalProgram; private IHeap heap; private Integer id; private static Set ids = new java.util.HashSet<>(); //endregion //region Exposed Methods public ProgramState(IStack stack, IDictionary symbolTable, IQueue output, IDictionary fileTable, IHeap heap,IStatement program) throws BaseException{ this.stack = stack; this.symbolTable = symbolTable; this.output = output; this.fileTable = fileTable; this.originalProgram = program.copy(); this.stack.push(program); this.heap = heap; this.id = getNewId(); } private static Integer getNewId(){ Integer newId = 1; synchronized (ids){ while(ids.contains(newId)){ newId++; } ids.add(newId); } return newId; } public IStack getStack(){ return this.stack; } public IDictionary getSymbolTable(){ return this.symbolTable; } public IQueue getOutput(){ return this.output; } public IDictionary getFileTable(){ return this.fileTable; } public IStatement getOriginalProgram(){ return this.originalProgram; } public IHeap getHeap(){ return this.heap; } public boolean isNotCompleted(){ return !this.stack.isEmpty(); } public ProgramState oneStep() throws BaseException{ IStack stack = this.getStack(); if (stack.isEmpty()) { throw new RuntimeException("Execution stack is empty!"); } IStatement currentStatement = stack.pop(); return currentStatement.execute(this); } public String toString(){ return "Id: " + id + "\n" + "Stack:\n" + this.stack.toString() + "\nSymbol Table:\n" + this.symbolTable.toString() + "\nOutput:\n" + this.output.toString() + "\nFile Table:\n" + this.fileTable.toString() + "\n" + "Heap:" + "\n" + this.heap.toString() + "\n"; } public void typeCheck() throws BaseException{ this.originalProgram.typeCheck(new GenericDictionary()); } //#endregion }