MCPcopy Create free account
hub / github.com/corbanbrook/dsp.js / preExpressionTransform

Function preExpressionTransform

examples/js/processing.js:11212–11299  ·  view source on GitHub ↗
(expr)

Source from the content-addressed store, hash-verified

11210 }
11211
11212 function preExpressionTransform(expr) {
11213 var s = expr;
11214 // new type[] {...} --> {...}
11215 s = s.replace(/\bnew\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)(?:\s*"C\d+")+\s*("A\d+")/g, function(all, type, init) {
11216 return init;
11217 });
11218 // new Runnable() {...} --> "F???"
11219 s = s.replace(/\bnew\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)(?:\s*"B\d+")\s*("A\d+")/g, function(all, type, init) {
11220 return addAtom(all, 'F');
11221 });
11222 // function(...) { } --> "H???"
11223 s = s.replace(functionsRegex, function(all) {
11224 return addAtom(all, 'H');
11225 });
11226 // new type[?] --> new ArrayList(?)
11227 s = s.replace(/\bnew\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)\s*("C\d+"(?:\s*"C\d+")*)/g, function(all, type, index) {
11228 var args = index.replace(/"C(\d+)"/g, function(all, j) { return atoms[j]; }).
11229 replace(/\[\s*\]/g, "[0]").replace(/\s*\]\s*\[\s*/g, ", ");
11230
11231 var arrayInitializer = "(" + args.substring(1, args.length - 1) + ")";
11232 return 'new ArrayList' + addAtom(arrayInitializer, 'B');
11233 });
11234 // .length() --> .length
11235 s = s.replace(/(\.\s*length)\s*"B\d+"/g, "$1");
11236 // #000000 --> 0x000000
11237 s = s.replace(/#([0-9A-Fa-f]+)/g, function(all, digits) {
11238 return digits.length < 6 ? "0x" + digits : "0xFF000000".substring(0, 10 - digits.length) + digits;
11239 });
11240 // delete (type)???, (int)??? -> 0|???
11241 s = s.replace(/"B(\d+)"(\s*(?:[\w$']|"B))/g, function(all, index, next) {
11242 var atom = atoms[index];
11243 if(!/^\(\s*[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*\s*(?:"C\d+"\s*)*\)$/.test(atom)) {
11244 return all;
11245 } else if(/^\(\s*int\s*\)$/.test(atom)) {
11246 return "0|" + next;
11247 } else {
11248 var indexParts = atom.split(/"C(\d+)"/g);
11249 if(indexParts.length > 1) {
11250 // even items contains atom numbers, can check only first
11251 if(! /^\[\s*\]$/.test(atoms[indexParts[1]])) {
11252 return all; // fallback - not a cast
11253 }
11254 }
11255 return "" + next;
11256 }
11257 });
11258 // super() -> $superCstr(), super. -> $super.;
11259 s = s.replace(/\bsuper(\s*"B\d+")/g, "$$superCstr$1").replace(/\bsuper(\s*\.)/g, "$$super$1");
11260 // 3.0f -> 3.0
11261 s = s.replace(/\b(\.?\d+)[fF]/g, "$1");
11262 // Weird (?) parsing errors with %
11263 s = s.replace(/([^\s])%([^=\s])/g, "$1 % $2");
11264 // Since frameRate() and frameRate are different things,
11265 // we need to differentiate them somehow. So when we parse
11266 // the Processing.js source, replace frameRate so it isn't
11267 // confused with frameRate(), as well as keyPressed and mousePressed
11268 s = s.replace(/\b(frameRate|keyPressed|mousePressed)\b(?!\s*"B)/g, "__$1");
11269 // "pixels" replacements:

Callers 1

expandExpressionFunction · 0.85

Calls 1

addAtomFunction · 0.85

Tested by

no test coverage detected