forked from MPETREMANN11/eForth-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix.txt
279 lines (238 loc) · 8.77 KB
/
infix.txt
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
\ *********************************************************************
\ convert infix to postfix notation
\ Filename: infix.txt
\ Date: 15 oct 2022
\ Updated: 21 jan 2023
\ File Version: 1.1
\ MCU: ueForth
\ Copyright: Marc PETREMANN
\ Author: Marc PETREMANN
\ GNU General Public License
\ *********************************************************************
DEFINED? --infix [if] forget --infix [then]
create --infix
\ Define operator stack
80 constant OPERATORS \ increase value 80 if needed
cell 2* OPERATORS * constant OP-SIZE
create op \ operator stack
OP-SIZE allot
\ pointer for operator pointer
0 value op-pointer
\ get current operator pointer
: opp@ ( --- addr )
op op-pointer +
;
\ increase operator pointer
: opp+ ( -- )
cell 2* +to op-pointer
;
\ decrease operator pointer
: opp- ( -- )
cell 2* negate +to op-pointer
;
\ execute or compile word
: ?interp ( cfa -- )
state @ \ 0 = interpret mode, -1 = compile mode
if , \ compile word
else execute \ execute word
then
;
\ store cfa and level on operator staock
: >op ( cfa level -- )
opp+
opp@ 2!
;
\ get cfa from operator stacl
: op> ( -- )
opp@ 2@
opp-
swap drop ?interp
;
\ get level pointed by current operator stack
: lev? ( -- level )
op-pointer op + @
;
\ end an algebric formula
: ]$
begin lev?
while op>
repeat
forth ; immediate
\ execute part of algebra operator
: dealing ( addr --- )
2@
begin over lev? > invert
while >r >r op> r> r>
repeat
>op
;
\ define algebra operator.
: infix ( n --- <word1> <word2> in compilation )
' \ get cfa word1
create \ create word2
swap , ,
immediate
does>
dealing
;
vocabulary algebra algebra definitions
7 infix * * 7 infix / /
6 infix + + 6 infix - -
5 infix > > 5 infix < < 5 infix = =
4 infix invert not
3 infix and and
2 infix or or
: noop ;
: (
1 ['] noop >op
; immediate
: )
[ forth ]
begin 1 lev? <
while op>
repeat
1 lev? =
if opp-
else ." miss " 40 emit space
-1 throw
then
; immediate
\ start algebric formula
forth definitions
: $[
op OP-SIZE 0 fill
0 to op-pointer
algebra
; immediate
\ *** examples: ******************************************************
2 value two
0 value result
: ex1
$[ ( ( two + 3 ) * ( 4 + 1 ) ) ]$
to result
\ two 3 + 4 1 + *
;
: ex2
$[ ( ( 2 + 3 ) * ( 4 - 1 ) / 2 ) ]$
\ 2 3 + 4 1 - * 2 /
;
: ex3
$[ 48 / 2 * ( 9 + 3 ) ]$
\ 48 2 / 9 3 + *
;
-1 constant true
0 constant false
0 value KELVIN
0 value FAHRENHEIT
0 value tempCelsius
: toK ( ---)
false to FAHRENHEIT
true to KELVIN
;
: toF ( ---)
false to KELVIN
true to FAHRENHEIT
;
: .temp ( deg --- deg2)
to tempCelsius
$[
( ( tempCelsius + 273 ) and KELVIN ) +
( ( ( tempCelsius * 9 / 5 ) + 32 ) and FAHRENHEIT )
]$
\ tempCelsius 273 + KELVIN AND tempCelsius 9 * 5 / 32 + FAHRENHEIT AND + .
.
;
\ usage:
\ 10 toF .temp 42 ok
\ 10 toK .temp 283 ok
\ *** Example with filled rectangles **************************
0 value DX \ current X coordinate
0 value DY \ current Y coordinate
: dx+ ( ---) \ increment DX 1 position
DX 1+ to DX ;
: dy+ ( ---) \ increment DY 1 position
DY 1+ to DY ;
0 value SX \ x Start rect 1
0 value EX \ x End rect 1
0 value SY \ y Start rect 1
0 value EY \ y Etart rect 1
: inRect? ( --- fl)
$[ ( NOT ( DX < SX ) ) AND
( NOT ( DX > EX ) ) AND
( NOT ( DY < SY ) ) AND
( NOT ( DY > EY ) )
]$ ;
: inLeftRect? ( --- fl)
4 to SX \ x Start rect left
40 to EX \ x End rect left
4 to SY \ y Start rect left
16 to EY \ y Etart rect left
inRect? ;
: inRightRect? ( --- fl)
22 to SX \ x Start rect right
58 to EX \ x End rect right
10 to SY \ y Start rect right
26 to EY \ y Etart rect right
inRect? ;
: dispChar ( ---)
$[ \ 46 is char .
( 46 * ( ( ( NOT inLeftRect? ) AND ( NOT inRightRect? ) ) abs ) )
\ 43 is char +
+ ( 43 * ( ( ( inLeftRect? ) AND ( NOT inRightRect? ) ) abs ) )
\ 88 is char X
+ ( 88 * ( inLeftRect? AND inRightRect? abs ) )
\ 111 is char o
+ ( 111 * ( ( ( inRightRect? ) and ( NOT inLeftRect? ) ) abs ) )
]$
emit
;
\ Execution of see dispChar display:
\ : dispChar
\ 46 inLeftRect? invert inRightRect? invert AND abs *
\ 43 inLeftRect? inRightRect? invert AND abs * +
\ 88 inLeftRect? inRightRect? abs AND * +
\ 111 inRightRect? inLeftRect? invert AND abs * +
\ emit
\ ;
: graphLoop ( ---)
30 for
cr
120 for
dispChar
dx+
next
dy+
0 to DX
next ;
\ Execution of graphLoop display:
\ .........................................................................................................................
\ .........................................................................................................................
\ .........................................................................................................................
\ .........................................................................................................................
\ ....+++++++++++++++++++++++++++++++++++++................................................................................
\ ....+++++++++++++++++++++++++++++++++++++................................................................................
\ ....+++++++++++++++++++++++++++++++++++++................................................................................
\ ....+++++++++++++++++++++++++++++++++++++................................................................................
\ ....+++++++++++++++++++++++++++++++++++++................................................................................
\ ....+++++++++++++++++++++++++++++++++++++................................................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ....++++++++++++++++++XXXXXXXXXXXXXXXXXXXoooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ ......................ooooooooooooooooooooooooooooooooooooo..............................................................
\ .........................................................................................................................
\ .........................................................................................................................
\ .........................................................................................................................
\ .........................................................................................................................