I devised this piece of code after getting especially frustrated with nested additions (damned "(+ (+ (+ (+....") and was greeted with some weird syntactic errors.
Expected functioning:
Here's the code:
It's a long piece of code, making use of mutual recursion, but I've already tested a (somewhat) similar algorithm in C++, and it works fine.
It evaluates the expression (exp) by adding/subtracting the terms (trm), which are calculated by multiplying/dividing all the factors (fct), which are in turn the evaluation of the expression or the number.
Each function inside calc takes an index as an argument and returns a string "(new index) (answer)" which is used by the next function. The only piece of code outside of the function definitions is "echo (at (exp -1) 0)"
This is what I get on startup:
Any ideas?
EDIT: Info:
Expected functioning:
[SELECT ALL] Code:
/calc 20 s o 2 m o 31 a 11 c d 7 c /*Basically means 20 - (2 * (31+11)/7) */
: 8
Here's the code:
[SELECT ALL] Code:
alias calc[
str = $arg1;
alias exp[
alias blah (trm $arg1)
alias ans (at $blah 0)
alias ind (at $blah 1)
alias more 1
while [more][
op = (at $str (+ $ind 1))
if (|| (strcmp "a" $op) (strcmp "s" $op) )[
alias ind (+ 1 $ind)
alias val (trm $ind)
if (strcmp "a" $op) [alias ans (+f $ans (at $val 0)) ][alias ans (-f (at $val 0)) ]
][alias more 0]
]
result (concat $ans $ind)
]
alias trm[
alias blah (fct $arg1)
alias ans (at $blah 0)
alias ind (at $blah 1)
alias more 1
while [more][
op = (at $str (+ $ind 1))
if (|| (strcmp "m" $op) (strcmp "d" $op) )[
alias ind (+ 1 $ind)
alias val (fct $ind)
if (strcmp "m" $op) [alias ans (*f $ans (at $val 0)) ][alias ans (divf (at $val 0)) ]
][alias more 0]
]
result (concat $ans $ind)
]
alias fct[
alias ans 0
alias ind $arg1
if (strcmp (at $str (+ $ind 1) ) "o" )[
alias ind (+ 1 $ind)
alias blah (exp $ind)
alias ans (at $blah 0)
alias ind (at $blah 1)
alias ind (+ 1 $ind)
][alias ans (at $str (+ $ind 1) )
alias ind (+ 1 $ind)
]
result (concat $ans $ind)
]
echo (at (exp -1) 0)
]
It's a long piece of code, making use of mutual recursion, but I've already tested a (somewhat) similar algorithm in C++, and it works fine.
It evaluates the expression (exp) by adding/subtracting the terms (trm), which are calculated by multiplying/dividing all the factors (fct), which are in turn the evaluation of the expression or the number.
Each function inside calc takes an index as an argument and returns a string "(new index) (answer)" which is used by the next function. The only piece of code outside of the function definitions is "echo (at (exp -1) 0)"
This is what I get on startup:
[SELECT ALL] Code:
unknown command: trm
unknown command: ]
unknown command: fct
unknown command: ]
unknown command: ]
unknown command: exp
exp
unknown command: ]
Any ideas?
EDIT: Info:
[SELECT ALL] Code:
o => (
c => )
a => +
s => -
m => *
d => /