public string ExtractBlock(ByteCollection coll) { // Read out a block of code that the VehicleEventController can determine vehicle control primitives from string output = ""; foreach (char c in coll.code.Substring(coll.index)) { if (c == '[' || c == ']') return output; output += c; } return output; }
public Controller(Vehicle v, MemoryBank mb, string pathToSourceCode, string pathToInputs) { // Create a ByteCollection collection = new ByteCollection(pathToSourceCode, 300); collection.FilterCode(); // Create an Interpreter interpreter = new Interpreter(pathToInputs, this); interpreter.PrepareToRun(collection); this.mb = mb; // Create a VehicleEventControl vec = new VehicleEventControl(interpreter.ExtractBlock(collection), v); }
public bool RunNext(ByteCollection coll) { // Execute the next instruction if (coll.index < coll.code.Length) // Only if there is an instruction to execute! { char instruction = coll.code[coll.index]; // Read the instruction char o = '\0'; if (instruction == '+') { coll.Get(coll.ptr).Increment(); // Increment memory contents } else if (instruction == '-') { coll.Get(coll.ptr).Decrement(); // Decrement memory contents } else if (instruction == '>') { coll.IncrementPointer(); // Increment pointer } else if (instruction == '<') { coll.DecrementPointer(); // Decrement pointer } else if (instruction == '[' && coll.Get(coll.ptr).Read() != 0) loop += 1; // Enter a loop else if (instruction == '[' && coll.Get(coll.ptr).Read() == 0) loop = forwardTrack(coll, loop); // Exit a loop else if (instruction == ']') loop = backTrack(coll, loop); // Go back to the start of a loop else if (instruction == '.') o = coll.Get(coll.ptr).ReadToStdOut(); // Write output to the Controller else if (instruction == ',') insertInput(coll); // Read input from the input string // Pass the output to the Controller if (callback != null) callback.PassCycleResult(new IOutputCallback.CycleResult(instruction, o, coll.ptr, coll.index)); coll.index += 1; // Increment the program counter return false; } else { return true; } }
public void MoveVehicle(ByteCollection coll) { vehicle.MoveTo(coll.ptr); }
private int backTrack(ByteCollection coll, int loop) { // Go to the start of a loop int goal = loop - 1; while (loop != goal) { coll.index -= 1; if (coll.code[coll.index] == ']') loop += 1; else if (coll.code[coll.index] == '[') loop -= 1; } coll.index -= 1; return loop; }
public void PrepareToRun(ByteCollection coll) { // Initialise the counters in anticipation of running a program loop = 0; coll.index = 0; }
private void insertInput(ByteCollection coll) { // Read input from the input string if (inIdx < input.Length) { coll.ReplaceByte((int)input[inIdx]); inIdx++; } }
private int forwardTrack(ByteCollection coll, int loop) { // Go to the end of a loop int goal = loop; while (true) { coll.index += 1; if (coll.code[coll.index] == '[') loop += 1; else if (coll.code[coll.index] == ']' && loop == goal) return loop - 1; else if (coll.code[coll.index] == ']') loop -= 1; } }