Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Adds [IF] [ELSE] [THEN]
  • Loading branch information
Jos-Ven committed Jan 13, 2020
1 parent 38f4759 commit c998b6a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions interp.forth
@@ -0,0 +1,40 @@
create: cmp$ 8 allot

: match-[str] ( cstr addr -- cstr addr fl ) dup strlen 2over compare ;

: [ELSE] ( -- ) \ ( interpretive control structure -- alternate selection )
1
begin ?dup
while word tuck cmp$ swap 8 min cmove cmp$ swap ( - if-count adr len )
"[IF]" match-[str]
if 2drop 1+
else "[ELSE]" match-[str]
if 2drop dup 1 <> and
else "[THEN]" match-[str]
if 2drop 1-
else "/end" match-[str]
if cr print: 'Missing [THEN] ' 3drop 0 /end
else 2drop
then then then then ( - if-count )
repeat ; immediate


: [IF] ( flag -- ) \ ( interpretive control structure -- select on true )
0= if postpone: [ELSE] then ; immediate

: [THEN] ( -- ) \ ( interpretive control structure -- end the structure )
; immediate
/end

\ Test:

cr cr

0 [IF] -3
[ELSE] -9
1 [IF] -21
[ELSE] -22
[THEN]
[THEN]

/end

0 comments on commit c998b6a

Please sign in to comment.