2014-11-23 19:04:25 +01:00
|
|
|
%{
|
2014-11-29 00:47:50 +01:00
|
|
|
/*
|
2014-11-29 21:38:52 +01:00
|
|
|
* Monicelli: an esoteric language compiler
|
2014-11-29 00:47:50 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Stefano Sanfilippo
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-11-28 19:01:15 +01:00
|
|
|
#include "Scanner.hpp"
|
|
|
|
#include "Parser.hpp"
|
2014-11-28 00:01:24 +01:00
|
|
|
|
|
|
|
#include <string>
|
2014-11-23 19:04:25 +01:00
|
|
|
|
2014-11-28 19:01:15 +01:00
|
|
|
static void meta(const char *);
|
2014-11-27 20:02:15 +01:00
|
|
|
|
|
|
|
using namespace monicelli;
|
2014-11-28 19:01:15 +01:00
|
|
|
typedef Parser::token token;
|
2014-11-28 20:02:41 +01:00
|
|
|
|
|
|
|
#define YY_USER_ACTION location->begin.columns(yyleng);
|
2014-11-30 13:19:19 +01:00
|
|
|
|
|
|
|
inline
|
|
|
|
bool in(const char *sub, const std::string &str) {
|
|
|
|
return str.find(sub) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2014-11-23 19:04:25 +01:00
|
|
|
%}
|
|
|
|
|
2014-11-28 19:01:15 +01:00
|
|
|
%option ecs stack warn c++
|
|
|
|
%option nodefault noyywrap nounput yylineno
|
|
|
|
%option yyclass="Scanner"
|
2014-11-23 19:04:25 +01:00
|
|
|
|
|
|
|
DIGIT [0-9]
|
|
|
|
HEXDIGIT [0-9a-zA-Z]
|
|
|
|
CHAR [a-zA-Z_]
|
|
|
|
|
2014-11-23 22:06:37 +01:00
|
|
|
%x shift
|
|
|
|
|
2014-11-23 19:04:25 +01:00
|
|
|
%%
|
2014-11-28 19:07:34 +01:00
|
|
|
|
2014-11-23 23:14:12 +01:00
|
|
|
"#"[^\n]* {
|
2014-11-28 19:01:15 +01:00
|
|
|
meta(yytext + 1);
|
2014-11-23 23:14:12 +01:00
|
|
|
}
|
2014-11-23 22:42:59 +01:00
|
|
|
|
2014-11-23 20:43:28 +01:00
|
|
|
"bituma"[^\n]* {}
|
|
|
|
|
2014-11-23 19:04:25 +01:00
|
|
|
"Lei ha clacsonato" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::MAIN;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"vaffanzum" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::RETURN;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"Necchi" {
|
2014-11-28 19:01:15 +01:00
|
|
|
lval->typeval = Type::INT;
|
|
|
|
return token::TYPENAME;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"Mascetti" {
|
2014-11-28 19:01:15 +01:00
|
|
|
lval->typeval = Type::CHAR;
|
|
|
|
return token::TYPENAME;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"Perozzi" {
|
2014-11-28 19:01:15 +01:00
|
|
|
lval->typeval = Type::FLOAT;
|
|
|
|
return token::TYPENAME;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"Melandri" {
|
2014-11-28 19:01:15 +01:00
|
|
|
lval->typeval = Type::BOOL;
|
|
|
|
return token::TYPENAME;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"Sassaroli" {
|
2014-11-28 19:01:15 +01:00
|
|
|
lval->typeval = Type::DOUBLE;
|
|
|
|
return token::TYPENAME;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"conte" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::STAR;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-23 20:43:42 +01:00
|
|
|
"voglio" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::VARDECL;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-23 22:18:03 +01:00
|
|
|
"come "("se ")?"fosse" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::ASSIGN;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-29 18:26:25 +01:00
|
|
|
("il"|"lo"|"la"|"l'"|"i"|"gli"|"le"|"un"|"un'"|"una"|"dei"|"delle") {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::ARTICLE;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"più" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_PLUS;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"meno" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_MINUS;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"per" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_TIMES;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"diviso" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_DIV;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-23 22:06:37 +01:00
|
|
|
"con scappellamento a" {
|
|
|
|
BEGIN(shift);
|
|
|
|
}
|
|
|
|
<shift>"per" {
|
|
|
|
BEGIN(INITIAL);
|
|
|
|
}
|
|
|
|
<shift>"sinistra" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_SHL;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-23 22:06:37 +01:00
|
|
|
<shift>"destra" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_SHR;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-27 22:52:12 +01:00
|
|
|
"minore "("di"|"del") {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_LT;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-27 22:52:12 +01:00
|
|
|
"maggiore "("di"|"del") {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_GT;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-27 21:30:29 +01:00
|
|
|
"minore o uguale "("a"|"di") {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_LTE;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-27 21:30:29 +01:00
|
|
|
"maggiore o uguale "("a"|"di") {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::OP_GTE;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"a posterdati" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::PRINT;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"mi porga" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::INPUT;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-23 22:06:11 +01:00
|
|
|
"ho visto" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::ASSERT;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"!" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::BANG;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"stuzzica" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::LOOP_BEGIN;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-29 18:30:04 +01:00
|
|
|
"e "("b"|"p")"rematura anche, se" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::LOOP_CONDITION;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"che cos'è" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::BRANCH_CONDITION;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"?" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::BRANCH_BEGIN;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-24 10:54:25 +01:00
|
|
|
"o tarapia tapioco" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::BRANCH_ELSE;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"e velocità di esecuzione" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::BRANCH_END;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
":" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::COLON;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"blinda la supercazzola" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::FUNDECL;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"con" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::PARAMS;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
"," {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::COMMA;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-29 18:30:04 +01:00
|
|
|
("b"|"p")"rematurata la supercazzola" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::FUNCALL;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-25 00:39:01 +01:00
|
|
|
"o scherziamo"("?")? {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::FUN_END;
|
2014-11-25 00:39:01 +01:00
|
|
|
}
|
2014-11-23 19:04:25 +01:00
|
|
|
"avvertite don ulrico" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::ABORT;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
2014-11-23 23:22:55 +01:00
|
|
|
"o magari" {
|
2014-11-28 19:01:15 +01:00
|
|
|
return token::CASE_END;
|
2014-11-23 19:24:10 +01:00
|
|
|
}
|
2014-11-23 19:04:25 +01:00
|
|
|
|
2014-11-28 20:02:41 +01:00
|
|
|
<INITIAL,shift>"\n" {
|
|
|
|
location->begin.lines();
|
|
|
|
}
|
|
|
|
|
|
|
|
<INITIAL,shift>[ \t\f\v\r] {
|
2014-11-28 19:07:34 +01:00
|
|
|
}
|
2014-11-23 19:04:25 +01:00
|
|
|
|
|
|
|
{CHAR}({DIGIT}|{CHAR})* {
|
2014-11-28 19:01:15 +01:00
|
|
|
lval->strval = new std::string(yytext);
|
|
|
|
return token::ID;
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
|
2014-11-29 22:37:57 +01:00
|
|
|
[-+]?(({DIGIT}*".")?{DIGIT}+|{DIGIT}+".")([eE][-+]?{DIGIT}+)? {
|
|
|
|
std::string value(yytext);
|
|
|
|
|
2014-11-30 13:19:19 +01:00
|
|
|
if (in(".", value) || in("e", value) || in("E", value)) {
|
2014-11-29 22:37:57 +01:00
|
|
|
lval->floatval = std::stod(value);
|
|
|
|
return token::FLOAT;
|
|
|
|
} else {
|
|
|
|
lval->intval = std::stol(value);
|
|
|
|
return token::NUMBER;
|
|
|
|
}
|
2014-11-23 19:04:25 +01:00
|
|
|
}
|
|
|
|
|
2014-11-23 23:14:43 +01:00
|
|
|
<INITIAL,shift>. {
|
2014-11-29 22:31:25 +01:00
|
|
|
return token::ERROR;
|
2014-11-27 21:30:49 +01:00
|
|
|
}
|
2014-11-23 19:04:25 +01:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
2014-11-28 19:01:15 +01:00
|
|
|
void meta(const char *text) {
|
|
|
|
while (*text != '\0' && *text == ' ') {
|
|
|
|
text += 1;
|
|
|
|
}
|
|
|
|
std::cerr << "META: " << text << std::endl;
|
|
|
|
}
|
|
|
|
|