-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodemusic.js
135 lines (111 loc) · 3.62 KB
/
codemusic.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
var fs = require("fs");
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var program = require("commander");
var _ = require("underscore");
program
.version("0.0.1")
.parse(process.argv);
if(!program.args.length){
console.log("You need to supply a file name.");
return;
}
var file = fs.readFileSync(program.args[0], "utf8");
console.log("Done reading the file!");
try {
var ast = jsp.parse(file, false, true);
} catch(ex){
console.log("Exception: ", ex);
return;
}
// https://github.com/mishoo/UglifyJS/blob/master/tmp/instrument.js
var w = pro.ast_walker();
//
// Idea: While walking the stack, give each block or function a random id. Call 'trace'
// with the id of every block or function we are inside at the current time.
// How do we know we're in the same function or block if we got to it from multiple places? Name
// can't be relied upon, so we have to compare its contents? Maybe we can hash the serialized contents
// of a block and use this for the id?
//
// Then, whenever 'trace' is called, we use a different instrument for each id and step the
// current frequency up one step for each level of 'depth'.
//
//
// example:
//
// function (){ // abc
// trace("abc", 1); // instrument abc plays the 1st note in its scale
// function(){ // def
// trace(["abc", "def"], 2); // instrument abc and def play the 2nd note in their scales
// }
// }
//
// Better way of doing that: keep track of current note for each instrument.
//
var analyzing = [];
function do_stat() {
var ret;
var current = this[0];
if (current.start && analyzing.indexOf(this) < 0) {
// console.log(this[0])
// without the `analyzing' hack, w.walk(this) would re-enter here leading
// to infinite recursion
analyzing.push(this);
// Insert the splice AFTER 'this'
var indent = "";
analyzing.forEach(function(){
indent += " ";
});
console.log(indent + current.start.line + " " + current.start.value);
if(current.start.value === "function"){
var fileSubstring = file.substring(current.start.endpos);
var firstParen = fileSubstring.indexOf("(")+1;
var secondParen = fileSubstring.indexOf(")");
var paramCount;
if(secondParen === firstParen){
paramCount = 0;
} else {
var paramString = fileSubstring.substring(firstParen, secondParen);
var paramCount = Math.max(1,paramString.split(",").length);
}
console.log(indent+" "+paramCount+" params");
}
ret = w.walk(this);
analyzing.pop(this);
}
return ret;
};
function containerIdentity(node){
return node[0].name;
}
function blockDepth(analyzing){
return _.filter(analyzing, function(node){
return node[0].name === "block" || node[0].name === "defun" || node[0].name === "function";
}).length;
}
var new_ast = w.with_walkers({
// "stat" : do_stat,
// "label" : do_stat,
// "break" : do_stat,
// "continue" : do_stat,
// "debugger" : do_stat,
// "var" : do_stat,
// "const" : do_stat,
// "return" : do_stat,
// "throw" : do_stat,
// "try" : do_stat,
"defun" : do_stat,
"function" : do_stat,
"if" : do_stat,
// "while" : do_stat,
// "do" : do_stat,
// "for" : do_stat,
// "for-in" : do_stat,
// "switch" : do_stat,
"block" : do_stat,
// "with" : do_stat,
}, function(){
return w.walk(ast);
});
var output = pro.gen_code(new_ast, { beautify: true });
// console.log(output);