From 3ab2cfc2941fc7bcbc18c5ce276b01c021348081 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Tue, 7 Nov 2023 09:41:33 -0500 Subject: [PATCH] 01-duskcc/11-eye: adapt to the new signature of "sfind" --- 01-duskcc/11-eye.md | 18 ++++++++++-------- 01-duskcc/11-eye/mycc.fs | 3 ++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/01-duskcc/11-eye.md b/01-duskcc/11-eye.md index 8a813e3..f0b1314 100644 --- a/01-duskcc/11-eye.md +++ b/01-duskcc/11-eye.md @@ -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`. diff --git a/01-duskcc/11-eye/mycc.fs b/01-duskcc/11-eye/mycc.fs index 0c4adbc..d1dc1dd 100644 --- a/01-duskcc/11-eye/mycc.fs +++ b/01-duskcc/11-eye/mycc.fs @@ -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 ( -- ) -- 2.45.2