CFLAGS:=-g -Wall -Wextra -pedantic
-lambda: lambda.tab.o lambda.yy.o main.o print.o mem.o reduce.o
+lambda: lambda.tab.o lambda.yy.o print.o mem.o reduce.o
$(LINK.c) $(LDLIBS) $^ $(OUTPUT_OPTION)
%.tab.c: %.y
struct lambda *make_application(struct lambda *, struct lambda *);
struct lambda *make_numeral(unsigned int i);
struct lambda *make_bool(bool b);
-void decls_free();
-void decls_print();
#define YYSTYPE struct lambda *
#endif
%{
#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
#include "print.h"
#include "mem.h"
-extern struct lambda *result;
-
int main()
{
int r = yyparse();
- int maxdepth = 10000;
- if(r == 0){
- decls_print();
- printf(" ");
- lambda_reduce(result, result, &maxdepth);
- lambda_print(result, NULL);
- }
- yylex_destroy();
- lambda_free(result);
decls_free();
return r;
}