From 40845c011b1c8034287081c92a669df017c2a5c6 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Thu, 27 Nov 2014 19:59:36 +0100 Subject: [PATCH] Cleanups. 1. move emit() to separate file. 2. move main to .cpp 3. use "namespace" for parser and lexer. 4. remove Type.h --- Monicelli.ll | 23 ++++++++++++----------- Monicelli.y | 1 + Type.h | 13 ------------- emit.c | 18 ++++++++++++++++++ main.c | 41 ----------------------------------------- main.cpp | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 65 deletions(-) delete mode 100644 Type.h create mode 100644 emit.c delete mode 100644 main.c create mode 100644 main.cpp diff --git a/Monicelli.ll b/Monicelli.ll index 81d8125..baca257 100644 --- a/Monicelli.ll +++ b/Monicelli.ll @@ -4,8 +4,9 @@ #include extern int lineNumber; -extern void yyerror(const char *); -extern void meta(const char *); + +void monicelli_error(const char *); +void monicelli_meta(const char *); // Extern void serve? %} %option noyywrap @@ -22,7 +23,7 @@ CHAR [a-zA-Z_] } "#"[^\n]* { - meta(yytext + 1); + monicelli_meta(yytext + 1); } "bituma"[^\n]* {} @@ -34,23 +35,23 @@ CHAR [a-zA-Z_] return RETURN; } "Necchi" { - yylval.typeval = TYPENAME_INT; + monicelli_lval.typeval = TYPENAME_INT; return TYPENAME; } "Mascetti" { - yylval.typeval = TYPENAME_CHAR; + monicelli_lval.typeval = TYPENAME_CHAR; return TYPENAME; } "Perozzi" { - yylval.typeval = TYPENAME_FLOAT; + monicelli_lval.typeval = TYPENAME_FLOAT; return TYPENAME; } "Melandri" { - yylval.typeval = TYPENAME_BOOL; + monicelli_lval.typeval = TYPENAME_BOOL; return TYPENAME; } "Sassaroli" { - yylval.typeval = TYPENAME_DOUBLE; + monicelli_lval.typeval = TYPENAME_DOUBLE; return TYPENAME; } "conte" { @@ -159,17 +160,17 @@ CHAR [a-zA-Z_] [ \t\f\v] {} {CHAR}({DIGIT}|{CHAR})* { - yylval.strval = strdup(yytext); + monicelli_lval.strval = strdup(yytext); return ID; } {DIGIT}+ { - yylval.intval = strtol(yytext, NULL, 10); + monicelli_lval.intval = strtol(yytext, NULL, 10); return NUMBER; } . { - yyerror("Unexpected token"); + monicelli_error("Unexpected token"); return -1; }; diff --git a/Monicelli.y b/Monicelli.y index 0f5b3aa..cd34592 100644 --- a/Monicelli.y +++ b/Monicelli.y @@ -16,6 +16,7 @@ extern int yylex(); double floatval; char *strval; } +%define api.prefix {monicelli_} %token MAIN %token RETURN diff --git a/Type.h b/Type.h deleted file mode 100644 index 03cea9f..0000000 --- a/Type.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef TYPE_H -#define TYPE_H - -typedef enum { - TYPENAME_INT, - TYPENAME_CHAR, - TYPENAME_FLOAT, - TYPENAME_BOOL, - TYPENAME_DOUBLE -} Type; - -#endif - diff --git a/emit.c b/emit.c new file mode 100644 index 0000000..ed1f103 --- /dev/null +++ b/emit.c @@ -0,0 +1,18 @@ +#include +#include +#include + +#define _(...) emit(__VA_ARGS__, NULL); + +void emit(const char *text, ...) { + va_list args; + va_start(args, text); + + const char *t; + for (t = text; t != NULL; t = va_arg(args, const char *)) { + printf("%s", t); + } + + va_end(args); +} + diff --git a/main.c b/main.c deleted file mode 100644 index f963129..0000000 --- a/main.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "Monicelli.tab.h" - -#include -#include -#include - -int lineNumber = 1; - -void yyerror(const char *message) { - fprintf(stderr, "At line %d: %s\n", lineNumber, message); - exit(1); -} - -#define _(...) emit(__VA_ARGS__, NULL); - -void emit(const char *text, ...) { - va_list args; - va_start(args, text); - - const char *t; - for (t = text; t != NULL; t = va_arg(args, const char *)) { - printf("%s", t); - } - - va_end(args); -} - -void meta(const char *text) { - while (text != '\0' && *text == ' ') { - text += 1; - } - fprintf(stderr, "META: %s\n", text); -} - -int main() { -#if YYDEBUG - yydebug = 1; -#endif - return yyparse(); -} - diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a9e6909 --- /dev/null +++ b/main.cpp @@ -0,0 +1,36 @@ +#include "Monicelli.tab.h" + +#include +#include + +using namespace monicelli; + +int lineNumber = 1; +Program *program; + + +void monicelli_error(const char *message) { + std::cerr << "At line " << lineNumber << ": " << message << std::endl; + std::exit(1); +} + +void monicelli_meta(const char *text) { + while (text != '\0' && *text == ' ') { + text += 1; + } + std::cerr << "META: " << text << std::endl; +} + +int main() { +#if YYDEBUG + yydebug = 1; +#endif + + program = new Program(); + monicelli_parse(); + + program->emit(std::cout); + + return 0; +} +