Pixel-Composer/scripts/string_eval_tree/string_eval_tree.gml

232 lines
5.9 KiB
Plaintext
Raw Normal View History

2023-03-31 06:59:08 +02:00
#region evaluator
2023-05-03 21:42:17 +02:00
enum EXPRESS_TREE_ANIM {
none,
base_value,
animated
}
2023-04-15 14:48:29 +02:00
function __funcTree(symbol, l = noone, r = noone) constructor {
2023-03-31 06:59:08 +02:00
self.symbol = symbol;
self.l = l;
self.r = r;
isFunc = false;
2023-05-03 21:42:17 +02:00
static getVal = function(val, inp = 0, getStr = false) {
2023-04-15 14:48:29 +02:00
if(is_struct(val)) return val.eval();
if(is_real(val)) return val;
var _val = 0;
2023-05-03 21:42:17 +02:00
if(val == "value") _val = inp;
else if(getStr) _val = val;
else _val = nodeGetData(val);
2023-04-15 14:48:29 +02:00
return _val;
}
static _validate = function(val) {
if(is_real(val)) return true;
if(is_struct(val)) return val.validate();
2023-05-03 21:42:17 +02:00
if(val == "value") return true;
if(GLOBAL.inputExist(val)) return true;
2023-04-15 14:48:29 +02:00
var strs = string_splice(val, ".");
2023-05-03 21:42:17 +02:00
if(array_length(strs) < 2) return false;
2023-04-15 14:48:29 +02:00
if(strs[0] == "Project") {
switch(strs[1]) {
case "frame" :
case "frameTotal" :
case "fps" :
return true;
}
return false;
}
var key = strs[0];
2023-05-03 21:42:17 +02:00
return ds_map_exists(NODE_NAME_MAP, key);
}
static validate = function() {
switch(symbol) {
case "|": return _validate(l);
}
return _validate(l) && _validate(r);
}
2023-04-15 14:48:29 +02:00
2023-05-03 21:42:17 +02:00
static _isAnimated = function(val) {
if(is_real(val)) return EXPRESS_TREE_ANIM.none;
if(is_struct(val)) return val._isAnimated();
2023-04-15 14:48:29 +02:00
2023-05-03 21:42:17 +02:00
if(val == "value") return EXPRESS_TREE_ANIM.base_value;
if(GLOBAL.inputExist(val)) {
var _inp = GLOBAL.getInput(val);
if(_inp.is_anim) return EXPRESS_TREE_ANIM.animated;
}
2023-04-15 14:48:29 +02:00
2023-05-03 21:42:17 +02:00
return EXPRESS_TREE_ANIM.none;
2023-04-15 14:48:29 +02:00
}
2023-05-03 21:42:17 +02:00
static isAnimated = function() {
var anim = EXPRESS_TREE_ANIM.none;
anim = max(anim, _isAnimated(l));
if(symbol != "|")
anim = max(anim, _isAnimated(r));
return anim;
2023-04-15 14:48:29 +02:00
}
static eval = function(inp = 0) {
var v1 = getVal(l, inp);
2023-05-03 21:42:17 +02:00
var v2 = getVal(r, inp, symbol == "|");
2023-04-15 14:48:29 +02:00
2023-05-05 08:58:56 +02:00
//print($"{string(v1)} {symbol} {string(v2)}");
//print($"symbol : {symbol}");
//print($"l : {l}");
//print($"r : {r}");
//print("====================");
2023-03-31 06:59:08 +02:00
switch(symbol) {
2023-05-05 08:58:56 +02:00
case "+": return (is_real(v1) && is_real(v2))? v1 + v2 : 0;
case "-": return (is_real(v1) && is_real(v2))? v1 - v2 : 0;
2023-05-22 20:31:55 +02:00
case "_": return is_real(v1)? -v1 : 0;
2023-05-05 08:58:56 +02:00
case "*": return (is_real(v1) && is_real(v2))? v1 * v2 : 0;
case "^": return (is_real(v1) && is_real(v2))? power(v1, v2) : 0;
case "/": return (is_real(v1) && is_real(v2))? v1 / v2 : 0;
2023-05-03 21:42:17 +02:00
case "|":
var val = is_real(v2)? array_safe_get(v1, v2) : 0;
2023-05-03 21:42:17 +02:00
return val;
2023-03-31 06:59:08 +02:00
2023-05-05 08:58:56 +02:00
case "sin" : return is_real(v1)? sin(v1) : 0;
case "cos" : return is_real(v1)? cos(v1) : 0;
case "tan" : return is_real(v1)? tan(v1) : 0;
case "abs" : return is_real(v1)? abs(v1) : 0;
case "round" : return is_real(v1)? round(v1) : 0;
case "ceil" : return is_real(v1)? ceil(v1) : 0;
case "floor" : return is_real(v1)? floor(v1) : 0;
2023-03-31 06:59:08 +02:00
}
2023-04-15 14:48:29 +02:00
return v1;
2023-03-31 06:59:08 +02:00
}
}
2023-04-15 14:48:29 +02:00
function evaluateFunctionTree(fx) {
2023-05-03 21:42:17 +02:00
static __BRACKETS = [ "(", ")", "[", "]" ];
2023-03-31 06:59:08 +02:00
var pres = global.EQUATION_PRES;
var vl = ds_stack_create();
var op = ds_stack_create();
fx = string_replace_all(fx, " ", "");
fx = string_replace_all(fx, "\n", "");
2023-05-03 21:42:17 +02:00
fx = string_replace_all(fx, "[", "|["); //add array accessor symbol arr[i] = arr|[i] = arr | (i)
2023-03-31 06:59:08 +02:00
var len = string_length(fx);
var l = 1;
2023-05-22 20:31:55 +02:00
var ch, cch, _ch;
2023-03-31 06:59:08 +02:00
while(l <= len) {
ch = string_char_at(fx, l);
2023-04-15 14:48:29 +02:00
if(ds_map_exists(pres, ch)) { //symbol is operator
2023-03-31 06:59:08 +02:00
if(ds_stack_empty(op)) ds_stack_push(op, ch);
else {
if(pres[? ch] > pres[? ds_stack_top(op)] || ds_stack_top(op) == "(") ds_stack_push(op, ch);
else {
2023-05-22 20:31:55 +02:00
if(ch == "-" && ds_map_exists(pres, _ch)) ch = "_"; //unary negative
2023-04-15 14:48:29 +02:00
while(pres[? ch] <= pres[? ds_stack_top(op)] && !ds_stack_empty(op))
ds_stack_push(vl, buildFuncTree(ds_stack_pop(op), vl));
2023-03-31 06:59:08 +02:00
ds_stack_push(op, ch);
}
}
l++;
} else if (ch == "(") {
ds_stack_push(op, ch);
l++;
} else if (ch == ")") {
while(ds_stack_top(op) != "(" && !ds_stack_empty(op)) {
2023-04-15 14:48:29 +02:00
ds_stack_push(vl, buildFuncTree(ds_stack_pop(op), vl));
2023-03-31 06:59:08 +02:00
}
2023-05-03 21:42:17 +02:00
ds_stack_pop(op);
l++;
} else if (ch == "[") {
ds_stack_push(op, ch);
l++;
} else if (ch == "]") {
while(ds_stack_top(op) != "[" && !ds_stack_empty(op))
ds_stack_push(vl, buildFuncTree(ds_stack_pop(op), vl));
2023-03-31 06:59:08 +02:00
ds_stack_pop(op);
l++;
} else {
var vsl = "";
while(l <= len) {
cch = string_char_at(fx, l);
2023-05-03 21:42:17 +02:00
if(ds_map_exists(pres, cch) || array_exists(__BRACKETS, cch)) break;
2023-03-31 06:59:08 +02:00
vsl += cch;
l++;
}
if(vsl == "") continue;
if(ds_map_exists(pres, vsl)) {
ds_stack_push(op, vsl);
} else {
switch(vsl) {
2023-04-15 14:48:29 +02:00
case "e": ds_stack_push(vl, 2.71828); break;
case "pi": ds_stack_push(vl, pi); break;
default : ds_stack_push(vl, isNumber(vsl)? toNumber(vsl) : vsl); break;
2023-03-31 06:59:08 +02:00
}
}
}
2023-05-22 20:31:55 +02:00
_ch = ch;
2023-03-31 06:59:08 +02:00
}
2023-04-15 14:48:29 +02:00
while(!ds_stack_empty(op))
ds_stack_push(vl, buildFuncTree(ds_stack_pop(op), vl));
2023-03-31 06:59:08 +02:00
ds_stack_destroy(op);
2023-04-15 14:48:29 +02:00
var tree = ds_stack_empty(vl)? noone : ds_stack_pop(vl)
ds_stack_destroy(vl);
if(is_string(tree))
tree = new __funcTree("", tree);
return tree;
}
function buildFuncTree(operator, vl) {
if(ds_stack_empty(vl)) return noone;
switch(operator) {
2023-05-03 21:42:17 +02:00
case "-": //deal with preceeding megative number -5
if(ds_stack_size(vl) >= 2) {
var _v1 = ds_stack_pop(vl);
var _v2 = ds_stack_pop(vl);
return new __funcTree("-", _v2, _v1);
} else return new __funcTree("-", ds_stack_pop(vl), 0);
2023-05-03 21:42:17 +02:00
case "+": //binary operators
2023-04-15 14:48:29 +02:00
case "*":
case "^":
case "/":
2023-05-03 21:42:17 +02:00
case "|":
if(ds_stack_size(vl) >= 2) {
var _v1 = ds_stack_pop(vl);
var _v2 = ds_stack_pop(vl);
return new __funcTree(operator, _v2, _v1);
}
2023-04-15 14:48:29 +02:00
default: return new __funcTree(operator, ds_stack_pop(vl));
}
return noone;
2023-03-31 06:59:08 +02:00
}
#endregion