private void program() { #if WRITE_DEBUG_INFO Console.WriteLine("program"); #endif m_functionList = new AST(new Token(Token.TokenType.STATEMENT_LIST, "<FUNCTION_LIST>")); AST statements = statementList(true); statements.addChildFirst(new AST(new Token(Token.TokenType.STATEMENT_LIST, "<GLOBAL_VARIABLE_DEFINITIONS_LIST>"))); m_programAST.addChild(statements); m_programAST.addChild(m_functionList); }
private AST loop() { #if WRITE_DEBUG_INFO Console.WriteLine("loop"); #endif AST loopBlockStatements = new AST(new Token(Token.TokenType.STATEMENT_LIST, "<LOOP_BLOCK_STATEMENTS>")); AST_LoopNode loopTree = new AST_LoopNode(match(Token.TokenType.LOOP)); bool isForeachLoop = false; if (lookAheadType(1) != Token.TokenType.NEW_LINE) { #if WRITE_DEBUG_INFO Console.WriteLine("foreach loop!"); #endif isForeachLoop = true; // __index__ AST_VariableDeclaration loopIndexDeclaration = new AST_VariableDeclaration(new Token(Token.TokenType.VAR_DECLARATION, "<VAR_DECL>"), ReturnValueType.NUMBER, "__index__"); loopBlockStatements.addChild(loopIndexDeclaration); AST_Assignment loopIndexAssignment = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), "__index__"); loopIndexAssignment.addChild(new AST(new TokenWithValue(Token.TokenType.NUMBER, "-1", new ReturnValue(-1.0f)))); loopBlockStatements.addChild(loopIndexAssignment); // match //match(Token.TokenType.PARANTHESIS_LEFT); AST arrayExpression = expression(); //match(Token.TokenType.PARANTHESIS_RIGHT); // __array__ (is a copy of the array to loop over) AST_VariableDeclaration loopArrayDeclaration = new AST_VariableDeclaration(new Token(Token.TokenType.VAR_DECLARATION, "<VAR_DECL>"), ReturnValueType.ARRAY, "__array__"); loopBlockStatements.addChild(loopArrayDeclaration); AST_Assignment loopArrayAssignment = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), "__array__"); if (arrayExpression != null) { loopArrayAssignment.addChild(arrayExpression); } else { throw new Error("Can't understand array expression in loop", Error.ErrorType.SYNTAX, loopArrayAssignment.getToken().LineNr, loopArrayAssignment.getToken().LinePosition); } loopBlockStatements.addChild(loopArrayAssignment); // __indexes__ (holds all the indexes in the array, since it works like a SortedDictionary) // __indexes = getIndexes(__array__) AST_VariableDeclaration indexesDeclaration = new AST_VariableDeclaration(new Token(Token.TokenType.VAR_DECLARATION, "<VAR_DECL>"), ReturnValueType.ARRAY, "__indexes__"); loopBlockStatements.addChild(indexesDeclaration); AST_FunctionCall getArrayIndexes = new AST_FunctionCall(new Token(Token.TokenType.FUNCTION_CALL, "getIndexes")); AST argumentList = new AST(new Token(Token.TokenType.NODE_GROUP, "<ARGUMENT_LIST>")); argumentList.addChild(new Token(Token.TokenType.NAME, "__array__")); getArrayIndexes.addChild(argumentList); AST_Assignment indexesAssignment = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), "__indexes__"); indexesAssignment.addChild(getArrayIndexes); loopBlockStatements.addChild(indexesAssignment); } else { #if WRITE_DEBUG_INFO Console.WriteLine("infinite loop!"); #endif } /* * loopParentTree * __index__-declaration * __array__-declaration & assigment * loop tree * loop body * foreach stuff * rest of statements * goto beginning of loop * * */ allowLineBreak(); AST loopBody = statementList(false); loopBody.addChild(new AST(new Token(Token.TokenType.GOTO_BEGINNING_OF_LOOP, "<GOTO_BEGINNING_OF_LOOP>"))); allowLineBreak(); match(Token.TokenType.BLOCK_END); if (isForeachLoop) { loopBody.addChildFirst(foreachStuff()); } loopTree.addChild(loopBody); loopBlockStatements.addChild(loopTree); AST_LoopBlockNode loopBlock = new AST_LoopBlockNode(new Token(Token.TokenType.LOOP_BLOCK, "<LOOP_BLOCK>")); loopBlock.addChild(loopBlockStatements); return(loopBlock); }