-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlmc.js
1280 lines (1253 loc) · 54.2 KB
/
lmc.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
var count = 0;
class LMC {
/**
* Returns the given value as an integer, modulo some limit value
*
* @param {number} val - the value to convert to integer & modulo
* @param {number} [end=1000] - the limit for the modulo operation
* @return {number} the value as unsigned integer, modulo end
*
* @example
*
* LMC.intmod(-13.3, 20) === 6
*/
static intmod(val, end=1000) {
return (Math.floor(val) % end + end) % end || 0;
}
/**
* Callback which the LMC will call when it needs the next value from the inbox.
*
* @callback inboxCallback
* @return {number|undefined} - The next available input from the input source.
* should be an integer in the range 0 to 999. Or, when no immediate input
* is available, undefined.
*/
/**
* Callback which the LMC will call when it wants to output a value to the outbox.
*
* @callback outboxCallback
* @param {number} - The value to be output. Will be in the range 0 to 999.
*
*/
/** Class for a Little Man Computer which can assemble and execute code
*
* @class
* @param {inboxCallback} inbox
* @param {outboxCallback} outbox
* @param {Object} [options] - set of options.
* @param {boolean} [options.setFlagOnOverflow=true] - Determines whether an ADD
* that leads to overflow will set the negative flag.
* @param {boolean} [options.zeroNeedsClearedFlag=true] - Determines whether BRZ is
* ignored when the negative flag is set.
* @param {boolean} [options.stopWhenUndefined=true] - Determines whether execution
* stops when the accumulator does not have a reliable value and its value is
* needed (for SUB, ADD, BRZ, STA, OUT or OTC)
* @param {boolean} [options.forbidProgramCounterOverflow=true] - Determines whether
* execution stops when program counter reaches 100. If not, it will continue
* at mailbox 0
* @return {LMC} - An instance of Little Man Computer. See member documentation.
*
* @example
*
* let lmc = new LMC({stopWhenUndefined:false})
*/
constructor(inbox, outbox, options={}) {
this.options = {
setFlagOnOverflow: true,
zeroNeedsClearedFlag: true,
stopWhenUndefined: true,
forbidProgramCounterOverflow: true,
strictOpcode: true,
...options
};
this._inbox = inbox;
this._outbox = outbox;
this._flag = false;
this._accumulator = 0;
this._programCounter = 0;
this.mailboxes = new Proxy([], {
set: (mailbox, address, objectValue) => {
if (!isNaN(address)) {
address = LMC.intmod(address, 100);
if (address > mailbox.length) throw "creating mailbox leaving gap: " + address;
}
return Reflect.set(mailbox, address, objectValue);
},
get: (mailbox, address) => {
if (!isNaN(address)) {
address = LMC.intmod(address, 100);
if (address >= mailbox.length) throw "accessing unexisting mailbox " + address;
}
return mailbox[address];
}
});
// Additional properties to aid disassembly
this.labelLength = 0;
this.err = null;
// Additional properties
this.isAccUndefined = true;
}
ensureMailbox(address) {
while (address >= this.mailboxes.length) this.createMailbox();
return this.mailboxes[address];
}
createMailbox(lineNo, label="") {
if (arguments.length === 0) {
lineNo = this.tokens.length;
this.tokens.push({
text: " ".repeat(this.labelLength+1) + "DAT",
address: this.mailboxes.length,
mnemonic: { offset: this.labelLength+1, text: "DAT" }
});
}
let tokens = this.tokens[lineNo];
let useArgument = tokens.argument ? tokens.argument.text : "";
let useMnemonic = tokens.mnemonic ? tokens.mnemonic.text : "";
let {value, isCode, isReference} = this.assemble(useMnemonic, useArgument);
// Create a template with placeholders for mnemonic and argument (as they might change when the program runs)
let lineTemplate = [tokens.text, "", ""];
if (useArgument) {
lineTemplate[2] = tokens.text.slice(tokens.argument.offset + useArgument.length);
lineTemplate[0] = tokens.text.slice(0, tokens.argument.offset);
}
if (useMnemonic) {
lineTemplate[1+!useArgument] = lineTemplate[0].slice(tokens.mnemonic.offset + useMnemonic.length);
lineTemplate[0] = lineTemplate[0].slice(0, tokens.mnemonic.offset);
if (useArgument && !lineTemplate[1]) lineTemplate[1] = " ";
}
if (isNaN(value)) throw "NaN";
let argumentNumber = !useArgument ? undefined : isNaN(useArgument) ? this.symbolTable[useArgument.toUpperCase()] : +useArgument;
let that = this; // for use in the setter below
let mailbox = {
...this.disassemble(value, isCode, isReference), // advised code
label,
isCode,
isReference,
lineNo,
_value: value,
useMnemonic,
useArgument,
argumentOffset: (lineTemplate[0] + useMnemonic + lineTemplate[1]).length,
lineTemplate,
line: tokens.text,
get value() {
return this._value;
},
set value(newValue) {
if (isNaN(newValue)) throw "NaN";
this._value = LMC.intmod(newValue);
Object.assign(this, that.disassemble(this._value, this.isCode, this.isReference));
this.useMnemonic = opcode === this.opcode
? useMnemonic // restore variant that was used
: this.mnemonic; // preferred mnemonic (3 letter, capitals)
this.useArgument = argumentNumber === this.argument
? useArgument
: this.argumentLabel;
if (this.useMnemonic && this.useArgument && !lineTemplate[1]) lineTemplate[1] = " ";
this.argumentOffset = (lineTemplate[0] + this.useMnemonic + lineTemplate[1]).length;
this.line = lineTemplate[0] + this.useMnemonic
+ lineTemplate[1] + this.useArgument
+ lineTemplate[2];
},
executable() {
if (!this.isCode) {
this.isCode = true;
this.value = this._value;
}
return this;
}
};
let {opcode} = mailbox; // save for later comparison
this.mailboxes.push(mailbox);
}
assemble(mnemonic, argument) {
mnemonic = mnemonic.toUpperCase();
let value = (LMC.mnemonics[mnemonic || "DAT"].opcode || 0)
+ (isNaN(argument) ? this.symbolTable[argument.toUpperCase()] || 0 : +argument);
let syntax = LMC.instructions[value];
if (!syntax) {
syntax = LMC.instructions[value - value % 100];
if (!syntax || syntax.arg === 0) syntax = LMC.mnemonics.DAT;
}
let isCode = mnemonic !== "DAT" && syntax !== LMC.mnemonics.DAT && (syntax.arg || syntax.opcode === value);
let isReference = (mnemonic === "DAT" || !mnemonic) && argument && isNaN(argument); // "DAT label" or "label label"
return {value, isCode, isReference};
}
disassemble(value, isCode=true, isReference=false) {
let syntax = LMC.instructions[value - value % 100];
syntax = isCode && (
LMC.instructions[value] ||
syntax && syntax.arg && syntax
) || LMC.mnemonics.DAT;
let argument, argumentLabel = "";
if (syntax.arg != 0) {
argumentLabel = (argument = value - (syntax.opcode || 0)) + "";
if ((syntax.mnemonic !== "DAT" || isReference) && this.symbolTable[argument] !== undefined) {
argumentLabel = this.symbolTable[argument] + "";
}
}
let isSloppy = isCode && !syntax.arg && this.options.strictOpcode && syntax.opcode !== value;
return {...syntax, argument, argumentLabel, isSloppy};
}
/**
* Resets the program counter without resetting any other registers or mailboxes
*
*/
reset() {
this.programCounter = 0;
this.err = null;
}
/**
* Clears the state and assembles the given program into instruction codes and stores those in the mailboxes.
*
* @param {string} program - the LMC assembly code to be assembled.
* Each line in the string needs to have one of the following formats:
* [label] mnemonic [argument] [comment]
* Or
* [label] [3 digit instruction code] [comment]
* Comments must start with a freely chosen non-alphanumerical delimiter, like /, # or ;
* The call may set an error object (this.error):
* { address: number, msg: string }
*
* @return {boolean} - success
*
* @example
*
* let program = "INP\n OUT\n HLT";
* lmc.load(program);
*
*/
load(program) {
this.program = program;
// Clear
this.mailboxes.length = 0;
this.labelLength = 0;
this.err = null;
let tokens = this.program.match(/^.*/gm).map(text =>
({ text, ...text.match(LMC.regex).groups})
);
// Add offsets to each group
// Mutually exclusive:
this.tokens = tokens = tokens.map(line => {
let offset = 0;
let res = { text: line.text };
for (let groupName of LMC.regex.groupNames) {
let text = line[groupName];
let key = groupName.replace(/\d/, "");
if (text !== undefined && key !== "s") res[key] = { offset, text };
offset += (text || "").length;
}
return res;
});
// Add addresses to lines, and collect label definitions
let address = 0;
this.symbolTable = {};
for (let [lineNo, line] of tokens.entries()) {
let {label, badArgument, bad, mnemonic, argument} = line;
if (bad && !bad.message) bad.message = `Unexpected '${bad.text}'`;
if (badArgument) {
line.bad = badArgument.text ? badArgument : mnemonic;
delete line.badArgument;
line.bad.message = badArgument.text ? `Invalid argument '${badArgument.text}'` : "Missing argument";
}
if (label) {
const labelUpper = label.text.toUpperCase();
// Semantic requirement: no duplicates
if (labelUpper in this.symbolTable) {
line.bad = line.label;
line.bad.message = `'${label.text}' cannot be defined twice`;
}
this.symbolTable[labelUpper] = address;
this.symbolTable[address] = label.text;
this.labelLength = Math.max(this.labelLength, label.text.length);
}
if ((label || mnemonic || argument) && address >= 100) {
tokens[lineNo] = {
bad: { offset: 0, text: line.text, message: "Program is too large to fit in the 100 mailboxes." }
}
}
if (mnemonic || argument) {
line.address = address++;
}
}
// Resolve symbols and fill mailboxes
let label = null;
for (let [lineNo, line] of tokens.entries()) {
let {address, mnemonic, argument, comment} = line;
if (line.label) label = line.label;
if (address === undefined) continue; // no code here...
if (!line.bad && argument && isNaN(argument.text)) { // convert reference label to mailbox number
let refValue = this.symbolTable[argument.text.toUpperCase()];
if (refValue === undefined) {
line.bad = argument;
line.bad.message = "Undefined label '" + argument.text + "'";
}
}
this.createMailbox(lineNo, label ? label.text : "");
label = null;
}
// Initialise calculator & program counter now that assembly is successful.
this._accumulator = 0;
this._flag = false;
this._programCounter = 0;
let lineNo = this.tokens.findIndex(line => line.bad);
if (lineNo > -1) this.err = { lineNo, msg: this.tokens[lineNo].bad.message };
return !this.err;
}
/**
* Runs the currently loaded program synchronously, starting at where the program counter points to.
* Running will stop when an error occurs, or when a HLT instruction is encountered, or
* when input is needed, but is not available.
*/
run() {
while (this.step()) {}
}
/**
* Get whether there is an error or the current instruction has opcode 0 (kinda look-ahead).
*
* @return {boolean} - true when running cannot continue.
*/
isDone() {
return this.err || this.programCounter >= this.mailboxes.length || this.mailboxes[this.programCounter].isSloppy;
}
// Getters and setters for the LMC's registers and mailboxes
get flag() {
return this._flag;
}
set flag(value) {
this._flag = !!value;
}
get accumulator() {
return this._accumulator;
}
set accumulator(value) {
/* Setting the accumulator, with LDA or INP, clears the negative flag */
this.flag = false;
this.isAccUndefined = false;
this._accumulator = LMC.intmod(value);
}
get programCounter() {
return this._programCounter;
}
set programCounter(next) {
this._programCounter = LMC.intmod(next, 100);
this.programCounterOverflowed = this._programCounter !== next;
}
getMailbox(address) {
address = address % 100;
return this.ensureMailbox(address).value;
}
setMailbox(address, value) {
address = address % 100;
this.ensureMailbox(address).value = value;
}
failWhenUndefined() {
if (this.options.stopWhenUndefined && this.isAccUndefined) {
this.error("Accumulator does not have reliable value.");
return true;
}
}
/* Performing a calculation, i.e. with SUB or ADD, never clears the flag.
* Only SUB can set the flag, in case the sum is negative.
* As a consequence there is no dependecy between accumulator value
* and flag: e.g. the accumulator can be zero and the flag set:
* LDA zero; SUB one; ADD one
* Now accumulator is still undefined, but in practice it could be 0
* So then BRP will not branch, but BRZ will.
*/
addValue(delta) {
if (this.failWhenUndefined()) return;
let value = this.accumulator + delta;
// Wikipedia: "Similarly to SUBTRACT, one could set the negative flag on overflow."
if (value < 0 || value > 999 && this.options.setFlagOnOverflow) this.flag = true;
if (value < 0 || value > 999) this.isAccUndefined = true;
// Do not use setter, as otherwise the flag would be cleared
this._accumulator = LMC.intmod(value);
}
// methods for each of the LMC instructions:
0() { // HLT
// Undo the increment of the program counter. This function must return undefined
this.programCounter--;
}
100(instruction) { // ADD
this.addValue(this.getMailbox(instruction));
}
200(instruction) { // SUB
this.addValue(-this.getMailbox(instruction));
}
300(instruction) { // STA
if (this.failWhenUndefined()) return;
this.setMailbox(instruction, this.accumulator);
}
500(instruction) { // LDA
this.accumulator = this.getMailbox(instruction);
}
600(instruction) { // BRA
this.programCounter = instruction % 100;
}
700(instruction) { // BRZ
// Wikipedia: "Whether the negative flag is taken into account is undefined. [...]"
// "Suggested behavior would be to branch if accumulator is zero and negative flag is not set."
if (this.failWhenUndefined()) return;
if (this.accumulator === 0 && !(this.options.zeroNeedsClearedFlag && this.flag)) this[600](instruction);
}
800(instruction) { // BRP
if (!this.flag) this[600](instruction); // BRA
}
901() { // INP
let inputValue = this._inbox();
if (inputValue === undefined) {
this[0]();
} else {
this.accumulator = inputValue;
}
}
902() { // OUT
if (this.failWhenUndefined()) return;
this._outbox(this.accumulator);
}
922() { // OTC
if (this.failWhenUndefined()) return;
this._outbox(String.fromCharCode(this.accumulator));
}
error(msg) {
this[0](); // HLT
this.err = {
lineNo: this.mailboxes[this.programCounter].lineNo,
msg
};
}
/* Performs the current instruction and updates the program counter. When input is needed and there is none, or when
the instruction is HLT or invalid, then the program counter is not altered.
In those cases the function returns false. In all other cases, true.
*/
step() {
// Fetch instruction
let pc = this.programCounter++;
let {value, opcode, isSloppy} = this.mailboxes[pc].executable();
// Check validity of the instruction
if (!this[opcode] || isSloppy) this.error("Invalid instruction " + value);
// Execute the instruction
else this[opcode](value);
if (this.programCounterOverflowed && this.options.forbidProgramCounterOverflow) this.error("Program counter exceeded 99.");
// If neccessary, create the mailbox that the program counter now refers to, and interpret it as code (for rendering)
this.ensureMailbox(this._programCounter).executable();
// Stop run() when program counter did not advance (like with an error, HLT, or INP without available input)
return pc !== this._programCounter;
}
}
[LMC.mnemonics, LMC.instructions] = (instructions => {
return [
Object.fromEntries(instructions.map(o => [o.mnemonic, o])),
Object.fromEntries(instructions.map(o => [o.opcode, o])),
];
})([
{ mnemonic: "DAT", opcode:null, arg: -1}, // Optional argument
{ mnemonic: "COB", opcode: 0, arg: 0 }, // alternative for HLT
{ mnemonic: "HLT", opcode: 0, arg: 0 }, // HALT (or COFFEE BREAK) ignores the argument
{ mnemonic: "ADD", opcode: 100, arg: 1 }, // ADD
{ mnemonic: "SUB", opcode: 200, arg: 1 }, // SUBTRACT
{ mnemonic: "STO", opcode: 300, arg: 1 }, // alternative for STA
{ mnemonic: "STA", opcode: 300, arg: 1 }, // STORE ACCUMULATOR
{ mnemonic: "LDA", opcode: 500, arg: 1 }, // LOAD ACCUMULATOR
{ mnemonic: "BR", opcode: 600, arg: 1 }, // alternative for BRA
{ mnemonic: "BRA", opcode: 600, arg: 1 }, // BRANCH ALWAYS
{ mnemonic: "BRZ", opcode: 700, arg: 1 }, // BRANCH IF ZERO
{ mnemonic: "BRP", opcode: 800, arg: 1 }, // BRANCH IF POSITIVE
{ mnemonic: "IN", opcode: 901, arg: 0 }, // alternative for INP
{ mnemonic: "INP", opcode: 901, arg: 0 }, // INPUT
{ mnemonic: "OUT", opcode: 902, arg: 0 }, // OUTPUT
{ mnemonic: "OTC", opcode: 922, arg: 0 }, // OUTPUT CHAR = non-standard character output
]);
LMC.regex = (() => {
const reReserved = "\\b(?:" + Object.keys(LMC.mnemonics).join("|") + ")\\b";
const reSimpleMnemonic = "\\b(?:" + Object.keys(LMC.mnemonics).filter(m => LMC.mnemonics[m].arg <= 0).join("|") + ")\\b";
const reMnemonic = "\\b(?:" + Object.keys(LMC.mnemonics).filter(m => LMC.mnemonics[m].arg > 0).join("|") + ")\\b";
const reLabel = "\\b(?!" + reReserved + ")(?!\\d)\\w+\\b"; // an identifier which is not a mnemonic
const reInstruction = "\\b\\d{1,3}\\b"; // a word of up to three digits
const reMailbox = "\\b\\d{1,2}\\b"; // a word of up to two digits
const reComment = "[^\\s\\w].*"; // a non-alphanumerical (non-white space) character marks the start of a comment
const regex = RegExp("^(?<s0>\\s*)(?<label>" + reLabel + ")?"
+ "(?<s1>\\s*)(?:"
+ "(?<mnemonic1>DAT)?(?<s2>\\s*)(?:(?<argument1>" + reInstruction + ")|(?<argument2>" + reLabel + "))"
+ "|(?<mnemonic2>" + reSimpleMnemonic + ")"
+ "|(?<mnemonic3>" + reMnemonic + ")(?<s3>\\s*)(?:(?<argument3>" + reMailbox + ")|(?<argument4>" + reLabel + ")|(?<badArgument>\\S*))"
+ ")?"
+ "(?<s4>\\s*)(?:"
+ "(?<comment>" + reComment + ")"
+ "|(?<bad>\\S+).*"
+ ")?", "i");
regex.groupNames = regex.toString().match(/\(\?<[^>]+/g).map(m => m.slice(3));
return regex;
})();
/*
LmcGui
Reads the first text node in the given DOM container element and loads it in a new LMC instance.
This text node is replaced by a widget allowing to run the program step by step.
*/
class LmcGui extends LMC {
static RUNNING = 1
static PAUSED = 2
static EDITING = 3
constructor(container, options={}) {
options = { // extend with option defaults for extended features
haltRequired: false,
onStateChange: () => null,
...options
};
// Initialise the LMC with inbox and outbox functions:
super(() => {
this.inputAnimation.complete();
let s = (this.gui.input.value.match(/\d{1,3}(?!\d)/g) || []).join(" ");
if (!s) {
this.gui.input.value = "";
this.gui.input.placeholder = "Waiting for your input...";
this.gui.input.focus();
return;
}
this.gui.input.value = s;
this.gui.input.removeAttribute("placeholder");
let val = parseInt(s);
// Animate the removal of the input value from the input queue
this.inputAnimation.start(50);
return val;
}, (val) => {
this.gui.output.scrollLeft = 10000;
if (typeof val === "number" && this.gui.output.value) val = " " + val;
this.outputAnimation.start(10);
this.gui.output.value += val;
}, options);
let programNode = container.childNodes[0];
let program = programNode.nodeValue.trim();
// Do not create the GUI when in automatic mode, and there is no program.
if (options.haltRequired && !/\sHLT\b/i.test(program)) return;
programNode.remove();
container.insertAdjacentHTML("afterbegin",
(container === document.body ? "<style>body, html { margin: 0; height: 100vh }</style>" : "") + `
<div class="lmc">
<div>
<div>
<div data-name="gutter"></div>
<div data-name="code"></div>
</div>
</div>
<div>
<button class="lmcEditButton" data-name="edit">🖉 Edit</button>
<span class="lmcNowrap"><span>Acc:</span><input type="text" readonly data-name="acc" size="3"></span>
<span class="lmcNowrap"><span>Neg:</span><input type="text" readonly data-name="neg" size="3"></span>
<span class="lmcNowrap"><span>Inp:</span><input type="text" data-name="input"></span>
<span class="lmcNowrap"><span>Out:</span><input type="text" readonly data-name="output"></span>
<span class="lmcActions">
<button data-name="run">▶▶ Run</button><button
data-name="walk">▶ Walk</button><button
data-name="step" title="Step and pause [F8]">❚❚ Step</button><button
data-name="totop" title="Set program counter to 0"><b>⭮</b> Reset</button>
</span>
<span data-name="err"></span>
</div>
</div>`);
this.outputAnimation = new LmcGui.Repeat(() => {
let left = this.gui.output.scrollLeft;
this.gui.output.scrollLeft = left + 2;
return left !== this.gui.output.scrollLeft;
});
this.runAnimation = new LmcGui.Repeat(() => {
if (this.state === LmcGui.RUNNING) this.step();
return this.state === LmcGui.RUNNING;
});
this.inputAnimation = new LmcGui.Repeat(() => {
let ch = this.gui.input.value[0];
let finish = !ch || ch === " ";
this.gui.input.readonly = !finish;
let i = finish ? (this.gui.input.value + " ").indexOf(" ")+1 : 1;
this.gui.input.value = this.gui.input.value.slice(i);
return !finish;
});
this.originalInput = "";
this.gui = {};
for (let elem of container.querySelectorAll(".lmc [data-name]")) {
this.gui[elem.dataset.name] = elem;
}
this.gui.run.onclick = () => this.run(1);
this.gui.walk.onclick = () => this.run(400);
this.gui.step.onclick = () => this.run(0);
document.body.addEventListener("keydown", (e) => e.key === 'F8' && this.run(0));
this.gui.totop.onclick = () => this.reset();
this.gui.edit.onclick = () => this.state === LmcGui.EDITING ? this.tidy() : this.load();
this.gui.input.onkeydown = (e) => { if (e.key === "Enter") this.run(this.delay); }
this._state = LmcGui.EDITING;
this.editor = new Editor(this.gui.code, (program) => this.parse(program));
program = this.load(program);
}
hideEditButton() {
// Hide the edit button temporary while the user is editing or running the program, so it does not obscure it.
clearTimeout(this.showButtonTimer);
this.gui.edit.style.display = "none";
this.showButtonTimer = setTimeout(() => {
this.gui.edit.style.display = "";
this.showButtonTimer = 0;
}, 1500);
}
parse(program) {
this.hideEditButton();
if (this.state === LmcGui.EDITING) {
super.load(program);
this.gui.run.disabled = this.gui.walk.disabled = this.gui.step.disabled = !this.mailboxes.length;
}
// Fill the gutter
let gutterLines = this.tokens.map(({address}) =>
`<div>${address !== undefined ? (address+":").padStart(3, "0") + (this.mailboxes[address].value+"").padStart(3, "0") : " \n"}</div>`
);
if (gutterLines[this.focusLine] && this.state !== LmcGui.EDITING) gutterLines[this.focusLine] = gutterLines[this.focusLine].replace('>', ' class="' + this.focusClass + '">');
this.gui.gutter.innerHTML = gutterLines.join("");
// Collect and return formatting information to be applied to the program editor
return this.tokens.map((line, lineNo) => {
let format = {};
if (lineNo === this.focusLine && this.state !== LmcGui.EDITING) format.background = this.focusClass;
if (line.address !== undefined) {
let mailbox = this.mailboxes[line.address];
if (mailbox.useMnemonic) {
let { offset } = line.mnemonic || line.argument;
format[offset] = { "class": "lmc" + mailbox.useMnemonic.toUpperCase() };
format[offset + mailbox.useMnemonic.length] = "";
}
if (mailbox.useArgument && mailbox.useMnemonic && !"BD".includes(mailbox.useMnemonic[0].toUpperCase())) {
let target = isNaN(mailbox.useArgument) ? this.symbolTable[mailbox.useArgument.toUpperCase()] : +mailbox.useArgument;
if (target in this.mailboxes) {
format[mailbox.argumentOffset] = { "title": "[" + mailbox.useArgument + "]=" + this.mailboxes[target].value };
format[mailbox.argumentOffset + (mailbox.useArgument+"").length] = "";
}
}
}
if (line.bad) {
format[line.bad.offset] = { "class": "lmcError", title: line.bad.message };
format[line.bad.offset+(line.bad.text.length || 1)] = "";
}
if (line.comment) format[line.comment.offset] = { "class": "lmcComment" };
return format;
});
}
/**
* Align the mnemonics, capitalise them, and use single space separator
* before argument and comment.
*/
tidy() {
let tab = this.labelLength;
let start = tab && (tab + 1);
this.editor.loadWithUndo(this.tokens.map(tokens => {
let {address, label, mnemonic, argument, comment, text} = tokens;
let coreText = text;
if (comment) coreText = coreText.slice(0, comment.offset);
coreText = coreText.trim().replace(/\s+/g, " ");
if (mnemonic || argument) {
coreText = label
? coreText.replace(" ", " ".repeat(start - label.text.length))
: " ".repeat(start) + coreText;
let size = (argument || mnemonic).offset + (argument || mnemonic).text.length
- (mnemonic || argument).offset;
// grab disassembly info:
let {mnemonic: mnemonic2, argumentLabel} = this.mailboxes[address];
if (!argument && mnemonic2.toUpperCase() === "DAT") argumentLabel = "";
coreText = coreText.slice(0, start) + (mnemonic2.toUpperCase() + " " + argumentLabel).trim()
+ coreText.slice(start + size);
}
return comment ? (coreText ? coreText + " " : "") + comment.text : coreText;
}).join("\n"));
}
static Repeat = class Repeat {
constructor(stepFunc) {
this._stepFunc = stepFunc;
this._timer = null;
}
_fun(abort) {
if (!abort && this._stepFunc()) return;
clearInterval(this._timer);
this._timer = null;
}
complete(abort) {
while (this._timer) this._fun(abort);
return this;
}
start(delay) {
this.complete(true);
this._timer = setInterval(() => this._fun(), delay);
return this;
}
}
get state() {
return this._state;
}
set state(toState) {
this.gui.edit.textContent = toState === LmcGui.EDITING ? "☷ Tidy" : "🖉 Edit";
if (![LmcGui.RUNNING, LmcGui.PAUSED, LmcGui.EDITING].includes(toState)) throw "invalid state " + toState;
let fromState = this._state;
this._state = toState;
if (fromState !== toState) {
if (this.options.onStateChange(fromState, toState) === false) {
this.delay = undefined;
}
}
}
step() { // override
if (!this.err) {
let wasEditing = this.state === LmcGui.EDITING;
this.state = LmcGui.RUNNING;
if (!wasEditing && !super.step()) this.state = LmcGui.PAUSED;
}
this.displayStatus();
}
run(delay) { // override
this.delay = delay;
if (this.state === LmcGui.EDITING) this.originalInput = this.gui.input.value;
this.runAnimation.complete(true);
if (this.delay > 0) this.runAnimation.start(delay);
if (this.delay >= 0) this.step();
if (!this.delay) this.state = LmcGui.PAUSED;
}
reset() { // override
super.reset();
this.inputAnimation.complete();
if (this.originalInput) this.gui.input.value = this.originalInput;
this.gui.input.removeAttribute("placeholder");
this.gui.input.focus();
this.gui.input.select();
this.gui.output.value = "";
if (this.state === LmcGui.RUNNING) this.state = LmcGui.PAUSED;
this.runAnimation.complete(true);
this.displayStatus();
}
load(program=this.program) { // override
this.state = LmcGui.EDITING;
this.inputAnimation.complete();
if (program.startsWith("#input:") && !this.gui.input.value) { // Get directive on first line
this.gui.input.value = program.match(/:(.*)/)[1].trim(); // pre-fill the input field.
}
this.editor.load(program); // will trigger this.parse (cf new Editor) > super.load
this.reset();
return program;
}
displayStatus() {
this.editor.readonly = this.state !== LmcGui.EDITING && "#aaa";
this.focusLine = this.err ? this.err.lineNo
: this._programCounter < this.mailboxes.length ? this.mailboxes[this._programCounter].lineNo
: -1;
this.focusClass = this.err ? "error" : "highlight";
this.gui.acc.value = this._accumulator;
this.gui.neg.value = this._flag ? "YES" : "NO";
this.gui.neg.style.backgroundColor = this._flag ? "orange" : "";
this.gui.err.textContent = this.err ? this.err.msg : "";
if (this.state === LmcGui.RUNNING) {
this.editor.load(this.tokens.map(tokens => tokens.address ? this.mailboxes[tokens.address].line : tokens.text).join("\n"));
}
this.editor.displayFormatted();
//this.gui.step.disabled = this.gui.run.disabled = this.gui.walk.disabled = this.isDone();
//this.gui.totop.disabled = !!this.err || this.state === LmcGui.EDITING;
// Scroll highlighted line into view (if there is a highlighted line)
let focusElem = this.gui.code.querySelector("." + this.focusClass);
if (focusElem) {
let focusTop = focusElem.getBoundingClientRect().top;
let container = this.gui.code.parentNode.parentNode;
let codeTop = container.getBoundingClientRect().top;
let add = (focusTop + focusElem.clientHeight) - (codeTop + container.clientHeight);
let sub = codeTop - focusTop;
if (add > 0) container.scrollTop += add + 0.5;
else if (sub > 0) container.scrollTop -= sub - 0.5;
}
}
}
class Editor {
constructor(container, formatter = (plainText) => [{ /* background: "", 1: { "class": "mnemonic" }, 4: null */ }]) {
this.container = container;
this.formatter = formatter;
container.spellcheck = false;
for (let event of Object.getOwnPropertyNames(Editor.prototype)) {
if (!event.startsWith("on")) continue;
container.addEventListener(event.slice(2), (e) => this.eventHandler(e));
}
this.load("");
document.addEventListener("selectionchange", (e) => this.onselectionchange(e));
this.readonly = false;
}
loadWithUndo(text) {
return this.perform(() => {
this.actionType = "complex";
this.lines = text.split(/\r?\n/);
});
}
load(text) {
this.lines = text.split(/\r?\n/);
if (!this.readonly) {
this.y1 = this.y2 = this.x1 = this.x2 = this.order = 0;
this.container.scrollLeft = this.container.scrollTop = 0;
this.undoStack = [];
this.redoStack = [];
this.dirty = false;
}
this.displayFormatted();
this.displaySelection();
}
get readonly() {
return this._readonly;
}
set readonly(isReadOnly) {
if (this._readonly === !!isReadOnly) return;
this._readonly = !!isReadOnly;
this.container.setAttribute("contenteditable", !isReadOnly);
if (isReadOnly) {
this.backup = [...this.lines];
this.backupBackground = this.container.style.background;
if (typeof isReadOnly === "string") this.container.style.background = isReadOnly;
} else if (this.backup) {
this.container.style.background = this.backupBackground;
this.lines = this.backup;
this.displayFormatted();
this.displaySelection();
}
}
eventHandler(e) {
let todo = this["on" + e.type](e);
if (!todo) return;
// A change to the text will occur:
e.preventDefault();
this.perform(todo);
}
perform(todo) {
let prevActionType = this.actionType;
if (this.syncRange()) prevActionType = "complex";
let before;
if (todo !== this.keyCtrlZ) {
before = JSON.stringify(this.lines);
this.undoStack.push([[...this.lines], this.order, this.y1, this.x1, this.y2, this.x2]);
}
if (typeof todo === "function") todo.call(this);
else this.insert(todo);
let hasChanged = before !== JSON.stringify(this.lines);
if (this.actionType === prevActionType && prevActionType !== "complex" || !hasChanged) {
this.undoStack.pop();
}
if (hasChanged) {
this.dirty = true;
this.displayFormatted();
}
this.displaySelection();
}
displayFormatted() {
let formats = this.formatter(this.text()) || [];
this.container.innerHTML = this.lines.map((line, y) => {
let {background, ...format} = formats[y] || {};
format = Object.entries(format).map(([k,v]) => [+k,v])
.sort(([a], [b]) => a - b); // pairs [x, style]
let shift = 0;
let i = 0;
for (let {index} of line.matchAll(/[<&]/g) || []) {
while (i < format.length && index >= format[i][0]) {
format[i++][0] += shift;
}
shift += line[index] === "&" ? 4 : line[index] === "<" ? 3 : 0; // "&" or "<"
}
line = line.replace(/&/g, "&").replace(/</g, "<");
let inside = false;
for (let [x, attrib] of format.reverse()) {
attrib = Object.entries(Object(attrib)).map(([k, v]) => ` ${k}="${v}"`).join("");
let tag = `</span><span${attrib}>`;
line = line.slice(0, x) + tag + line.slice(x);
}
line = `<span>${line}</span>`.replace(/<span>([^<]*)<\/span>/g, "$1");
background = background ? ` class="${background}"` : "";
return `<div${background}>${line}<br></div>`;
}).join("");
}
displaySelection() {
// set Selection:
var range = document.createRange();
range.setStart(...this.getNodeAndOffset(this.y1, this.x1));
range.setEnd(...this.getNodeAndOffset(this.y2, this.x2));
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
onselectionchange(e) {
if (this.readonly) return;
// Verify that the selection is within the container:
let sel = window.getSelection();
let {rangeCount, focusNode, focusOffset, anchorNode, anchorOffset} = sel;
if (!rangeCount || !this.container.contains(anchorNode) || !this.container.contains(focusNode)) return;
// Create a fine-grained range that corresponds to the caret position
let isForward = focusNode === anchorNode
? focusOffset >= anchorOffset
: anchorNode.compareDocumentPosition(focusNode) & Node.DOCUMENT_POSITION_FOLLOWING;
let range = sel.getRangeAt(0).cloneRange();
range.collapse(!isForward);
// Drill down
while (range.startContainer.nodeType === 1 && range.startContainer.childNodes.length) {
let child = range.startContainer.childNodes[range.startOffset];
if (!child) break;
range.setEnd(child, !isForward ? 0 : child.nodeType === 1 ? child.childNodes.length : child.textContent.length);
range.collapse();
}
// For some reason BR must be selected for it to get any meaningful getBoundingClientRect result (FireFox)
if (range.startContainer.nodeName === "BR" && range.startOffset === 0) {
range.selectNode(range.startContainer);
}
// Scroll selection into view if needed:
for (let container = this.container; container !== document.body; container = container.parentNode) {
let focus = range.getBoundingClientRect();
let style = getComputedStyle(container);
let {left, top} = container.getBoundingClientRect();
let width = container.clientWidth / 3;
let height = container.clientHeight / 3
left += parseFloat(style.borderLeftWidth);
top += parseFloat(style.borderTopWidth);
let right = left + container.clientWidth;
let bottom = top + container.clientHeight;
if (focus.right >= right) {
container.scrollLeft += focus.right - right + width;
} else if (focus.left < left + width) {
container.scrollLeft -= left - focus.left + width;
}
if (focus.bottom >= bottom) {
container.scrollTop += focus.bottom - bottom;
} else if (focus.top < top + height) {
container.scrollTop -= top - focus.top + height;
}
}
}
getNodeAndOffset(y, x) {
function getOffset(node, x) {
if (!node.childNodes || !node.childNodes.length) return [node, x];
for (let child of node.childNodes) {
if (child.textContent.length >= x) {
return getOffset(child, x);
}
x -= child.textContent.length;
}
}
let pair = getOffset(this.container.childNodes[y], x);
return pair;
}
getLineAndColumn(node, offset) {
let charCount = node.nodeType === 3 ? offset : 0;
while (node !== this.container) {
if (node === document.documentElement) return [0, 0];
if (node.nodeType !== 3) {
for (let i = 0; i < offset; i++) {
charCount += node.childNodes[i].textContent.length;
}
}
offset = Array.from(node.parentNode.childNodes).indexOf(node);
node = node.parentNode;
}
// Avoid a reference to a non-existing trailing childnode.
if (offset >= this.container.childNodes.length && this.container.childNodes.length > 0) {
if (charCount) throw "getLineAndColumn finds an offset beyond the size of the content??";
return [offset-1, this.container.childNodes[this.container.childNodes.length-1].textContent.length];
}
return [offset, charCount];
}
syncRange() {
let selection = window.getSelection();
let [y1, x1] = this.getLineAndColumn(selection.anchorNode, selection.anchorOffset);
let [y2, x2] = this.getLineAndColumn(selection.focusNode, selection.focusOffset);
let order = y2 - y1 || x2 - x1;
if (order < 0) [y1, x1, y2, x2] = [y2, x2, y1, x1];
let changedPosition = order !== this.order || x1 != this.x1 || x2 != this.x2 || y1 != this.y1 || y2 != this.y2;
this.order = order;
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
return changedPosition;
}
text() {
return this.lines.join("\n");
}
selectedText() {
if (this.y1 === this.y2) return this.lines[this.y1].slice(Math.min(this.x1, this.x2), Math.max(this.x1, this.x2));
return this.lines[this.y1].slice(this.x1) + "\n"
+ this.lines.slice(this.y1+1, this.y2).map(line => line + "\n").join("")
+ this.lines[this.y2].slice(0, this.x2);
}
isEmpty() {
return !this.order;
}
deleteRange() {
this.lines[this.y1] = this.lines[this.y1].slice(0, this.x1) + this.lines[this.y2].slice(this.x2);