Skip to content

Instantly share code, notes, and snippets.

@fbwright
Last active August 29, 2015 14:15
Show Gist options
  • Save fbwright/61cecc433491c9c449e5 to your computer and use it in GitHub Desktop.
Save fbwright/61cecc433491c9c449e5 to your computer and use it in GitHub Desktop.
/r/dailyprogrammer Challenge #202 - I AM BENDER. PLEASE INSERT GIRDER.
( ch202.fs 2015-02-22T19.49 by fbwright )
1024 CONSTANT buffer-size
CREATE buffer-in buffer-size CHARS ALLOT
CREATE index-in 1 CELLS ALLOT
CREATE buffer-out buffer-size 8 / CHARS ALLOT
CREATE index-out 1 CELLS ALLOT
( Iterates over buffer-in: every 8 cells it pushes a char to )
( buffer-out and increases index-out by 1 )
: parse-buffer ( -- ) 0 index-out ! 0 index-in @ 0 u+do
7 i 8 mod - buffer-in i + c@ case
'0 of drop endof
'1 of 1 swap lshift or endof
( else ) ." Error: unknown character '" emit
." ' at position " i . '. emit cr
endcase
i 8 mod 7 = if buffer-out index-out @ + c!
1 index-out +! 0 then loop drop ;
( Reads a line [max 80 chars], puts it into the buffer and )
( returns whether it read anything )
: read-line ( -- ? ) cr ." > " buffer-in index-in @ + 80 accept
dup index-in +! 0= ;
: buffer-filled? ( -- ? ) index-in @ buffer-size < ;
: translate ( -- ) 0 index-in ! begin
read-line buffer-filled? and
until parse-buffer cr buffer-out index-out @ type cr ;
translate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment