/** * Runs the program state only one step * @param prg - type ProgState, * @throws VarNotFound * @throws EmptyStack */ public void OneStepForAll(ArrayList <ProgState> prgl) { List <ProgState> prgl2 = prgl.ToList(); foreach (ProgState p in prgl2) { PrintState(p); } List <Task <ProgState> > taskList = (from prg in prgl2 select Task <ProgState> .Factory.StartNew(() => prg.OneStep())).ToList(); List <ProgState> newPrgList = (from tsk in taskList where tsk.Result != null select tsk.Result).ToList(); int i = 0; while (i < newPrgList.Count) { ProgState p = (ProgState)newPrgList.ElementAt(i); prgl2.Add(p); i++; } ArrayList <ProgState> wtf = new ArrayList <ProgState>(); for (i = 0; i < prgl2.Count; i++) { wtf.Add(prgl2.ElementAt(i)); } foreach (ProgState p in prgl2) { PrintState(p); } repo.SetPrgList(wtf); }
void Start() { // Delete everything in the temp folder DebugMessage("Deleting temporary files..."); DirectoryInfo dirInfo = new DirectoryInfo(Application.persistentDataPath + "/Temp/"); if (!dirInfo.Exists) { dirInfo.Create(); } try { foreach (string file in System.IO.Directory.GetFiles(Application.persistentDataPath + "/Temp/")) { File.Delete(file); } } catch (Exception e) { DebugMessage(e.Message, "error"); } GO_camera.SetActive(true); AR_camera.SetActive(false); LoadFilePaths(); // Notify the system that the user has entered the Login screen currState = ProgState.LOGIN; prevState = ProgState.LOGIN; ChangeState(ProgState.LOGIN); // Initialize the mouse state mouseState = MouseState.EDITING; }
public void UioneStep() { try { ctrl.AllStep(); ProgState prg = new ProgState(new ArrayList <int>(), new ArrayDict <string, int>(), new ArrayStack <IStmt>(), new ArrayDict <int, int>()); //Console.WriteLine(prg.ToString()); //prg = ctrl.GetProg(); //Console.WriteLine(prg.ToString()); ctrl.AddProg(prg); } catch (VariableNotFoundException e) { Console.WriteLine("Exception: " + e.Message); } catch (UnsupportedOperationException e) { Console.WriteLine("Exception: " + e.Message); } catch (EmptyStackException e) { Console.WriteLine("Exception: " + e.Message); } catch (DivByZeroException e) { Console.WriteLine("Exception: " + e.Message); } catch (EmptyDictException e) { Console.WriteLine("Exception: " + e.Message); } }
public void addPs(IOutBuffer ob, IDictionary vt, Statement st) { ++numProg; pslist[numProg] = new ProgState(new ExeStack(), ob, vt.clone(), cid, hp); ++cid; pslist[numProg].es.push(st); }
void init_interpreter(ProgState state, char[] inner_code, int inner_width, int inner_height) { tape = (int[])state.tape.Clone(); // Clone() is important here index = state.index; x = 0; y = state.y; dir = RIGHT; // init code/width/height width = inner_width + 2; height = inner_height + 2; int len = width * height; Array.Resize(ref code, len); set(code, ' '); set(code, '*', 0, width); // top edge set(code, '*', width * (height - 1), width); // bottom for (int y = 1; y < height - 1; y++) { code[width * y] = '*'; // left edge code[width * y + width - 1] = '*'; // right Array.Copy(inner_code, inner_width * (y - 1), code, width * y + 1, inner_width); } code[y * width] = ' '; // entry point; y is Program.y output = -1; }
/** * Adds a statemtn to the repository * @param stmt - type IStmt, statement to be added */ public void Add(IStmt stmt) { ProgState prg = new ProgState(new ArrayList <int>(), new ArrayDict <string, int>(), new ArrayStack <IStmt>(), new ArrayDict <int, int>()); prg.GetExe.Push(stmt); repo.AddState(prg); }
Cell[] generate(ProgState start_state, int inner_height, int bit) { Cell[] cells = new Cell[0]; // retval int max_inner_width = 9; //9 for (int inner_width = 1; inner_width <= max_inner_width; inner_width++) { int code_len = inner_width * inner_height; char[] inner_code = new char[code_len]; int max_asterisks = min(5, code_len - 2); for (int n_asterisks = 0; n_asterisks <= max_asterisks; n_asterisks++) { // lexicographically smallest permutation is the one with all the asterisks at the end, eg. _____*** set(inner_code, ' '); set(inner_code, '*', code_len - n_asterisks, n_asterisks); while (true) // for all permutations of code { init_interpreter(start_state, inner_code, inner_width, inner_height); if (execute() && output == bit) { code[width * y + x] = ' '; // make exit hole // Two Clone() calls below are not necessary, but I do them to clarify things. ProgState exit_state = new ProgState { tape = (int[])tape.Clone(), index = index, y = y }; int i = Array.FindIndex(cells, cell => cell.exit_state.Equals(exit_state)); if (i == -1) // found a cell with new exit_state { // if found first cell then limit max_inner_width, so // we are looking only for cells with current inner_width and current inner_width plus one if (cells.Length == 0) { max_inner_width = min(inner_width + 1, max_inner_width); } Array.Resize(ref cells, cells.Length + 1); cells[cells.Length - 1] = new Cell { code = (char[])code.Clone(), width = width, height = height, exit_state = exit_state }; stat.min_inner_width = min(inner_width, stat.min_inner_width); stat.max_inner_width = max(inner_width, stat.max_inner_width); stat.min_asterisks = min(n_asterisks, stat.min_asterisks); stat.max_asterisks = max(n_asterisks, stat.max_asterisks); } } if (!next_permutation(inner_code)) { break; } } } } return(cells); }
public Repo() { numProg = 1; ob = new OutBuffer(); hp = new Heap(); pslist[numProg] = new ProgState(new ExeStack(), ob, new VarTable(), cid, hp); ++cid; }
public void allStep() { ProgState prg = repo.getCurrentProgr(); // repo is the controller field of type MyRepoInterface while (!prg.getExeStack.isEmpty()) { oneStep(prg); repo.logCurrentProgramState(); } }
public ProgState oneStep(ProgState state) { ExeStack <Statement> stack = state.getExeStack; if (stack.isEmpty()) { throw (new InterpreterException("error: no current state")); } else { Statement crtStmt = stack.pop(); return(crtStmt.execute(state)); } }
public void PrintState(ProgState p) { if (toFile) { try { repo.AddState(p); repo.WriteToFile(filename); } catch (IOException e) { Console.WriteLine("Exception in printstate ctrl"); } } else { Console.WriteLine(p.ToString()); } }
public void Init() { IList <int> ot = new ArrayList <int>(); IDict <string, int> symtbl = new ArrayDict <string, int>(); IDict <int, int> heap = new ArrayDict <int, int>(); IStack <IStmt> stk = new ArrayStack <IStmt>(); IStmt stmt = new PrintStmt(new ArithExpr(new ConstExpr(3), "+", new ConstExpr(4))); ot.Add(11); ot.Add(12); symtbl.Add("bb", 11); symtbl.Add("bc", 12); stk.Push(stmt); prg = new ProgState(ot, symtbl, stk, heap); Assert.AreEqual("[ print( 3 + 4 ) ]", prg.ToStrExe()); Assert.AreEqual("[ 11, 12 ]", prg.ToStrOut()); Assert.AreEqual("[ bb:11, bc:12 ]", prg.ToStrSym()); }
public ProgState SerializeFromFile() { try { FileStream fs = File.OpenRead("toyInterpreter.txt"); BinaryFormatter bformatter = new BinaryFormatter(); ps = (ProgState)bformatter.Deserialize(fs); fs.Close(); return(ps); } catch (SerializationException e) { Console.WriteLine("ERROR: could not read: " + e.Message); } catch (IOException e) { Console.WriteLine("ERROR: could not read: " + e.Message); } return(null); }
public void WriteToFile(string filename) { List <ProgState> prgl = GetProgList(); for (int i = 0; i < prgl.Count; i++) { ProgState prg = prgl.ElementAt(i); StreamWriter filen = new StreamWriter(filename, true); filen.Write("Id: "); filen.Write(prg.GetId); filen.Write("\n"); filen.Write("ExeStack \n"); filen.Write(prg.GetExe.ToString()); filen.Write("\n"); filen.Write("SymTable \n"); filen.Write(prg.GetSym.ToString()); filen.Write("\n"); filen.Write("Out \n"); filen.Write(prg.GetOut.ToString()); filen.Write("\n"); filen.Close(); } }
static void Main(string[] args) { Statement statement = new CompStmt( new AssignStmt("v", new ConstExpr(2)), new PrintStmt(new VarExpr("v"))); Statement st2 = new CompStmt( new AssignStmt( "a", new ArithExpr('+', new ConstExpr(2), new ArithExpr('*', new ConstExpr(3), new ConstExpr(5) ) )), new CompStmt( new AssignStmt( "b", new ArithExpr('+', new VarExpr("a"), new ConstExpr(1) )), new PrintStmt(new VarExpr("b")))); Statement st3 = new CompStmt( new AssignStmt("v", new ArithExpr('/', new VarExpr("2"), new ConstExpr(0) )), new PrintStmt(new VarExpr("v"))); Statement st4 = new CompStmt( new OpenFileStmt("var_f", "test.in"), new CompStmt( new ReadFileStmt(new VarExpr("var_f"), "var_c"), new CompStmt( new PrintStmt(new VarExpr("var_c")), new CompStmt( new IfStmt( new VarExpr("var_c"), new CompStmt( new ReadFileStmt(new VarExpr("var_f"), "var_c"), new PrintStmt(new VarExpr("var_c")) ), new PrintStmt(new ConstExpr(0)) ), new CloseFileStmt(new VarExpr("var_f"))) ) ) ); ProgState prg8 = new ProgState(new SymbolTable <String, int>(), new ExeStack <Statement>(), new Output <int>(), new FileTable <int, FileData>(), statement); IRepository repo8 = new Repository(prg8, "file88.txt"); Controller ctr8 = new Controller(repo8); ProgState prg2 = new ProgState(new SymbolTable <String, int>(), new ExeStack <Statement>(), new Output <int>(), new FileTable <int, FileData>(), st2); IRepository repo2 = new Repository(prg2, "file1.txt"); Controller ctr2 = new Controller(repo2); ProgState prg3 = new ProgState(new SymbolTable <String, int>(), new ExeStack <Statement>(), new Output <int>(), new FileTable <int, FileData>(), st3); IRepository repo3 = new Repository(prg3, "file2.txt"); Controller ctr3 = new Controller(repo3); ProgState prg4 = new ProgState(new SymbolTable <String, int>(), new ExeStack <Statement>(), new Output <int>(), new FileTable <int, FileData>(), st4); IRepository repo4 = new Repository(prg4, "file3.txt"); Controller ctr4 = new Controller(repo4); TextMenu menu = new TextMenu(); menu.addCommand(new ExitCommand("0", "exit")); menu.addCommand(new AllStepCommand("10", statement.toString(), ctr8)); menu.addCommand(new AllStepCommand("2", st2.toString(), ctr2)); menu.addCommand(new AllStepCommand("3", st3.toString(), ctr3)); menu.addCommand(new AllStepCommand("4", st4.toString(), ctr4)); menu.show(); }
public void add(ProgState programState) { this.state = programState; }
public Repository(ProgState state, string logFile) { this.state = state; this.logFilee = logFile; }
public void addPrgState(ProgState prog) { list.add(prog); }
void main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int inner_height = 4; ProgState start_state = new ProgState { index = 2, y = 3 }; // y = 1..inner_height, y=1 always fails Cell root = new Cell { height = inner_height + 2, exit_state = start_state }; // dummy root cell Cell[] cells = { root }; string str = "Hello, World!"; //bool separate_cells = true; int len = str.Length * 8; for (int i = 0; i < len; i++) { int bit = (str[i / 8] >> (7 - i % 8)) & 1; cells = find_next(cells, bit); if (cells == null) { error("failed to generate"); } stat.min_cells = min(cells.Length, stat.min_cells); stat.max_cells = max(cells.Length, stat.max_cells); // to speed things up, keep only first max_cells cells with shortest total width Array.Sort(cells, (cell1, cell2) => total_width(cell1) - total_width(cell2)); Array.Resize(ref cells, min(cells.Length, max_cells)); // show progress if (i % 8 == 7) // if finished one char { Console.Error.Write(str[i / 8]); // print it } } Console.Error.WriteLine(); Cell[] prog = new Cell[len]; Cell cell = cells[0]; for (int i = 0; i < len; i++, cell = cell.prev) { prog[len - i - 1] = cell; } string result = get_program_start(inner_height, start_state.y); string result_with_gaps = result; for (int i = 0; i < len; i++) { cell = prog[i]; string code_ = code2str(cell.code, cell.width, cell.height); int gap = /*separate_cells ? 1 :*/ (i == 0 ? 0 : -1); result = append_2dcode(result, code_, gap); result_with_gaps = append_2dcode(result_with_gaps, code_, 1); } Console.WriteLine(result); // <--- !!! Console.WriteLine(result_with_gaps); stopwatch.Stop(); print_time(stopwatch.ElapsedMilliseconds); }
public int y; // IP y coord // note: start x is always 0 // start dir is alway RIGHT public bool Equals(ProgState s) // no need to override { return(y == s.y && index == s.index && tape.SequenceEqual(s.tape)); }
/** * Adds a Program state to the repository * @param state */ public void AddState(ProgState prg) { prgList.Add(prg); }
/** * Adds a program state to the repository * @param prg - type ProgState, progState to be added */ public void AddPrg(ProgState prg) { repo.AddState(prg); }
public MemRepo(ProgState ps) { this.ps = ps; }
public void ChangeState(ProgState p) { lock_changingStates = true; HideGUIs(); // Figure out the center of the screen float x0 = Screen.width / 2.0f; float y0 = Screen.height / 2.0f; int current; int offset; prevState = currState; currState = p; switch (currState) { case ProgState.LOGIN: uName = ""; uPass = ""; MS_gui.GetComponent <scr_Login> ().PassVars(x0, y0, uName, uPass); MS_gui.GetComponent <scr_Login> ().ShowGUI(true); break; case ProgState.GALLERY: indexCur = 0; folderType = "Gallery"; fName = "New Gallery Name"; currentGallery = ""; MS_gui.GetComponent <scr_Gallery> ().PassVars(x0, y0, uName); MS_gui.GetComponent <scr_Gallery> ().ShowGUI(true); break; case ProgState.PIECE: indexCur = 0; folderType = "Piece"; fName = "New Piece Name"; currentPiece = ""; rawMesh = ""; for (int i = 0; i < dim_tex_x.Length; i++) { dim_tex_x [i] = 0; dim_tex_y [i] = 0; texFormat [i] = ""; mipMap [i] = 0; } specularity = "0.1"; GO_camera.GetComponent <scr_CameraControl> ().ResetCamera(); txform_mod = new string[] { "0.0", "0.0", "0.0", // Translations "0.0", "0.0", "0.0", // Rotations "1.0", "1.0", "1.0" // Scaling }; txform_cam = new string[] { GO_camera.transform.position.x + "", GO_camera.transform.position.y + "", GO_camera.transform.position.z + "", // Translations GO_camera.transform.eulerAngles.x + "", GO_camera.transform.eulerAngles.y + "", GO_camera.transform.eulerAngles.z + "", // Rotations "1.0", "1.0", "1.0" // Scaling }; txform_lit = new string[] { "0.0", "0.0", "0.0", // Translations GO_light.transform.eulerAngles.x + "", GO_light.transform.eulerAngles.y + "", "0.0", // Rotations "1.0", "1.0", "1.0" // Scaling }; color_lite = new string[] { "255", "255", "255" }; intensity = "0.5"; GO_light.GetComponent <Light> ().color = new Color(1.0f, 1.0f, 1.0f); GO_light.GetComponent <Light> ().intensity = 0.5f; shader_bump = false; shader_spec = false; shader_tran = false; MS_gui.GetComponent <scr_Piece> ().PassVars(x0, y0, uName); MS_gui.GetComponent <scr_Piece> ().ShowGUI(true); break; case ProgState.EDIT: showEditGUI = true; GO_model.GetComponent <MeshRenderer> ().enabled = true; GO_marker.GetComponent <MeshRenderer> ().enabled = true; mouseState = MouseState.EDITING; StartCoroutine(this.GetComponent <scr_Download>().Activate()); break; case ProgState.DELETE: string delType = "gallery"; if (prevState == ProgState.PIECE) { delType = "piece"; } uMess = "Delete " + delType + "?"; MS_gui.GetComponent <scr_Deletion> ().PassVars(x0, y0, uName); MS_gui.GetComponent <scr_Deletion> ().ShowGUI(true); break; default: break; } lock_changingStates = false; }
public void addPrgState(ProgState state) { rep.addPrgState(state); }
public string currentProgramToString() { ProgState currentProgram = repo.getCurrentProgr(); return(currentProgram.ToString()); }
public void SetCurrentProgState(ProgState ps) { currState = ps; }