-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterminal-interface.js
141 lines (139 loc) · 5.7 KB
/
terminal-interface.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
/**
* terminal-interface.js
* Made by: code-explorer786 (https://github.com/code-explorer786)
* Date of creation: 17-08-2021
* Time of creation: 4:41 GMT (11:41 in the author's timezone)
*
* Command-line "REPL" created for easier code execution,
* just insert the code in and enjoy.
*
* HOW TO USE:
* Insert the code. You can copy-paste.
* Type "run-all" and hit Enter.
* To check printB, simply type "check printB" and hit Enter.
* If you want to exit, simply type "exit" and hit Enter.
* You can view more at the cheat sheet.
*
* HOW TO RUN:
* You can use one of the commands below:
* node ./terminal-inteface.js
* npm run emulation
*/
import * as LogicExecutor from './LogicExecutor.js';
// import * as MessageBlock from './messageBlock.js';
// import * as SwitchBlock from './SwitchBlock.js';
import * as promptSync from 'prompt-sync';
var processor = new LogicExecutor.LogicExecutor();
// var mblock = new MessageBlock.MessageBlock();
// var sblock = new SwitchBlock.SwitchBlock();
const prompt = promptSync.default({sigint:true});
var lastLine = "";
var relativeLines = 0;
var numLines = 0;
var displaySettings = {
debugInstructions : false,
totalLines : true,
relativeLines : true
}
var targetLine = 0;
var limit;
var deconstructed;
var editLine;
console.log("\x1b[32mWelcome!\x1b[0m\nFormat: \x1b[33mtotal lines \x1b[32mrelative lines \x1b[0m>")
while(lastLine != "exit"){
lastLine = prompt((displaySettings.totalLines ? (`\x1b[33m${numLines}\x1b[0m `) : "")
+ (displaySettings.relativeLines ? (`\x1b[32m${relativeLines}\x1b[0m `) : "")
+ "> ");
switch(lastLine){
case "run":
targetLine = processor.counter + relativeLines;
while(processor.counter < targetLine) {
if(displaySettings.debugInstructions){
console.log(processor.counter + " | " + processor.statement(processor.code.split("\n")[processor.counter]));
}
processor.doInstruction();
}
relativeLines = 0;
break;
case "run-all":
limit = prompt("Instruction limit (leave blank for infinite limit): ");
limit = (limit == "") ? true : Number(limit)
targetLine = processor.counter + relativeLines;
processor.counter = 0;
while(processor.counter < targetLine && limit) {
if(displaySettings.debugInstructions){
console.log(processor.counter + " | " + processor.statement(processor.code.split("\n")[processor.counter]));
}
processor.doInstruction();
(limit === true) ? 0 : limit--;
}
relativeLines = 0;
break;
case "run-limit":
limit = Number(prompt("Limit (number of instructions): [ ]\b\b\b\b"))
targetLine = processor.counter + relativeLines;
while(processor.counter < targetLine && limit) {
if(displaySettings.debugInstructions){
console.log(processor.counter + " | " + processor.statement(processor.code.split("\n")[processor.counter]));
}
processor.doInstruction();
limit--;
}
relativeLines = 0;
break;
case "exit":
break;
case "check printB":
console.log(processor.printB);
break;
case "settings":
console.log(`\x1b[31mWhat settings do you want to tick?\x1b[33m\n[0] (${displaySettings.totalLines}) Total Lines\n\x1b[32m[1] (${displaySettings.relativeLines}) Relative Lines\x1b[0m\n[2] (${displaySettings.debugInstructions}) Debug instructions`);
switch(prompt("[ ]\b\b")){
case "0":
displaySettings.totalLines = !displaySettings.totalLines;
console.log("Successfully ticked total lines.")
break;
case "1":
displaySettings.relativeLines = !displaySettings.relativeLines;
console.log("Successfully ticked relative lines.")
break;
case "2":
displaySettings.debugInstructions = !displaySettings.debugInstructions;
console.log("Successfully ticked debug instructions.")
break;
default:
break;
}
break;
case "full code":
console.log(processor.code);
break;
case "edit line":
deconstructed = processor.code.split("\n");
console.log("These are the lines of code avaliable.");
for(let i = 0; i < deconstructed.length; i++){
console.log(`\x1b[33m${i}\x1b[0m | ${deconstructed[i]}`);
}
editLine = prompt("What line of code do you want to edit? ")
while(!isNaN(parseInt(editLine))){
editLine = parseInt(editLine);
deconstructed[editLine] = prompt(`\x1b[33m${editLine}\x1b[0m >> `);
editLine = prompt("What line of code do you want to edit? ")
}
processor.code = deconstructed.join("\n");
break;
case "blackhole":
if(prompt("\x1b[31mTHIS WILL DELETE ALMOST ALL OF YOUR DATA. ARE YOU SURE? (yes/other) ").toLowerCase() === "yes"){
numLines = 0;
relativeLines = 0;
lastLine = "";
processor = new LogicExecutor.LogicExecutor();
}
break;
default:
relativeLines += 1;
numLines += 1;
processor.code += lastLine + "\n";
break;
}
}