public PrgState execute(PrgState state) { int id = exp.Eval(state.getSymTable()); Tuple <string, System.IO.StreamReader> pair = state.getFileTable().get(id); if (pair == null) { throw new MyException("File not found!"); } state.getFileTable().remove(id); pair.Item2.Close(); return(null); }
public PrgState execute(PrgState state) { int id = exp.Eval(state.getSymTable()); Tuple <string, StreamReader> pair = state.getFileTable().get(id); if (pair == null) { throw new MyException("File not opened!"); } string line = pair.Item2.ReadLine(); if (line != null) { if (state.getSymTable().isDefined(var)) { state.getSymTable().update(var, Convert.ToInt32(line)); } else { state.getSymTable().put(var, Convert.ToInt32(line)); } } else if (state.getSymTable().isDefined(var)) { state.getSymTable().update(var, 0); } else { state.getSymTable().put(var, 0); } return(null); }
public void execute(PrgState state) { ImyDict <int, Tuple <string, StreamReader> > fileTable = state.getFileTable(); ImyDict <string, int> symTable = state.getSymTable(); try { if (fileTable.isInTuple(this.filename)) { throw new StmtException("File allready opened"); } } catch (Exception e1) { throw new StmtException("Statement \"" + this.ToString() + "\" encountered a dictionary exception: " + e1.Message); } StreamReader buffReader; try { buffReader = new StreamReader(filename); } catch (FileNotFoundException) { throw new StmtException("File not found \"" + filename + "\""); } Tuple <string, StreamReader> entry = new Tuple <string, StreamReader>(filename, buffReader); int id = fileTable.generateId(); fileTable.add(id, entry); symTable.add(var_file_id, id); }
public PrgState execute(PrgState state) { MyIStack <IStmt> stk = state.getExeStack(); // stk.pop(); MyIDictionary <String, int> symTbl = state.getSymTable(); IFileTable <int, FileTuple> fileTbl = state.getFileTable(); int value_exp = this.exp_file_id.eval(symTbl); FileTuple ft = fileTbl.getValue(value_exp); if (!fileTbl.isDefined(value_exp)) { throw new Exception("The key is not defined in the file table"); } StreamReader streamReader = fileTbl.getValue(value_exp).getStreamReader(); try { streamReader.Close(); } catch (IOException ex) { throw new Exception(ex.Message); } fileTbl.remove(value_exp); return(state); }
public PrgState execute(PrgState state) { if (!File.Exists(filename)) { throw new MyException("File does not exist!"); } state.getFileTable().put(unique_key, Tuple.Create(filename, new StreamReader(filename))); state.getSymTable().put(id, unique_key); unique_key += 1; return(null); }
public void logPrgState(PrgState ps) { try { StreamWriter logFile = File.AppendText(this.filename); StringBuilder s = new StringBuilder(); s.Append(ps.ExeStack); s.Append(ps.getSymbolTable()); s.Append(ps.getFileTable()); s.Append(ps.getOutput()); logFile.WriteLine(s.ToString()); logFile.Close(); } catch (IOException e) { throw new IException(e.Message); } }
public PrgState execute(PrgState state) { MyIStack <IStmt> stk = state.getExeStack(); MyIDictionary <String, int> symTbl = state.getSymTable(); IFileTable <int, FileTuple> fileTbl = state.getFileTable(); int value_exp = this.exp_file_id.eval(symTbl); FileTuple ft = fileTbl.getValue(value_exp); if (!symTbl.isDefined(ft.getFileName())) { //throw new MyStmtExecException("The key is not defined in the file table"); } StreamReader streamReader = ft.getStreamReader(); int val = -1; try { String line = streamReader.ReadLine(); if (line == null) { val = 0; } else { try { val = int.Parse(line); } catch (Exception ex) { Console.WriteLine(ex.Message); } } symTbl.put(this.var_name, val); } catch (IOException ex) { throw new Exception(ex.Message); } return(state); }
public PrgState execute(PrgState state) { MyIStack <IStmt> stk = state.getExeStack(); IFileTable <int, FileTuple> filetable = state.getFileTable(); MyIDictionary <String, int> symTbl = state.getSymTable(); try { FileStream reader = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader bufferedStream = new StreamReader(reader); int id = IdGenerator.generateId(); FileTuple tuple = new FileTuple(this.filename, bufferedStream); filetable.put(id, tuple); symTbl.put(this.var_file_id, id); } catch (Exception ex) { throw new Exception(ex.Message); } return(state); }
public void execute(PrgState state) { ImyDict <string, int> symTable = state.getSymTable(); ImyDict <int, Tuple <string, StreamReader> > fileTable = state.getFileTable(); int v; StreamReader buffReader; try { v = exp_file_id.eval(symTable); } catch (ExpException e) { throw new StmtException("Expression \"" + exp_file_id + "\" in statement + \"" + this.ToString() + "\" is invalid: " + e.Message); } try { buffReader = fileTable.get(v).Item2; } catch (Exception) { throw new StmtException("File does not exist or hasn't been open \"" + exp_file_id.ToString() + "\""); } try { string readValue = buffReader.ReadLine(); int value; if (readValue == null) { value = 0; } else { Int32.TryParse(readValue, out value); } symTable.add(var_name, value); } catch (IOException e2) { throw new StmtException("Statement \"" + this.ToString() + "\" encountered an exception: " + e2.Message); } }
public PrgState execute(PrgState state) { ImyDict <String, int> symTable = state.getSymTable(); ImyDict <int, Tuple <String, StreamReader> > fileTable = state.getFileTable(); int v; StreamReader buffReader; try { v = exp_file_id.eval(symTable); } catch (ExpException e) { throw new StmtException("Expression \"" + exp_file_id + "\" in statement + \"" + this.ToString() + "\" is invalid: " + e.Message); } try { buffReader = fileTable.get(v).Item2; buffReader.Close(); fileTable.remove(v); } catch (Exception) { throw new StmtException("File does not exist or hasn't been open"); } return(null); }