aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.nim
diff options
context:
space:
mode:
authorJulien Dessaux2021-10-06 23:25:41 +0200
committerJulien Dessaux2021-10-06 23:25:41 +0200
commitcca0eb2117a98695a0280adbc1693b3c92cfab7a (patch)
treea9167286c69938fb39cea9a45afded2990638621 /src/interpreter.nim
parentImplemented missing functions for the interpreter (diff)
downloadnimfunge98-cca0eb2117a98695a0280adbc1693b3c92cfab7a.tar.gz
nimfunge98-cca0eb2117a98695a0280adbc1693b3c92cfab7a.tar.bz2
nimfunge98-cca0eb2117a98695a0280adbc1693b3c92cfab7a.zip
Implemented the interpreter
Diffstat (limited to 'src/interpreter.nim')
-rw-r--r--src/interpreter.nim33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/interpreter.nim b/src/interpreter.nim
new file mode 100644
index 0000000..56313b5
--- /dev/null
+++ b/src/interpreter.nim
@@ -0,0 +1,33 @@
+import field
+import pointer
+
+type
+ Interpreter = object
+ f: ref Field
+ p: ref Pointer
+ argv: seq[string]
+
+func NewInterpreter*(f: ref Field, p: ref Pointer): ref Interpreter =
+ new(result)
+ result.f = f
+ result.p = p
+
+proc Step*(i: var Interpreter): ref int =
+ var prev: ref Pointer
+ var p = i.p
+ while p != nil:
+ let (d, v) = p[].Exec(i.f[])
+ if v != nil:
+ return v
+ if d:
+ if prev == nil:
+ i.p = p.next
+ else:
+ prev.next = p.next
+ p = p.next
+
+proc Run*(i: var Interpreter): int =
+ while i.p != nil:
+ let v = i.Step()
+ if v != nil:
+ return v[]