summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--quine.asm132
1 files changed, 85 insertions, 47 deletions
diff --git a/quine.asm b/quine.asm
index 7211256..910013d 100644
--- a/quine.asm
+++ b/quine.asm
@@ -6361,18 +6361,57 @@ cold_start:
   dq lit, 0, early_comma
   dq litstring, "=", early_find, entry_to_execution_token, early_comma
   dq litstring, "0branch", early_find, entry_to_execution_token, early_comma
-  dq lit, 5*8, early_comma
+  dq lit, 28*8, early_comma
+
+  ; If the length is zero, there is no input, but we can still try calling
+  ; the "refill" word.
+  ; (metadata pointer)
+  dq litstring, "dup", early_find, entry_to_execution_token, early_comma
+  dq litstring, "input-buffer-refill", early_find, entry_to_execution_token
+  dq early_comma
+  dq litstring, "@", early_find, entry_to_execution_token, early_comma
+  dq litstring, "dup", early_find, entry_to_execution_token, early_comma
+  dq litstring, "0branch", early_find, entry_to_execution_token, early_comma
+  dq lit, 17*8, early_comma
+
+  ; If the refill word is nonzero, call it. It expects a copy of the metadata
+  ; pointer as its parameter, so set that up.
+  ; (metadata pointer, refill word)
+  dq litstring, "swap", early_find, entry_to_execution_token, early_comma
+  dq litstring, "dup", early_find, entry_to_execution_token, early_comma
+  dq litstring, "3roll", early_find, entry_to_execution_token, early_comma
+  dq litstring, "execute", early_find, entry_to_execution_token, early_comma
+  ; (metadata pointer)
+  ; Now we check if the length is still zero.
+  dq litstring, "dup", early_find, entry_to_execution_token, early_comma
+  dq litstring, "buffer-logical-length", early_find, entry_to_execution_token
+  dq early_comma
+  dq litstring, "@", early_find, entry_to_execution_token, early_comma
+  dq litstring, "lit", early_find, entry_to_execution_token, early_comma
+  dq lit, 0, early_comma
+  dq litstring, "=", early_find, entry_to_execution_token, early_comma
+  dq litstring, "0branch", early_find, entry_to_execution_token, early_comma
+  dq lit, 10*8, early_comma
 
-  ; If the length is zero, there is no input; return zero.
-  ; TODO This would also be the place to call the "refill" word.
+  ; The length is zero even after calling the refill word, so return null.
   ; (metadata pointer)
   dq litstring, "drop", early_find, entry_to_execution_token, early_comma
   dq litstring, "lit", early_find, entry_to_execution_token, early_comma
   dq lit, 0, early_comma
   dq litstring, "exit", early_find, entry_to_execution_token, early_comma
 
-  ; The buffer is non-empty, so read a byte from it.
-  ; (metadata pointer, input pointer)
+  ; If the refill word is zero, we can't help, just return null.
+  ; (metadata pointer, refill word)
+  dq litstring, "drop", early_find, entry_to_execution_token, early_comma
+  dq litstring, "drop", early_find, entry_to_execution_token, early_comma
+  dq litstring, "lit", early_find, entry_to_execution_token, early_comma
+  dq lit, 0, early_comma
+  dq litstring, "exit", early_find, entry_to_execution_token, early_comma
+
+  ;   The buffer is non-empty, so read a byte from it. We might have reached
+  ; this point either from the original check, or from the second check after
+  ; calling the refill word.
+  ; (metadata pointer)
   dq litstring, "dup", early_find, entry_to_execution_token, early_comma
   dq litstring, "buffer-logical-start", early_find, entry_to_execution_token
   dq early_comma
@@ -7336,12 +7375,15 @@ cold_start:
   dq litstring, "exit", early_find, entry_to_execution_token, early_comma
 
   ; If it's neither in the dictionary nor a number, just print an error.
-  dq litstring, "dropstring", early_find, entry_to_execution_token
-  dq early_comma
   dq litstring, "litstring", early_find, entry_to_execution_token, early_comma
-  dq lit, "No such ", early_comma, lit, "word.", early_comma
+  dq lit, "No such ", early_comma, lit, "word: ", early_comma
   dq litstring, "emitstring", early_find, entry_to_execution_token
   dq early_comma
+  dq litstring, "value@", early_find, entry_to_execution_token, early_comma
+  dq litstring, "emitstring", early_find, entry_to_execution_token
+  dq early_comma
+  dq litstring, "dropstring", early_find, entry_to_execution_token
+  dq early_comma
   dq litstring, "exit", early_find, entry_to_execution_token, early_comma
   dq early_here, fetch, lit, 8, packalign, early_here_store
 
@@ -11405,29 +11447,22 @@ defword boot_source, 0x40
   dq "      { drop drop } if-else ;                                   "
 
 
