console.c
Description du code
Fonctions d'affichage Compilateur LSD010Code source ou contenu du fichier
Code c (console.c) (706 lignes)
/* * console.c : output (terminal and files) helper file * Part of the compiler project for LSD10 language * Gaudry Stéphane * More information on http://www.gaudry.be/langages-lex-yacc-intro.html */ #include <stdio.h> #include <stdlib.h> #include <string.h> #if(VERBOSE_LEVEL<=DEB_E) #include <errno.h> #endif #if(VERBOSE_LEVEL<=DEB_I) #include <time.h> #endif //includes for the var args #include <stdarg.h> //end of var args includes #include "common.h" #include "graphVizHelper.h" #include "symbolsTableDataRepresentation.h" extern int lexLinesCount; extern char* yytext; extern int *yylineno; extern FILE *yyin; extern AstNode *rootNode; extern DebugInfo *debugInfo; void printHTMLTree(FILE *htmlFile, AstNode *node, int depth); void printHTMLNode(FILE *htmlFile, AstNode *node, int depth); void printXMLTree(FILE *htmlFile, AstNode *node, int depth); void printXMLNode(FILE *htmlFile, AstNode *node, int depth); /**************************************************************/ /** * Generates Yacc error with custom message * todo: 2 function; this is used only to call the yyerror function and producing a exit(EXIT_SUCCESS), * and another to print KO with an exit(EXIT_FAILURE) on compiler inexpected bug (like allocation error) * Exposed method */ void onError(char* errorMsg, char* compilerFile, int compilerLine, AstNode *node) { if(node!=NULL) { lexLinesCount=node->debug->line; //lexCharsCountBeforeToken=node->debug->line; todo : yylloc.first_line?? debugInfo=node->debug; } yytext=NULL; #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_W, errorMsg, compilerFile, compilerLine); #endif yyerror(errorMsg); } void onNotForwardDeclarationError(AstNode *current, AstNode *found) { char errorStr[1024];//todo: minimize length char *str = errorStr; if(current!=found) { str, "Backward declaration failure for %s %s line %d char %d, possible forward declaration on %s %s line %d col %d", typeToString(current->type), current->info->name, current->debug->line, current->debug->linePsn, typeToString(found->type), found->info->name, found->debug->line, found->debug->linePsn ); } else { str, "Backward declaration failure for %s %s line %d col %d", typeToString(current->type), current->info->name, current->debug->line, current->debug->linePsn ); } onError(str, __FILE__, __LINE__, current); } void onNotIntegerNotBooleanTypeError(AstNode *node, char *file, int line) { char errorStr[1024];//todo: minimize length char *str = errorStr; str, "Type check failure : %s or %s expected, but %s found for '%s' line %d col %d (compiler %s, line %d)", typeToString(AST_INTEGER_VAR_TYPE), typeToString(AST_BOOLEAN_VAR_TYPE), typeToString(node->info->computedType), node->info->name, node->debug->line, node->debug->linePsn, file, line ); lexLinesCount=node->debug->line; yytext=NULL; yyerror(str); //failure for the parsed code, but success for the compiler (it must stop here) } /** * Generates Yacc error if a type is not recognized * Exposed method */ void onUnrecognizedTypeError(int type, char* file, int line) { char errorStr[1024];//todo: minimize length onError(errorStr, file, line, NULL); } /** * Print custom message on stdout, depending on the Debug level * Exposed method */ void printMsg(int msgType, char *msg, char *file, int line) { /*if(VERBOSE_LEVEL>msgType)return;*/ switch(msgType) { case DEB_W : printf(";\n;\tWarning : '%s' On %s, Line %d\n;\t-------------------------------------------------------------\n", msg, file, line); break; case DEB_E : printf(";\n;\tError : '%s' On %s, Line %d\n;\t-------------------------------------------------------------\n", msg, file, line); break; case DEB_EXEC : break; default : break; } return; } /******************************************************************************/ void printDebugTree(char *htmlFileName, char *xmlFileName) { FILE * htmlFile; if(htmlFileName==NULL)htmlFileName=AST_HTML_FILE; if(xmlFileName==NULL)xmlFileName=AST_XML_FILE; if(htmlFile!=NULL) { printHTMLTree(htmlFile, rootNode, 0); #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"\t...OK HTML printed", __FILE__, __LINE__); #endif } else { #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"Printing AST into HTML file ... Can't open file", __FILE__, __LINE__); #endif } if(htmlFile!=NULL) { printXMLTree(htmlFile, rootNode, 0); #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"\t...OK XML printed", __FILE__, __LINE__); #endif } else { #if(VERBOSE_LEVEL<=DEB_EXEC) printMsg(DEB_EXEC,"Printing AST into XML file ... Can't open file", __FILE__, __LINE__); #endif } } /** * Generates an HTML file and an XML file with AST nodes. * These files are generated on the current directory. * An error message is displayed if a problem occurs on opening files * Exposed method */ void printTree() { printDebugTree(NULL, NULL); } //alternate row int altRowSTI=0; void printScopeStack(ScopeStack *scopeStack, FILE * htmlFile, int topItem) { if(scopeStack!=NULL) { int altRow=0; altRowSTI=0; if(topItem!=0) { htmlFile, //"<table width=\"100%%\">", "<table width=\"100%%\" border=\"1\" cellpadding=\"2\" cellspacing=\"1\" class=\"table\">" ); htmlFile, "<thead><tr><td colspan=\"5\">Pile du symbole : %s</td></tr>", scopeStack->declarationNode->info->name ); fprintf(htmlFile, "<tr><th>Profondeur</th><th>Portée</th><th>Type</th><th>Ligne</th><th>Col</th></tr>"); } htmlFile, (++altRow%2==0)? "<tr class=\"td\">": "<tr class=\"td2\">" ); htmlFile, "<td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>", scopeStack->declarationNode->info->scopeDepth, scopeStack->declarationNode->info->scopeId, typeToString(scopeStack->declarationNode->info->type), scopeStack->declarationNode->debug->line, scopeStack->declarationNode->debug->linePsn ); printScopeStack(scopeStack->parentPtr, htmlFile, 0); if(topItem!=0) { } } } int printSymbolsTableHeader() { FILE * htmlFile; char *htmlFileName=SYMBOLSTABLE_HTML_FILE; if(htmlFile==NULL) { return EXIT_FAILURE; } return EXIT_SUCCESS; } int printSymbolsTableFooter() { //printSymbolsTableItem(); FILE * htmlFile; //time_t genTime = time(NULL); char *htmlFileName=SYMBOLSTABLE_HTML_FILE; if(htmlFile==NULL) { return EXIT_FAILURE; } return EXIT_SUCCESS; } // first null index int startNull = INITIAL_INT; // @todo: define a astnodedebug as parameter to show where // this scope enter/exit was found int printSymbolsTableItem(char *title) { if(declarations!=NULL) { FILE * htmlFile; //time_t genTime = time(NULL); char *htmlFileName=SYMBOLSTABLE_HTML_FILE; if(htmlFile!=NULL) { fprintf(htmlFile, "<?php \n?><h2>%s (portée actuelle : %d)</h2>", title, scopeHelperGetCurrentScope()); //fprintf(htmlFile, "<table width=\"100%%\" border=\"1\"><thead>"); fprintf(htmlFile, "<?php \n?><table width=\"100%%\" border=\"1\" cellpadding=\"2\" cellspacing=\"1\" class=\"table\"><thead>"); fprintf(htmlFile, "<tr><td>Index</td><td>Liste chaînée de <pre>SymTableEntry</pre></td></tr>"); int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]==NULL) { if(startNull==INITIAL_INT) { startNull=i; } } else { if(startNull!=-INITIAL_INT) { if(startNull<-i-1) { } else { } startNull = INITIAL_INT; } SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeStack(entry->scopeStack, htmlFile, 1); entry = entry->next; } } } if(startNull!=INITIAL_INT) { if(startNull<i-1) { } else { } startNull = INITIAL_INT; } return EXIT_SUCCESS; } } else { printMsg(DEB_EXEC,"-------------------------Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } char *variableUsageToString(VariableUsage usage) { switch(usage) { case VAR_USAGE_NEVER: return "n'est jamais utilisée"; break; case VAR_USAGE_SOMETIMES: return "est parfois utilisée"; break; case VAR_USAGE_ALWAYS: return "est toujours utilisée"; break; default: return "[Erreur (valeur hors norme)]"; } } void printScopeUsage(ScopeStack *scopeStack) { if(scopeStack!=NULL) { "; %s(...) : la %s '%s' %s\n", scopeStack->functionNode==NULL?"Program":scopeStack->functionNode->info->name, scopeStack->declarationNode->type==NODE_TYPE_FUNCTION?"fonction":"variable", scopeStack->declarationNode->info->name, variableUsageToString(scopeStack->usage) ); printScopeUsage(scopeStack->parentPtr); } } int printSymbolsUsage() { if(declarations!=NULL) { int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]!=NULL) { SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeUsage(entry->scopeStack); entry = entry->next; } } } return EXIT_SUCCESS; } else { printMsg(DEB_EXEC,"Can't print it: Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } void printScopeDebug(ScopeStack *scopeStack, int tableId, int stackId) { if(scopeStack!=NULL) { "\n; Table[%3d][%2d]= %7s %10s line %3d, col %3d; scope %d; %s function, ", tableId, stackId, typeToString(scopeStack->declarationNode->info->type), scopeStack->declarationNode->info->name, scopeStack->declarationNode->debug->line, scopeStack->declarationNode->debug->linePsn, scopeStack->declarationNode->info->scopeId, scopeStack->functionNode==NULL?"No ":scopeStack->functionNode->info->name ); printScopeDebug(scopeStack->parentPtr, tableId, ++stackId); } } int printSymbolsTableDebug(char*file, int line) { printMsg(DEB_EXEC,"-------------------------Symbols table state", file, line); if(declarations!=NULL) { int i; for(i=0;i<TABLES_SIZE;i++) { if(declarations[i]!=NULL) { SymTableEntry *entry = declarations[i]; while(entry!=NULL) { printScopeDebug(entry->scopeStack, i, 0); entry = entry->next; } } } return EXIT_SUCCESS; } else { printMsg(DEB_EXEC,"-------------------------Symbols table is null", __FILE__, __LINE__); } return EXIT_FAILURE; } /******************************************************************************/ /** * Prints an AST node into the html file * Internal business */ void printHTMLNode(FILE *file, AstNode *node, int depth) { if(node!=NULL) { if(node->info!=NULL) { file, "<br /><b>Type :</b> %s <br /><b>Nom :</b> %s<br /><b>Valeur :</b> %d\n", typeToString(node->info->type), node->info->name, node->info->value ); while(node->rightBrother!=NULL) { printNode(file, node->rightBrother, depth); }*/ } /** * Prints the AST into the html file * Internal business */ void printHTMLTree(FILE *file, AstNode *node, int depth) { if(node!=NULL) { printHTMLNode(file, node, depth); printHTMLTree(file, node->left, depth+1); printHTMLTree(file, node->right, depth+1); }/** * Prints an AST node into the xml file * Internal business */ void printXMLNode(FILE *file, AstNode *node, int depth) { if(node!=NULL) { if(node->parent!=NULL) { fprintf(file, "<parent><type>%s</type><pmemaddress>%p</pmemaddress></parent>", typeToString(node->parent->type), node->parent); }else { } if(node->info!=NULL) { file, "<info><rightmemaddress>%p</rightmemaddress><leftmemaddress>%p</leftmemaddress><infotype>%s</infotype><infoname>%s</infoname><infoval>%d</infoval></info>\n", node->right, node->left, typeToString(node->info->type), node->info->name, node->info->value, depth ); } } }/** * Prints the AST into the xml file * Internal business */ void printXMLTree(FILE *file, AstNode *node, int depth) { printXMLNode(file, node, depth); if(node!=NULL) { printXMLTree(file, node->left, depth+1); printXMLTree(file, node->right, depth+1); } } /******************************************************************************/ /** * Returns a human readable string from a given type constant value * Expected : one of the DEB_xxx constants * Exposed method */ char* debugLevelToString(int type) { char *str = NULL; switch(type) { case DEB_L: str = "Lex tokens"; break; case DEB_Y: str = "Yacc parsing"; break; case DEB_O: str = "Misc files"; break; case DEB_I: str = "Information messages"; break; case DEB_P: str = "P-code generation"; break; case DEB_W: str = "Warning messages"; break; case DEB_EXEC: str = "Minimum excution messages"; break; case DEB_E: str = "Error messages"; break; case DEB_NONE: str = "Silent (no other output than p-code, and OK or KO)"; break; default: str = "undefined"; #if(VERBOSE_LEVEL<=DEB_W) char str[1024];//todo: minimize length printMsg(DEB_W, str, __FILE__, __LINE__); #endif break; } return str; } /******************************************************************************/ /** * Generates p-code output on stdout, * and (if GENERATE_PCODE_FILE is set to 1) write it into a file * source http://www.pps.jussieu.fr/~rifflet/enseignements/LC4/vararg.html * Only one digit format allowed (examples %s, %d, %f, %l) */ void printPCode(FILE* pcodeFile, char *format, ...) { if(PCODE_GENERATION_BYPASS) { return; } va_list p_list; int i; long l; double f; char *s; int validFormat; validFormat=0; #if(VERBOSE_LEVEL==DEB_NONE) if(*format!=';') { #endif while (*format != 0) { switch(*format){ case '%' : validFormat=1; break; case 'd': /* type int */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; case 'l': /* type long */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; case 'f' : /* type double */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; case 's' : /* type char[] */ if(validFormat==1) { if(pcodeFile!=NULL){ } } break; // case 'c' : /* type char * */ // if(validFormat==1) // { // s = va_arg(p_list, char *); // if(pcodeFile!=NULL){ // fprintf(pcodeFile, "%s", s); // } // printf("%s", s); // } // break; default : /* type char[] */ validFormat=0; break; } if(validFormat==0) { if(pcodeFile!=NULL){ } } format++; } #if(VERBOSE_LEVEL==DEB_NONE) } #endif }
Autres extraits de codes en c
- DisquetteDispo Vérifier la disponibilité du lecteur de disquette
- Suite de Fibonacci Exemple d'itération en C
- Suite de Fibonacci Exemple de récursion en C
- astDataRepresentation.h Représentation de données de l'arbre syntaxique abstrait Compilateur LSD010
- ast.h Arbre syntaxique abstrait Compilateur LSD010
- ast.c Arbre syntaxique abstrait Compilateur LSD010
- symbolsTableDataRepresentation.h Représentation de données de la table des symboles Compilateur LSD010
- symbolsTable.h Fonctions de gestion de la table des symboles Compilateur LSD010
- symbolsTable.c Fonctions de gestion de la table des symboles Compilateur LSD010
- hashCode.h Fonctions de hachage Compilateur LSD010
- hashCode.c Fonctions de hachage Compilateur LSD010
- scopeStack.h Fonctions de gestion d'une pile de portées Compilateur LSD010
- scopeStack.c Fonctions de gestion d'une pile de portées Compilateur LSD010
- scopeHelper.h Fonctions de gestion de la portée courante Compilateur LSD010
- console.h Fonctions d'affichage Compilateur LSD010
- console.c Fonctions d'affichage Compilateur LSD010
- graphVizHelper.h Génération d'une image d'un arbre syntaxique abstrait.
Classe d'intégration de l'outil GraphViz. Compilateur LSD010 - graphVizHelper.c Génération d'une image d'un arbre syntaxique abstrait.
Classe d'intégration de l'outil GraphViz. Compilateur LSD010 - common.h Définition des constantes et variables communes Compilateur LSD010
- pcode.c Génération de p-code Compilateur LSD010
- pcode.h Génération de p-code Compilateur LSD010
- Tous les extraits
Version en cache
21/11/2024 11:50:24 Cette version de la page est en cache (à la date du 21/11/2024 11:50:24) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 05/10/2009, dernière modification le 28/10/2018
Source du document imprimé : https://www.gaudry.be/sniplet-rf-lsd010/project/source/console.c.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.