-
Notifications
You must be signed in to change notification settings - Fork 11
/
repl.es6
79 lines (61 loc) 路 2.61 KB
/
repl.es6
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
import readline from 'readline';
import colors from 'colors';
import CheddarScope from '../interpreter/core/env/scope';
import NIL from '../interpreter/core/consts/nil';
import cheddar from '../interpreter/exec';
import tokenizer from '../tokenizer/tok';
/*== ENVIORNMENT GENERATION DEPENDENCIES ==*/
// TODO Externalize this
import dep_String from '../interpreter/core/primitives/String';
import dep_Bool from '../interpreter/core/primitives/Bool';
import dep_Number from '../interpreter/core/primitives/Number';
import dep_Array from '../interpreter/core/primitives/Array';
import CheddarVariable from '../interpreter/core/env/var';
let REPL = readline.createInterface(process.stdin, process.stdout);
REPL.setPrompt('cheddar> '.yellow.bold);
REPL.prompt();
let USI = 0;
// Workaround
REPL._setPrompt = REPL.setPrompt;
REPL.setPrompt = (prompt, length) =>
REPL._setPrompt(prompt, length ? length : prompt.split(/[\r\n]/).pop().stripColors.length);
const REPL_ERROR = text => console.log("T_REPL:ERROR".red.underline.bold + " - ".dim + text);
const REPL_HEAD = text => console.log(`鈹佲攣 ${text} 鈹佲攣`.bold.magenta);
const CONSTANT = { Writeable: false };
let GLOBAL_SCOPE = new CheddarScope(null, new Map([
["String" , new CheddarVariable(dep_String, CONSTANT)],
["Number" , new CheddarVariable(dep_Number, CONSTANT)],
["Array" , new CheddarVariable(dep_Array , CONSTANT)],
["Boolean", new CheddarVariable(dep_Bool , CONSTANT)]
]));
REPL.on('line', function(STDIN) {
if (STDIN === 'quit') REPL.close();
let Tokenizer = new tokenizer(STDIN, 0);
let Result = Tokenizer.exec();
let Executor = new cheddar(Result, GLOBAL_SCOPE);
let Output = Executor.exec();
if (Output) {
if (typeof Output === "string") {
REPL_ERROR(Output);
} else if (Output instanceof NIL) {
// do nothing?
} else if (!Output) {
console.log(Output);
} else if (Output.constructor.Name === "String") {
console.log(`"${Output.value}"`.magenta);
} else if (Output && Output.constructor.Cast && Output.constructor.Cast.has('String')) {
console.log(
`${Output.constructor.Cast.get('String')(Output).value}`.magenta
);
} else if (Output instanceof CheddarScope) {
console.log(`< Instance of "${Output.constructor.Name}" >`);
} else if (Output.prototype instanceof CheddarScope) {
console.log(`< Class "${Output.Name}" >`);
} else if (typeof Output === "symbol") {
console.log(Output.toString().magenta);
} else {
console.log(`< Unprintable object of class "${Output.constructor.name.magenta}" with literal value ${Output.magenta} >`);
}
}
REPL.prompt();
}).on('close', () => process.exit(0));