aboutsummaryrefslogtreecommitdiff
path: root/forth/printer.fs
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2015-02-16 20:12:44 -0500
committerChouser <chouser@n01se.net>2015-02-21 13:22:44 -0500
commitbf6a574e00a221dfe564ba11148deaa73ba8a229 (patch)
tree39e511f1913353335df9655ce75a4867d129e18d /forth/printer.fs
parentcd21ff0d3ccfbec62fe6af95e6656fe9c38f8254 (diff)
downloadmal-bf6a574e00a221dfe564ba11148deaa73ba8a229.tar.gz
mal-bf6a574e00a221dfe564ba11148deaa73ba8a229.zip
forth: Add step 6, clean up comment parsing
Diffstat (limited to 'forth/printer.fs')
-rw-r--r--forth/printer.fs41
1 files changed, 9 insertions, 32 deletions
diff --git a/forth/printer.fs b/forth/printer.fs
index d035e94..5309745 100644
--- a/forth/printer.fs
+++ b/forth/printer.fs
@@ -130,40 +130,17 @@ MalKeyword
kw unpack-keyword str-append ;;
drop
-: insert-\ ( str-addr str-len insert-idx -- str-addr str-len )
- -rot 0 str-append-char { addr len }
- dup dup addr + dup 1+ ( i i from to )
- rot len swap - cmove> ( i ) \ shift " etc to the right
- addr + [char] \ swap c! \ escape it!
- addr len
- ;
-
: escape-str { addr len }
s\" \"" str-append
- 0 ( i )
- begin
- dup len <
- while
- dup addr + c@ ( i char )
- dup [char] " = over [char] \ = or if ( i char )
- drop dup addr len rot insert-\ to len to addr
- 1+
- else
- dup 10 = if ( i ) \ newline?
- drop dup addr len rot insert-\ to len to addr
- dup addr + 1+ [char] n swap c!
- 1+
- else
- 13 = if ( i ) \ return?
- dup addr len rot insert-\ to len to addr
- dup addr + 1+ [char] r swap c!
- 1+
- endif
- endif
- endif
- 1+
- repeat
- drop addr len str-append
+ addr len + addr ?do
+ i c@ case
+ [char] " of s\" \\\"" str-append endof
+ [char] \ of s\" \\\\" str-append endof
+ 10 of s\" \\n" str-append endof
+ 13 of s\" \\r" str-append endof
+ -rot i 1 str-append rot
+ endcase
+ loop
s\" \"" str-append ;
MalString