-
Notifications
You must be signed in to change notification settings - Fork 0
/
erlang.js
137 lines (112 loc) · 3.55 KB
/
erlang.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
#!/usr/bin/env node
var program = require('commander');
var fs = require('fs');
var esprima = require('esprima');
var exec = require('exec');
var path = require('path');
var mv = require('mv');
//Config flags
var TMP_ERL_FILE_PATH = "./tmp.erl";
var INPUT_ERLANG_FILE_PATH = undefined;
var JS_AST_OUTPUT_ENABLED = false;
var DEFAULT_JS_OUTPUT_DIR_PATH = "./";
var JS_OUTPUT_FILE = undefined;
function ouptutAst() {
return JS_AST_OUTPUT_ENABLED = true;
}
function outputFile(path) {
JS_OUTPUT_FILE = path;
}
function parse(code, callback) {
fs.writeFile(TMP_ERL_FILE_PATH, code, function(err) {
if (err) {
throw err;
}
parseFile(TMP_ERL_FILE_PATH, function(ast){
if (!ast){
throw new Error("No ast tree was generated");
}else{
callback(ast);
}
});
});
}
function wrap(code, callback) {
var wrap_str = "-module(tmp).\n-compile({parse_transform, shen}).\n-compile(export_all).\n-js([start/0]).\nstart() ->\n";
var wrapped_code = wrap_str + code;
fs.writeFile(TMP_ERL_FILE_PATH, wrapped_code, function(err) {
if (err) throw err;
parseFile(TMP_ERL_FILE_PATH, function(ast){
if (!ast){
throw new Error("No ast tree was generated");
}else{
callback(ast);
}
});
});
}
function deleteFiles(filename) {
var jsfile = filename + ".js";
var beamfile = filename + ".beam";
if (fs.existsSync(jsfile))
fs.unlinkSync(jsfile);
if (fs.existsSync(beamfile))
fs.unlinkSync(beamfile);
if (fs.existsSync(TMP_ERL_FILE_PATH))
fs.unlinkSync(TMP_ERL_FILE_PATH);
}
//Handle user input
program
.version('0.0.1')
.option('-t, --ast', 'Output js ast tree', ouptutAst)
.option('-o, --output <file>', 'Output to js file', outputFile)
.parse(process.argv);
if (program.args.length > 0 && program.args[0]!="test/test.js") {
INPUT_ERLANG_FILE_PATH = program.args[0];
} else {
console.log("Please enter erlang file path. i.e. examples/sample.erl")
}
function cleanUp(code, filename) {
deleteFiles(filename);
}
if (INPUT_ERLANG_FILE_PATH){
parseFile(INPUT_ERLANG_FILE_PATH);
}
function parseFile(erlang_file_path, callback) {
if (fs.existsSync(erlang_file_path)) {
exec(['erlc', '-pa', './deps', erlang_file_path], function(err, out, code) {
if (err instanceof Error)
throw err;
process.stderr.write(err);
var erlang_fileName = path.basename(erlang_file_path);
var filename = erlang_fileName.split(".")[0];
var js_filename = filename + ".js";
var shen_output_js = DEFAULT_JS_OUTPUT_DIR_PATH + js_filename
var jsCode = fs.readFileSync(shen_output_js, "utf8");
var ast = esprima.parse(jsCode, {
range: true
});
callback(ast);
if (JS_AST_OUTPUT_ENABLED) {
process.stdout.write(JSON.stringify(ast, null, 4));
}
if (JS_OUTPUT_FILE) {
mv(shen_output_js, JS_OUTPUT_FILE, {
mkdirp: true
}, function(err) {
if (err instanceof Error)
throw err;
cleanUp(code, filename);
});
} else {
cleanUp(code, filename);
}
});
} else {
console.log("erlang file path is invalid");
}
}
module.exports={
parse: parse,
wrap: wrap
}