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:
parent
c9b53a7470
commit
40845c011b
23
Monicelli.ll
23
Monicelli.ll
@ -4,8 +4,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
extern int lineNumber;
|
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
|
%option noyywrap
|
||||||
@ -22,7 +23,7 @@ CHAR [a-zA-Z_]
|
|||||||
}
|
}
|
||||||
|
|
||||||
"#"[^\n]* {
|
"#"[^\n]* {
|
||||||
meta(yytext + 1);
|
monicelli_meta(yytext + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
"bituma"[^\n]* {}
|
"bituma"[^\n]* {}
|
||||||
@ -34,23 +35,23 @@ CHAR [a-zA-Z_]
|
|||||||
return RETURN;
|
return RETURN;
|
||||||
}
|
}
|
||||||
"Necchi" {
|
"Necchi" {
|
||||||
yylval.typeval = TYPENAME_INT;
|
monicelli_lval.typeval = TYPENAME_INT;
|
||||||
return TYPENAME;
|
return TYPENAME;
|
||||||
}
|
}
|
||||||
"Mascetti" {
|
"Mascetti" {
|
||||||
yylval.typeval = TYPENAME_CHAR;
|
monicelli_lval.typeval = TYPENAME_CHAR;
|
||||||
return TYPENAME;
|
return TYPENAME;
|
||||||
}
|
}
|
||||||
"Perozzi" {
|
"Perozzi" {
|
||||||
yylval.typeval = TYPENAME_FLOAT;
|
monicelli_lval.typeval = TYPENAME_FLOAT;
|
||||||
return TYPENAME;
|
return TYPENAME;
|
||||||
}
|
}
|
||||||
"Melandri" {
|
"Melandri" {
|
||||||
yylval.typeval = TYPENAME_BOOL;
|
monicelli_lval.typeval = TYPENAME_BOOL;
|
||||||
return TYPENAME;
|
return TYPENAME;
|
||||||
}
|
}
|
||||||
"Sassaroli" {
|
"Sassaroli" {
|
||||||
yylval.typeval = TYPENAME_DOUBLE;
|
monicelli_lval.typeval = TYPENAME_DOUBLE;
|
||||||
return TYPENAME;
|
return TYPENAME;
|
||||||
}
|
}
|
||||||
"conte" {
|
"conte" {
|
||||||
@ -159,17 +160,17 @@ CHAR [a-zA-Z_]
|
|||||||
<INITIAL,shift>[ \t\f\v] {}
|
<INITIAL,shift>[ \t\f\v] {}
|
||||||
|
|
||||||
{CHAR}({DIGIT}|{CHAR})* {
|
{CHAR}({DIGIT}|{CHAR})* {
|
||||||
yylval.strval = strdup(yytext);
|
monicelli_lval.strval = strdup(yytext);
|
||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
{DIGIT}+ {
|
{DIGIT}+ {
|
||||||
yylval.intval = strtol(yytext, NULL, 10);
|
monicelli_lval.intval = strtol(yytext, NULL, 10);
|
||||||
return NUMBER;
|
return NUMBER;
|
||||||
}
|
}
|
||||||
|
|
||||||
<INITIAL,shift>. {
|
<INITIAL,shift>. {
|
||||||
yyerror("Unexpected token");
|
monicelli_error("Unexpected token");
|
||||||
return -1;
|
return -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ extern int yylex();
|
|||||||
double floatval;
|
double floatval;
|
||||||
char *strval;
|
char *strval;
|
||||||
}
|
}
|
||||||
|
%define api.prefix {monicelli_}
|
||||||
|
|
||||||
%token MAIN
|
%token MAIN
|
||||||
%token RETURN
|
%token RETURN
|
||||||
|
13
Type.h
13
Type.h
@ -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
18
emit.c
Normal 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
41
main.c
@ -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
36
main.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user