Implement INTERPRET-STRING
[rrq/jonasforth.git] / bootstrap.asm
index dd72ed9e85e8f53ec96830962457f2f4cd6d8d04..4dd77de2152879f910f50efb7638369afe2c4382 100644 (file)
@@ -100,3 +100,31 @@ forth INTERPRET, 'INTERPRET'
   dq READ_WORD
   dq INTERPRET_WORD
   dq EXIT
+
+;; INTERPRET_STRING is a variant of INTERPRET that reads from a string instead
+;; of from the user. It takes a string as a (buffer, length) pair on the stack
+;; and interprets the entire string, even if the string has more than one word.
+forth INTERPRET_STRING, 'INTERPRET-STRING'
+  dq INPUT_LENGTH, PUT
+  dq INPUT_BUFFER, PUT
+
+  ;; Check if the buffer is-non-empty
+  ;; [TODO] This probably won't work for strings with whitespace at the end.
+  dq INPUT_LENGTH, GET
+  dq ZBRANCH, 8 * 19 ; to EXIT
+
+  dq INPUT_BUFFER, GET
+  dq INPUT_LENGTH, GET
+  dq POP_WORD
+
+  ;; Stack is (buffer buffer-length word word-length)
+
+  dq ROT, ROT
+  dq INPUT_LENGTH, PUT
+  dq ROT, ROT
+  dq INPUT_BUFFER, PUT
+
+  dq INTERPRET_WORD
+  dq BRANCH, -8 * 19 ; to INPUT-LENGTH @
+
+  dq EXIT