-
Notifications
You must be signed in to change notification settings - Fork 2
/
functionForth.pde
666 lines (637 loc) · 15.2 KB
/
functionForth.pde
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// Forth Functions
// dup (a -- a a) Duplicate the top value
// drop (a -- ) Remove the top value
// swap (a b -- b a ) Exchage the top two value
// over (a b -- a b a) Copy the second value on top
// rot (a b c -- b c a) Move the third value on top
// nip (a b -- b) Delete the the second value
// tuck (a b -- b a b) Copy the top value below the second value.
//
// 2dup (a b -- a b a b) Duplicate the top couple of value
// 2drop (a b -- ) Remove the top couple of value
// 2swap (a b c d -- c d a b) Exchange the top two couple of value
// 2over (a b c d -- a b c d a b) Copy the second couple of value on top
// 2rot (a b c d e f -- c d e f a b) Move third couple of value on top
// 2nip (a b c d -- c d) Delete the second couple of value
// 2tuck (a b c d -- c d a b c d) Copy the top couple of value below the second couple of value.
// pick ( xu...x1 x0 u -- xu...x1 x0 xu ) Remove u. Copy the xu to the top of the stack. Num items >= u+2.
// roll ( xu xu-1 ... x0 u -- xu-1 ... x0 xu ) Remove u. Rotate u+1 items on the top of the stack. Num items > u+2.
//--------------------------------------------------------------------------
// DUP (a -- a a) Duplica il valore in cima alla pila
void fnDUP() // duplicate top value
{
pila.push(digitNUM);
digitSTR="";
isResult = true;
isBlocked = false;
outputSTR = String.valueOf(digitNUM);
}
// DROP (a -- ) Elimina il valore in cima alla pila
void fnDROP() // eliminate top value
{
if (pila.stackSize() > 0)
{
digitNUM = pila.pop();
isResult = true;
isBlocked = false;
}
else
{
digitNUM = 0.0;
isResult = true;
isBlocked = true;
}
outputSTR = String.valueOf(digitNUM);
}
// SWAP (a b -- b a) Scambia i due valori in cima alla pila
void fnSWAP() // swap the two top value
{
if (pila.stackSize() > 0)
{
double temp = digitNUM;
digitNUM = pila.pop();
pila.push(temp);
isResult = true;
isBlocked = false;
outputSTR = String.valueOf(digitNUM);
}
else { outputSTR = "SWAP: two numbers required."; }
}
// OVER (a b -- a b a) Copia il secondo valore in cima alla pila
void fnOVER() // copy the second value on top
{
if (pila.stackSize() > 0)
{
double b = digitNUM;
double a = pila.pop();
pila.push(a);
pila.push(b);
digitNUM = a;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "OVER: two numbers required."; }
}
// ROT (a b c -- b c a) Sposta il terzo valore in cima alla pila
void fnROT() // Move third value on top
{
if (pila.stackSize() > 1)
{
double c = digitNUM;
double b = pila.pop();
double a = pila.pop();
pila.push(b);
pila.push(c);
digitNUM = a;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "ROT: three numbers required."; }
}
// NIP (a b -- b) Elimina il secondo valore
void fnNIP() // delete the second value
{
if (pila.stackSize() > 0)
{
double b = digitNUM;
double a = pila.pop();
digitNUM = b;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "NIP: two numbers required."; }
}
// tuck (a b -- b a b)
void fnTUCK() // copy the top value below the second value
{
if (pila.stackSize() > 0)
{
double b = digitNUM;
double a = pila.pop();
pila.push(b);
pila.push(a);
digitNUM = b;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "TUCK: two numbers required."; }
}
// 2DUP (dd -- dd dd) Duplica la coppia di valori in cima alla pila
// 2DUP (a b -- a b a b) Duplica la coppia di valori in cima alla pila
void fn2DUP()
{
if (pila.stackSize() > 0)
{
double b = digitNUM;
double a = pila.pop();
pila.push(a);
pila.push(b);
pila.push(a);
digitNUM = b;
isResult = true;
isBlocked = false;
outputSTR = String.valueOf(digitNUM);
}
else { outputSTR = "2DUP: two numbers required."; }
}
// 2DROP (dd -- ) Elimina la coppia di valori in cima alla pila
// 2DROP (a b -- )
void fn2DROP()
{
if (pila.stackSize() > 0)
{
if (pila.stackSize() == 1)
{
double b = digitNUM;
double a = pila.pop();
digitNUM = 0.0;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = true;
}
else
{
double b = digitNUM;
double a = pila.pop();
digitNUM = pila.pop();
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
else { outputSTR = "2DROP: two numbers required."; }
}
// 2SWAP (d1 d2 -- d2 d1) Scambia le due coppie di valori in cima alla pila
// 2SWAP (a b c d -- c d a b)
void fn2SWAP()
{
if (pila.stackSize() > 2)
{
double d = digitNUM;
double c = pila.pop();
double b = pila.pop();
double a = pila.pop();
pila.push(c);
pila.push(d);
pila.push(a);
digitNUM = b;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "2SWAP: four numbers required."; }
}
// 2OVER (dd1 dd2 -- dd1 dd2 dd1) Copia la seconda coppia di valori in cima alla pila
// 2OVER (a b c d -- a b c d a b)
void fn2OVER()
{
if (pila.stackSize() > 2)
{
double d = digitNUM;
double c = pila.pop();
double b = pila.pop();
double a = pila.pop();
pila.push(a);
pila.push(b);
pila.push(c);
pila.push(d);
pila.push(a);
digitNUM = b;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "2OVER: four numbers required."; }
}
// 2ROT (dd1 dd2 dd3 -- dd2 dd3 dd1) Sposta la terza coppia di valori in cima alla pila
// 2ROT (a b c d e f -- c d e f a b)
void fn2ROT()
{
if (pila.stackSize() > 4)
{
double f = digitNUM;
double e = pila.pop();
double d = pila.pop();
double c = pila.pop();
double b = pila.pop();
double a = pila.pop();
pila.push(c);
pila.push(d);
pila.push(e);
pila.push(f);
pila.push(a);
digitNUM = b;
isResult = true;
isBlocked = false;
outputSTR = String.valueOf(digitNUM);
}
else { outputSTR = "2ROT: six numbers required."; }
}
// 2NIP (dd1 dd2 -- dd2)
// 2NIP (a b c d -- c d)
void fn2NIP() // elimina la seconda coppia di valori
{
if (pila.stackSize() > 2)
{
double d = digitNUM;
double c = pila.pop();
double b = pila.pop();
double a = pila.pop();
pila.push(c);
digitNUM = d;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "2NIP: four numbers required."; }
}
// 2TUCK (dd1 dd2 -- dd2 dd1 dd2)
// 2TUCK (a b c d -- c d a b c d)
void fn2TUCK() // copy the top value below the second value
{
if (pila.stackSize() > 2)
{
double d = digitNUM;
double c = pila.pop();
double b = pila.pop();
double a = pila.pop();
pila.push(c);
pila.push(d);
pila.push(a);
pila.push(b);
pila.push(c);
digitNUM = d;
isResult = true;
isBlocked = false;
outputSTR = String.valueOf(digitNUM);
}
else { outputSTR = "2TUCK: four numbers required."; }
}
// Duplicate Stack
void fnTWINS()
{
lastX = digitNUM;
// copy (pop) stack values to array
int items = pila.stackSize() + 1;
double[] lista = new double[items];
lista[0] = digitNUM;
for (int i=1; i<items; i++)
{
lista[i] = pila.pop();
}
// write (push) array values to stack (two times)
for(int i=items-1; i > -1; i--)
{
pila.push(lista[i]);
}
for(int i=items-1; i>0; i--)
{
pila.push(lista[i]);
}
digitNUM = lista[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
// Copy Stack
void fnCOPYS()
{
int items = pila.stackSize() + 1;
clipboardItems = items;
// copy (pop) stack values to array
clipboard[0] = digitNUM;
for (int i=1; i<clipboardItems; i++)
{
clipboard[i] = pila.pop();
}
// write (push) array values to stack
for(int i=items-1; i > 0; i--)
{
pila.push(clipboard[i]);
}
isResult = true;
isBlocked = false;
}
// Paste Stack
// Paste function erase all previous values on stack
void fnPASTESold()
{
lastX = digitNUM;
pila.clearStack();
for (int i=clipboardItems-1; i>0; i--)
{
pila.push(clipboard[i]);
}
digitNUM = clipboard[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
void fnPASTES()
{
lastX = digitNUM;
//pila.clearStack();
if (!isBlocked && isResult) { pila.push(digitNUM); }
//pila.push(digitNUM);
for (int i=clipboardItems-1; i >= 0; i--)
{
pila.push(clipboard[i]);
}
digitNUM = pila.pop();
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
// Rotate Down stack
void fnRDOWNS()
{
if (pila.stackSize() > 0)
{
lastX = digitNUM;
// copy stack values to array
int items = pila.stackSize() + 1;
double[] lista = new double[items];
lista[0] = digitNUM;
for (int i=1; i<items; i++)
{
lista[i] = pila.pop();
}
// Left Rotate array
double first = lista[lista.length-1];
for (int i = lista.length-1; i > 0; i--)
{ lista[i] = lista[i-1]; }
lista[0] = first;
// Push array on stack
for(int i=items-1; i>0; i--)
{
pila.push(lista[i]);
}
digitNUM = lista[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "R DOWN: at least two numbers required."; }
}
// Rotate Up stack
void fnRUPS()
{
if (pila.stackSize() > 0)
{
lastX = digitNUM;
// copy stack values to array
int items = pila.stackSize() + 1;
double[] lista = new double[items];
lista[0] = digitNUM;
for (int i=1; i<items; i++)
{
lista[i] = pila.pop();
}
// Right Rotate array
double last = lista[0];
for (int i = 0; i < lista.length-1; i++)
{ lista[i] = lista[i+1]; }
lista[lista.length-1] = last;
// Push array on stack
for(int i=items-1; i>0; i--)
{
pila.push(lista[i]);
}
digitNUM = lista[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "R UP: at least two numbers required."; }
}
// Rotate Up the stack by N items
void fnROLLS()
{
int idx = (int) digitNUM;
if ((pila.stackSize() > 0) && (idx != 0))
{
lastX = digitNUM;
// copy stack values to array
int items = pila.stackSize();
double[] lista = new double[items];
for (int i=0; i<items; i++)
{
lista[i] = pila.pop();
}
if (idx > 0) // Right Rotate array N times
{
for (int n = 0; n < idx; n++)
{
double last = lista[0];
for (int i = 0; i < lista.length-1; i++)
{ lista[i] = lista[i+1]; }
lista[lista.length-1] = last;
}
}
else // Left Rotate array N times
{
idx = Math.abs(idx);
for (int n = 0; n < idx; n++)
{
double first = lista[lista.length-1];
for (int i = lista.length-1; i > 0; i--)
{ lista[i] = lista[i-1]; }
lista[0] = first;
}
}
// Push array on stack
for(int i=items-1; i>0; i--)
{
pila.push(lista[i]);
}
digitNUM = lista[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else { outputSTR = "ROLL: at least two numbers required."; }
}
// Pick the N-th items from stack and copy it on top
// The number N is taken from top of stack
// Index of stack start by one (The number N has index 0)
void fnPICKS()
{
int idx = (int) digitNUM;
if (pila.stackSize() >= idx && idx > 0)
{
lastX = digitNUM;
double pickValue = 0.0;
// copy stack values to array
int items = pila.stackSize();
double[] lista = new double[items];
for (int i=0; i<items; i++)
{
lista[i] = pila.pop();
if (i == idx-1) { pickValue = lista[i]; }
}
// push array on stack
for(int i=items-1; i>=0; i--)
{
pila.push(lista[i]);
}
digitNUM = pickValue;
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
else
{
//outputSTR = "PICK: positive number required [1..Items-on-stack].";
fnDROP();
}
}
// Invert the stack order value
void fnFLIPS()
{
if (pila.stackSize() > 0)
{
lastX = digitNUM;
// copy stack values to array
int items = pila.stackSize()+1;
double[] lista = new double[items];
lista[0] = digitNUM;
for (int i=1; i<items; i++)
{
lista[i] = pila.pop();
}
// Push array on stack
for(int i=0; i<items; i++)
{
pila.push(lista[i]);
}
digitNUM = pila.pop();
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
//------------------------------------------------------------
// Memory function
void fnSTO1() { mem[0] = digitNUM; outputSTR = String.valueOf(digitNUM); isResult = true; isBlocked = false; }
void fnSTO2() { mem[1] = digitNUM; outputSTR = String.valueOf(digitNUM); isResult = true; isBlocked = false; }
void fnSTO3() { mem[2] = digitNUM; outputSTR = String.valueOf(digitNUM); isResult = true; isBlocked = false; }
void fnSTO4() { mem[3] = digitNUM; outputSTR = String.valueOf(digitNUM); isResult = true; isBlocked = false; }
void fnSTO5() { mem[4] = digitNUM; outputSTR = String.valueOf(digitNUM); isResult = true; isBlocked = false; }
void fnRCL1()
{
if (mem[0] != null)
{
lastX = digitNUM;
if (!isBlocked && isResult) { pila.push(digitNUM); }
//pila.push(digitNUM);
digitNUM = mem[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
void fnRCL2()
{
if (mem[1] != null)
{
lastX = digitNUM;
if (!isBlocked && isResult) { pila.push(digitNUM); }
//pila.push(digitNUM);
digitNUM = mem[1];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
void fnRCL3()
{
if (mem[2] != null)
{
lastX = digitNUM;
if (!isBlocked && isResult) { pila.push(digitNUM); }
//pila.push(digitNUM);
digitNUM = mem[2];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
void fnRCL4()
{
if (mem[3] != null)
{
lastX = digitNUM;
if (!isBlocked && isResult) { pila.push(digitNUM); }
//pila.push(digitNUM);
digitNUM = mem[3];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
void fnRCL5()
{
if (mem[4] != null)
{
lastX = digitNUM;
if (!isBlocked && isResult) { pila.push(digitNUM); }
//pila.push(digitNUM);
digitNUM = mem[4];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
// write mem1..mem5 on stack
void fnRCLALL()
{
int valid = 0;
double[] v = new double[Nmem];
for(int i=0; i<Nmem; i++)
{
if (mem[i] != null)
{
v[i] = mem[i];
valid++;
}
}
if (valid > 0)
{
lastX = digitNUM;
if (!isBlocked && isResult) { pila.push(digitNUM); }
for (int i=valid-1; i>=1 ; i--)
{
pila.push(v[i]);
}
digitNUM = v[0];
outputSTR = String.valueOf(digitNUM);
isResult = true;
isBlocked = false;
}
}
// write stack data on mem1..mem5
void fnSTOALL()
{
int n = pila.stackSize();
if (n > Nmem-1) { n = Nmem-1; }
mem[0] = digitNUM;
for(int i = 0; i<n; i++)
{
mem[i+1] = pila.getItem(i);
}
isResult = true;
isBlocked = false;
}
void fnPRINTMEM()
{
println();
for(int i=0; i<Nmem; i++)
{
print(mem[i]+",");
}
println();
}