-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
99 lines (86 loc) · 2.47 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%{
#include "Expression.h"
#include "Parser.h"
#include "Lexer.h"
int yyerror(SExpression **expression, yyscan_t scanner, const char *msg) {
printf("PARSING ERROR:%s\n" , msg);
}
%}
%code requires {
typedef void* yyscan_t;
}
%output "Parser.cpp"
%defines "Parser.h"
%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { SExpression **expression }
%parse-param { yyscan_t scanner }
%union {
int value;
char name[0x100];
SExpression *expression;
Arglist *arglist;
}
%token TOKEN_SEMICOLON ";"
%token TOKEN_DEF "def"
%token TOKEN_DEFUN "defun"
%token TOKEN_IF "if"
%token TOKEN_ELSE "else"
%token TOKEN_COLON ":"
%token TOKEN_COMMA ","
%token TOKEN_LSPAREN "["
%token TOKEN_RSPAREN "]"
%token TOKEN_LPAREN "("
%token TOKEN_RPAREN ")"
%token TOKEN_RBRACE "}"
%token TOKEN_LBRACE "{"
%token TOKEN_PLUS "+"
%token TOKEN_MINUS "-"
%token TOKEN_STAR "*"
%token TOKEN_DIV "/"
%token TOKEN_EQEQ "=="
%token TOKEN_LESS "<"
%token <value> TOKEN_NUMBER "number"
%token <name> TOKEN_NAME "name"
%type <expression> expr
%type <arglist> arglist
%left ";"
%left ":"
%right ","
%left "else"
%left "<"
%left "=="
%left "+"
%left "-"
%left "*"
%left "/"
%%
input
: expr { *expression = $1; }
;
expr
: expr[L] "+" expr[R] { $$ = createOperation( eADD, $L, $R ); }
| expr[L] ";" expr[R] { $$ = createOperation( eSEM, $L, $R ); }
| expr[L] "," expr[R] { $$ = createOperation( eCOMMA, $L, $R ); }
| expr[L] ";" { $$ = $L; }
| expr[L] "*" expr[R] { $$ = createOperation( eMULTIPLY, $L, $R ); }
| expr[L] "-" expr[R] { $$ = createOperation( eMINUS, $L, $R ); }
| expr[L] "<" expr[R] { $$ = createOperation( eLESS, $L, $R ); }
| expr[L] "==" expr[R] { $$ = createOperation( eEQEQ, $L, $R ); }
| expr[L] "/" expr[R] { $$ = createOperation( eDIV, $L, $R ); }
| "defun" "name"[N] "(" arglist[L] ")" "{" expr[E] "}"{ $$ = createDefun($N, $L, $E); }
| "def" "name"[N] ":" expr[E] { $$ = createDef($N, $E); }
| "if" "(" expr[C] ")" "{" expr[L] "}" "else" "{" expr[R] "}" { $$ = createIF($C, $L, $R); }
| "name" "(" expr[E] ")" { $$ = createCall($1, $E); }
| "(" expr[E] ")" { $$ = $E; }
| "-" expr[E] { $$ = createOperation(eMINUS, NULL, $E); }
| "name" {$$ = createRef($1);}
| "[" expr[L] ":" expr[R] "]" { $$ = createOperation(eCOLON, $L, $R); }
| "number" { $$ = createNumber($1); }
;
arglist
: {$$ = NULL;}
| "name"[L] { $$ = appendArglist($L, NULL); }
| "name"[L] "," arglist[R] { $$ = appendArglist($L, $R); }
;
%%