public override int VisitCompileUnit(CCompileUnit node) { m_translatedFile = new CFile(CodeBlockType.CB_FILE, 3, null); m_translatedFile.AddCode("#include <stdio.h>\n", CFile.CB_PREPROCESSOR); m_translatedFile.AddCode("#include <stdlib.h>\n", CFile.CB_PREPROCESSOR); m_parents.Push(m_translatedFile.MMainFunctionDefinition); m_parentContexts.Push(CCFunctionDefinition.CB_FUNCTIONDEFINITION_BODY); // Visit Statements Context and emmit code to main functions foreach (ASTVisitableElement child in node.GetChildrenContext(CCompileUnit.CT_COMPILEUNIT_STATEMENTS)) { Visit(child); } m_parents.Pop(); m_parentContexts.Pop(); m_parents.Push(m_translatedFile); m_parentContexts.Push(CFile.CB_FUNDEFS); // Visit Function Definitions and emmit code to distinct functions foreach (ASTVisitableElement child in node.GetChildrenContext(CCompileUnit.CT_COMPILEUNIT_FUNDEFS)) { Visit(child); } m_parents.Pop(); m_parentContexts.Pop(); return(0); }
public override int VisitFunction(CFunction node) { //1. Create Output File CFile parent = m_parents.Peek() as CFile; CodeContainer repDeclare = new CodeContainer(CodeBlockType.CB_FILE, parent); CCFunctionDefinition rep = new CCFunctionDefinition(CodeBlockType.CB_FUNCTIONDEFINITION, 2, parent); //2. Add Function Definition to the File in the appropriate context parent.AddCode(repDeclare, CFile.CB_GLOBALS); parent.AddCode(rep, CFile.CB_FUNDEFS); m_parents.Push(rep); m_parentContexts.Push(CCFunctionDefinition.CB_FUNCTIONDEFINITION_DECLARATIONS); //3. Assemble the function header CIdentifier id = node.GetChild(CFunction.CT_FNAME, 0) as CIdentifier; m_translatedFile.DeclareFunction(id.M_Name); rep.AddCode("float " + id.M_Name + "(", CCFunctionDefinition.CB_FUNCTIONDEFINITION_DECLARATIONS); repDeclare.AddCode("float " + id.M_Name + "(", CFile.CB_GLOBALS); string last = node.GetChildrenContext(CFunction.CT_FARGS).Last().M_GraphVizName; foreach (ASTElement s in node.GetChildrenContext(CFunction.CT_FARGS)) { repDeclare.AddCode("float " + s.M_Name, CFile.CB_GLOBALS); rep.AddCode("float " + s.M_Name, CCFunctionDefinition.CB_FUNCTIONDEFINITION_DECLARATIONS); if (!s.M_GraphVizName.Equals(last)) { repDeclare.AddCode(", ", CFile.CB_GLOBALS); rep.AddCode(", ", CCFunctionDefinition.CB_FUNCTIONDEFINITION_DECLARATIONS); } parent.DeclareLocalFunvtionVariable(id.M_Name, s.M_Name); } repDeclare.AddCode(");\n", CFile.CB_GLOBALS); rep.AddCode(")", CCFunctionDefinition.CB_FUNCTIONDEFINITION_DECLARATIONS); m_parents.Pop(); m_parentContexts.Pop(); m_parents.Push(rep); m_parentContexts.Push(CCFunctionDefinition.CB_FUNCTIONDEFINITION_BODY); m_functionNames.Push(id.M_Name); foreach (ASTVisitableElement child in node.GetChildrenContext(CFunction.CT_BODY)) { Visit(child); } m_functionNames.Pop(); m_parents.Pop(); m_parentContexts.Pop(); return(0); }