Arithmetic Expressions
#1
I devised this piece of code after getting especially frustrated with nested additions (damned "(+ (+ (+ (+....") and was greeted with some weird syntactic errors.

Expected functioning:
/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:

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:
unknown command: trm
unknown command: ]
unknown command: fct
unknown command: ]
unknown command: ]
unknown command: exp
exp
unknown command: ]

Any ideas?

EDIT: Info:
o => (
c => )
a => +
s => -
m => *
d => /
Thanks given by:


Messages In This Thread
Arithmetic Expressions - by Breeze - 30 Sep 10, 05:49PM
RE: Arithmetic Expressions - by Alien - 30 Sep 10, 05:56PM
RE: Arithmetic Expressions - by Breeze - 30 Sep 10, 07:37PM
RE: Arithmetic Expressions - by tempest - 30 Sep 10, 09:22PM
RE: Arithmetic Expressions - by Breeze - 01 Oct 10, 06:27AM
RE: Arithmetic Expressions - by Breeze - 03 Oct 10, 10:58PM