protected void Visit(ASTNode node) { if (node == null) return; else if (node is CLASS_DECLARATION) { CLASS_DECLARATION cd = (CLASS_DECLARATION)node; if (cd.name == "__MAIN") return; constructorFound = false; foreach (Statement stmt in cd.stmt_list) Visit(stmt); // if no user defined constructor is found, add one ArrayList modifiers, parameters; StatementList stmt_list; Statement ctorDecl; if (!constructorFound) { modifiers = new ArrayList(); modifiers.Add(PHP.Core.Modifiers.PUBLIC); parameters = new ArrayList(); stmt_list = new StatementList(); ctorDecl = new FUNCTION_DECLARATION(modifiers, false, "__construct", parameters, stmt_list, 0, 0); cd.stmt_list.Add(ctorDecl); } // in every case, add a static constructor modifiers = new ArrayList(); modifiers.Add(PHP.Core.Modifiers.PUBLIC); modifiers.Add(PHP.Core.Modifiers.STATIC); parameters = new ArrayList(); stmt_list = new StatementList(); ctorDecl = new FUNCTION_DECLARATION(modifiers, false, "__constructStatic", parameters, stmt_list, 0, 0); cd.stmt_list.Add(ctorDecl); } else if (node is FUNCTION_DECLARATION) { FUNCTION_DECLARATION fd = (FUNCTION_DECLARATION)node; if (fd.name == "__construct") constructorFound = true; } }
public void RemoveRange(StatementList stmt_list) { foreach (Statement stmt in stmt_list) list.Remove(stmt); }
public void AddRange(StatementList stmt_list) { foreach (Statement stmt in stmt_list) list.Add(stmt); }
public MainMethodVisitor() { // create public class __MAIN cd__MAIN = new CLASS_DECLARATION(PHP.Core.Modifiers.PUBLIC, "__MAIN", null, new StatementList(), 0, 0); // create public function __MAIN() ArrayList modifiers = new ArrayList(); modifiers.Add(PHP.Core.Modifiers.PUBLIC); modifiers.Add(PHP.Core.Modifiers.STATIC); fd__MAIN = new FUNCTION_DECLARATION(modifiers, false, "__MAIN", new ArrayList(), new StatementList(), 0, 0); // add function __MAIN() to class __MAIN cd__MAIN.stmt_list.Add(fd__MAIN); // create modified statement list modifiedStmtList = new StatementList(); }
protected void Visit(ASTNode node) { if (node == null) return; else if (node is CLASS_DECLARATION) { CLASS_DECLARATION cd = (CLASS_DECLARATION)node; foreach (Statement stmt in cd.stmt_list) Visit(stmt); } else if (node is FUNCTION_DECLARATION) { FUNCTION_DECLARATION fd = (FUNCTION_DECLARATION)node; bool returnReached = false; StatementList newStmtList = new StatementList(); // copy each statement to newStmtList until top return is reached foreach (Statement stmt in fd.stmt_list) { if (stmt is RETURN) { newStmtList.Add(stmt); returnReached = true; break; } else newStmtList.Add(stmt); } // if there was no return statement, add one if (!returnReached) newStmtList.Add(new RETURN(null, 0, 0)); // replace statement list of function with new one fd.stmt_list = newStmtList; } }
public void Visit(AST ast) { StatementList originalClassDeclarations = new StatementList(); StatementList reorderedClassDeclarations = new StatementList(); // move class declarations from ast.stmt_list to originalClassDeclarations for (int i = 0; i < ast.stmt_list.Count(); i++) { Statement stmt = ast.stmt_list.Get(i); if (stmt is CLASS_DECLARATION) { ast.stmt_list.Remove(stmt); i--; originalClassDeclarations.Add(stmt); } } // reorder class declarations ArrayList processedInPreviousIteration; ArrayList toBeProcessedInNextIteration = new ArrayList(); // start with classes with no parent specified for (int i = 0; i < originalClassDeclarations.Count(); i++) { CLASS_DECLARATION cd = (CLASS_DECLARATION)originalClassDeclarations.Get(i); if (cd.extends == null) { originalClassDeclarations.Remove(cd); i--; reorderedClassDeclarations.Add(cd); toBeProcessedInNextIteration.Add(cd.name); } } processedInPreviousIteration = toBeProcessedInNextIteration; // in each iteration, add those class delcarations which inherit from class declarations processed in the previous iteration while (originalClassDeclarations.Count() > 0) { toBeProcessedInNextIteration = new ArrayList(); for (int i = 0; i < originalClassDeclarations.Count(); i++) { CLASS_DECLARATION cd = (CLASS_DECLARATION)originalClassDeclarations.Get(i); if (processedInPreviousIteration.Contains(cd.extends)) { originalClassDeclarations.Remove(cd); i--; reorderedClassDeclarations.Add(cd); toBeProcessedInNextIteration.Add(cd.name); } } // if there are still classes left, but nothing happened in this iteration, a parent wasn't declared or there is a cycle in inheritance if (processedInPreviousIteration.Count == 0) Report.Error(100); processedInPreviousIteration = toBeProcessedInNextIteration; } // add reordered class declarations to ast.stmt_list ast.stmt_list.AddRange(reorderedClassDeclarations); }
public AST(StatementList stmt_list) { this.stmt_list = stmt_list; }
public FUNCTION_DECLARATION(ArrayList modifiers, bool return_by_reference, string name, ArrayList parameters, StatementList stmt_list, int line, int column) : base(line, column) { this.modifiers = modifiers; this.return_by_reference = return_by_reference; this.name = name; this.parameters = parameters; this.stmt_list = stmt_list; }
public CLASS_DECLARATION(int modifier, string name, string extends, StatementList stmt_list, int line, int column) : base(line, column) { this.modifier = modifier; this.name = name; this.extends = extends; this.stmt_list = stmt_list; }
public BLOCK(StatementList stmt_list, int line, int column) : base(line, column) { this.stmt_list = stmt_list; }