Visualizer kinda works.
[jars.git] / parse.scm
diff --git a/parse.scm b/parse.scm
new file mode 100644 (file)
index 0000000..c2bec6a
--- /dev/null
+++ b/parse.scm
@@ -0,0 +1,34 @@
+(import (chicken irregex))
+
+(define (string->instr str)
+  (let ((idx 0)
+        (l (string-length str))
+        (whitespace-irx (irregex "[ \t]*"))
+        (newline-irx (irregex "\n"))
+        (comment-irx (irregex ";[^\n]*")))
+    (define (accept-token irx mandatory)
+      (let ((wsmatch (irregex-match whitespace-irx (substr str idx))))
+        (set! idx (+ idx (irregex-match-end-index wsmatch)))) ;Skip leading whitespace
+      (let ((res (irregex-match irx (substring str idx))))
+        (if res
+            (begin
+              (set! idx (+ idx (irregex-match-end-index res)))
+              (irregex-match-substring res))
+            (if mandatory
+                (error "Unexpected token at input string index" idx)
+                #f))))
+    (define (accept-token-string token-str mandatory)
+      (accept-token (irregex token-str) mandatory))
+    (define (load-file)
+      (let loop ()
+        (if (line)
+            (loop))))
+    (define (line)
+      (or (accept-token comment-irx #f)
+          (accept-token newline-irx #f)
+          (and(accept-token newline-irx #t))))
+    (define (instruction)
+      (and (opcode)
+           (accept-token period-irx #t)
+           (accept-modifier)))))
+