better printing
[lambda.git] / lambda.y
1 %define parse.error verbose
2 %{
3 #include "lambda.h"
4 #include "lambda.tab.h"
5 #include "mem.h"
6 #include "print.h"
7
8 struct lambda *result;
9 struct decllist *decls = NULL;
10 extern int yylex();
11
12 int yydebug=1;
13 void yyerror(const char *str)
14 {
15 fprintf(stderr, "parse error: %s\n", str);
16 }
17
18 int yywrap()
19 {
20 return 1;
21 }
22
23 struct lambda *make_lambda()
24 {
25 return malloc(sizeof (struct lambda));
26 }
27
28 struct lambda *make_ident(char *i)
29 {
30 struct lambda *r = make_lambda();
31 r->which = lambda_ident;
32 r->data.identifier.ident = strdup(i);
33 r->data.identifier.revision = 0;
34 return r;
35 }
36
37 struct lambda *make_abstraction(char *i, struct lambda *t)
38 {
39 struct lambda *r = make_lambda();
40 r->which = lambda_abs;
41 r->data.abstraction.ident = strdup(i);
42 r->data.abstraction.revision = 0;
43 r->data.abstraction.expr = t;
44 return r;
45 }
46
47 struct lambda *make_application(struct lambda *t1, struct lambda *t2)
48 {
49 struct lambda *r = make_lambda();
50 r->which = lambda_app;
51 r->data.application.expr1 = t1;
52 r->data.application.expr2 = t2;
53 return r;
54 }
55
56 void decls_prepend(char *ident, struct lambda *value)
57 {
58 struct decllist *head = malloc(sizeof (struct decllist));
59 head->next = decls;
60 head->ident = strdup(ident);
61 head->value = value;
62 decls = head;
63 }
64
65 struct lambda *decls_lookup(char *ident)
66 {
67 struct decllist *c = decls;
68 while(c != NULL){
69 if(strcmp(c->ident, ident) == 0)
70 return copy(c->value);
71 c = c->next;
72 }
73 return make_ident(ident);
74 }
75
76 void decls_print()
77 {
78 struct decllist *c = decls;
79 unsigned int maxlen = 0, len;
80 while(c != NULL){
81 len = strlen(c->ident);
82 maxlen = maxlen < len ? len : maxlen;
83 c = c->next;
84 }
85
86 c = decls;
87 while(c != NULL){
88 printf("%s ", c->ident);
89 len = strlen(c->ident);
90 for(unsigned int i = 1; i<maxlen-len; i++)
91 putchar(' ');
92 printf("= ");
93 lambda_print(c->value, NULL);
94 c = c->next;
95 }
96 }
97
98 void decls_free()
99 {
100 struct decllist *t;
101 while(decls != NULL){
102 free(decls->ident);
103 lambda_free(decls->value);
104 t = decls->next;
105 free(decls);
106 decls = t;
107 }
108 }
109
110 %}
111
112 %token LAMBDA DOT OBRACE CBRACE IDENT FUNC SEMICOLON ASSIGN
113
114 %%
115
116 lambda
117 : decl lambda
118 | term
119 { result = $$; }
120 decl
121 : FUNC ASSIGN term SEMICOLON
122 {
123 decls_prepend($1->data.identifier.ident, $3);
124 lambda_free($1);
125 }
126 term
127 : term appterm
128 { $$ = make_application($1, $2); }
129 | appterm
130 { $$ = $1; }
131 appterm
132 : FUNC
133 {
134 $$ = decls_lookup($1->data.identifier.ident);
135 lambda_free($1);
136 }
137 | IDENT
138 {
139 $$ = make_ident($1->data.identifier.ident);
140 lambda_free($1);
141 }
142 | LAMBDA abstraction
143 { $$ = $2; }
144 | OBRACE term CBRACE
145 { $$ = $2; }
146 abstraction
147 : IDENT abstraction
148 {
149 $$ = make_abstraction($1->data.identifier.ident, $2);
150 lambda_free($1);
151 }
152 | DOT term
153 { $$ = $2; }