-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreoleForth.js
1436 lines (1276 loc) · 55.3 KB
/
CreoleForth.js
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Creole Forth for JavaScript
// Version 0.01
// Copyright 2018 Joseph M. O'Connor
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// From Crockford's JavaScript: The Good Parts
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
this.prototype.toString = name;
return this;
}
};
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
String.method('trim', function () {
return this.replace(/^\s+|\s$/g, '');
});
// Start of Creole Forth-specific code
var BasicForthConstants = function () {
"use strict";
if (!(this instanceof BasicForthConstants)) {
throw new Error("BasicForthConstants needs to be called with the new keyword");
}
this.SmudgeFlag = "SMUDGED";
this.ImmediateVocab = "IMMEDIATE";
this.PrefilterVocab = "PREFILTER";
this.PostfilterVocab = "POSTFILTER";
this.ExecZeroAction = "EXEC0";
this.CompLitAction = "COMPLIT";
};
var GlobalSimpleProps = function (cfb) {
"use strict";
if (!(this instanceof GlobalSimpleProps)) {
throw new Error("GlobalSimpleProps needs to be called with the new keyword");
}
this.CreoleForthBundle = cfb;
this.DataStack = [];
this.ReturnStack = [];
this.VocabStack = [];
this.PrefilterStack = [];
this.PostfilterStack = [];
this.PADarea = [];
this.ParsedInput = [];
this.loopLabels = ["I", "J", "K"];
this.LoopLabelPtr = 0;
this.LoopCurrIndexes = [0, 0, 0];
this.OuterPtr = 0;
this.InnerPtr = 0;
this.ParamFieldPtr = 0;
this.InputArea = "";
this.OutputArea = "";
this.CurrentVocab = "";
this.HelpCommentField = "";
this.SoundField = "";
this.CompiledList = [];
this.BFC = new BasicForthConstants();
this.MinArgsSwitch = true; // When true MinArgs is checked
this.pause = false;
this.onContinue = null
};
GlobalSimpleProps.method("cleanFields", function () {
this.DataStack = [];
this.ReturnStack = [];
this.PADarea = [];
this.ParsedInput = [];
this.LoopLabelPtr = 0;
this.LoopCurrIndexes = [0, 0, 0];
this.OuterPtr = 0;
this.InnerPtr = 0;
this.ParamFieldPtr = 0;
this.InputArea = "";
this.OutputArea = "";
this.HelpCommentField = "";
this.SoundField = "";
this.CompiledList = [];
this.pause = false;
});
GlobalSimpleProps.method('hasMinArgs', function (stack, minSize) {
var i;
if (this.MinArgsSwitch === false) {
return;
}
for (i = stack.length - 1; i >= 0; i--) {
if (stack[i] == "" || stack.length < minSize) {
alert("Error: Stack underflow");
this.cleanFields();
return false;
}
}
return true;
});
// These objects are to be stored on the return stack
var ReturnLoc = function (dictAddr, pfAddr) {
"use strict";
if (!(this instanceof ReturnLoc)) {
throw new Error("ReturnLoc needs to be called with the new keyword");
}
this.DictAddr = dictAddr;
this.ParamFieldAddr = pfAddr;
};
var LoopInfo = function(label, index, limit) {
"use strict";
if (!(this instanceof LoopInfo)) {
throw new Error("LoopInfo needs to be called with the new keyword");
}
this.Label = label;
this.Index = index;
this.Limit = limit;
}
// Colon definitions are built in the PAD area - each new entry is a triplet consisting of the
// word's fully qualified name, its dictionary address, and associated compilation action.
var CompileInfo = function (fqName, address, compileAction) {
"use strict";
if (!(this instanceof CompileInfo)) {
throw new Error("CompileInfo needs to be called with the new keyword");
}
this.FQName = fqName;
this.Address = address;
this.CompileAction = compileAction;
};
var CorePrims = function () {
"use strict";
if (!(this instanceof CorePrims)) {
throw new Error("CorePrims needs to be called with the new keyword");
}
this.title = "Core Primitives grouping"; "use strict";
if (!(this instanceof CorePrims)) {
throw new Error("CorePrims needs to be called with the new keyword");
}
this.title = "Core Primitives grouping";
};
CorePrims.method("doNOP", function (gsp) {
// Does exactly nothing
});
CorePrims.method("doPlus", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
var sum = Number(val1) + Number(val2);
gsp.DataStack.push(sum);
});
CorePrims.method("doMinus", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
var difference = Number(val1) - Number(val2);
gsp.DataStack.push(difference);
});
CorePrims.method("doMultiply", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
var product = Number(val2) * Number(val1);
gsp.DataStack.push(product);
});
CorePrims.method("doDivide", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
var quotient = Number(val1) / Number(val2);
gsp.DataStack.push(quotient);
});
CorePrims.method("doMod", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
var remainder = Number(val1) % Number(val2);
gsp.DataStack.push(remainder);
});
CorePrims.method("doDup", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var val = gsp.DataStack.pop();
gsp.DataStack.push(val);
gsp.DataStack.push(val);
});
CorePrims.method("doSwap", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
gsp.DataStack.push(val2);
gsp.DataStack.push(val1);
});
// ( n1 n2 n3 -- n2 n3 n1 ) Rotates the third value to the top of the stack
CorePrims.method("doRot", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 3) === false) {
return;
}
var val3 = gsp.DataStack.pop();
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
gsp.DataStack.push(val2);
gsp.DataStack.push(val3);
gsp.DataStack.push(val1);
});
// ( n1 n2 n3 -- n3 n1 n2 ) Rotates top value of the stack to the third position
CorePrims.method("doMinusRot", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 3) === false) {
return;
}
var val3 = gsp.DataStack.pop();
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
gsp.DataStack.push(val3);
gsp.DataStack.push(val1);
gsp.DataStack.push(val2);
});
CorePrims.method("doNip", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val1 = gsp.DataStack.pop();
gsp.DataStack.pop();
gsp.DataStack.push(val1);
});
CorePrims.method("doTuck", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
gsp.DataStack.push(val2);
gsp.DataStack.push(val1);
gsp.DataStack.push(val2);
});
CorePrims.method("doOver", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var val2 = gsp.DataStack.pop();
var val1 = gsp.DataStack.pop();
gsp.DataStack.push(val1);
gsp.DataStack.push(val2);
gsp.DataStack.push(val1);
});
CorePrims.method("doDrop", function (gsp) {
var beforeArgCount = gsp.DataStack.length;
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
gsp.DataStack.pop();
var afterArgCount = gsp.DataStack.length;
if (beforeArgCount === afterArgCount) {
alert("Error: stack underflow");
gsp.cleanFields();
}
});
CorePrims.method("doDepth", function (gsp) {
gsp.DataStack.push(gsp.DataStack.length);
});
CorePrims.method("doHello", function (gsp) {
alert("Hello World");
});
CorePrims.method("doTulip", function (gsp) {
alert("Tulip");
});
CorePrims.method("doMsgBox", function (gsp) {
var msg = gsp.DataStack.pop();
alert(msg);
});
CorePrims.method("doEval", function (gsp) {
var jsCode = gsp.DataStack.pop();
if (jsCode.search(/^alert/) === -1) {
alert("Sorry, only alerts allowed");
}
else {
eval(jsCode);
}
});
CorePrims.method("doVList", function (gsp) {
var definitionTable = [];
var i;
var cw;
var dtString;
definitionTable[0] = gsp.CreoleForthBundle.row + 1 + " definitions<br><table>";
definitionTable.push("<th>Index</th><th>Name</th><th>Vocabulary</th><th>Code Field</th><th>Param Field</th><th>Help Field</th>");
for (i = 0; i <= gsp.CreoleForthBundle.row; i++) {
cw = gsp.CreoleForthBundle.Address[i];
if (cw != null) {
definitionTable.push("<tr>" +
"<td>" + gsp.CreoleForthBundle.Address[i].IndexField + "</td>" +
"<td>" + gsp.CreoleForthBundle.Address[i].NameField + "</td>" +
"<td>" + gsp.CreoleForthBundle.Address[i].Vocabulary + "</td>" +
"<td>" + gsp.CreoleForthBundle.Address[i].CodeFieldStr + "</td>" +
"<td>" + gsp.CreoleForthBundle.Address[i].ParamField + "</td>" +
"<td>" + gsp.CreoleForthBundle.Address[i].HelpField + "</td>" +
"</tr>");
}
}
definitionTable.push("</table>");
dtString = definitionTable.join("\n");
gsp.HelpCommentField = dtString;
});
CorePrims.method("doToday", function (gsp) {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
var mmddyyyy = [];
var fmtDate;
dd = dd < 10 ? "0" + dd : dd;
mm = mm < 10 ? "0" + mm : mm;
mmddyyyy.push(mm);
mmddyyyy.push(dd);
mmddyyyy.push(yyyy);
fmtDate = mmddyyyy.join("/");
alert(fmtDate);
});
CorePrims.method("doNow", function (gsp) {
gsp.DataStack.push(Date.now());
});
CorePrims.method("doToHoursMinSecs", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var timeDiff0 = Math.abs(gsp.DataStack.pop()) / 1000;
var hours = Math.floor(timeDiff0 / 3600);
var timeDiff1 = timeDiff0 - (hours * 3600);
var minutes = Math.floor(timeDiff1 / 60);
var seconds = timeDiff1 - (minutes * 60);
var hhmmss = [];
var fmtTime;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
hhmmss.push(hours);
hhmmss.push(minutes);
hhmmss.push(seconds);
fmtTime = hhmmss.join(":");
alert(fmtTime);
});
var Interpreter = function () {
"use strict";
if (!(this instanceof Interpreter)) {
throw new Error("Interpreter needs to be called with the new keyword");
}
this.title = "Interpreter grouping";
};
Interpreter.method("doParseInput", function (gsp) {
// splits the input into individual words
var lines = gsp.InputArea.split(/\n/);
var i;
for (i = 0; i < lines.length; i++)
{
lines[i] += " __#EOL#__";
}
var codeLine = lines.join(" ");
gsp.ParsedInput = codeLine.trim().split(/\s+/);
});
Interpreter.method("doInner", function (gsp) {
gsp.CreoleForthBundle.Address[gsp.InnerPtr].CodeField(gsp);
});
Interpreter.method("doColon", function(gsp) {
// gsp.ParamFieldPtr = 0;
var currWord = gsp.CreoleForthBundle.Address[gsp.InnerPtr];
var paramField = currWord.ParamField;
var addrInPF;
var codeField;
var rLoc;
while (gsp.ParamFieldPtr < paramField.length) {
addrInPF = paramField[gsp.ParamFieldPtr];
codeField = gsp.CreoleForthBundle.Address[addrInPF].CodeField;
gsp.ParamFieldPtr += 1;
rLoc = new ReturnLoc(gsp.InnerPtr, gsp.ParamFieldPtr);
gsp.ReturnStack.push(rLoc);
codeField(gsp);
rLoc = gsp.ReturnStack.pop();
gsp.InnerPtr = rLoc.DictAddr;
gsp.ParamFieldPtr = rLoc.ParamFieldAddr;
}
});
Interpreter.method("doOnly", function (gsp) {
gsp.VocabStack = [];
gsp.VocabStack.push("ONLY");
});// Search vocabularies from top to bottom for word. If found, then execute.
Interpreter.method("doForth", function (gsp) {
gsp.VocabStack.push("FORTH");
});
Interpreter.method("doAppSpec", function (gsp) {
gsp.VocabStack.push("APPSPEC");
});
// Search vocabularies from top to bottom for word. If found, then execute.
// iF not, it goes on the stack
Interpreter.method("doOuter", function (gsp) {
var rawWord = "";
var fqWord = "";
var searchVocabPtr;
var isFound = false;
gsp.OuterPtr = 0;
gsp.InnerPtr = 0;
gsp.ParamFieldPtr = 0;
while (gsp.OuterPtr < gsp.ParsedInput.length) {
if (gsp.pause === false) {
rawWord = gsp.ParsedInput[gsp.OuterPtr];
searchVocabPtr = gsp.VocabStack.length - 1;
while (searchVocabPtr >= 0) {
fqWord = rawWord.toUpperCase() + "." + gsp.VocabStack[searchVocabPtr];
if (fqWord in gsp.CreoleForthBundle) {
gsp.InnerPtr = gsp.CreoleForthBundle[fqWord].IndexField;
this.doInner(gsp);
isFound = true;
break;
}
else {
searchVocabPtr -= 1;
}
}
// Have to assign stack parameters this way to account for the way JavaScript initializes arrays
if (isFound === false) {
if (gsp.DataStack.length === 1 && gsp.DataStack[0] == "") {
gsp.DataStack[0] = rawWord;
}
else {
gsp.DataStack.push(rawWord);
}
}
gsp.OuterPtr += 1;
isFound = false;
}
}
});
var Compiler = function () {
"use strict";
if (!(this instanceof Compiler)) {
throw new Error("Compiler needs to be called with the new keyword");
}
this.title = "Compiler grouping";
};
Compiler.method("doComma", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var token = gsp.DataStack.pop();
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
newCreoleWord.ParamField.push(token);
newCreoleWord.ParamFieldStart += 1;
gsp.ParamFieldPtr = newCreoleWord.ParamField.length - 1;
});
// Executes at time zero of colon compilation, when CompileInfo triplets are placed in the PAD area.
// Example : comment handling. The pointer is moved past the comments.
Compiler.method("doSingleLineCmts", function (gsp) {
while (gsp.ParsedInput[gsp.OuterPtr] != "__#EOL#__") {
gsp.OuterPtr += 1;
}
});
Compiler.method("doParenCmts", function (gsp) {
while (gsp.ParsedInput[gsp.OuterPtr].search(/\)/) === -1) {
gsp.OuterPtr += 1;
}
});
Compiler.method("doCompileList", function (gsp) {
var joinedList;
gsp.CompiledList = [];
gsp.OuterPtr += 1;
while (gsp.ParsedInput[gsp.OuterPtr].search(/\}/) === -1) {
gsp.CompiledList.push(gsp.ParsedInput[gsp.OuterPtr]);
gsp.OuterPtr += 1;
}
joinedList = gsp.CompiledList.join(" ");
gsp.DataStack.push(joinedList);
});
// Executes at time one of colon compilation, then the information in the CompileInfo triplets are
// passed to the standar interpreter for execution. Example : immediate words such as IF.
Compiler.method("doExecute", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var address = gsp.DataStack.pop();
gsp.InnerPtr = address;
gsp.CreoleForthBundle.Address[address].CodeField(gsp);
});
Compiler.method("doHere", function (gsp) {
var hereLoc = gsp.CreoleForthBundle.row + 1;
gsp.DataStack.push(hereLoc);
});
Compiler.method("doMyAddress", function (gsp) {
var cw = gsp.CreoleForthBundle.Address[gsp.InnerPtr];
gsp.DataStack.push(cw.IndexField);
});
Compiler.method("doCreate", function (gsp) {
var hereLoc = gsp.CreoleForthBundle.row + 1;
var name = gsp.ParsedInput[gsp.OuterPtr + 1];
var params = [];
var data = [];
var help = "TODO: ";
var cw = new CreoleWord(name, gsp.CreoleForthBundle.Modules.Compiler.doMyAddress, "Compiler.doMyAddress", gsp.CurrentVocab, "COMPINPF", help, hereLoc - 1, hereLoc, hereLoc - 1, hereLoc, params, data);
// The smudge flag avoids accidental recursion. But it's easy enough to get around if you want to.
var fqName = name + "." + gsp.CurrentVocab;
gsp.CreoleForthBundle[fqName] = cw;
gsp.CreoleForthBundle.row += 1;
gsp.CreoleForthBundle.Address[gsp.CreoleForthBundle.row] = gsp.CreoleForthBundle[fqName];
gsp.OuterPtr += 2;
});
Compiler.method("CompileColon", function (gsp) {
var hereLoc = gsp.CreoleForthBundle.row + 1 ;
var name = gsp.ParsedInput[gsp.OuterPtr + 1];
var params = [];
var data = [];
var help = "TODO: ";
var rawWord;
var searchVocabPtr;
var isFound;
var compAction;
var compInfo;
var isSemiPresent = false;
var colonIndex = -1;
var gspComp = new GlobalSimpleProps(gsp.CreoleForthBundle);
var i;
var codeField;
// Elementary syntax check to avoid allowing the page to hang. If A colon isn't followed by a matching semicolon, you get an error message
// and the stacks and input are cleared.
for (i = 0; i < gsp.ParsedInput.length; i++) {
if (gsp.ParsedInput[i] === ":") {
colonIndex = i;
}
if (gsp.ParsedInput[i] === ";" && i > colonIndex) {
isSemiPresent = true;
}
}
if (isSemiPresent === false) {
alert("Error: colon def must have matching semicolon");
gsp.cleanFields();
return;
}
// Compilation is started when the IMMEDIATE vocabulary is pushed onto the vocabulary stack. No need for the usual Forth STATE flag.
gsp.VocabStack.push(gsp.BFC.ImmediateVocab);
var cw = new CreoleWord(name, gsp.CreoleForthBundle.Modules.Interpreter.doColon, "Interpreter.doColon", gsp.CurrentVocab, "COMPINPF", help, hereLoc - 1, hereLoc, hereLoc - 1, hereLoc, params, data);
// The smudge flag avoids accidental recursion. But it's easy enough to get around if you want to.
var fqNameSmudged = name + "." + gsp.CurrentVocab + "." + gsp.BFC.SmudgeFlag;
var fqName = name + "." + gsp.CurrentVocab;
var fqWord;
gsp.CreoleForthBundle[fqNameSmudged] = cw;
gsp.CreoleForthBundle.row += 1;
gsp.CreoleForthBundle.Address[gsp.CreoleForthBundle.row] = gsp.CreoleForthBundle[fqNameSmudged];
gsp.OuterPtr += 2;
// parameter field contents are set up in the PAD area. Each word is looked up one at a time in the dictionary, and its name, address, and compilation action
// are placed in the CompileInfo triplet.
while (gsp.OuterPtr < gsp.ParsedInput.length && gsp.VocabStack[gsp.VocabStack.length - 1] === gsp.BFC.ImmediateVocab && gsp.ParsedInput[gsp.OuterPtr] != ";" ) {
rawWord = gsp.ParsedInput[gsp.OuterPtr];
searchVocabPtr = gsp.VocabStack.length - 1;
isFound = false;
while (searchVocabPtr >= 0) {
fqWord = rawWord.toUpperCase() + "." + gsp.VocabStack[searchVocabPtr];
if (fqWord in gsp.CreoleForthBundle) {
compAction = gsp.CreoleForthBundle[fqWord].CompileActionField;
if (compAction != gsp.BFC.ExecZeroAction) {
compInfo = new CompileInfo(fqWord, gsp.CreoleForthBundle[fqWord].IndexField, compAction);
gsp.PADarea.push(compInfo);
}
else {
// This is stuff where the outer ptr is manipulated such as comments
codeField = gsp.CreoleForthBundle[fqWord].CodeField;
codeField(gsp);
}
isFound = true;
break;
}
else {
searchVocabPtr -= 1;
}
}
// if no dictionary entry is found, it's tagged as a literal.
if (isFound === false) {
compInfo = new CompileInfo(rawWord, rawWord, gsp.BFC.CompLitAction);
gsp.PADarea.push(compInfo);
}
gsp.OuterPtr += 1;
}
// 1. Builds the definition in the parameter field from the PAD area. Very simple; the address of each word appears before its associated
// compilation action. Most of the time, it will be COMPINPF, which will simply compile the word into the parameter field (it's actually
// , (comma) with a different name for readability purposes).
// Compiling words such as CompileIf will execute since that's the compilation action they're tagged with.
// 2. Attaches it to the smudged definition
// 3. "Unsmudges" the new definition by copying it to its proper fully-qualified property and place in the rows array.
// 4. Deletes the smudged definition.
// 5. Pops the IMMEDIATE vocabulary off the vocabulary stack and halts compilation.
i = 0;
gspComp.VocabStack = gsp.VocabStack;
// Putting the args and compilation actions together and executing then executing them seems to cause a problem with compiling words.
// Getting around this by putting one arg on the stack, one in the input area, then executing.
while (i < gsp.PADarea.length)
{
compInfo = gsp.PADarea[i];
gspComp.DataStack.push(compInfo.Address);
gspComp.InputArea = compInfo.CompileAction;
gspComp.CreoleForthBundle.Modules.Interpreter.doParseInput(gspComp);
gspComp.CreoleForthBundle.Modules.Interpreter.doOuter(gspComp);
i += 1;
}
gspComp.InputArea = ";";
gspComp.CreoleForthBundle.Modules.Interpreter.doParseInput(gspComp);
gspComp.CreoleForthBundle.Modules.Interpreter.doOuter(gspComp);
cw = gspComp.CreoleForthBundle.Address[gspComp.CreoleForthBundle.row];
gsp.CreoleForthBundle[fqName] = cw;
delete gsp.CreoleForthBundle[fqNameSmudged];
gsp.VocabStack.pop();
gsp.PADarea = [];
});
Compiler.method("doSemi", function (gsp) {
gsp.PADarea = [];
// alert("Compilation is complete");
console.log("Compilation is complete");
});
// Compiling wordsCreoleWord = gsp.CreoleForthBundle.Address[row]; have two separate actions -
// a compile-time and a run-time action.
Compiler.method("CompileIf", function (gsp) {
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var zeroBranchAddr = gsp.CreoleForthBundle["0BRANCH.IMMEDIATE"].IndexField;
newCreoleWord.ParamField.push(zeroBranchAddr);
newCreoleWord.ParamField.push(-1);
gsp.ParamFieldPtr = newCreoleWord.ParamField.length - 1;
gsp.DataStack.push(gsp.ParamFieldPtr);
});
Compiler.method("CompileElse", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var jumpAddr = gsp.CreoleForthBundle["JUMP.IMMEDIATE"].IndexField;
var elseAddr = gsp.CreoleForthBundle["doElse.IMMEDIATE"].IndexField;
var jumpAddrPFLoc;
var zeroBrAddrPFLoc;
newCreoleWord.ParamField.push(jumpAddr);
newCreoleWord.ParamField.push(-1);
jumpAddrPFLoc = newCreoleWord.ParamField.length - 1;
newCreoleWord.ParamField.push(elseAddr);
zeroBrAddrPFLoc = gsp.DataStack.pop();
newCreoleWord.ParamField[zeroBrAddrPFLoc] = newCreoleWord.ParamField.length - 1;
gsp.DataStack.push(jumpAddrPFLoc);
gsp.ParamFieldPtr = newCreoleWord.ParamField.length - 1;
});
Compiler.method("CompileThen", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var branchPFLoc = gsp.DataStack.pop();
var thenAddr = gsp.CreoleForthBundle["doThen.IMMEDIATE"].IndexField;
newCreoleWord.ParamField.push(thenAddr);
newCreoleWord.ParamField[branchPFLoc] = newCreoleWord.ParamField.length - 1;
});
Compiler.method("do0Branch", function (gsp) {
var currWord = gsp.CreoleForthBundle.Address[gsp.InnerPtr];
var paramField = currWord.ParamField;
var rLoc = gsp.ReturnStack.pop();
var jumpAddr = paramField[rLoc.ParamFieldAddr];
var branchFlag = gsp.DataStack.pop();
if (branchFlag == 0) {
gsp.ParamFieldPtr = jumpAddr;
}
else {
gsp.ParamFieldPtr += 1;
}
rLoc.ParamFieldAddr = gsp.ParamFieldPtr;
gsp.ReturnStack.push(rLoc);
});
Compiler.method("CompileBegin", function (gsp) {
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var beginAddr = gsp.CreoleForthBundle["doBegin.IMMEDIATE"].IndexField;
var beginLoc;
newCreoleWord.ParamField.push(beginAddr);
beginLoc = newCreoleWord.ParamField.length - 1;
gsp.DataStack.push(beginLoc);
});
Compiler.method("CompileUntil", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var beginLoc = gsp.DataStack.pop();
var zeroBranchAddr = gsp.CreoleForthBundle["0BRANCH.IMMEDIATE"].IndexField;
newCreoleWord.ParamField.push(zeroBranchAddr);
newCreoleWord.ParamField.push(beginLoc);
});
Compiler.method("CompileDo", function (gsp) {
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var doStartDoAddr = gsp.CreoleForthBundle["doStartDo.IMMEDIATE"].IndexField;
var doAddr = gsp.CreoleForthBundle["doDo.IMMEDIATE"].IndexField;
var doLoc;
newCreoleWord.ParamField.push(doStartDoAddr);
newCreoleWord.ParamField.push(doAddr);
doLoc = newCreoleWord.ParamField.length - 1;
gsp.DataStack.push(doLoc);
});
Compiler.method("CompileLoop", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var loopAddr = gsp.CreoleForthBundle["doLoop.IMMEDIATE"].IndexField;
var doLoc = gsp.DataStack.pop();
newCreoleWord.ParamField.push(loopAddr);
newCreoleWord.ParamField.push(doLoc);
});
Compiler.method("CompilePlusLoop", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var loopAddr = gsp.CreoleForthBundle["doPlusLoop.IMMEDIATE"].IndexField;
var doLoc = gsp.DataStack.pop();
newCreoleWord.ParamField.push(loopAddr);
newCreoleWord.ParamField.push(doLoc);
});
Compiler.method("doStartDo", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 2) === false) {
return;
}
var rLoc = gsp.ReturnStack.pop();
var startIndex = gsp.DataStack.pop();
var loopEnd = gsp.DataStack.pop();
var li = new LoopInfo(gsp.loopLabels[gsp.LoopLabelPtr], startIndex, loopEnd);
gsp.LoopCurrIndexes = [0, 0, 0];
gsp.LoopLabelPtr += 1;
gsp.ReturnStack.push(li);
gsp.ReturnStack.push(rLoc);
});
Compiler.method("doPlusLoop", function (gsp) {
// gsp.MinArgsSwitch = false;
var incVal = gsp.DataStack.pop();
var currWord = gsp.CreoleForthBundle.Address[gsp.InnerPtr];
var paramField = currWord.ParamField;
var rLoc = gsp.ReturnStack.pop();
var li = gsp.ReturnStack.pop();
var jumpAddr = paramField[rLoc.ParamFieldAddr];
var loopLimit = li.Limit;
var loopLabel = li.Label;
var currIndex = li.Index;
if (incVal < 0) {
loopLimit += incVal;
}
else {
loopLimit -= incVal;
}
if ( ( (Number(incVal) > 0) && (Number(currIndex) >= Number(loopLimit)) ) ||
( (Number(incVal) < 0) && (Number(currIndex) <= Number(loopLimit)) )
) {
gsp.ParamFieldPtr += 1;
rLoc.ParamFieldAddr = gsp.ParamFieldPtr;
gsp.LoopLabelPtr -= 1;
}
else {
gsp.ParamFieldPtr = jumpAddr;
currIndex = Number(currIndex) + incVal;
li.Index = currIndex;
rLoc.ParamFieldAddr = gsp.ParamFieldPtr;
gsp.ReturnStack.push(li);
}
switch (loopLabel) {
case "I" : gsp.LoopCurrIndexes[0] = currIndex;
break;
case "J" : gsp.LoopCurrIndexes[1] = currIndex;
break;
case "K" : gsp.LoopCurrIndexes[2] = currIndex;
break;
}
gsp.ReturnStack.push(rLoc);
// gsp.MinArgsSwitch = true;
});
Compiler.method("doArgsCheckOff", function (gsp) {
gsp.MinArgsSwitch = false;
});
Compiler.method("doArgsCheckOn", function (gsp) {
gsp.MinArgsSwitch = true;
});
Compiler.method("doLoop", function (gsp) {
gsp.DataStack.push(1);
var codeField = gsp.CreoleForthBundle["doPlusLoop.IMMEDIATE"].CodeField;
codeField(gsp);
});
Compiler.method("doIndexI", function (gsp) {
gsp.DataStack.push(gsp.LoopCurrIndexes[0]);
});
Compiler.method("doIndexJ", function (gsp) {
gsp.DataStack.push(gsp.LoopCurrIndexes[1]);
});
Compiler.method("doIndexK", function (gsp) {
gsp.DataStack.push(gsp.LoopCurrIndexes[2]);
});
Compiler.method("doDoes", function (gsp) {
var currWord = gsp.CreoleForthBundle.Address[gsp.InnerPtr];
var codeFieldStr = currWord.CodeFieldStr;
var execToken;
console.log("Code field is " + currWord.CodeFieldStr);
// DOES> has to react differently depending on whether it's inside a colon
// definition or not
if (codeFieldStr === "doDoes") {
execToken = currWord.IndexField;
console.log("Direct execution of doDoes")
gsp.ParamFieldPtr = currWord.ParamFieldStart;
gsp.DataStack.push(execToken);
gsp.CreoleForthBundle.Modules.Interpreter.doColon(gsp);
}
else {
execToken = currWord.ParamField[gsp.ParamFieldPtr - 1];
console.log(gsp.ParamFieldPtr);
console.log("Execution token is " + execToken);
gsp.DataStack.push(execToken);
gsp.CreoleForthBundle.Modules.Compiler.doExecute(gsp);
}
});
// 1. Copy the code beyond DOES> into the defining word to the new definition
// 2. Advance the parameter field pointer past the runtime code that was copied
// so it won't be executed.
// Example: : CONSTANT CREATE , DOES> @ ;
// 3 CONSTANT THREE
Compiler.method("CompileDoes", function (gsp) {
var rLoc = gsp.ReturnStack.pop();
var parentRow = rLoc.DictAddr;
var newRow = gsp.CreoleForthBundle.row;
var parentCreoleWord = gsp.CreoleForthBundle.Address[parentRow];
var childCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var fqNameField = childCreoleWord.fqNameField;
var doesAddr = gsp.CreoleForthBundle["DOES>.FORTH"].IndexField;
var i = 0;
var startCopyPoint;
childCreoleWord.CodeField = gsp.CreoleForthBundle.Modules.Compiler.doDoes;
childCreoleWord.CodeFieldStr = "doDoes";
// Find the location of the does address in the parent definition
while (i < parentCreoleWord.ParamField.length) {
if (parentCreoleWord.ParamField[i] == doesAddr) {
startCopyPoint = i + 1;
break;
}
else {
i += 1;
}
}
// Need the definition's address so doDoes can get it easily either when it's being
// called from the interpreter for from within a compiled definition
childCreoleWord.ParamField.push(newRow);
childCreoleWord.ParamFieldStart = childCreoleWord.ParamField.length;
i = 0;
while (startCopyPoint < parentCreoleWord.ParamField.length) {
childCreoleWord.ParamField.push(parentCreoleWord.ParamField[startCopyPoint]);
startCopyPoint += 1
i += 1;
}
rLoc.ParamFieldAddr += i;
gsp.ReturnStack.push(rLoc);
gsp.CreoleForthBundle.Address[newRow] = childCreoleWord;
gsp.CreoleForthBundle[fqNameField] = childCreoleWord;
});
Compiler.method("doJump", function (gsp) {
var currWord = gsp.CreoleForthBundle.Address[gsp.InnerPtr];
var paramField = currWord.ParamField;
var jumpAddr = paramField[gsp.ParamFieldPtr + 1];
var rLoc = gsp.ReturnStack.pop();
gsp.ParamFieldPtr = jumpAddr;
rLoc.ParamFieldAddr = gsp.ParamFieldPtr;
gsp.ReturnStack.push(rLoc);
});
Compiler.method("CompileLiteral", function (gsp) {
if (gsp.hasMinArgs(gsp.DataStack, 1) === false) {
return;
}
var newRow = gsp.CreoleForthBundle.row;
var newCreoleWord = gsp.CreoleForthBundle.Address[newRow];
var doLitAddr = gsp.CreoleForthBundle["doLiteral.IMMEDIATE"].IndexField;
var litVal = gsp.DataStack.pop();
newCreoleWord.ParamField.push(doLitAddr);
newCreoleWord.ParamField.push(litVal);