-; TODO this is the step for after refilling works
-;  dq ": relink-main-input-buffer-to-stdin                             "
-;  dq "  1024 allocate-input-buffer main-input-buffer !                "
-;  dq "  ' refill-input-buffer-from-stdin entry-to-execution-token     "
-;  dq "  main-input-buffer @ input-buffer-refill !                     "
-;  dq "  main-input-buffer @ hexdump bye                               "
-;  dq "  ;                                                             "
-;  ; In:
-;  ;   pointer to buffer metadata
-;  dq ": attach-stdin-to-input-buffer                                  "
-;  dq "  dup buffer-physical-start here swap !                         "
-;  ; (metadata pointer)
-;  dq "  dup buffer-physical-end here 1024 + dup here ! swap !         "
-;  ; (metadata pointer)
-;  dq "  dup buffer-physical-start @ swap dup 3unroll                  "
-;  dq "  buffer-logical-start !                                        "
-;  dq "  dup buffer-physical-end @ swap dup 3unroll                    "
-;  dq "  buffer-logical-end !                                          "
-;  dq "  hexdump ;                                                     "
-;
-;  dq "main-input-buffer @ attach-stdin-to-input-buffer bye            "
-;  dq "relink-main-input-buffer-to-stdin                               "
-;  dq ": hmm 16 here sys-read stack here hexdump ; hmm                 "
+  ;   Though we only do it once, this is a bit involved so we provide it as a
+  ; word. Notionally there are situations in which it could come up again, so
+  ; it seems worth having around.
+  ;
+  ;   It actually needs to run as a compiled word no matter what; if it were
+  ; run in interpret mode it would cut itself off from the rest of itself.
+  ; However, if we didn't want to keep it around we could have it forget
+  ; itself...
+  dq ": relink-main-input-buffer-to-stdin                             "
+  dq "  1024 allocate dup main-input-buffer buffer-physical-start !   "
+  dq "  main-input-buffer buffer-logical-start !                      "
+  dq "  1024 main-input-buffer buffer-physical-length !               "
+  dq "  0 main-input-buffer buffer-logical-length !                   "
+  dq "  ' refill-input-buffer-from-stdin entry-to-execution-token     "
+  dq "  main-input-buffer input-buffer-refill !                       "
+  dq "  ;                                                             "
 
 
   ;   The word named "docol" has the job of returning the value that gets used
@@ -11538,20 +11573,20 @@ defword boot_source, 0x40
   ;dq "68719607808 containing-entry stackhex                           "
   ;dq "list-dictionary                                                 "
   ;dq "describe-all                                                    "
-  dq ": doittoit 8    allocate-input-buffer                           "
-  dq "  dup stackhex refill-input-buffer-from-stdin                   "
-  dq "  dup key-from drop                                             "
-  dq "  dup key-from drop                                             "
-  dq "  dup key-from drop                                             "
-  dq "  dup key-from drop                                             "
-  dq "  stackhex dup 256 hexdump-from                                 "
-  dq "  dup refill-input-buffer-from-stdin                            "
-  dq "  stackhex dup 256 hexdump-from                                 "
-  dq "  dup refill-input-buffer-from-stdin                            "
-  dq "  stackhex dup 256 hexdump-from                                 "
-  dq "  dup refill-input-buffer-from-stdin                            "
-  dq "  stackhex dup 256 hexdump-from                                 "
-  dq "  bye ; doittoit                                                "
+  ;dq ": doittoit 8    allocate-input-buffer                           "
+  ;dq "  dup stackhex refill-input-buffer-from-stdin                   "
+  ;dq "  dup key-from drop                                             "
+  ;dq "  dup key-from drop                                             "
+  ;dq "  dup key-from drop                                             "
+  ;dq "  dup key-from drop                                             "
+  ;dq "  stackhex dup 256 hexdump-from                                 "
+  ;dq "  dup refill-input-buffer-from-stdin                            "
+  ;dq "  stackhex dup 256 hexdump-from                                 "
+  ;dq "  dup refill-input-buffer-from-stdin                            "
+  ;dq "  stackhex dup 256 hexdump-from                                 "
+  ;dq "  dup refill-input-buffer-from-stdin                            "
+  ;dq "  stackhex dup 256 hexdump-from                                 "
+  ;dq "  bye ; doittoit                                                "
   dq "                                                                "
 
   ; TODO define ( ... ) comments
@@ -11569,8 +11604,11 @@ defword boot_source, 0x40
   ; TODO consider ";asm" or something
   ; TODO consider a welcome message
 
-  ; If we get to this point, clean up and leave.
-  dq "bye                                                             "
+  ; The last in a long series of perilous handoffs. :)
+  dq "  relink-main-input-buffer-to-stdin                             "
+  ; We'll never get here, but if we do, it's an error.
+  dq "  1 sys-exit                                                    "
+
   dq 0