Browse Source

Enable locations.

search
Danila Fedorin 2 years ago
parent
commit
0e40c9e216
  1. 2
      code/compiler/13/main.cpp
  2. 24
      code/compiler/13/parser.y
  3. 49
      code/compiler/13/scanner.l

2
code/compiler/13/main.cpp

@ -17,7 +17,7 @@
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetMachine.h"
void yy::parser::error(const std::string& msg) {
void yy::parser::error(const yy::location& loc, const std::string& msg) {
std::cout << "An error occured: " << msg << std::endl;
}

24
code/compiler/13/parser.y

@ -39,6 +39,8 @@ extern yy::parser::symbol_type yylex();
%define api.value.type variant
%define api.token.constructor
%locations
%type <std::vector<std::string>> lowercaseParams
%type <std::vector<branch_ptr>> branches
%type <std::vector<constructor_ptr>> constructors
@ -78,26 +80,26 @@ lowercaseParams
;
aAdd
: aAdd PLUS aMul { $$ = ast_ptr(new ast_binop(PLUS, std::move($1), std::move($3))); }
| aAdd MINUS aMul { $$ = ast_ptr(new ast_binop(MINUS, std::move($1), std::move($3))); }
: aAdd PLUS aMul { $$ = ast_ptr(new ast_binop(PLUS, std::move($1), std::move($3), @$)); }
| aAdd MINUS aMul { $$ = ast_ptr(new ast_binop(MINUS, std::move($1), std::move($3), @$)); }
| aMul { $$ = std::move($1); }
;
aMul
: aMul TIMES app { $$ = ast_ptr(new ast_binop(TIMES, std::move($1), std::move($3))); }
| aMul DIVIDE app { $$ = ast_ptr(new ast_binop(DIVIDE, std::move($1), std::move($3))); }
: aMul TIMES app { $$ = ast_ptr(new ast_binop(TIMES, std::move($1), std::move($3), @$)); }
| aMul DIVIDE app { $$ = ast_ptr(new ast_binop(DIVIDE, std::move($1), std::move($3), @$)); }
| app { $$ = std::move($1); }
;
app
: app appBase { $$ = ast_ptr(new ast_app(std::move($1), std::move($2))); }
: app appBase { $$ = ast_ptr(new ast_app(std::move($1), std::move($2), @$)); }
| appBase { $$ = std::move($1); }
;
appBase
: INT { $$ = ast_ptr(new ast_int($1)); }
| LID { $$ = ast_ptr(new ast_lid(std::move($1))); }
| UID { $$ = ast_ptr(new ast_uid(std::move($1))); }
: INT { $$ = ast_ptr(new ast_int($1, @$)); }
| LID { $$ = ast_ptr(new ast_lid(std::move($1), @$)); }
| UID { $$ = ast_ptr(new ast_uid(std::move($1), @$)); }
| OPAREN aAdd CPAREN { $$ = std::move($2); }
| case { $$ = std::move($1); }
| let { $$ = std::move($1); }
@ -106,17 +108,17 @@ appBase
let
: LET OCURLY definitions CCURLY IN OCURLY aAdd CCURLY
{ $$ = ast_ptr(new ast_let(std::move($3), std::move($7))); }
{ $$ = ast_ptr(new ast_let(std::move($3), std::move($7), @$)); }
;
lambda
: BACKSLASH lowercaseParams ARROW OCURLY aAdd CCURLY
{ $$ = ast_ptr(new ast_lambda(std::move($2), std::move($5))); }
{ $$ = ast_ptr(new ast_lambda(std::move($2), std::move($5), @$)); }
;
case
: CASE aAdd OF OCURLY branches CCURLY
{ $$ = ast_ptr(new ast_case(std::move($2), std::move($5))); }
{ $$ = ast_ptr(new ast_case(std::move($2), std::move($5), @$)); }
;
branches

49
code/compiler/13/scanner.l

@ -6,33 +6,38 @@
#include "definition.hpp"
#include "parser.hpp"
yy::parser::location_type location;
#define YY_DECL yy::parser::symbol_type yylex()
#define YY_USER_ACTION location.step(); location.columns(yyleng);
%}
%%
[ \n]+ {}
\\ { return yy::parser::make_BACKSLASH(); }
\+ { return yy::parser::make_PLUS(); }
\* { return yy::parser::make_TIMES(); }
- { return yy::parser::make_MINUS(); }
\/ { return yy::parser::make_DIVIDE(); }
[0-9]+ { return yy::parser::make_INT(atoi(yytext)); }
defn { return yy::parser::make_DEFN(); }
data { return yy::parser::make_DATA(); }
case { return yy::parser::make_CASE(); }
of { return yy::parser::make_OF(); }
let { return yy::parser::make_LET(); }
in { return yy::parser::make_IN(); }
\{ { return yy::parser::make_OCURLY(); }
\} { return yy::parser::make_CCURLY(); }
\( { return yy::parser::make_OPAREN(); }
\) { return yy::parser::make_CPAREN(); }
, { return yy::parser::make_COMMA(); }
-> { return yy::parser::make_ARROW(); }
= { return yy::parser::make_EQUAL(); }
[a-z][a-zA-Z]* { return yy::parser::make_LID(std::string(yytext)); }
[A-Z][a-zA-Z]* { return yy::parser::make_UID(std::string(yytext)); }
\n { location.lines(); }
[ ]+ {}
\\ { return yy::parser::make_BACKSLASH(location); }
\+ { return yy::parser::make_PLUS(location); }
\* { return yy::parser::make_TIMES(location); }
- { return yy::parser::make_MINUS(location); }
\/ { return yy::parser::make_DIVIDE(location); }
[0-9]+ { return yy::parser::make_INT(atoi(yytext), location); }
defn { return yy::parser::make_DEFN(location); }
data { return yy::parser::make_DATA(location); }
case { return yy::parser::make_CASE(location); }
of { return yy::parser::make_OF(location); }
let { return yy::parser::make_LET(location); }
in { return yy::parser::make_IN(location); }
\{ { return yy::parser::make_OCURLY(location); }
\} { return yy::parser::make_CCURLY(location); }
\( { return yy::parser::make_OPAREN(location); }
\) { return yy::parser::make_CPAREN(location); }
, { return yy::parser::make_COMMA(location); }
-> { return yy::parser::make_ARROW(location); }
= { return yy::parser::make_EQUAL(location); }
[a-z][a-zA-Z]* { return yy::parser::make_LID(std::string(yytext), location); }
[A-Z][a-zA-Z]* { return yy::parser::make_UID(std::string(yytext), location); }
<<EOF>> { return yy::parser::make_YYEOF(location); }
%%

Loading…
Cancel
Save