Skip to content

Commit 62107b7

Browse files
committed
switch to simple walk
1 parent 1f0a50f commit 62107b7

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

src/webgl/ShaderGen.js

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,31 @@
66
*/
77

88
import { parse } from 'acorn';
9-
import * as walk from 'acorn-walk';
9+
import { simple } from 'acorn-walk';
1010
import escodegen from 'escodegen';
1111

1212
function shadergen(p5, fn) {
1313
let GLOBAL_SHADER;
1414

1515
const oldModify = p5.Shader.prototype.modify
1616

17-
p5.Shader.prototype.modify = function(arg) {
18-
19-
if (arg instanceof Function) {
20-
const code = arg.toString()
21-
const ast = parse(code, { ecmaVersion: 2021, locations: true });
17+
p5.Shader.prototype.modify = function(modifier) {
18+
if (modifier instanceof Function) {
19+
const code = modifier.toString()
20+
const ast = parse(code, { ecmaVersion: 2021 /*, locations: true*/ });
2221

23-
walk.ancestor(ast, ASTCallbacks, null, {myData: 123});
22+
simple(ast, ASTCallbacks);
2423

25-
const transformed = escodegen.generate(ast);
26-
console.log(transformed)
24+
const transpiledArg = escodegen.generate(ast);
25+
console.log(transpiledArg)
2726

28-
// const program = new ShaderProgram(arg)
27+
// const program = new ShaderProgram(modifier)
2928
// const newArg = program.generate();
3029
// console.log(newArg.vertex)
3130
// return oldModify.call(this, newArg);
3231
}
3332
else {
34-
return oldModify.call(this, arg)
33+
return oldModify.call(this, modifier)
3534
}
3635
}
3736

@@ -48,11 +47,14 @@ function shadergen(p5, fn) {
4847
}
4948

5049
const ASTCallbacks = {
51-
Literal(node, state, ancestors) {
50+
// TODO: automatically making uniforms
51+
Literal(node) {
5252
},
53-
AssignmentExpression(node, _state, ancestors) {
54-
// Operator overloading
55-
if (node.operator != '=') {
53+
54+
// The callbacks for AssignmentExpression and BinaryExpression handle
55+
// operator overloading including +=, *= assignment expressions
56+
AssignmentExpression(node) {
57+
if (node.operator !== '=') {
5658
const rightReplacementNode = {
5759
type: 'CallExpression',
5860
callee: {
@@ -68,31 +70,12 @@ function shadergen(p5, fn) {
6870
},
6971
arguments: [node.right]
7072
}
71-
7273
node.operator = '=';
7374
node.right = rightReplacementNode;
7475
}
7576
},
76-
BinaryExpression(node, state, ancestors) {
77-
// let i = ancestors.length - 1;
78-
// let ancestor = ancestors[i]; // ancestor === node
79-
// while (ancestor.type === 'BinaryExpression') {
80-
// ancestor = ancestors[i--];
81-
// }
82-
83-
console.log("\n NEW NODE:")
84-
85-
const transformed = escodegen.generate(node);
86-
const l = escodegen.generate(node.left);
87-
const r = escodegen.generate(node.right);
88-
console.log("Transformed: ", transformed);
89-
console.log("Left: ", l);
90-
console.log("Right: ", r);
91-
92-
console.log(node.left);
93-
77+
BinaryExpression(node) {
9478
node.type = 'CallExpression';
95-
console.log("OPERATOR: ", node.operator)
9679
node.callee = {
9780
type: "MemberExpression",
9881
object: node.left,
@@ -102,12 +85,13 @@ function shadergen(p5, fn) {
10285
},
10386
};
10487
node.arguments = [node.right];
105-
10688
},
10789
}
10890

10991

110-
// JS API
92+
// Javascript Node API.
93+
// These classes are for expressing GLSL functions in Javascript with
94+
// needing to transpile the user's code.
11195

11296
class BaseNode {
11397
constructor(isInternal) {
@@ -121,7 +105,7 @@ function shadergen(p5, fn) {
121105
this.usedIn = [];
122106
this.dependsOn = [];
123107
this.srcLine = null;
124-
108+
// Stack Capture is used to get the original line of user code for Debug purposes
125109
if (isInternal === false) {
126110
try {
127111
throw new Error("StackCapture");

0 commit comments

Comments
 (0)