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:
#2
you need switch thinking to functional coding...
Thanks given by:
#3
Erm... The entire thing is in functions. Except for one line. Which is debug anyway (should have been a return and not an echo statement).
Thanks given by:
#4
Whitespaces are crucial in CubeScript!
alias exp[
must be
alias exp [
and so on.
Thanks given by:
#5
Wow, thanks, it cleared them all out! I've got some logical errors though; will post a corrected version soon.
Thanks given by:
#6
Sorry for the delay, got lost in Minecraft logic circuits :P (Awesome game btw)

I managed to fix most of the bugs, turned out most of them were because variables in CS can be accessed outside of their scope. Spent a good hour debugging before I realized that o_O

alias calc         [
    str = $arg1

    alias exp     [
        alias blah (trm $arg1)
        alias anse (at $blah 0)
        alias ind (at $blah 1)
        alias moree 1

        while [moree]        [
            ope = (at $str (+ $ind 1))
            if (|| (strcmp "+" $ope) (strcmp "-" $ope) )            [
                alias ind (+ 1 $ind)
                alias vale (trm $ind)
                alias ind (at $vale 1)
                if (strcmp "+" $ope) [alias anse (+f $anse (at $vale 0)) ]                [alias anse (-f $anse (at $vale 0)) ]
            ]            [alias moree 0]
        ]
        result (concat $anse $ind)
    ]

    alias trm    [
        alias blah (fct $arg1)
        alias anst (at $blah 0)
        alias ind (at $blah 1)
        alias moret 1

        while [moret]        [
            opt = (at $str (+ $ind 1))
            if (|| (strcmp "*" $opt) (strcmp "/" $opt) )            [
                alias ind (+ 1 $ind)
                alias valt (fct $ind)
                alias ind (at $valt 1)
                if (strcmp "*" $opt) [alias anst (*f $anst (at $valt 0)) ]                [alias anst (divf $anst (at $valt 0)) ]
            ]            [alias moret 0]
        ]
        result (concat $anst $ind)
    ]

    alias fct    [
        alias ansf 0
        alias ind $arg1

        if (strcmp (at $str (+ $ind 1) ) "[" )        [
            alias ind (+ 1 $ind)

            alias blah (exp $ind)
            alias ansf (at $blah 0)
            alias ind (at $blah 1)

            alias ind (+ 1 $ind)
        ]        [
            alias ansf (at $str (+ $ind 1) )
            alias ind (+ 1 $ind)
        ]
        result (concat $ansf $ind)
    ]

    echo (at (exp -1) 0)
]

It works pretty well, except for the brackets.

When I type:
{ a + b } * c
It gives me the correct output, but when I type:
c * { a + b }
It gives me b/(a-b) as the answer (judging by multiple trials, at least).

I'm working on this now, but it's still usable if there's at most one bracket on the left-most side and none anywhere else. Also, it can be used as a function - just change the last echo statement into a result statement. This means I don't have to use those bloody nested operations anymore :D

PS. Excuse the weird indenting, it was typed in a C++ IDE and all '{'/'}' were then replaced by '['/']' later. I was too lazy to remove the tabs.

PPS. Notice I changed the operations to their actual signs, except for brackets which cause a "Missing )" problem.

PPPS. Don't try to break the program just yet, it's still in development, and loopholes will hopefully be fixed soon. Also, keep note: All operators/numbers should be kept at least one space apart.
Thanks given by: