Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug.js
More file actions
Latest commit
90 lines (76 loc) · 1.93 KB
/
Copy pathdebug.js
File metadata and controls
90 lines (76 loc) · 1.93 KB
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
/*jshint evil:true*/
varcompiler=require('./lib/compiler');
varpath=require('path');
varsh=require('shelljs');
varesprima=require('./lib/esprima')
,escodegen=require('./lib/escodegen')
,transform=require('./lib/transform').transform;
varDEBUG=__dirname+"/debug";
varEXAMPLES=__dirname+"/examples";
/**
* create debug dir
*/
if(!sh.test('-d',DEBUG)){
sh.mkdir(DEBUG);
}
/**
* util for saving debug files
*/
functionsave(data,file){
if(typeofdata!=='string'){
data=JSON.stringify(data,null,' ');
}
data.to(DEBUG+'/'+file);
}
functionremoveLoc(obj){
varresult;
if(Object.prototype.toString.call(obj)==='[object Array]'){
result=[];
obj.forEach(function(o){
result.push(removeLoc(o));
});
returnresult;
}elseif(obj===null){
returnnull;
}elseif(typeofobj==='object'){
result={};
Object.keys(obj).forEach(function(k){
if(k!=='loc'){
result[k]=removeLoc(obj[k]);
}
});
returnresult;
}else{
returnobj;
}
}
varsample=process.argv[2];
if(!sample){
sample=EXAMPLES+'/sample.ws';
}elseif(sample.indexOf('.ws')!==sample.length-3){
sample=EXAMPLES+'/'+sample+'.ws';
}
varfileName=path.basename(sample,'.ws');
varsource=sh.cat(sample);
varast,transformed,output;
try{
ast=esprima.parse(source,{loc: true});
save(removeLoc(ast),fileName+'-ast.json');
transformed=transform(ast);
save(removeLoc(transformed),fileName+'-transformed-ast.json');
output=escodegen.generate(transformed);
save(output,fileName+'.js');
}catch(ex){
if(ex.messages){
console.error(ex.message);
ex.messages.forEach(function(err){
console.error(' - '+err);
});
if(ex.newAst){
save(removeLoc(ex.newAst),fileName+'-transformed-ast.json');
}
}else{
console.error(ex.stack);
}
}
eval(output);