File: toolsext.fs

package info (click to toggle)
gforth 0.4.9.19990617-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,040 kB
  • ctags: 868
  • sloc: ansic: 3,794; sh: 1,928; lisp: 1,335; makefile: 649; sed: 129
file content (161 lines) | stat: -rw-r--r-- 6,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
\ Copyright (C) 1995,1998 Free Software Foundation, Inc.

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation; either version 2
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program; if not, write to the Free Software
\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Warnings off

Variable countif

: dummy ;  immediate
: >exec  >r ; restrict ( :-)
: scanIF   f83find  dup 0=  IF  drop ['] dummy >head  THEN  ;

Create [struct]-search    ' scanIF A,  ' (reveal) A,  ' drop A, ' drop A,
Create [struct]-voc       [struct]-search A,
                          NIL A,       NIL A,       NIL A,

: ?if  countif @ 0<
  IF  [ [struct]-voc 3 cells + ] ALiteral @ lookup !  THEN ;

UNLOCK  Tlast @ TNIL Tlast !  LOCK
\ last @  0 last !

: [IF]
  1 countif +! ?if ;       immediate
: [THEN]
  -1 countif +! ?if ;       immediate
: [ELSE]
  postpone [THEN] postpone [IF] ;
                                     immediate
' [IF]   Alias [IFDEF]               immediate
' [IF]   Alias [IFUNDEF]             immediate
' [THEN] Alias [ENDIF]               immediate
' [IF]   Alias [BEGIN]               immediate
' [IF]   Alias [WHILE]               immediate
' [THEN] Alias [UNTIL]               immediate
' [THEN] Alias [AGAIN]               immediate
' [IF]   Alias [DO]                  immediate
' [IF]   Alias [?DO]                 immediate
' [THEN] Alias [LOOP]                immediate
' [THEN] Alias [+LOOP]               immediate
: [REPEAT]  postpone [AGAIN] postpone [THEN] ;
                                     immediate
' ( Alias (                          immediate
' \ Alias \                          immediate

UNLOCK Tlast @ swap Tlast ! LOCK
\ last @ swap last !
1 cells - [struct]-voc cell+ !

\ Interpretative Structuren                            30apr92py

: defined   bl word find nip 0<> ; immediate

: [IF] ( flag | flag "<spaces>name ..." -- ) \ tools-ext bracket-if
  \G If flag is @code{TRUE} do nothing (and therefore
  \G execute subsequent words as normal). If flag is @code{FALSE},
  \G parse and discard words from the parse
  \G area (refilling it if necessary using
  \G @code{REFILL}) including nested instances of @code{[IF]}..
  \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
  \G until the balancing @code{[ELSE]} or @code{[THEN]} has been
  \G parsed and discarded. Immediate word.
       0= IF  countif off
              lookup @ [ [struct]-voc 3 cells + ] ALiteral !
	      [struct]-voc lookup !
          THEN ;                                      immediate

: [IFDEF] ( "<spaces>name" -- ) \ gforth bracket-if-def
  \G If name is found in the current search-order, behave like
  \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
  \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
  postpone defined    postpone [IF] ;                 immediate

: [IFUNDEF] ( "<spaces>name" -- ) \ gforth bracket-if-un-def
  \G If name is not found in the current search-order, behave like
  \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
  \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
  postpone defined 0= postpone [IF] ;                 immediate

: [ELSE]  ( "<spaces>name ..." -- ) \ tools-ext bracket-else
  \G Parse and discard words from the parse
  \G area (refilling it if necessary using
  \G @code{REFILL}) including nested instances of @code{[IF]}..
  \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
  \G until the balancing @code{[THEN]} has been parsed and discarded.
  \G @code{[ELSE]} only gets executed if the balancing @code{[IF]}
  \G was @code{TRUE}; if it was @code{FALSE}, @code{[IF]} would
  \G have parsed and discarded the @code{[ELSE]}, leaving the
  \G subsequent words to be executed as normal.
  \G Immediate word.
  0 postpone [IF] ;                                   immediate

: [THEN] ( -- ) \ tools-ext bracket-then
  \G Do nothing; used as a marker for other words to parse
  \G and discard up to. Immediate word.
  ;                                                   immediate

: [ENDIF] ( -- ) \ gforth bracket-end-if
  \G Do nothing; synonym for @code{[THEN]}
  ;                                                   immediate

\ Structs for interpreter                              28nov92py

User (i)

: [DO]  ( n-limit n-index -- ) \ gforth bracket-do
  >in @ -rot
  DO   I (i) ! dup >r >in ! interpret r> swap +LOOP  drop ;
                                                      immediate

: [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
  2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
                                                      immediate

: [+LOOP] ( n -- ) \ gforth bracket-question-plus-loop
  rdrop rdrop ;                                       immediate

: [LOOP] ( -- ) \ gforth bracket-loop
  1 rdrop rdrop ;                                     immediate

: [FOR] ( n -- ) \ gforth bracket-for
  0 swap postpone [DO] ;                              immediate

: [NEXT] ( n -- ) \ gforth bracket-next
  -1 rdrop rdrop ;                                    immediate

:noname (i) @ ;
:noname (i) @ postpone Literal ;
interpret/compile: [I] ( -- n ) \ gforth bracket-i

: [BEGIN] ( -- ) \ gforth bracket-begin
  >in @ >r BEGIN r@ >in ! interpret UNTIL rdrop ;     immediate

' [+LOOP]  Alias [UNTIL] ( flag -- ) \ gforth bracket-until
                                                      immediate

: [REPEAT]  ( -- ) \ gforth bracket-repeat
  false rdrop rdrop ;                                 immediate

' [REPEAT] Alias [AGAIN] ( -- ) \ gforth bracket-again
                                                      immediate

: [WHILE]   ( flag -- ) \ gforth bracket-while
  0= IF   postpone [ELSE] true rdrop rdrop 1 countif +!  THEN ;
                                                      immediate

Warnings on