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

Commit

Permalink
Redesign - Added CLI options and more! (#7)
Browse files Browse the repository at this point in the history
* Added CLI options, including the headless option 

* Improved animation speed formula
  • Loading branch information
margual56 committed Nov 10, 2021
1 parent 5817230 commit 5ca3d4f
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 11 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
A [Turing machine](https://en.wikipedia.org/wiki/Turing_machine) interpreter made in Java using Processing libraries.

![](resources/turing.gif)<br/>
_This is the [Example2.tm](https://github.com/margual56/TuringMachine/blob/f42250c67b4bccfd45451fed96c9bcbdbd805cdc/Examples/Example2.tm) program running in real time_
_This is the [Example1.tm](https://github.com/margual56/TuringMachine/blob/f42250c67b4bccfd45451fed96c9bcbdbd805cdc/Examples/Example2.tm) (which adds the two input numbers) running in real time_

## Using the Turing Machine in the CLI
```
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)
-e, --example Print an example program and exit
Note:
You can only provide one argument at a time. If more than one is provided, just the first one is going to be taken into account.
```


## Programming in Turing Machine code
Inside the [exmaples folder](https://github.com/margual56/TuringMachine/tree/master/Examples), there are 5 example programs of how to code a Turing Machine program. The extension of the files __has__ to be `.tm`.
Inside the [examples folder](https://github.com/margual56/TuringMachine/tree/master/Examples), there are 5 example programs of how to code a Turing Machine program. The extension of the files __has__ to be `.tm`.

Firstly, you define the initial state of the tape (example):
```Td
Expand Down
Binary file modified resources/turing.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions src/Machines/TM.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ private void setInitialState(String state) {

for (i = 0; i < tape.length; i++)
tape[i] = state.charAt(i);

System.out.println(tape);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Machines/TMd.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void show(float x0, float y0, float wid, float hei, int headspace, PApple
public void animateTape(float fps, PApplet app) throws RuntimeError {
if(head != prevHead) {
if(transition == 0) transition = 0.01f;
else transition += 50/(app.frameRate*fps);
else transition += 5/(app.frameRate+(fps-10)*2);

if(transition > 0.99) {
transition = 0;
Expand Down
127 changes: 121 additions & 6 deletions src/app/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app;

import java.io.IOException;
import java.nio.file.Paths;

import javax.swing.JFileChooser;
Expand All @@ -10,6 +11,8 @@

import Exceptions.RuntimeError;
import Exceptions.SyntaxError;

import Machines.TM;
import Machines.TMd;
import processing.core.PApplet;

Expand Down Expand Up @@ -77,8 +80,6 @@ public void setup() {
}

text = turing.toString();

frameRate(60);
}

public void draw() {
Expand Down Expand Up @@ -175,9 +176,123 @@ private void doStep() {
}
}

public static void main(String[] args) {
String[] processingArgs = {"Turing Machine"};
MainWindow mySketch = new MainWindow();
PApplet.runSketch(processingArgs, mySketch);
public static void main(String[] args) {
if(args.length == 0) {
String[] processingArgs = {"Turing Machine"};
MainWindow mySketch = new MainWindow();
PApplet.runSketch(processingArgs, mySketch);
} else {
for(int i = 0; i<args.length; i++) {
if(args[i].compareToIgnoreCase("-h") == 0 || args[i].compareToIgnoreCase("--help") == 0) {
System.out.println(help());
System.exit(0);
}else if(args[i].compareToIgnoreCase("--headless") == 0) {
if(i == args.length-1) {
System.err.println("You need to provide a file when running the headless mode!");
System.out.println(help());
System.exit(1);
}

String file = args[i+1];

try {
String result = headless(file);

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

System.exit(0);
} catch (SyntaxError | RuntimeError sE) {
System.err.println(sE);
System.exit(1);
} catch (IOException e) {
System.err.println("The file \"" + file + "\" does not exist");
System.exit(2);
}

}else if(args[i].compareToIgnoreCase("-e") == 0 || args[i].compareToIgnoreCase("--example") == 0) {
System.out.println(example());
System.exit(0);
}
}

System.err.println("No argument you provided was valid.");
System.out.println(help());
System.exit(1);
}
}

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

int code = 1;
while(code != 0) {
code = machine.update();

if(code == -1)
throw new RuntimeError("A Halt state was reached, but it wasn't a final state!");
}

return machine.output();
}

private static String help() {
return """
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)
-e, --example Print an example program and exit
Note:
You can only provide one argument at a time. If more than one is provided, just the first one is going to be taken into account.
Author:
Marcos Gutiérrez Alonso
Repository:
https://github.com/margual56/TuringMachine (GPLv3)
""";
}

private static String example() {
return """
Printing the Example 1:
// a + b
{q011111011};
#define F = {f};
(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);
(f, 0, 0, H, f);
(f, 1, 1, H, f);
""";
}
}

0 comments on commit 5ca3d4f

Please sign in to comment.