adalogo.visitor

Class InterpreterVisitor

Implemented Interfaces:
LangVisitor

public class InterpreterVisitor
extends java.lang.Object
implements LangVisitor

This is the heart of the interpreter in AdaLogo. After using Lang.jjt with JavaCC-3.2 a nice parse tree was built, so it "should be not a big thing" to interpret the building tree. ;)

Nested Class Summary

Constructor Summary

InterpreterVisitor(Engine engine, VisitorMaster master, SimpleNode node)
create and start an interpreter.

Method Summary

boolean
getExecute()
Get execute.
void
setExecute(boolean b)
Set the interpreter to execute the code or only to check the semantic.
Object
visit(ASTAdditionNode node, Object data)
Take the 1st and the 2nd child and add
Object
visit(ASTAndNode node, Object data)
OrNode has 2 children.
Object
visit(ASTAssignmentIdentifier node, Object data)
everything will be done by parent AssignmentStatement
Object
visit(ASTAssignmentStatement node, Object data)
Assignment of BooleanExpression or IntegerExpression to a defining variable.
Object
visit(ASTBooleanDeclarationNode node, Object data)
"DummyNode" - the parent nodes do the work....
Object
visit(ASTCompilationUnit node, Object data)
The begin of every program.
Object
visit(ASTDashNode node, Object data)
Take the 1st child and reverse.
Object
visit(ASTDeclaration node, Object data)
DeclarationPart of every Procedure If the child is a ProcedureDeclaration, so do it in the symtab and do not execute ProcedureDeclaration.
Object
visit(ASTDivisionNode node, Object data)
Take the 1st and the 2nd child and div
Object
visit(ASTElsePart node, Object data)
Just do what ElsePart want to be done, without checking.
Object
visit(ASTElsifPart node, Object data)
Just do what ElsifPart want to be done, without checking.
Object
visit(ASTEqualNode node, Object data)
done by helpRelationalExpression
Object
visit(ASTExitStatement node, Object data)
throws an ExitStatementException.
Object
visit(ASTFalseNode node, Object data)
turn back false
Object
visit(ASTForIdentifier node, Object data)
Will be done in ForStatement
Object
visit(ASTForReverse node, Object data)
"Dummy"-Node
Object
visit(ASTForStatement node, Object data)
A ForStatement.
Object
visit(ASTForwardStatement node, Object data)
Execute ForwardStatement from Turtle.
Object
visit(ASTGetXExpression node, Object data)
Turn back the x-position of the turtle
Object
visit(ASTGetYExpression node, Object data)
Turn back the y-position of the turtle
Object
visit(ASTGreaterEqualNode node, Object data)
done by helpRelationalExpression
Object
visit(ASTGreaterThanNode node, Object data)
done by helpRelationalExpression
Object
visit(ASTIdentifier node, Object data)
Look in the symtab and return.
Object
visit(ASTIfStatement node, Object data)
check all the booleanPart of children (+ elsif) and at least do else.
Object
visit(ASTIntegerDeclarationNode node, Object data)
"DummyNode" - the parent nodes do the work....
Object
visit(ASTIntegerLiteral node, Object data)
Look in the SimpleNode and return getValue()
Object
visit(ASTJumpToStatement node, Object data)
Let Turtle jump to point (x,y);
Object
visit(ASTLessEqualNode node, Object data)
done by helpRelationalExpression
Object
visit(ASTLessThanNode node, Object data)
done by helpRelationalExpression
Object
visit(ASTLoopStatement node, Object data)
Loop till the inphinity.
Object
visit(ASTMaxExpression node, Object data)
This function becomes 2 IntegerExpression and turn the bigger one back
Object
visit(ASTMinExpression node, Object data)
This function becomes 2 IntegerExpression and turn the smaller one back
Object
visit(ASTModNode node, Object data)
Take the 1st and the 2nd child and mod
Object
visit(ASTMultiplicationNode node, Object data)
Take the 1st and the 2nd child and mult
Object
visit(ASTNewLineStatement node, Object data)
new_line; put a new line to the console.
Object
visit(ASTNotEqualNode node, Object data)
done by helpRelationalExpression
Object
visit(ASTNotNode node, Object data)
NotNode has 1 child.
Object
visit(ASTNullStatement node, Object data)
No children, do nothing.
Object
visit(ASTOrNode node, Object data)
OrNode has 2 children.
Object
visit(ASTPenDownStatement node, Object data)
Turtle.penDown();
Object
visit(ASTPenUpStatement node, Object data)
Turtle.penUp();
Object
visit(ASTProcedureCallStatement node, Object data)
Look into the symtab and copy the procedure node out and excecute.
Object
visit(ASTProcedureCallStatementIdentifier node, Object data)
"DummyNode" - the parent nodes do the work....
Object
visit(ASTProcedureCallStatementParameters node, Object data)
Object
visit(ASTProcedureDeclaration node, Object data)
Go down __without__ Identifier and Parameters.
Object
visit(ASTProcedureDeclarationIdentifier node, Object data)
"DummyNode" - the parent nodes do the work....
Object
visit(ASTProcedureDeclarationParameters node, Object data)
"Dummy-node", everything should be done by parent node ProcedureDeclaration
Object
visit(ASTPutLineStatement node, Object data)
This will put a line with result of BooleanExpression, IntegerExpression or StringLiteral.
Object
visit(ASTPutStatement node, Object data)
This will put the result of BooleanExpression, IntegerExpression or StringLiteral.
Object
visit(ASTRandomExpression node, Object data)
RandomExpression get 2 IntegerExpression and return a value between this to ranges, inclusive the smaller range, exclusive the bigger range.
Object
visit(ASTRemNode node, Object data)
Take the 1st and the 2nd child and rem
Object
visit(ASTResetTurtleStatement node, Object data)
Reset turtle to point(0,0); The direction of the turtle is -90
Object
visit(ASTSemi node, Object data)
This should be ok.
Object
visit(ASTSequenceOfStatement node, Object data)
Visit every child and execute
Object
visit(ASTStringLiteral node, Object data)
return "String" (Object).
Object
visit(ASTSubtractionNode node, Object data)
Take the 1st and the 2nd child and sub
Object
visit(ASTTrueNode node, Object data)
turn back true
Object
visit(ASTTurnStatement node, Object data)
Turtle.turn();
Object
visit(ASTTurnToStatement node, Object data)
This procedure let the turtle turn to the degree you want.
Object
visit(ASTVariableDeclaration node, Object data)
In the Declaration a boolean/integer can de declare with or without an assigment of BooleanExpression.
Object
visit(ASTVariableDeclarationIdentifier node, Object data)
"DummyNode" - the parent nodes do the work....
Object
visit(ASTWhileStatement node, Object data)
WhileStatement has 2 children.
Object
visit(SimpleNode node, Object data)
Do nothing.

