~vdupras/tumbleforth

3ab2cfc2941fc7bcbc18c5ce276b01c021348081 — Virgil Dupras 10 months ago a4569be
01-duskcc/11-eye: adapt to the new signature of "sfind"
2 files changed, 12 insertions(+), 9 deletions(-)

M 01-duskcc/11-eye.md
M 01-duskcc/11-eye/mycc.fs
M 01-duskcc/11-eye.md => 01-duskcc/11-eye.md +10 -8
@@ 112,12 112,13 @@ dumping the contents of `args` after having run `cc`:

Such a string list can easily be iterated upon and thus fulfills our
requirement to map names to indexes. The "str" library has a word for this,
`sfind ( str list -- idx )` which returns the index of the specified string in
the list, or -1 if not found:
`sfind ( str list -- idx? f )` which returns the index of the specified string
in the list. It has the same kind of dynamic stack effect as `cidx` presented
in the previous article:

    S" a" args sfind . \ prints 0
    S" b" args sfind . \ prints 1
    S" hello" args sfind . \ prints -1
    S" a" args sfind . . \ prints 10
    S" b" args sfind . . \ prints 11
    S" hello" args sfind . \ prints 0

Memory-wise, you can see that I chose a static buffer for simplicity, with the
tradeoff that it can only handle `ARGSLEN` bytes in its list. Parsing a


@@ 140,7 141,8 @@ C source of our `foo()` function compiled and then ran?

    : assert ( f -- ) not if err then ;
    : parseExpression ( -- ) 
      nextt args sfind nextt S" +" expect nextt args sfind ( idx1 idx2 )
      nextt args sfind assert nextt S" +" expect
      nextt args sfind assert ( idx1 idx2 )
      ?swap 1 = assert 0 = assert ( )
      eax esi addr[], ;
    : argscnt ( -- cnt ) 0 args begin dup c@ while swap 1+ swap s) repeat drop ;


@@ 168,8 170,8 @@ The rest of the code is usage of the i386 assembler you've written yourself.
So this is it! it's done! *Mission Accomplished!* as they say in America with
much fanfare.

Of course, this parsing code only works for a tiny
subset of the C language. It can't:
Of course, this parsing code only works for a tiny subset of the C language. It
can't:

1. Have more than one statement in its body.
2. Have a statement other than `return`.

M 01-duskcc/11-eye/mycc.fs => 01-duskcc/11-eye/mycc.fs +2 -1
@@ 15,7 15,8 @@ create args ARGSLEN allot
    drop repeat ( a tok )
  S" )" expect 0 swap c! ;
: parseExpression ( -- )
  nextt args sfind nextt S" +" expect nextt args sfind ( idx1 idx2 )
  nextt args sfind assert nextt S" +" expect
  nextt args sfind assert ( idx1 idx2 )
  ?swap 1 = assert 0 = assert ( )
  eax esi addr[], ;
: parseStatement ( -- )