-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.y
63 lines (51 loc) · 1.24 KB
/
lambda.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
%{
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "stack.h"
extern int yylex();
void yyerror(const char* s);
struct ast_node *parse_res;
struct stack *backups;
unsigned debruijn = 0;
unsigned indices[256] = {0};
%}
%union {
char var;
struct ast_node *node;
}
%token LAMBDA DOT LBRAC RBRAC
%token<var> IDENT
%type<node> bracexpr applist callableExpr var expr lambda curry lastapp
%start start
%initial-action {
debruijn = 1;
backups = new_stack();
for (int i = 0; i < 256; i++) {
indices[i] = 0;
}
}
%%
start: expr { parse_res = $1; };
expr: applist | lastapp | lambda;
lastapp: applist lambda { $$ = ast_new_app($1, $2); };
applist: applist callableExpr { $$ = ast_new_app($1, $2); } | callableExpr;
callableExpr: bracexpr | var;
lambda: LAMBDA curry { $$ = $2; };
curry: DOT expr { $$ = $2; }
| IDENT {
backups = stack_push(backups, indices[$1]);
indices[$1] = debruijn++;
} curry {
$$ = ast_new_func($1, $3);
indices[$1] = stack_pop(backups);
debruijn--;
};
bracexpr: LBRAC expr RBRAC { $$ = $2; };
var: IDENT { $$ = ast_new_var($1,
indices[$1] == 0 ? 0 : debruijn - indices[$1]); };
%%
void yyerror(const char* s) {
fprintf(stderr, "Parse error: %s\n", s);
exit(1);
}