Constructor Details

InterpreterVisitor

public InterpreterVisitor(Engine engine,
                          VisitorMaster master,
                          SimpleNode node)
create and start an interpreter.

Parameters:
engine -
master - the thread for this interpeter (for step by step).
node - the AST node which the interpreter should interpret.

Method Details

getExecute

public boolean getExecute()
Get execute. Execute tells you, if the interpreter will execute the code or only check the semantic.


setExecute

public void setExecute(boolean b)
Set the interpreter to execute the code or only to check the semantic.


visit

public Object visit(ASTAdditionNode node,
                    Object data)
Take the 1st and the 2nd child and add
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTAndNode node,
                    Object data)
OrNode has 2 children. Go down and operate And to the 2 children.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTAssignmentIdentifier node,
                    Object data)
everything will be done by parent AssignmentStatement
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTAssignmentStatement node,
                    Object data)
Assignment of BooleanExpression or IntegerExpression to a defining variable. If the variable not exists -> throws Exception At this place we have also to solve the ambiguity of Identifier.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTBooleanDeclarationNode node,
                    Object data)
"DummyNode" - the parent nodes do the work....
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTCompilationUnit node,
                    Object data)
The begin of every program. Take the children and go down.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTDashNode node,
                    Object data)
Take the 1st child and reverse.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTDeclaration node,
                    Object data)
DeclarationPart of every Procedure If the child is a ProcedureDeclaration, so do it in the symtab and do not execute ProcedureDeclaration. otherwise the children do the work. see: Lang.jjt or Lang.jjt.html
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTDivisionNode node,
                    Object data)
Take the 1st and the 2nd child and div
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTElsePart node,
                    Object data)
Just do what ElsePart want to be done, without checking. IfStatement do the work
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTElsifPart node,
                    Object data)
Just do what ElsifPart want to be done, without checking. IfStatement do the work.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTEqualNode node,
                    Object data)
done by helpRelationalExpression
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTExitStatement node,
                    Object data)
throws an ExitStatementException. This will be catched by For-, While- or LoopStatement
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTFalseNode node,
                    Object data)
turn back false
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTForIdentifier node,
                    Object data)
Will be done in ForStatement
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTForReverse node,
                    Object data)
"Dummy"-Node
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTForStatement node,
                    Object data)
A ForStatement. Evaluate the 2 ranges and loop, with or without reverse. Execute SequenceOfStatement in the loop.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTForwardStatement node,
                    Object data)
Execute ForwardStatement from Turtle.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTGetXExpression node,
                    Object data)
Turn back the x-position of the turtle
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTGetYExpression node,
                    Object data)
Turn back the y-position of the turtle
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTGreaterEqualNode node,
                    Object data)
done by helpRelationalExpression
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTGreaterThanNode node,
                    Object data)
done by helpRelationalExpression
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTIdentifier node,
                    Object data)
Look in the symtab and return.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTIfStatement node,
                    Object data)
check all the booleanPart of children (+ elsif) and at least do else. and select one posibility to do.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTIntegerDeclarationNode node,
                    Object data)
"DummyNode" - the parent nodes do the work....
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTIntegerLiteral node,
                    Object data)
Look in the SimpleNode and return getValue()
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTJumpToStatement node,
                    Object data)
Let Turtle jump to point (x,y);
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTLessEqualNode node,
                    Object data)
done by helpRelationalExpression
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTLessThanNode node,
                    Object data)
done by helpRelationalExpression
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTLoopStatement node,
                    Object data)
Loop till the inphinity. Or if there is a ExitStatement which throws ExitStatementException, then go out.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTMaxExpression node,
                    Object data)
This function becomes 2 IntegerExpression and turn the bigger one back
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTMinExpression node,
                    Object data)
This function becomes 2 IntegerExpression and turn the smaller one back
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTModNode node,
                    Object data)
Take the 1st and the 2nd child and mod
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTMultiplicationNode node,
                    Object data)
Take the 1st and the 2nd child and mult
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTNewLineStatement node,
                    Object data)
new_line; put a new line to the console.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTNotEqualNode node,
                    Object data)
done by helpRelationalExpression
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTNotNode node,
                    Object data)
NotNode has 1 child. Go down and return the reverse result.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTNullStatement node,
                    Object data)
No children, do nothing.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTOrNode node,
                    Object data)
OrNode has 2 children. Go down and operate Or to the 2 children.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTPenDownStatement node,
                    Object data)
Turtle.penDown();
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTPenUpStatement node,
                    Object data)
Turtle.penUp();
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTProcedureCallStatement node,
                    Object data)
Look into the symtab and copy the procedure node out and excecute. If there are parameters, so checked the length and the type of the parameters and execute the procedure node with this parameters, other wise throws exception.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTProcedureCallStatementIdentifier node,
                    Object data)
"DummyNode" - the parent nodes do the work....
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTProcedureCallStatementParameters node,
                    Object data)
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTProcedureDeclaration node,
                    Object data)
Go down __without__ Identifier and Parameters. Identifier and Parameters are done by CompilationUnit or ProcedureCallStatement
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTProcedureDeclarationIdentifier node,
                    Object data)
"DummyNode" - the parent nodes do the work....
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTProcedureDeclarationParameters node,
                    Object data)
"Dummy-node", everything should be done by parent node ProcedureDeclaration
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTPutLineStatement node,
                    Object data)
This will put a line with result of BooleanExpression, IntegerExpression or StringLiteral. Uses helpPutOrPutLineStatement
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTPutStatement node,
                    Object data)
This will put the result of BooleanExpression, IntegerExpression or StringLiteral. Uses helpPutOrPutLineStatement
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTRandomExpression node,
                    Object data)
RandomExpression get 2 IntegerExpression and return a value between this to ranges, inclusive the smaller range, exclusive the bigger range. It does not matter which range is bigger.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTRemNode node,
                    Object data)
Take the 1st and the 2nd child and rem
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTResetTurtleStatement node,
                    Object data)
Reset turtle to point(0,0); The direction of the turtle is -90
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTSemi node,
                    Object data)
This should be ok. Do nothing. Increments ProgramCounter. Thread Highlighting the line in editor.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTSequenceOfStatement node,
                    Object data)
Visit every child and execute
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTStringLiteral node,
                    Object data)
return "String" (Object).
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTSubtractionNode node,
                    Object data)
Take the 1st and the 2nd child and sub
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTTrueNode node,
                    Object data)
turn back true
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTTurnStatement node,
                    Object data)
Turtle.turn();
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTTurnToStatement node,
                    Object data)
This procedure let the turtle turn to the degree you want. It get an IntegerExpression as value
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTVariableDeclaration node,
                    Object data)
In the Declaration a boolean/integer can de declare with or without an assigment of BooleanExpression. If there is no assigment directly, a random value is put in the symtab.
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTVariableDeclarationIdentifier node,
                    Object data)
"DummyNode" - the parent nodes do the work....
Specified by:
visit in interface LangVisitor


visit

public Object visit(ASTWhileStatement node,
                    Object data)
WhileStatement has 2 children. Check the 1st child, if true then execute the 2nd child and execute this node again.
Specified by:
visit in interface LangVisitor


visit

public Object visit(SimpleNode node,
                    Object data)
Do nothing. this should never have been called.
Specified by:
visit in interface LangVisitor