Skip to content

Instantly share code, notes, and snippets.

@lsparrish
lsparrish / blocker.rst
Created December 22, 2009 08:11 — forked from crcx/blocker.rst

Block Conversion

Author: Charles Childers
Version: 2009.12.21

Blocker User's Guide

Chorded Key Entry

Author: Luke Parrish
Version: 2009.12.22

Chorder User Guide

Overview

( This is an implementation of crc's ifExists which demonstrates the )
( parsing of curly braces for the conditional evaluation of text. )
{{
: cut ( a-a )
dup getLength over + 1- dup @ 32 =if
0 swap ! else drop then ;
: "cut ( c"-a ) accept tib tempString cut ;
: '{ ( "-a ) char: { "cut ;
: '} ( "-a ) char: } "cut ;
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( For loop with an index that counts from zero, placed on the )
( top of the stack with each iteration. Uses NORS to keep )
( track of the original value, so nexti must be used to clean )
( up when loop is complete. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
: fori ` dup ` push ` for ` pop ` pop ` 2dup
` push ` push ` swap ` - ; compile-only
: nexti ` next ` pop ` drop ; compile-only
( This is an attempt at creating some rather sophisticated macros )
( using fori/nexti. It does not work yet. )
{{
variable addr
: pad ( - ) addr @ @ 6 for dup base @ r pow <if 32 emit then next ;
: .wrap ( - ) ." ( " later ." )" ;
: .name ( a- ) .wrap d->name type ;
( Stack and list manipulation. )
: allot heap +! ;
: -allot heap -! ;
: sav ` dup ` push ; immediate
: tempsave ( ?n- ) for here r 1- + ! next ;
: tempload ( n-? ) for here r 1- + @ next ;
( A macro for appending code blocks. )
{{
: prepend ( $$-$ ) here -rot 2 for dup here swap getLength dup allot copy next 0 , ;
: $space ( $-$ ) s" " prepend ;
---reveal---
: append-block 32 accept tib @ char: { =if
char: } accept tib tempString keepString literal,
` $space ` swap ` prepend then ; compile-only
: code ( "- ) ` append-block ` dup ` getLength ` eval ; compile-only
}}
: | char: | accept ;
{{
| find $- ::: true if $ is a word. |
| accept c"- ::: parsed text (delimited by c) goes at tib |
| tib -$ ::: used to access text after parsing. |
| nip xy-y ::: shorthand for swap drop |
| dup n-nn ::: duplicate top of stack |
| char: "-c ::: macro, gets a char at compile time. |
| if f- ::: jump to then if flag is false (at execute time) |
| then - ::: macro for the place that if jumps to when flag is false. |
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( do ... until )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Value n is taken from the stack and stored to a virtual )
( variable. When this number is equal to the TOS at the time )
( until is executed, the loop terminates. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
{{
: skipped-nop ( - ) 8 , here 2 + , 0 , ;

The program:

( buffering input )

{{
64 constant width
4  constant lines
variable num