Pseudocode:
        method evaluate
                takes tree
                switch
                        tree.token
                case BODY is
                        foreach kid in tree.children
                                evaluate kid
                case If is
                        evaluate tree.children[0]
                        if tree.children[0].result==True then
                                evaluate tree.children[1]
                        else for
                                index := 2
                        while
                                index < tree.children.length
                        loop
                                switch
                                        tree.children[index].token
                                case Elseif is
                                        evaluate tree.chldren[index+1]
                                        if tree.children[index+1].result==True 
then
                                                evaluate tree.children[index+2]
                                        index :+= 3
                                case Else is
                                        evaluate tree.children[index+1]
                                        index :+= 2
                                default
                                        error "internal error: unexpected token 
" tree.children[index].token
                case While is
                        by
                                evaluate tree.children[0]
                        while
                                tree[0].result==True
                        loop
                                evaluate tree.children[1]
                case '=' is
                        evaluate tree.children[1]
                        assign(lookup(tree.children[0]), 
tree.children[1].result)
                        tree.result := tree.children[1].result
                case ID is
                        tree.result := lookup(tree).value
                case '+' is
                        evaluate tree.children[0]
                        evaluate tree.children[1]
                        tree.result := 
tree.children[0].result+tree.children[1].result
                case '<' is
                        evaluate tree.children[0]
                        evaluate tree.children[1]
                        tree.result := 
tree.children[0].result<tree.children[1].result
                case For is
                        # I do not know the syntax of your for loop, but you 
get the idea.

Obviously you need a case for each valid token in the tree, and to take care of 
types if you distinguish Boolean from Integer and so on; but the direct answer 
to the question of how to execute each statement of a BODY node is: simply 
recursively evaluate each child of that node.

On 2011 May 28, at 07:29, David Smith wrote:

> I need to "wrap" a collection of statements as a code body in order 
> to mechanize if, for and while loops.  In my tree generator, this 
> works great as:
> 
> prog:   body EOF! {if($body.tree!=null)
>                        System.out.println($body.tree.toStringTree());}
>         ;
> 
> body    : parts -> ^(BODY parts)
>         ;
> 
> parts:
>         ( (stat
>         | ifStat)  )*
>         ;
> 
> This produces, for example, [I inserted some newlines to make it legible]
> (BODY (= a 3)
>       (= b 4)
>       (if (> a b) (BODY (= a (+ b 1)))
>        elseif (< a b) (BODY (= b (- a b)) (= c 42) (= d 4))
>        else (BODY (= b 4) (= c (- a b)))
>        end)
> )
> I intend the Java code that implements 'if' to receive the "children" 
> bodies as sub-trees and invoke the original tree walker on the 
> appropriate tree. However, I have no idea how the tree walker should 
> parse ^(BODY parts) to execute each statement in that body.
> Any ideas?
> 
>                                 DMS
> 
> David M. Smith http://www.cc.gatech.edu/fac/David.Smith
> Georgia Institute of Technology, College of Computing
> Sent from my ASR-33 Teletype 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address

--
Trevor John Thompson    (425) 246-4023
net: ti...@me.com
Quidquid Latine dictum sit, altum videtur.


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to