include term-colours.4th
0 constant number-type
+1 constant boolean-type
: istype? ( obj -- obj b )
over = ;
then
;
+: boolean? ( -- bool )
+ nextchar [char] # <> if false exit then
+
+ 1 parse-idx +!
+
+ nextchar [char] t <>
+ nextchar [char] f <>
+ and if 1 parse-idx -! false exit then
+
+ 1 parse-idx -!
+ true
+;
+
: readnum ( -- num-atom )
minus? dup if
1 parse-idx +!
number-type
;
+: readbool ( -- bool-atom )
+ 1 parse-idx +!
+
+ nextchar [char] f = if
+ false
+ else
+ true
+ then
+
+ boolean-type
+;
+
\ Parse a counted string into a scheme expression
: read ( -- obj )
eatspaces
+
number? if
readnum
exit
then
- ." Error parsing string at character" parse-idx ? ." . Aborting." cr
+ boolean? if
+ readbool
+ exit
+ then
+
+ bold fg red ." Error parsing string starting at character '"
+ nextchar emit
+ ." '. Aborting." reset-term cr
abort
;
\ ---- Eval ----
: self-evaluating? ( obj -- obj bool )
- number-type istype? ;
+ number-type istype? if true exit then
+ boolean-type istype? if true exit then
+ false ;
: eval
self-evaluating? if
exit
then
- ." Error evaluating expression - unrecognized type. Aborting." cr
+ bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
abort
;
\ ---- Print ----
-: print ( obj -- )
- number-type istype? if
- drop .
+: printnum ( numobj -- ) drop . ;
+: printbool ( numobj -- )
+ drop if
+ ." #t"
+ else
+ ." #f"
then
;
+: print ( obj -- )
+ ." ;"
+ number-type istype? if printnum exit then
+ boolean-type istype? if printbool exit then
+;
+
\ ---- REPL ----
create repl-buffer 161 allot
getline
eof? if
- fg blue ." Moriturus te saluto." reset-term
+ bold fg blue ." Moriturus te saluto." reset-term
exit
then
0 parse-idx !
read
eval
- print
+ fg cyan print reset-term
then
again
;
escape [char] 0 emit escape-end
;
+: clear-term
+ escape [char] 2 emit [char] J emit
+ escape [char] 0 emit [char] ; emit [char] 0 emit [char] f emit
+;
+
: bold
escape [char] 1 emit escape-end
;