/* Compile the source code using the constructed Abstract Syntax Tree Arguments are the root nodes of - 1) Global declaration AST, 2) Function definitions AST 3) main() function definition AST */ void compile(Tnode *gdeclroot,Tnode *fdefroot,Tnode *mainroot) { error = 0; loc = 0; globalInstall(gdeclroot); if(fdefroot != NULL) { funcSemanticCheck(fdefroot); checkFuncDecl(fdefroot->LINE); funcSemanticCheck(mainroot); } printf("\nRUN - \n"); if(error == 0) { Gallocate(); funcroot = fdefroot; mroot = mainroot; struct Lsymbol *Ltable; Ltable = NULL; interpreter(mainroot,&Ltable); fp = fopen("SIMCode","w"); regcnt = 0; labelcnt = -1; codeGenerate(mainroot->Ptr2); fclose(fp); } }
/* Compile the source code using the constructed Abstract Syntax Tree Arguments are the root nodes of - 1) Global declaration AST, 2) Function definitions AST 3) main() function definition AST */ void compile(Tnode *gdeclroot,Tnode *fdefroot,Tnode *mainroot) { error = 0; locpos = -1; labelcnt = 0; globalInstall(gdeclroot); if(fdefroot != NULL) { funcSemanticCheck(fdefroot); checkFuncDecl(fdefroot->LINE); funcSemanticCheck(mainroot); } printf("\nRUN - \n"); if(error == 0) { module = INTERPRET; Gallocate(); funcroot = fdefroot; mroot = mainroot; struct Lsymbol *Ltable; Ltable = NULL; interpreter(mainroot,&Ltable); get = 0; fre = 0; regcnt = -1; module = CODEGEN; fp = fopen("SIM_Simulator/SIMcode","w"); funcCodeGen(fdefroot); funcCodeGen(mainroot); callToMain(); fclose(fp); } }
/* Install all global declarations (identifiers,functions,arrays) present in source program into a global symbol table */ void globalInstall(Tnode *root) { struct Gsymbol *gnode; ArgStruct *arg; if(root == NULL) return; switch(root->NODETYPE) { case CONTINUE : globalInstall(root->Ptr1); globalInstall(root->Ptr2); break; case DECLSTATEMENT : decnode = root->Ptr1; globalInstall(root->Ptr2); break; case IDFRDECL : case ARRAYDECL : case FUNCDECL : gnode = Glookup(root->NAME); if(gnode != NULL) { if(root->TYPE != gnode->TYPE) printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME); else printf("\nSIL:%d: Error: redeclaration of '%s'",root->LINE,root->NAME); error++; } globalInstall(root->ArgList); if(gnode == NULL) Ginstall(root->NAME,decnode->TYPE,root->VALUE,Arghead); break; case ARGSTATEMENT : argnode = root->Ptr1; entry = 1; globalInstall(root->Ptr2); break; case IDALIASARG : case IDARG : arg = argLookup(root->NAME,Arghead); if(arg != NULL) { if(root->TYPE != arg->TYPE) printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME); else printf("\nSIL:%d: Error: redefinition of parameter '%s'",root->LINE,root->NAME); error++; } else argInstall(root->NAME,argnode->TYPE); break; } }