Skip to content

Commit 50fc695

Browse files
committed
better variable ids
1 parent 2a6c622 commit 50fc695

File tree

2 files changed

+1504
-1054
lines changed

2 files changed

+1504
-1054
lines changed

Resources/OSL Utils.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -584,36 +584,36 @@ class OSLUtils {
584584
memMap.set(cur, size)
585585
// store the size of the map for each node, allowing the memory location to be specific to the node itself
586586
// references allow this to work because the queue is full of references, *not values*
587-
switch (cur.type) {
587+
switch (cur?.type) {
588588
case "var":
589589
out.push(size, types.var, cur.data, 0)
590590
break
591591
case "num": case "unk": case "str": case "raw":
592592
out.push(size, types[cur.type], cur.data, 0)
593593
break
594594
case "opr": case "cmp": case "log": case "bit":
595-
if (types[cur.data] === undefined) throw new Error()
595+
if (types[cur.data] === undefined) throw new Error(`'${cur.data}' is unsupported by bysl`)
596596
out.push(size, types[cur.data], memMap.get(cur.left), memMap.get(cur.right))
597597
break
598598
case "mtd":
599599
out.push(size, types.var, cur?.data?.[0].data, 0)
600600
const data = cur.data
601601
for (let j = 1; j < data.length; j++) {
602602
const cur2 = data[j]
603-
if (cur2.type !== "var") throw new Error()
603+
if (cur2.type !== "var") throw new Error(`'${cur2.data}' in mtd must be a var`)
604604
out.push(size, types.prp, memMap.get(cur), cur2.data)
605605
}
606606
break
607607
default:
608-
throw new Error()
608+
throw new Error(`Unsupported node for bysl: '${JSON.stringify(cur)}'`)
609609
}
610610
}
611611

612612
// let the interpreter know how much memory is nessecary for this bysl
613613
// bad for memory but might be the best way here
614614
out.unshift(0, types.tot, memMap.size, 0)
615615
return { success: true, code: out }
616-
} catch {
616+
} catch(e) {
617617
return { success: false, code: out } // catch when it fails to generate
618618
}
619619
}
@@ -1896,26 +1896,28 @@ class OSLUtils {
18961896
const t = this.tkn;
18971897
switch (node.num) {
18981898
case t.fnc:
1899-
if (node.data === "function") {// we found a function def
1899+
if (node.data === "function") { // we found a function def
19001900
const blk = node.parameters[1];
19011901
if (blk?.num !== t.blk) break;
19021902
const params = node.parameters[0];
19031903
if (params?.num !== t.str) break;
19041904

19051905
if (node.parameters[2]?.data === true) return
1906-
const func_vars = new Map();
19071906

19081907
const parts = params.data.split(",")
1909-
for (const part of parts)
1910-
if (!func_vars.has(part))
1911-
func_vars.set(part, func_vars.size)
1908+
let newParams = []
1909+
for (const part of parts) {
1910+
if (!vars.has(part))
1911+
vars.set(part, vars.size)
1912+
newParams.push(vars.get(part))
1913+
}
1914+
1915+
node.parameters[0].data = newParams.join(",")
19121916

1913-
node.vars = this._stepAstNode(
1917+
this._stepAstNode(
19141918
node.parameters[1],
1915-
func_vars
1919+
vars
19161920
)
1917-
if (node.vars)
1918-
node.vars = Object.fromEntries(node.vars);
19191921
} else {
19201922
for (const param of node.parameters)
19211923
this._stepAstNode(param, vars)
@@ -1928,13 +1930,26 @@ class OSLUtils {
19281930
break
19291931
case t.var:
19301932
if (vars === null) return;
1931-
if (!vars.has(node.data)) vars.set(node.data, vars.size);
1932-
node.id = vars.get(node.data);
1933+
if (vars.has(node.data))
1934+
node.id = vars.get(node.data);
1935+
break;
1936+
case t.asi:
1937+
const l = node.left;
1938+
if (l) {
1939+
if (!vars.has(l.data) && node.data === "@=" || node.data === "=") {
1940+
if (l.num === t.var)
1941+
vars.set(l.data, vars.size);
1942+
if (l.num === t.rmt && l.objPath[0].data === "this" && l.objPath.length === 1)
1943+
vars.set(l.final.data, vars.size);
1944+
}
1945+
1946+
this._stepAstNode(node.left, vars)
1947+
}
1948+
if (node.right) this._stepAstNode(node.right, vars)
19331949
break;
19341950
case t.bit:
19351951
case t.opr:
19361952
case t.cmp:
1937-
case t.asi:
19381953
if (node.left) this._stepAstNode(node.left, vars)
19391954
if (node.right) this._stepAstNode(node.right, vars)
19401955
break;
@@ -1970,9 +1985,10 @@ class OSLUtils {
19701985
}
19711986

19721987
_applyVariableIds(ast) {
1988+
const vars = new Map();
19731989
for (const line of ast) {
19741990
for (const node of line) {
1975-
this._stepAstNode(node, null)
1991+
this._stepAstNode(node, vars);
19761992
}
19771993
}
19781994
return ast
@@ -3473,7 +3489,7 @@ class OSLUtils {
34733489

34743490
if (typeof Scratch !== "undefined") {
34753491
Scratch.extensions.register(new OSLUtils());
3476-
} else if (typeof module !== "undefined" && module.exports) {
3492+
} else if (false && typeof module !== "undefined" && module.exports) {
34773493
module.exports = OSLUtils;
34783494
} else {
34793495
let utils = new OSLUtils();

0 commit comments

Comments
 (0)