From 59038a10f0e3ad65675cafdb149eb61405e334d3 Mon Sep 17 00:00:00 2001 From: Chouser Date: Fri, 6 Feb 2015 00:38:34 -0500 Subject: forth: Added lists, ints, symbols for step 1 --- forth/reader.fs | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 forth/reader.fs (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs new file mode 100644 index 0000000..6ed9fb5 --- /dev/null +++ b/forth/reader.fs @@ -0,0 +1,92 @@ +require types.fs +require printer.fs + +\ Drop a char off the front of string by advancing the addr and +\ decrementing the length, and fetch next char +: adv-str ( str-addr str-len -- str-addr str-len char ) + swap 1+ swap 1- + dup 0= if 0 ( eof ) + else over c@ endif ; + +: skip-spaces ( str-addr str-len char -- str-addr str-len non-space-char ) + begin + dup bl = + while ( str-addr str-len space-char ) + drop adv-str + repeat ; + +: mal-digit? ( char -- flag ) + dup [char] 9 <= if + [char] 0 >= + else + drop 0 + endif ; + +: char-in-str? ( char str-addr str-len ) + rot { needle } + begin ( str-addr str-len ) + adv-str needle = if + 2drop -1 -1 \ success! drop and exit + else + dup 0= if + 2drop 0 -1 \ str consumed, char not found. + else + 0 \ continue + endif + endif + until ; + +s\" []{}()'\"`,; " constant non-sym-chars-len constant non-sym-chars +: sym-char? ( char -- flag ) + non-sym-chars non-sym-chars-len char-in-str? 0= ; + +defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) + +: read-int ( str-addr str-len digit-char -- str-addr str-len non-digit-char mal-int ) + 0 { int } + begin ( str-addr str-len digit-char ) + [char] 0 - int 10 * + to int ( str-addr str-len ) + adv-str dup mal-digit? 0= ( str-addr str-len digit-char ) + until + int MalInt. ; + +: read-symbol-str ( str-addr str-len sym-char -- str-addr str-len sym-addr sym-len ) + new-str { sym-addr sym-len } + begin ( str-addr srt-len sym-char ) + sym-addr sym-len rot str-append-char to sym-len to sym-addr + adv-str dup sym-char? 0= + until + sym-addr sym-len ; + +: read-list ( str-addr str-len open-paren-char -- str-addr str-len non-paren-char mal-list ) + \ push objects onto "dictionary" -- maybe not the best stack for this? + 0 { len } + drop adv-str + begin ( str-addr str-len char ) + skip-spaces ( str-addr str-len non-space-char ) + dup [char] ) <> + while ( str-addr str-len non-space-non-paren-char ) + read-form , len 1+ to len + repeat + drop adv-str + + \ pop objects out of "dictionary" into MalList + mal-nil + len 0 ?do + 0 cell - allot + here @ swap conj + loop + ; + +: read-form2 ( str-addr str-len char -- str-addr str-len mal-obj ) + skip-spaces + dup mal-digit? if read-int else + dup [char] ( = if read-list else + read-symbol-str MalSymbol. + endif + endif + ; +' read-form2 is read-form + +: read-str ( str-addr str-len - mal-obj ) + over c@ read-form -rot 2drop ; -- cgit v1.2.3 From 168fb5dc56fee6653816ee8236259940e575c7ec Mon Sep 17 00:00:00 2001 From: Chouser Date: Fri, 6 Feb 2015 23:58:41 -0500 Subject: forth: Add step 1, but not maps --- forth/reader.fs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 13 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 6ed9fb5..57f3e8d 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -1,6 +1,8 @@ require types.fs require printer.fs +-2 constant skip-elem + \ Drop a char off the front of string by advancing the addr and \ decrementing the length, and fetch next char : adv-str ( str-addr str-len -- str-addr str-len char ) @@ -10,7 +12,11 @@ require printer.fs : skip-spaces ( str-addr str-len char -- str-addr str-len non-space-char ) begin - dup bl = + dup bl = if + -1 + else + dup [char] , = + endif while ( str-addr str-len space-char ) drop adv-str repeat ; @@ -50,21 +56,52 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) until int MalInt. ; -: read-symbol-str ( str-addr str-len sym-char -- str-addr str-len sym-addr sym-len ) +: read-comment ( str-addr str-len sym-char -- str-addr str-len char skim-elem ) + drop + begin + adv-str = 10 + until + adv-str skip-elem ; + +: read-symbol-str ( str-addr str-len sym-char -- str-addr str-len char sym-addr sym-len ) new-str { sym-addr sym-len } - begin ( str-addr srt-len sym-char ) + begin ( str-addr str-len sym-char ) sym-addr sym-len rot str-append-char to sym-len to sym-addr adv-str dup sym-char? 0= until sym-addr sym-len ; +: read-string-literal ( in-addr in-len quote-char -- in-addr in-len mal-string ) + new-str { out-addr out-len } + drop \ drop leading quote + begin ( in-addr in-len ) + adv-str over 0= if + 2drop s\" expected '\"', got EOF\n" safe-type 1 throw + endif + dup [char] " <> + while + dup [char] \ = if + drop adv-str + dup [char] n = if drop 10 endif + dup [char] r = if drop 13 endif + endif + out-addr out-len rot str-append-char to out-len to out-addr + repeat + drop adv-str \ skip trailing quote + out-addr out-len MalString. ; + : read-list ( str-addr str-len open-paren-char -- str-addr str-len non-paren-char mal-list ) \ push objects onto "dictionary" -- maybe not the best stack for this? - 0 { len } + 0 { close-char len } drop adv-str begin ( str-addr str-len char ) skip-spaces ( str-addr str-len non-space-char ) - dup [char] ) <> + over 0= if + drop 2drop + s\" expected '" close-char str-append-char + s\" ', got EOF" str-append safe-type 1 throw + endif + dup close-char <> while ( str-addr str-len non-space-non-paren-char ) read-form , len 1+ to len repeat @@ -78,15 +115,33 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) loop ; -: read-form2 ( str-addr str-len char -- str-addr str-len mal-obj ) - skip-spaces - dup mal-digit? if read-int else - dup [char] ( = if read-list else +: read-wrapped ( buf-addr buf-len quote-char sym-addr sym-len -- buf-addr buf-len char mal-list ) + MalSymbol. { sym } ( buf-addr buf-len char ) + read-form mal-nil conj ( buf-addr buf-len char mal-list ) + sym swap conj ; + +: read-form2 ( str-addr str-len char -- str-addr str-len char mal-obj ) + begin + skip-spaces + dup mal-digit? if read-int else + dup [char] ( = if [char] ) read-list else + dup [char] [ = if [char] ] read-list MalVector new tuck MalVector/list ! else + dup [char] " = if read-string-literal else + dup [char] ; = if read-comment else + dup [char] @ = if drop adv-str s" deref" read-wrapped else + dup [char] ' = if drop adv-str s" quote" read-wrapped else + dup [char] ` = if drop adv-str s" quasiquote" read-wrapped else + dup [char] ~ = if + drop adv-str + dup [char] @ = if drop adv-str s" splice-unquote" read-wrapped + else s" unquote" read-wrapped + endif + else read-symbol-str MalSymbol. - endif - endif - ; + endif endif endif endif endif endif endif endif endif + dup skip-elem = + while drop repeat ; ' read-form2 is read-form : read-str ( str-addr str-len - mal-obj ) - over c@ read-form -rot 2drop ; + over c@ read-form { obj } drop 2drop obj ; -- cgit v1.2.3 From 2e78e94eb894e511e583db03286a3c13b9ecc780 Mon Sep 17 00:00:00 2001 From: Chouser Date: Sat, 7 Feb 2015 10:01:31 -0500 Subject: forth: Finished step 1 --- forth/reader.fs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 57f3e8d..7ff46fd 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -126,6 +126,7 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) dup mal-digit? if read-int else dup [char] ( = if [char] ) read-list else dup [char] [ = if [char] ] read-list MalVector new tuck MalVector/list ! else + dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else dup [char] " = if read-string-literal else dup [char] ; = if read-comment else dup [char] @ = if drop adv-str s" deref" read-wrapped else @@ -136,9 +137,16 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) dup [char] @ = if drop adv-str s" splice-unquote" read-wrapped else s" unquote" read-wrapped endif + else + dup [char] ^ = if + drop adv-str + read-form { meta } read-form { obj } + meta mal-nil conj + obj swap conj + s" with-meta" MalSymbol. swap conj else read-symbol-str MalSymbol. - endif endif endif endif endif endif endif endif endif + endif endif endif endif endif endif endif endif endif endif endif dup skip-elem = while drop repeat ; ' read-form2 is read-form -- cgit v1.2.3 From 9da223a35a176d94fbb75cbcc1000871ff5aff0b Mon Sep 17 00:00:00 2001 From: Chouser Date: Thu, 12 Feb 2015 19:27:00 -0500 Subject: forth: Add step 2 --- forth/reader.fs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 7ff46fd..edd99fc 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -90,7 +90,8 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) drop adv-str \ skip trailing quote out-addr out-len MalString. ; -: read-list ( str-addr str-len open-paren-char -- str-addr str-len non-paren-char mal-list ) +: read-list ( str-addr str-len open-paren-char close-paren-char + -- str-addr str-len non-paren-char mal-list ) \ push objects onto "dictionary" -- maybe not the best stack for this? 0 { close-char len } drop adv-str @@ -112,7 +113,25 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) len 0 ?do 0 cell - allot here @ swap conj - loop + loop ; + +: read-array ( str-addr str-len open-paren-char close-paren-char + -- str-addr str-len non-paren-char mal-array ) + here { close-char old-here } + drop adv-str + begin ( str-addr str-len char ) + skip-spaces ( str-addr str-len non-space-char ) + over 0= if + drop 2drop + s\" expected '" close-char str-append-char + s\" ', got EOF" str-append safe-type 1 throw + endif + dup close-char <> + while ( str-addr str-len non-space-non-paren-char ) + read-form , + repeat + drop adv-str + old-here here>MalArray ; : read-wrapped ( buf-addr buf-len quote-char sym-addr sym-len -- buf-addr buf-len char mal-list ) @@ -124,11 +143,12 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) begin skip-spaces dup mal-digit? if read-int else - dup [char] ( = if [char] ) read-list else - dup [char] [ = if [char] ] read-list MalVector new tuck MalVector/list ! else + dup [char] ( = if [char] ) read-array else + dup [char] [ = if [char] ] read-array MalVector new tuck MalVector/list ! else dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else dup [char] " = if read-string-literal else dup [char] ; = if read-comment else + dup [char] : = if drop adv-str read-symbol-str MalKeyword. else dup [char] @ = if drop adv-str s" deref" read-wrapped else dup [char] ' = if drop adv-str s" quote" read-wrapped else dup [char] ` = if drop adv-str s" quasiquote" read-wrapped else @@ -146,7 +166,7 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) s" with-meta" MalSymbol. swap conj else read-symbol-str MalSymbol. - endif endif endif endif endif endif endif endif endif endif endif + endif endif endif endif endif endif endif endif endif endif endif endif dup skip-elem = while drop repeat ; ' read-form2 is read-form -- cgit v1.2.3 From 69972a8399efe4abb8567526e90262e131f90d26 Mon Sep 17 00:00:00 2001 From: Chouser Date: Sat, 14 Feb 2015 13:40:07 -0500 Subject: forth: Add step 3 --- forth/reader.fs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index edd99fc..8f7e3e3 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -135,9 +135,10 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) ; : read-wrapped ( buf-addr buf-len quote-char sym-addr sym-len -- buf-addr buf-len char mal-list ) - MalSymbol. { sym } ( buf-addr buf-len char ) - read-form mal-nil conj ( buf-addr buf-len char mal-list ) - sym swap conj ; + here { old-here } + MalSymbol. , ( buf-addr buf-len char ) + read-form , ( buf-addr buf-len char ) + old-here here>MalArray ; : read-form2 ( str-addr str-len char -- str-addr str-len char mal-obj ) begin @@ -145,7 +146,7 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) dup mal-digit? if read-int else dup [char] ( = if [char] ) read-array else dup [char] [ = if [char] ] read-array MalVector new tuck MalVector/list ! else - dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else + dup [char] { = if [char] } read-array MalMap new tuck MalMap/list ! else dup [char] " = if read-string-literal else dup [char] ; = if read-comment else dup [char] : = if drop adv-str read-symbol-str MalKeyword. else -- cgit v1.2.3 From c05d35e8dd1ebbc371d7c9239d788ddf844eae31 Mon Sep 17 00:00:00 2001 From: Chouser Date: Sat, 14 Feb 2015 15:18:18 -0500 Subject: forth: Get rid of car/cdr style lists Rename MalArray to MalList --- forth/reader.fs | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 8f7e3e3..f65db2c 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -91,32 +91,7 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) out-addr out-len MalString. ; : read-list ( str-addr str-len open-paren-char close-paren-char - -- str-addr str-len non-paren-char mal-list ) - \ push objects onto "dictionary" -- maybe not the best stack for this? - 0 { close-char len } - drop adv-str - begin ( str-addr str-len char ) - skip-spaces ( str-addr str-len non-space-char ) - over 0= if - drop 2drop - s\" expected '" close-char str-append-char - s\" ', got EOF" str-append safe-type 1 throw - endif - dup close-char <> - while ( str-addr str-len non-space-non-paren-char ) - read-form , len 1+ to len - repeat - drop adv-str - - \ pop objects out of "dictionary" into MalList - mal-nil - len 0 ?do - 0 cell - allot - here @ swap conj - loop ; - -: read-array ( str-addr str-len open-paren-char close-paren-char - -- str-addr str-len non-paren-char mal-array ) + -- str-addr str-len non-paren-char mal-list ) here { close-char old-here } drop adv-str begin ( str-addr str-len char ) @@ -131,22 +106,22 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) read-form , repeat drop adv-str - old-here here>MalArray + old-here here>MalList ; : read-wrapped ( buf-addr buf-len quote-char sym-addr sym-len -- buf-addr buf-len char mal-list ) here { old-here } MalSymbol. , ( buf-addr buf-len char ) read-form , ( buf-addr buf-len char ) - old-here here>MalArray ; + old-here here>MalList ; : read-form2 ( str-addr str-len char -- str-addr str-len char mal-obj ) begin skip-spaces dup mal-digit? if read-int else - dup [char] ( = if [char] ) read-array else - dup [char] [ = if [char] ] read-array MalVector new tuck MalVector/list ! else - dup [char] { = if [char] } read-array MalMap new tuck MalMap/list ! else + dup [char] ( = if [char] ) read-list else + dup [char] [ = if [char] ] read-list MalVector new tuck MalVector/list ! else + dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else dup [char] " = if read-string-literal else dup [char] ; = if read-comment else dup [char] : = if drop adv-str read-symbol-str MalKeyword. else -- cgit v1.2.3 From 60801ed68d5b2c6630c83883de150ccce98767f9 Mon Sep 17 00:00:00 2001 From: Chouser Date: Sun, 15 Feb 2015 13:33:44 -0500 Subject: forth: Add step 4, but not varargs --- forth/reader.fs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index f65db2c..2ed3446 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -141,8 +141,13 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) obj swap conj s" with-meta" MalSymbol. swap conj else - read-symbol-str MalSymbol. - endif endif endif endif endif endif endif endif endif endif endif endif + read-symbol-str + 2dup s" true" str= if 2drop mal-true + else 2dup s" false" str= if 2drop mal-false + else 2dup s" nil" str= if 2drop mal-nil + else + MalSymbol. + endif endif endif endif endif endif endif endif endif endif endif endif endif endif endif dup skip-elem = while drop repeat ; ' read-form2 is read-form -- cgit v1.2.3 From bf6a574e00a221dfe564ba11148deaa73ba8a229 Mon Sep 17 00:00:00 2001 From: Chouser Date: Mon, 16 Feb 2015 20:12:44 -0500 Subject: forth: Add step 6, clean up comment parsing --- forth/reader.fs | 119 +++++++++++++++++++++++++------------------------------- 1 file changed, 52 insertions(+), 67 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 2ed3446..6547a79 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -1,8 +1,6 @@ require types.fs require printer.fs --2 constant skip-elem - \ Drop a char off the front of string by advancing the addr and \ decrementing the length, and fetch next char : adv-str ( str-addr str-len -- str-addr str-len char ) @@ -10,17 +8,6 @@ require printer.fs dup 0= if 0 ( eof ) else over c@ endif ; -: skip-spaces ( str-addr str-len char -- str-addr str-len non-space-char ) - begin - dup bl = if - -1 - else - dup [char] , = - endif - while ( str-addr str-len space-char ) - drop adv-str - repeat ; - : mal-digit? ( char -- flag ) dup [char] 9 <= if [char] 0 >= @@ -30,22 +17,32 @@ require printer.fs : char-in-str? ( char str-addr str-len ) rot { needle } - begin ( str-addr str-len ) - adv-str needle = if - 2drop -1 -1 \ success! drop and exit + false -rot + over + swap ?do + i c@ needle = if drop true leave endif + loop ; + +: sym-char? ( char -- flag ) + s\" \n\r\t\000[]{}()'\"`,; " char-in-str? 0= ; + +: skip-spaces ( str-addr str-len char -- str-addr str-len non-space-char ) + begin + begin + dup s\" \n\r\t, " char-in-str? + while ( str-addr str-len space-char ) + drop adv-str + repeat + dup [char] ; = if + drop + begin + adv-str s\" \n\r\000" char-in-str? + until + adv-str false else - dup 0= if - 2drop 0 -1 \ str consumed, char not found. - else - 0 \ continue - endif + true endif until ; -s\" []{}()'\"`,; " constant non-sym-chars-len constant non-sym-chars -: sym-char? ( char -- flag ) - non-sym-chars non-sym-chars-len char-in-str? 0= ; - defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) : read-int ( str-addr str-len digit-char -- str-addr str-len non-digit-char mal-int ) @@ -56,13 +53,6 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) until int MalInt. ; -: read-comment ( str-addr str-len sym-char -- str-addr str-len char skim-elem ) - drop - begin - adv-str = 10 - until - adv-str skip-elem ; - : read-symbol-str ( str-addr str-len sym-char -- str-addr str-len char sym-addr sym-len ) new-str { sym-addr sym-len } begin ( str-addr str-len sym-char ) @@ -106,8 +96,7 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) read-form , repeat drop adv-str - old-here here>MalList - ; + old-here here>MalList ; : read-wrapped ( buf-addr buf-len quote-char sym-addr sym-len -- buf-addr buf-len char mal-list ) here { old-here } @@ -116,40 +105,36 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) old-here here>MalList ; : read-form2 ( str-addr str-len char -- str-addr str-len char mal-obj ) - begin - skip-spaces - dup mal-digit? if read-int else - dup [char] ( = if [char] ) read-list else - dup [char] [ = if [char] ] read-list MalVector new tuck MalVector/list ! else - dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else - dup [char] " = if read-string-literal else - dup [char] ; = if read-comment else - dup [char] : = if drop adv-str read-symbol-str MalKeyword. else - dup [char] @ = if drop adv-str s" deref" read-wrapped else - dup [char] ' = if drop adv-str s" quote" read-wrapped else - dup [char] ` = if drop adv-str s" quasiquote" read-wrapped else - dup [char] ~ = if - drop adv-str - dup [char] @ = if drop adv-str s" splice-unquote" read-wrapped - else s" unquote" read-wrapped - endif - else - dup [char] ^ = if - drop adv-str - read-form { meta } read-form { obj } - meta mal-nil conj - obj swap conj - s" with-meta" MalSymbol. swap conj + skip-spaces + dup mal-digit? if read-int else + dup [char] ( = if [char] ) read-list else + dup [char] [ = if [char] ] read-list MalVector new tuck MalVector/list ! else + dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else + dup [char] " = if read-string-literal else + dup [char] : = if drop adv-str read-symbol-str MalKeyword. else + dup [char] @ = if drop adv-str s" deref" read-wrapped else + dup [char] ' = if drop adv-str s" quote" read-wrapped else + dup [char] ` = if drop adv-str s" quasiquote" read-wrapped else + dup [char] ~ = if + drop adv-str + dup [char] @ = if drop adv-str s" splice-unquote" read-wrapped + else s" unquote" read-wrapped + endif + else + dup [char] ^ = if + drop adv-str + read-form { meta } read-form { obj } + meta mal-nil conj + obj swap conj + s" with-meta" MalSymbol. swap conj + else + read-symbol-str + 2dup s" true" str= if 2drop mal-true + else 2dup s" false" str= if 2drop mal-false + else 2dup s" nil" str= if 2drop mal-nil else - read-symbol-str - 2dup s" true" str= if 2drop mal-true - else 2dup s" false" str= if 2drop mal-false - else 2dup s" nil" str= if 2drop mal-nil - else - MalSymbol. - endif endif endif endif endif endif endif endif endif endif endif endif endif endif endif - dup skip-elem = - while drop repeat ; + MalSymbol. + endif endif endif endif endif endif endif endif endif endif endif endif endif endif ; ' read-form2 is read-form : read-str ( str-addr str-len - mal-obj ) -- cgit v1.2.3 From 794bfca1361fc6900f0ea0186d64111c3a02b0f8 Mon Sep 17 00:00:00 2001 From: Chouser Date: Mon, 16 Feb 2015 21:28:05 -0500 Subject: forth: Add step 7 --- forth/reader.fs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 6547a79..1daa650 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -98,9 +98,15 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) drop adv-str old-here here>MalList ; +s" deref" MalSymbol. constant deref-sym +s" quote" MalSymbol. constant quote-sym +s" quasiquote" MalSymbol. constant quasiquote-sym +s" splice-unquote" MalSymbol. constant splice-unquote-sym +s" unquote" MalSymbol. constant unquote-sym + : read-wrapped ( buf-addr buf-len quote-char sym-addr sym-len -- buf-addr buf-len char mal-list ) here { old-here } - MalSymbol. , ( buf-addr buf-len char ) + , ( buf-addr buf-len char ) read-form , ( buf-addr buf-len char ) old-here here>MalList ; @@ -112,13 +118,13 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) dup [char] { = if [char] } read-list MalMap new tuck MalMap/list ! else dup [char] " = if read-string-literal else dup [char] : = if drop adv-str read-symbol-str MalKeyword. else - dup [char] @ = if drop adv-str s" deref" read-wrapped else - dup [char] ' = if drop adv-str s" quote" read-wrapped else - dup [char] ` = if drop adv-str s" quasiquote" read-wrapped else + dup [char] @ = if drop adv-str deref-sym read-wrapped else + dup [char] ' = if drop adv-str quote-sym read-wrapped else + dup [char] ` = if drop adv-str quasiquote-sym read-wrapped else dup [char] ~ = if drop adv-str - dup [char] @ = if drop adv-str s" splice-unquote" read-wrapped - else s" unquote" read-wrapped + dup [char] @ = if drop adv-str splice-unquote-sym read-wrapped + else unquote-sym read-wrapped endif else dup [char] ^ = if -- cgit v1.2.3 From 580c4eef9d61f39264813b662fe5335c3c3c4ee5 Mon Sep 17 00:00:00 2001 From: Chouser Date: Tue, 17 Feb 2015 18:47:23 -0500 Subject: forth: Add step 9, just try*/throw - Moved some stuff out of printer into str, to support throwing strings in types.fs - Fixed an apparently completely broken 'nth' - Still failing 120 step9 tests --- forth/reader.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'forth/reader.fs') diff --git a/forth/reader.fs b/forth/reader.fs index 1daa650..134749b 100644 --- a/forth/reader.fs +++ b/forth/reader.fs @@ -66,7 +66,7 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) drop \ drop leading quote begin ( in-addr in-len ) adv-str over 0= if - 2drop s\" expected '\"', got EOF\n" safe-type 1 throw + 2drop 0 0 s\" expected '\"', got EOF" ...throw-str endif dup [char] " <> while @@ -87,9 +87,9 @@ defer read-form ( str-addr str-len -- str-addr str-len mal-obj ) begin ( str-addr str-len char ) skip-spaces ( str-addr str-len non-space-char ) over 0= if - drop 2drop - s\" expected '" close-char str-append-char - s\" ', got EOF" str-append safe-type 1 throw + drop 2drop 0 0 s" ', got EOF" + close-char pad ! pad 1 + s" expected '" ...throw-str endif dup close-char <> while ( str-addr str-len non-space-non-paren-char ) -- cgit v1.2.3