-
Notifications
You must be signed in to change notification settings - Fork 16
/
repl.js
80 lines (71 loc) · 2.09 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Created by Josh on 3/21/17.
*/
//implementation of Oblivion repl
var prs= require("./src/parse/parser");
var env = require("./src/Env");
var io = require('./src/IO');
var readline = require('readline');
//function that starts the REPL
var OblRepl = function(){
var rl = readline.createInterface(process.stdin, process.stdout);
var saved = "";
var notBalanced = false;
var ENV = new env.Environment.Env();
rl.setPrompt('Obl> ');
rl.prompt();
rl.on('line', function(line) {
if (line === "exit") rl.close();
else {
console.log(interpret(line.trim()));
rl.prompt();
}
}).on('close',function(){
process.exit(0);
});
var interpret = function(code){
if(notBalanced){
saved += code;
var tParsed = isBalanced(saved);
if(tParsed){
rl.setPrompt('Obl> ');
notBalanced = false;
saved = "";
if(tParsed["node"] === '?program') {
for(var i=0;i<tParsed['args'].length;i++){
ENV.callLib(ENV, tParsed['args'][i].node, tParsed['args'][i].args);
}
}
//returns only the StdOut of the IO
return io.IO.getFlushOut();
}
else {
return "";
}
}
var parsed = isBalanced(code);
if(parsed){
if(parsed["node"] === '?program') {
for(var i=0;i<parsed['args'].length;i++){
ENV.callLib(ENV, parsed['args'][i].node, parsed['args'][i].args);
}
}
//returns only the StdOut of the IO
return io.IO.getFlushOut();
}
else {
notBalanced = true;
rl.setPrompt('...> ');
saved += code;
return io.IO.getFlushOut();
}
};
var isBalanced = function(code){
try{
return prs.parse(code);
} catch(err){
return false;
}
};
};
exports.OblRepl = OblRepl;