Skip to content

Commit

Permalink
Update OSL Ast.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Mistium authored Dec 2, 2024
1 parent 68bcaef commit 855d161
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Resources/OSL Ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,30 @@ function tokenise(CODE) {
let letter = 0;
let depth = "";
let brackets = 0;
let b_depth = 0;
let out = [];
let split = [];
let escaped = false;
const len = CODE.length;

while (letter < len) {
depth = CODE[letter];
if (brackets === 0 && !escaped) {
if (depth === "[" || depth === "{") b_depth ++
if (depth === "]" || depth === "}") b_depth --
}
if (depth === '"' && !escaped) {
brackets = 1 - brackets;
out.push('"');
} else if (depth === '\\') {
} else if (depth === '\\' && !escaped) {
escaped = !escaped;
out.push("\\");
} else {
out.push(depth);
escaped = false;
}
if (brackets === 0) {
if (depth === "[" || depth === "{") b_depth ++
if (depth === "]" || depth === "}") b_depth --
}
letter++;

if (brackets === 0 && CODE[letter] === " " && b_depth === 0) {
split.push(out.join(""));
out = [];
Expand All @@ -88,6 +89,11 @@ function tokenise(CODE) {


class OSLComp {

constructor() {
this.regex = /"[^"]+"|{[^}]+}|\[[^\]]+\]|[^."(]*\((?:(?:"[^"]+")*[^.]+)*|\d[\d.]+\d|[^." ]+/g;
}

evalToken(cur) {
if ((cur[0] === "{" && cur[cur.length - 1] === "}") || (cur[0] === "[" && cur[cur.length - 1] === "]")) {
try {
Expand Down Expand Up @@ -172,7 +178,7 @@ function tokenise(CODE) {
return { type: "log", data: cur }
} else if (["|", "&", "<<", ">>", "^^"].indexOf(cur) !== -1) {
return { type: "bit", data: cur }
} else if (cur.indexOf(".") !== -1) {
} else if ((cur.split("(",1)[0] ?? cur).indexOf(".") !== -1) {
let method = cur.match(this.regex)
for (let i = 0; i < method.length; i++) {
method[i] = this.evalToken(method[i])
Expand All @@ -194,7 +200,6 @@ function tokenise(CODE) {

let ast = []
let tokens = autoTokenise(CODE)
console.log(tokens)
for (let i = 0; i < tokens.length; i++) {
const cur = tokens[i]
ast.push(this.evalToken(cur))
Expand Down Expand Up @@ -232,5 +237,5 @@ function tokenise(CODE) {
let comp = new OSLComp()

console.log(JSON.stringify(comp.generateAST({
CODE: "wow = [var * 10]"
CODE: "jn input_name.matchregex(\"/[^.]+\\.[^.]+/gm\") 10"
}), null, 4))

0 comments on commit 855d161

Please sign in to comment.