%{
#include "lambda.h"
#include "lambda.tab.h"
+#include "lambda.yy.h"
#include "mem.h"
#include "print.h"
+#include "reduce.h"
-struct lambda *result;
struct decllist *decls = NULL;
extern int yylex();
int yywrap()
{
+ struct decllist *t;
+ while(decls != NULL){
+ free(decls->ident);
+ lambda_free(decls->value);
+ t = decls->next;
+ free(decls);
+ decls = t;
+ }
return 1;
}
return make_ident(ident);
}
-void decls_print()
+int main()
{
- struct decllist *c = decls;
- unsigned int maxlen = 0, len;
- while(c != NULL){
- len = strlen(c->ident);
- maxlen = maxlen < len ? len : maxlen;
- c = c->next;
- }
-
- c = decls;
- while(c != NULL){
- printf("%s ", c->ident);
- len = strlen(c->ident);
- for(unsigned int i = 1; i<maxlen-len; i++)
- putchar(' ');
- printf("= ");
- lambda_print(c->value, NULL);
- c = c->next;
- }
-}
-
-void decls_free()
-{
- struct decllist *t;
- while(decls != NULL){
- free(decls->ident);
- lambda_free(decls->value);
- t = decls->next;
- free(decls);
- decls = t;
- }
+ int r = yyparse();
+ yylex_destroy();
+ return r;
}
%}
%%
+program
+ :
+ | lambda SEMICOLON program
lambda
- : decl lambda
- | term
- { result = $$; }
-decl
- : FUNC ASSIGN term SEMICOLON
+ : term
+ {
+ int maxdepth = 1000;
+ printf(" ");
+ lambda_reduce($1, $1, &maxdepth);
+ lambda_print($1, NULL);
+ lambda_free($1);
+ }
+ | FUNC ASSIGN term
{
decls_prepend($1->data.identifier.ident, $3);
+ printf("%s = ", $1->data.identifier.ident);
+ lambda_print($3, NULL);
lambda_free($1);
}
term