-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.js
59 lines (49 loc) · 1.23 KB
/
repl.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
// * imports
import readline from 'readline-sync';
import math from './functions/math.js';
import utility from './functions/utility.js';
// * keywords
const MATH_FUNCTIONS = ['add', 'mul', 'sub', 'div'];
const UTILITY_FUNCTIONS = ['echo'];
// * STORAGE
const STORAGE = {};
// * REPL
while (true) {
// * parsing
const userInput = readline.question('>'); // grab user string
const userInputs = userInput.split(' '); // split by spaces
const userFunction = userInputs[0]; // grab function
let args = userInputs.slice(1); // grab args
args = args.map((element) => { // substitute in variables
if (element in STORAGE) {
return STORAGE[element];
}
return element;
})
let response;
// * MATH
if (MATH_FUNCTIONS.includes(userFunction))
{
response = '' + math(userFunction, ...args);
}
// * UTILITIES
else if (UTILITY_FUNCTIONS.includes(userFunction))
{
response = '' + utility(userFunction, ...args);
}
// * NATIVE
else if (userFunction === 'def')
{
STORAGE[args[0]] = args[1]; // assign x to 10
response = args[1]; // return 10
}
else if (userFunction === 'alldata') {
response = STORAGE;
}
// * ERROR
else
{
response = 'invalid';
}
console.log(response);
}