parser start
This commit is contained in:
parent
7fecbd04b3
commit
c76b84682a
14 changed files with 589 additions and 21 deletions
37
src/parser.y
37
src/parser.y
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
%code requires {
|
||||
#include <string>
|
||||
#include "ast.hpp"
|
||||
class Scanner;
|
||||
}
|
||||
|
||||
|
|
@ -23,52 +24,60 @@
|
|||
}
|
||||
|
||||
%parse-param { Scanner* scanner }
|
||||
%parse-param { std::unique_ptr<Module>* result }
|
||||
|
||||
// ---------------------- Semantic tokens ----------------------
|
||||
%token <std::string> IDENTIFIER STRING
|
||||
%token <int> NUMBER
|
||||
%token <std::string> NUMBER
|
||||
|
||||
// ---------------------- Keywords ----------------------
|
||||
%left KW_OR
|
||||
%left KW_AND
|
||||
%right KW_NOT // unary logical NOT
|
||||
%token KW_DEF KW_IF KW_RETURN KW_FOR KW_IN KW_PASS KW_ELSE KW_ELIF KW_CLASS
|
||||
|
||||
// ---------------------- Structure / punctuation ----------------------
|
||||
%token L_PAREN R_PAREN L_BRACKET R_BRACKET L_BRACE R_BRACE
|
||||
%token DOT COLON SEMICOLON COMMA
|
||||
%token INDENT DEDENT NEWLINE EOF
|
||||
|
||||
// ---------------------- Operators ----------------------
|
||||
// Bitwise
|
||||
%left BIT_OR
|
||||
%left BIT_XOR
|
||||
%left BIT_AND
|
||||
%left LSHIFT RSHIFT
|
||||
%token BIT_NOT
|
||||
|
||||
// Comparison
|
||||
%nonassoc EQ NE LT LTE GT GTE
|
||||
|
||||
// Arithmetic
|
||||
%left PLUS MINUS
|
||||
%left MULTIPLY DIVIDE MODULO
|
||||
%right UMINUS // unary minus
|
||||
|
||||
// Assignment (right-associative)
|
||||
%right ASSIGN PLUS_ASSIGN MINUS_ASSIGN MULTIPLY_ASSIGN DIVIDE_ASSIGN MODULO_ASSIGN
|
||||
%right BIT_AND_ASSIGN BIT_OR_ASSIGN BIT_XOR_ASSIGN LSHIFT_ASSIGN RSHIFT_ASSIGN
|
||||
|
||||
// Add types for your grammar rules:
|
||||
%type <std::unique_ptr<Expr>> expr
|
||||
%type <std::unique_ptr<Stmt>> stmt
|
||||
%type <std::vector<std::unique_ptr<Stmt>>> stmt_list
|
||||
|
||||
|
||||
%%
|
||||
input:
|
||||
/* empty */
|
||||
| input element
|
||||
stmt_list EOF { *result = std::make_unique<Module>(std::move($1));}
|
||||
;
|
||||
|
||||
element:
|
||||
IDENTIFIER { std::cout << "IDENTIFIER: " << $1 << "\n"; }
|
||||
stmt_list:
|
||||
/* empty */ { $$ = std::vector<std::unique_ptr<Stmt>>(); }
|
||||
| stmt_list stmt { $1.push_back(std::move($2)); $$ = std::move($1); }
|
||||
;
|
||||
|
||||
stmt:
|
||||
expr { $$ = std::make_unique<ExprStmt>(std::move($1)); }
|
||||
;
|
||||
|
||||
expr:
|
||||
NUMBER { $$ = std::make_unique<Number>($1); }
|
||||
| IDENTIFIER { $$ = std::make_unique<Identifier>($1); }
|
||||
| expr PLUS expr { $$ = std::make_unique<BinOp>(
|
||||
BinaryOperator(BinaryOperator::ADD),
|
||||
std::move($1), std::move($3)); }
|
||||
;
|
||||
|
||||
%%
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue