// $ANTLR 2.7.7 (2006-11-01): "grammar-as-semant.g" -> "SfalWalker.java"$

import antlr.TreeParser;
import antlr.Token;
import antlr.collections.AST;
import antlr.RecognitionException;
import antlr.ANTLRException;
import antlr.NoViableAltException;
import antlr.MismatchedTokenException;
import antlr.SemanticException;
import antlr.collections.impl.BitSet;
import antlr.ASTPair;
import antlr.collections.impl.ASTArray;


/***************************************************************

	TreeWalker

 ***************************************************************/
public class SfalWalker extends antlr.TreeParser       implements SfalWalkerTokenTypes
 {

  SymbolTable top = null;
  Validator validator = new Validator(true);
  Translator translator = null;
  Debug log = new Debug(false);
  boolean isAnimator = false;
  boolean isIfBlock = false;
  StringBuffer programBuffer = new StringBuffer();
public SfalWalker() {
	tokenNames = _tokenNames;
}

	public final void program(AST _t) throws RecognitionException {
		
		AST program_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
			String s = null;
			/*programBuffer.append("package {\n" +
				"import flash.display.Sprite;\n" +
		"import flash.text.TextField;\n" +
		"import flash.events.Event;\n\n" +
		"public class {0} extends Sprite {\n\n" +
		"public function {0}() {\n"
		);*/
		
		
		try {      // for error handling
			AST __t101 = _t;
			AST tmp1_AST_in = (AST)_t;
			match(_t,PROG);
			_t = _t.getFirstChild();
			
						SymbolTable saved_environment = top; top = new SymbolTable(top); 
						translator = new Translator(top);
					
			{
			_loop103:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_tokenSet_0.member(_t.getType()))) {
					s=stmt(_t);
					_t = _retTree;
				}
				else {
					break _loop103;
				}
				
			} while (true);
			}
			top = saved_environment;
			_t = __t101;
			_t = _t.getNextSibling();
			
				
				programBuffer.append("addEventListener(Event.ENTER_FRAME, animate);");
				programBuffer.append("\n}");
				
				
				programBuffer.append("public function animate(event:Event):void {");
				
				programBuffer.append(translator.translateAnimator());
				
				programBuffer.append("\n}");
				
				// System.out.println("GRAMMAR:TOP: " + top);
				
				programBuffer.append(translator.translateSymbolTable());
				
				
				programBuffer.append("\n} ");
				programBuffer.append("\n} ");
				
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
	}
	
	public final String  stmt(AST _t) throws RecognitionException {
		String s;
		
		AST stmt_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
				s = null;
				String s1=null, s2=null, s3=null; // e=null; 
				Expr e1, e2;
				java.util.LinkedList l = new java.util.LinkedList();
			
		
		try {      // for error handling
			if (_t==null) _t=ASTNULL;
			switch ( _t.getType()) {
			case ASGN:
			{
				AST __t105 = _t;
				AST tmp2_AST_in = (AST)_t;
				match(_t,ASGN);
				_t = _t.getFirstChild();
				e1=expr(_t);
				_t = _retTree;
				e2=expr(_t);
				_t = _retTree;
				_t = __t105;
				_t = _t.getNextSibling();
				
							/* ID = FUNC_CALL | ID | LITERAL | EXPR */
							
							// System.err.println("ASGN: E1: " + e1.toString() + " E2: "  + e2.toString());
							
							//-------------------------------------------------
							// Handle SymbolTable 
							//-------------------------------------------------
							
							if (e2.type.equals("FUNC_CALL")) {
								
								// System.out.println("in symtable already: " + e1.value + ": " + top.get(e1.value));
								
								
								/* local variable declarations */
								boolean shoulddeclare = 
									/* variable e1.value not in symboltable yet */
									(top.get(e1.value) == null)
									
									/* This means we're in a new scope, declare
									local variables if necessary */ 
									&& (top.outer != null); 
									
								/* Map ID to the return type of the function call */
								top.put(e1.value,
									(String) Meta.FUNC_TO_TYPE.get(e2.value), null);
									
								if (shoulddeclare) {
								
									if (!isAnimator) {
										programBuffer.append(translator.translateSymbolTableLocal());
									}
									else {
										translator.buildAnimator(translator.translateSymbolTableLocal());
									}
									
								}
							}
							else {
							
								
								/* 
								Map ID to "type" associated to the node name, e.g. "Number", "Sprite"
								If e2 is an ID, get the type from the SymbolTable
								*/
								
								if (e2.type.equals("ID")) {
									SymbolTable.SymTabEntry t = (SymbolTable.SymTabEntry) top.get(e2.value);
									if (t==null) throw new RecognitionException("undeclared right value ID: " 
										+ e2.value);
								}
								else {
									top.put(e1.value, (String) Meta.NODE_TO_TYPE.get(e2.type), e2.value);
									
									if (!isAnimator) {
										programBuffer.append(translator.translateSymbolTableLocal());
									}
									else {
										translator.buildAnimator(translator.translateSymbolTableLocal());
									}
									
								}
								
								
							}
							
							// System.out.println("GRAMMAR:TOP: " + top);
							
							//-------------------------------------------------
							// END Handle SymbolTable 
							//-------------------------------------------------
							
							if (isIfBlock) {/* do nothing */}
							else {
								if (!isAnimator) {
									programBuffer.append(
									translator.translate(ASGN, e1.toString() + e2.toString()));	
								}
								else {
									translator.buildAnimator(
									translator.translate(ASGN, e1.toString() + e2.toString()));
								}
							}
							
							
							if (!validator.validate(ASGN, e1, e2, top))
						throw new RecognitionException("BAD Assignment: " + e1.error);
							
							
							// return this for if statement
							s = translator.translate(ASGN, e1.toString() + e2.toString());
							log.trace(top);
						
				break;
			}
			case LITERAL_if:
			{
				AST __t106 = _t;
				AST tmp3_AST_in = (AST)_t;
				match(_t,LITERAL_if);
				_t = _t.getFirstChild();
				
								isIfBlock=true;
								StringBuffer ifBuffer = new StringBuffer("if (");
								
				e1=expr(_t);
				_t = _retTree;
				ifBuffer.append(e1).append(") {");
				s2=stmt(_t);
				_t = _retTree;
					ifBuffer.append(s2).append('}');
								if (_t != null) {
									ifBuffer.append(" else {");
								}
							
				{
				if (_t==null) _t=ASTNULL;
				switch ( _t.getType()) {
				case ASGN:
				case BLOCK_STMT:
				case LITERAL_if:
				case LITERAL_animate:
				case LITERAL_loop:
				{
					s3=stmt(_t);
					_t = _retTree;
					break;
				}
				case 3:
				{
					break;
				}
				default:
				{
					throw new NoViableAltException(_t);
				}
				}
				}
				_t = __t106;
				_t = _t.getNextSibling();
				
								/*if (_t != null) {
									ifBuffer.append("}");
								}*/
								
							if (!isAnimator) {
								programBuffer.append(
								ifBuffer.toString());	
							}
							else {
								translator.buildAnimator(
								ifBuffer.toString());
							}
							isIfBlock=false;
							
							
				break;
			}
			case LITERAL_loop:
			{
				AST __t108 = _t;
				AST tmp4_AST_in = (AST)_t;
				match(_t,LITERAL_loop);
				_t = _t.getFirstChild();
				
				l=expr_list(_t);
				_t = _retTree;
				
							
							if (!validator.validateLoop(l))
					throw new RecognitionException("BAD LOOP format");
								
							programBuffer.append(translator.translate(1001, 
								((Expr)l.get(0)).toString() + 
								((Expr)l.get(1)).toString() + 
								((Expr)l.get(2)).toString()));
				s2=stmt(_t);
				_t = _retTree;
				_t = __t108;
				_t = _t.getNextSibling();
				break;
			}
			case BLOCK_STMT:
			{
				AST __t109 = _t;
				AST tmp5_AST_in = (AST)_t;
				match(_t,BLOCK_STMT);
				_t = _t.getFirstChild();
				if (!isAnimator) programBuffer.append("{");
				
							SymbolTable saved_environment = top; 
							top = new SymbolTable(top); 
							/* Update the translator w/ the SymbolTable for the new scope, 
							in case we need to declare local variables */
							translator.symTable=top;
							
				{
				_loop111:
				do {
					if (_t==null) _t=ASTNULL;
					if ((_tokenSet_0.member(_t.getType()))) {
						s=stmt(_t);
						_t = _retTree;
					}
					else {
						break _loop111;
					}
					
				} while (true);
				}
				_t = __t109;
				_t = _t.getNextSibling();
				if (!isAnimator) programBuffer.append("}");
				
							top = saved_environment; 
							/* Update the translator with the saved SymbolTable */
							translator.symTable=top;
							
				break;
			}
			case LITERAL_animate:
			{
				AST __t112 = _t;
				AST tmp6_AST_in = (AST)_t;
				match(_t,LITERAL_animate);
				_t = _t.getFirstChild();
				isAnimator=true;
				s2=stmt(_t);
				_t = _retTree;
				_t = __t112;
				_t = _t.getNextSibling();
				isAnimator=false;
				break;
			}
			default:
			{
				throw new NoViableAltException(_t);
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
		return s;
	}
	
	public final Expr  expr(AST _t) throws RecognitionException {
		Expr e;
		
		AST expr_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		AST num = null;
		AST str = null;
		AST hex = null;
		AST id = null;
		
				e = null;
				String s; 
				Expr a, b; 
				java.util.LinkedList l;
			
		
		try {      // for error handling
			if (_t==null) _t=ASTNULL;
			switch ( _t.getType()) {
			case FUNC_CALL:
			{
				AST __t114 = _t;
				AST tmp7_AST_in = (AST)_t;
				match(_t,FUNC_CALL);
				_t = _t.getFirstChild();
				AST tmp8_AST_in = (AST)_t;
				match(_t,ID);
				_t = _t.getNextSibling();
				l=expr_list(_t);
				_t = _retTree;
				_t = __t114;
				_t = _t.getNextSibling();
				
							e = new Expr("FUNC_CALL");
							e.value = tmp8_AST_in.getText();
							e.values = l;
							
							if (!validator.validate(FUNC_CALL, e, top)) 
								throw new RecognitionException("BAD FUNC_CALL: " + e.value);
						
				break;
			}
			case MULT:
			{
				AST __t115 = _t;
				AST tmp9_AST_in = (AST)_t;
				match(_t,MULT);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t115;
				_t = _t.getNextSibling();
				
							e = new Expr("*");
							e.child1=a;
						e.child2=b;
						
						// System.out.println("MULT: " + e);
							// System.out.println("Top: " + top);
						
							if (!validator.validate(MULT, e, top)) 
								throw new RecognitionException("RE: *: " + e.error);
						
				break;
			}
			case DIV:
			{
				AST __t116 = _t;
				AST tmp10_AST_in = (AST)_t;
				match(_t,DIV);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t116;
				_t = _t.getNextSibling();
				
						e = new Expr("/");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(DIV, e, top)) 
								throw new RecognitionException("RE: /: " + e.error);
						
				break;
			}
			case MOD:
			{
				AST __t117 = _t;
				AST tmp11_AST_in = (AST)_t;
				match(_t,MOD);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t117;
				_t = _t.getNextSibling();
				
						e = new Expr("%");
							e.child1=a;
						e.child2=b;
						if (!validator.validate(MOD, e, top)) 
								throw new RecognitionException("RE: %: " + e.error);
						
				break;
			}
			case PLUS:
			{
				AST __t118 = _t;
				AST tmp12_AST_in = (AST)_t;
				match(_t,PLUS);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t118;
				_t = _t.getNextSibling();
				
						e = new Expr("+");
						e.child1=a;
						e.child2=b;
						if (!validator.validate(PLUS, e, top)) 
								throw new RecognitionException("RE: +: " + e.error);
						
				break;
			}
			case MINUS:
			{
				AST __t119 = _t;
				AST tmp13_AST_in = (AST)_t;
				match(_t,MINUS);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t119;
				_t = _t.getNextSibling();
				
							e = new Expr("-");
							e.child1=a;
						e.child2=b;
							/* if (!validator.validate(MINUS, e, top)) 
								throw new RecognitionException("RE: -: " + e.error); */
						
				break;
			}
			case UMINUS:
			{
				AST __t120 = _t;
				AST tmp14_AST_in = (AST)_t;
				match(_t,UMINUS);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				_t = __t120;
				_t = _t.getNextSibling();
				
							e = new Expr("U-");
							e.child1=a;
							
							/* fix
							if (!validator.validate(UMINUS, e, top)) 
								throw new RecognitionException("RE: U-: " + e.error); */
						
				break;
			}
			case AND:
			{
				AST __t121 = _t;
				AST tmp15_AST_in = (AST)_t;
				match(_t,AND);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t121;
				_t = _t.getNextSibling();
				
							e = new Expr("&&");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(AND, e, top)) 
								throw new RecognitionException("RE: &&: " + e.error);
						
				break;
			}
			case OR:
			{
				AST __t122 = _t;
				AST tmp16_AST_in = (AST)_t;
				match(_t,OR);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t122;
				_t = _t.getNextSibling();
				
							e = new Expr("||");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(OR, e, top)) 
								throw new RecognitionException("RE: ||: " + e.error);
						
				break;
			}
			case GT:
			{
				AST __t123 = _t;
				AST tmp17_AST_in = (AST)_t;
				match(_t,GT);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t123;
				_t = _t.getNextSibling();
				
							e = new Expr(">");
							e.child1=a;
						e.child2=b;
						
						/* fix
							if (!validator.validate(GT, e, top)) 
								throw new RecognitionException("RE: >: " + e.error); */
						
				break;
			}
			case LT:
			{
				AST __t124 = _t;
				AST tmp18_AST_in = (AST)_t;
				match(_t,LT);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t124;
				_t = _t.getNextSibling();
				
							e = new Expr("<");
							e.child1=a;
						e.child2=b;
						/* fix
							if (!validator.validate(LT, e, top)) 
								throw new RecognitionException("RE: <: " + e.error); */
						
				break;
			}
			case EQ:
			{
				AST __t125 = _t;
				AST tmp19_AST_in = (AST)_t;
				match(_t,EQ);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t125;
				_t = _t.getNextSibling();
				
							e = new Expr("==");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(EQ, e, top)) 
								throw new RecognitionException("RE: ==: " + e.error);
						
				break;
			}
			case NEQ:
			{
				AST __t126 = _t;
				AST tmp20_AST_in = (AST)_t;
				match(_t,NEQ);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t126;
				_t = _t.getNextSibling();
				
							e = new Expr("!=");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(NEQ, e, top)) 
								throw new RecognitionException("RE: !=: " + e.error);
						
				break;
			}
			case GE:
			{
				AST __t127 = _t;
				AST tmp21_AST_in = (AST)_t;
				match(_t,GE);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t127;
				_t = _t.getNextSibling();
				
							e = new Expr(">=");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(GE, e, top)) 
								throw new RecognitionException("RE: >=: " + e.error);
						
				break;
			}
			case LE:
			{
				AST __t128 = _t;
				AST tmp22_AST_in = (AST)_t;
				match(_t,LE);
				_t = _t.getFirstChild();
				a=expr(_t);
				_t = _retTree;
				b=expr(_t);
				_t = _retTree;
				_t = __t128;
				_t = _t.getNextSibling();
				
							e = new Expr("<=");
							e.child1=a;
						e.child2=b;
							if (!validator.validate(LE, e, top)) 
								throw new RecognitionException("RE: <=: " + e.error);
						
				break;
			}
			case NUMBER:
			{
				num = (AST)_t;
				match(_t,NUMBER);
				_t = _t.getNextSibling();
				
							e = new Expr("Number");
							e.value=num.getText();
						
				break;
			}
			case STRING:
			{
				str = (AST)_t;
				match(_t,STRING);
				_t = _t.getNextSibling();
				
							e = new Expr("String");
							e.value=str.getText();
						
				break;
			}
			case HEXNUM:
			{
				hex = (AST)_t;
				match(_t,HEXNUM);
				_t = _t.getNextSibling();
				
							e = new Expr("Hex");
							e.value=hex.getText();
						
				break;
			}
			case LITERAL_true:
			{
				AST tmp23_AST_in = (AST)_t;
				match(_t,LITERAL_true);
				_t = _t.getNextSibling();
				
							e = new Expr("true");
							e.value="true";
						
				break;
			}
			case LITERAL_false:
			{
				AST tmp24_AST_in = (AST)_t;
				match(_t,LITERAL_false);
				_t = _t.getNextSibling();
				
							e = new Expr("false");
							e.value = "false";
						
				break;
			}
			case ID:
			{
				id = (AST)_t;
				match(_t,ID);
				_t = _t.getNextSibling();
				
							/* since untyped do this in the ASGN stmt
							s = top.get(id.getText()) == null ? null : id.getText();
							if (s == null) {
								/* don't want to do this with an expression list 
								there, we are only reading the id
								otherwise, we can be declaring it 
								
								top.put(id.getText(), "ID");
							}*/
							
							e = new Expr("ID");
							e.value = id.getText();
							
							/*
							System.out.println("SymbolTable: ");
							System.out.println(top);*/
						
				break;
			}
			default:
			{
				throw new NoViableAltException(_t);
			}
			}
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
		return e;
	}
	
	public final java.util.LinkedList  expr_list(AST _t) throws RecognitionException {
		java.util.LinkedList l;
		
		AST expr_list_AST_in = (_t == ASTNULL) ? null : (AST)_t;
		
			l = new java.util.LinkedList();
			Expr e = null;
		
		
		try {      // for error handling
			AST __t130 = _t;
			AST tmp25_AST_in = (AST)_t;
			match(_t,EXPR_LIST);
			_t = _t.getFirstChild();
			{
			_loop132:
			do {
				if (_t==null) _t=ASTNULL;
				if ((_tokenSet_1.member(_t.getType()))) {
					e=expr(_t);
					_t = _retTree;
					l.addLast(e);
				}
				else {
					break _loop132;
				}
				
			} while (true);
			}
			_t = __t130;
			_t = _t.getNextSibling();
		}
		catch (RecognitionException ex) {
			reportError(ex);
			if (_t!=null) {_t = _t.getNextSibling();}
		}
		_retTree = _t;
		return l;
	}
	
	
	public static final String[] _tokenNames = {
		"<0>",
		"EOF",
		"<2>",
		"NULL_TREE_LOOKAHEAD",
		"ALPHA",
		"DIGIT",
		"HEX",
		"HEXNUM",
		"ID",
		"NUMBER",
		"WS",
		"NL",
		"COMMENT",
		"LPAREN",
		"RPAREN",
		"LBRACE",
		"RBRACE",
		"MULT",
		"PLUS",
		"MINUS",
		"DIV",
		"MOD",
		"SEMI",
		"ASGN",
		"COMMA",
		"GE",
		"LE",
		"GT",
		"LT",
		"EQ",
		"NEQ",
		"OR",
		"AND",
		"STRING",
		"PROG",
		"STATEMENT",
		"BLOCK_STMT",
		"FUNC_CALL",
		"EXPR_LIST",
		"UPLUS",
		"UMINUS",
		"\"if\"",
		"\"else\"",
		"\"animate\"",
		"\"loop\"",
		"\"not\"",
		"LDV",
		"\"true\"",
		"\"false\""
	};
	
	private static final long[] mk_tokenSet_0() {
		long[] data = { 28656030187520L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
	private static final long[] mk_tokenSet_1() {
		long[] data = { 423466566026112L, 0L};
		return data;
	}
	public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
	}
	
