Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Verbose output, fixed bug and more #10

Merged
merged 17 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions Examples/Example1.tm
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,12 @@

{q011111011};

#define F = {f};
#define F = {q2};

(q0, 1, 0, R, q1);

(q1, 1, 1, R, q1);
(q1, 0, 0, R, q2);//1
(q1, 0, 0, R, q2);

(q2, 0, 1, L, q3);
(q2, 1, 1, R, q2);

(q3, 1, 1, L ,q4);
(q3, 0, 0, L ,q3);

(q4, 1, 1, L, q4);
(q4, 0, 0, L, q5);//2

(q5, 1, 1, L, q5);
(q5, 0, 0, R, q0);

(q0, 0, 0, R, q6);//3

(q6, 0, 0, R, q6);
(q6, 1, 0, R, q7);//4

(q7, 1, 0, R, f);

(f, 0, 0, H, f);
(f, 1, 1, H, f);
(q2, 1, 0, H, q2);
(q2, 0, 0, H, q2);
30 changes: 13 additions & 17 deletions Examples/Example2.tm
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
//Number x2

{p01111};
{q01111};

#deq0ine F = {q0};
#define F = {q1};

(p0, 1, 0, R, p1);
(p0, 0, 0, L, p5);
(q0, 1, 0, R, q1);

//Move to the end of the number
(p1, 1, 1, R, p1);
(p1, 0, 0, R, p2);
(q1, 1, 0, R, q2);
(q1, 0, 0, H, q1);

(p2, 1, 1, R, p2);
(p2, 0, 1, L, p3);
(q2, 1, 1, R, q2);
(q2, 0, 0, R, q3);

(p3, 1, 1, L, p3);
(p3, 0, 0, L, p4);
(q3, 1, 1, R, q3);
(q3, 0, 1, L, q4);

(p4, 1, 1, L, p4);
(p4, 0, 1, R, p0);
(q4, 1, 1, L, q4);
(q4, 0, 0, L, q5);

(p5, 1, 1, L, p5);
(p5, 0, 0, R, q0);
(q5, 1, 1, L, q5);
(q5, 0, 1, R, q1);

(q0, 0, 0, H, q0);
(q0, 1, 1, H, q0);
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ Download [the latest version](https://github.com/margual56/TuringMachine/release

## Using the Turing Machine in the CLI
(From [version 3](https://github.com/margual56/TuringMachine/releases?q=v3) onwards)

```
Usage: java -jar TuringMachine.jar [options]

A simple Turing Machine simulator with a GUI. It uses the syntax we use at class in Computability (EPI Gijón).

Optional arguments:
(none) Run the program normally (GUI mode)
-h, --help Show this help message and exit
--headless FILE Run in headless mode (print the result and exit, no GUI)
-h, --help Show this help message and exit
--headless FILE [-v] Run in headless mode (print the result and exit, no GUI). Write `-v` to get the verbose output.
-e, --example Print an example program and exit

Note:
Expand Down
4 changes: 4 additions & 0 deletions src/Exceptions/RuntimeError.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package Exceptions;

import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class RuntimeError extends Exception {
String str = "";

public RuntimeError() {
JOptionPane.showMessageDialog(null, "Unknown runtime error", "Runtime error", JOptionPane.ERROR_MESSAGE);
}

public RuntimeError(String text) {
str = text;
JOptionPane.showMessageDialog(null, text, "Runtime error", JOptionPane.ERROR_MESSAGE);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/Machines/TM.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public String getTape() {

for (int i = 0; i < tape.length; i++) {
if (i == head) {
t += state + tape[i];
t += "(" + state + ")" + tape[i];
} else {
t += tape[i] + "";
}
Expand Down
5 changes: 5 additions & 0 deletions src/Machines/TMd.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ public String getInstruction() throws RuntimeError {
else
return getCurrentInstruction();
}

public void resetAnimation() {
prevHead = head;
transition = 1;
}

void needle(float x, float y, float size, String state, PApplet app){
float xOffset = x-size/2;
Expand Down
56 changes: 26 additions & 30 deletions src/app/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MainWindow extends PApplet {

TMd turing;
String text;
boolean pause = false, finished;
boolean pause = true, finished;
int state = 1;
int fps = 10;

Expand Down Expand Up @@ -123,13 +123,14 @@ public void draw() {
textAlign(CENTER, TOP);
text(turing.getInstruction(), width/2, height/2+150);
} catch (RuntimeError e) {
e.printStackTrace();
System.err.println(e);
}
}

public void keyPressed() {
if(keyCode == RIGHT) {
doStep();
turing.resetAnimation();
}else if(keyCode == ENTER) {
while(!finished)
doStep();
Expand Down Expand Up @@ -169,7 +170,6 @@ private void doStep() {

try {
state = turing.update();
text = turing.printTape();
} catch (Exception error) {
print(error);
finished = true;
Expand All @@ -182,6 +182,14 @@ public static void main(String[] args) {
MainWindow mySketch = new MainWindow();
PApplet.runSketch(processingArgs, mySketch);
} else {
boolean verbose = false;
for(int i = 0; i<args.length; i++) {
if(args[i].compareToIgnoreCase("-v") == 0) {
verbose = true;
break;
}
}

for(int i = 0; i<args.length; i++) {
if(args[i].compareToIgnoreCase("-h") == 0 || args[i].compareToIgnoreCase("--help") == 0) {
System.out.println(help());
Expand All @@ -196,7 +204,7 @@ public static void main(String[] args) {
String file = args[i+1];

try {
String result = headless(file);
String result = headless(file, verbose);

System.out.println("Result: " + result);

Expand All @@ -221,12 +229,19 @@ public static void main(String[] args) {
}
}

private static String headless(String file) throws SyntaxError, IOException, RuntimeError {
private static String headless(String file, boolean verbose) throws SyntaxError, IOException, RuntimeError {
TM machine = new TM(Paths.get(file));

if(verbose)
System.out.println(machine.getTape());

int code = 1;
while(code != 0) {

code = machine.update();

if(verbose)
System.out.println(machine.getTape());

if(code == -1)
throw new RuntimeError("A Halt state was reached, but it wasn't a final state!");
Expand All @@ -244,7 +259,7 @@ A simple Turing Machine simulator with a GUI. It uses the syntax we use at class
Optional arguments:
(none) Run the program normally (GUI mode)
-h, --help Show this help message and exit
--headless FILE Run in headless mode (print the result and exit, no GUI)
--headless FILE [-v] Run in headless mode (print the result and exit, no GUI). Write `-v` to get the verbose output.
-e, --example Print an example program and exit

Note:
Expand All @@ -262,37 +277,18 @@ private static String example() {
return """
Printing the Example 1:
// a + b

{q011111011};

#define F = {f};
#define F = {q2};

(q0, 1, 0, R, q1);

(q1, 1, 1, R, q1);
(q1, 0, 0, R, q2);//1

(q2, 0, 1, L, q3);
(q2, 1, 1, R, q2);

(q3, 1, 1, L ,q4);
(q3, 0, 0, L ,q3);

(q4, 1, 1, L, q4);
(q4, 0, 0, L, q5);//2

(q5, 1, 1, L, q5);
(q5, 0, 0, R, q0);

(q0, 0, 0, R, q6);//3

(q6, 0, 0, R, q6);
(q6, 1, 0, R, q7);//4

(q7, 1, 0, R, f);
(q1, 0, 0, R, q2);

(f, 0, 0, H, f);
(f, 1, 1, H, f);
(q2, 1, 0, H, q2);
(q2, 0, 0, H, q2);
""";
}
}