summaryrefslogtreecommitdiff
path: root/godel/js
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-11-16 14:56:56 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-11-16 14:56:56 -0700
commitec2b924fdac0b609c2bda4e857113674965732af (patch)
tree1997edfae1227bbee0654a6ca05c2c2585e89c20 /godel/js
parent30f9f2bc185b88669030f7b1a433d79c39c9f1bf (diff)
downloadsimponic.xyz-ec2b924fdac0b609c2bda4e857113674965732af.tar.gz
simponic.xyz-ec2b924fdac0b609c2bda4e857113674965732af.zip
godel init foo
Diffstat (limited to 'godel/js')
-rw-r--r--godel/js/main.js5
-rw-r--r--godel/js/observable.js14
-rw-r--r--godel/js/turing_machine.js72
3 files changed, 91 insertions, 0 deletions
diff --git a/godel/js/main.js b/godel/js/main.js
new file mode 100644
index 0000000..000ce7a
--- /dev/null
+++ b/godel/js/main.js
@@ -0,0 +1,5 @@
+const MESSAGES = {};
+
+// -- the "real" code
+
+const state = new Observable();
diff --git a/godel/js/observable.js b/godel/js/observable.js
new file mode 100644
index 0000000..1299fc6
--- /dev/null
+++ b/godel/js/observable.js
@@ -0,0 +1,14 @@
+class Observable {
+ constructor() {
+ this.observers = [];
+ }
+ subscribe(f) {
+ this.observers.push(f);
+ }
+ unsubscribe(f) {
+ this.observers = this.observers.filter((subscriber) => subscriber !== f);
+ }
+ notify(data) {
+ this.observers.forEach((observer) => observer(data));
+ }
+}
diff --git a/godel/js/turing_machine.js b/godel/js/turing_machine.js
new file mode 100644
index 0000000..a61b43a
--- /dev/null
+++ b/godel/js/turing_machine.js
@@ -0,0 +1,72 @@
+class TuringMachine {
+ constructor(tape = [], rules = [], initialState = "q0", acceptState = "f") {
+ this.tape = tape;
+ this.rules = this.parseRules(rules);
+ this.state = initialState;
+ this.head = 0;
+ this.acceptState = acceptState;
+
+ this.iteration = 0;
+ }
+
+ getStateStatus() {
+ return `State: ${this.state}, Step: ${this.iteration}`;
+ }
+
+ getHead() {
+ return this.head;
+ }
+
+ getState() {
+ return this.state;
+ }
+
+ getTapeAtCell(idx) {
+ return this.tape[idx];
+ }
+
+ setTapeAtCell(idx, val) {
+ this.tape[idx] = val;
+ }
+
+ isAccepting() {
+ return this.state == this.acceptState;
+ }
+
+ parseRules(rules) {
+ const parsedRules = {};
+ for (const [currentState, readSymbol, action, newState] of rules) {
+ const key = `${currentState},${readSymbol}`;
+ const value = `${newState},${action}`;
+ parsedRules[key] = value;
+ }
+ return parsedRules;
+ }
+
+ step() {
+ const currentSymbol = this.tape[this.head];
+ const key = `${this.state},${currentSymbol}`;
+ if (!(key in this.rules)) {
+ return false;
+ }
+ const rule = this.rules[key];
+ const [newState, action] = rule.split(",");
+
+ this.state = newState;
+ this.iteration++;
+
+ if (action === "R") {
+ this.head += 1;
+ } else if (action === "L") {
+ this.head -= 1;
+ } else {
+ this.tape[this.head] = action;
+ }
+
+ if (this.isAccepting()) {
+ return false;
+ }
+
+ return this.head >= 0 && this.head < this.tape.length;
+ }
+}