bordemstirs
Member
Okay, so I'm starting to write the bones of an evaluator (essentially an extension of modifier_eval(); ). My problem is... difficult to explain, or even understand what's causing it.
The string replace() is just because conversion to buffer was... being kind of a hassle, and I had no idea how to clear a buffer once filled with data. (buffer a="" threw an error)
So, when testing with
I'm getting results that I can't really follow.
The first line works as hoped.
The second, somehow, when it takes everything within the outer-most parentheses and evaluates (at this point that being "6.0-1.0") when it comes back down to the first instance of mathlibeval() somehow the "5.0" result is placed after "6.0-" and it executes again.
Adding more parentheses merely repeats the behavior further times.
The print()s and sp parameter were added for debugging, but I'm still at a loss as to where the string is getting extra information.
Code:
string replace(string o, int s, int f, string n){
buffer b;
append(b,o);
replace(b,s,f,n);
return to_string(b);
}
//eval function for strings containing math commands. Very much WIP.
//current restriction, all mathlib functions must be passed with square brackets
float mathlibeval(string x,string sp){
int pdepth;
string inner;
string c;
string n;
int open;
int close;
while (contains_text(x,"(")){
open=0;
pdepth=1;
while(substring(x,open,open+1)!="(")open=open+1;
close=open+1;
if(contains_text(substring(x,open),")"))
while(close<length(x)){
c=substring(x,close,close+1);
if(c=="(") pdepth+=1;
if(c==")") pdepth-=1;
if(pdepth==0) break;
close=close+1;
}
else close=length(x);
inner=substring(x,open+1,close);
print(sp+"B: "+x+" -- "+inner);
n=replace(x,open,close+1,to_string(mathlibeval(inner,sp+"-")));
print(sp+"A: "+n);
x=n;
}
print(sp+"E: "+x);
return modifier_eval(x);
}
So, when testing with
Code:
ash mathlibeval("(2+4)-(0+1)");
ash mathlibeval("((2+4)-(0+1))");
The first line works as hoped.
The second, somehow, when it takes everything within the outer-most parentheses and evaluates (at this point that being "6.0-1.0") when it comes back down to the first instance of mathlibeval() somehow the "5.0" result is placed after "6.0-" and it executes again.
Adding more parentheses merely repeats the behavior further times.
The print()s and sp parameter were added for debugging, but I'm still at a loss as to where the string is getting extra information.
Last edited: