Cleanups.

1. move emit() to separate file.
2. move main to .cpp
3. use "namespace" for parser and lexer.
4. remove Type.h
This commit is contained in:
Stefano Sanfilippo 2014-11-27 19:59:36 +01:00
parent c9b53a7470
commit 40845c011b
6 changed files with 67 additions and 65 deletions

View File

@ -4,8 +4,9 @@
#include <stdio.h>
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_]
<INITIAL,shift>[ \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;
}
<INITIAL,shift>. {
yyerror("Unexpected token");
monicelli_error("Unexpected token");
return -1;
};

View File

@ -16,6 +16,7 @@ extern int yylex();
double floatval;
char *strval;
}
%define api.prefix {monicelli_}
%token MAIN
%token RETURN

13
Type.h
View File

@ -1,13 +0,0 @@
#ifndef TYPE_H
#define TYPE_H
typedef enum {
TYPENAME_INT,
TYPENAME_CHAR,
TYPENAME_FLOAT,
TYPENAME_BOOL,
TYPENAME_DOUBLE
} Type;
#endif

18
emit.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#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);
}

41
main.c
View File

@ -1,41 +0,0 @@
#include "Monicelli.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
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();
}

36
main.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "Monicelli.tab.h"
#include <iostream>
#include <cstdlib>
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;
}