public PrgState Execute(PrgState state) { Adt.IDictionary <string, int> symTable = state.SymTable; IMap <ITuple <string, StreamReader> > fileTable = state.FileTable; int fd = exp.Eval(symTable); try { StreamReader sr = fileTable.LookUp(fd).Second; string line; line = sr.ReadLine(); int number; if (line == null) { number = 0; } else { int.TryParse(line, out number); } symTable.Add(varName, number); return(state); } catch (InvalidKeyException) { throw new FileDescriptorException(); } }
public PrgState Execute(PrgState state) { HashSet <string> fileNames = GetFileNames(state); if (fileNames.Contains(fileName)) { throw new FileAlreadyOpenedException(); } StreamReader sr = new StreamReader(fileName); IMap <ITuple <string, StreamReader> > fileTable = state.FileTable; ITuple <string, StreamReader> tuple = new MyTuple <string, StreamReader>(fileName, sr); fileTable.Add(tuple); Adt.IDictionary <string, int> symTable = state.SymTable; symTable.Add(varFileId, fileTable.Key); return(state); }
public override int Eval(Adt.IDictionary <string, int> tbl) { switch (op) { case "+": return(exp1.Eval(tbl) + exp2.Eval(tbl)); case "-": return(exp1.Eval(tbl) - exp2.Eval(tbl)); case "*": return(exp1.Eval(tbl) * exp2.Eval(tbl)); case "/": return(exp1.Eval(tbl) / exp2.Eval(tbl)); default: throw new InvalidOperatorException(); } }