Arithmetic Expressions
#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:


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