-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.js
55 lines (46 loc) · 1.06 KB
/
interpreter.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
const fs = require('fs');
const readlineSync = require('readline-sync');
// ocaml grammar
var parser = require('./ocaml').parser;
// start 'interpreter'
while (true) {
var line = '';
var promptChar = '# ';
// read successive lines until ';;' is entered
while (! line.includes(';;')) {
var input = readlineSync.question(promptChar);
line += ' ' + input;
// don't print '# ' for successive lines in single input
promptChar = '';
}
ocaml_input = line.trim().split(';;')[0];
// get file if #use command
if (ocaml_input.includes('#use')) {
var fp = ocaml_input.split('#use')[1].trim();
console.log(useFile(fp));
}
else {
// else transpile
console.log(parse(ocaml_input));
}
}
function useFile(fp) {
if (fs.existsSync(fp)) {
var ocaml_input = fs.readFileSync(fp, 'utf8');
var js_out = parse(ocaml_input);
return js_out;
}
// file error
else {
return "File Path Error!";
}
}
function parse(str) {
try {
return parser.parse(str);
}
catch (e) {
console.log(e);
return "Syntax Error!";
}
}