Supposedly streameing
authorMart Lubbers <mart@martlubbers.net>
Fri, 18 May 2018 10:17:56 +0000 (12:17 +0200)
committerMart Lubbers <mart@martlubbers.net>
Fri, 18 May 2018 10:20:07 +0000 (12:20 +0200)
Makefile
lambda.h
lambda.y
main.c

index 84b6636..79e13c7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 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
index 457e5cf..8256f04 100644 (file)
--- a/lambda.h
+++ b/lambda.h
@@ -37,7 +37,5 @@ struct lambda *make_abstraction(char *, struct lambda *);
 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
index a9b0f14..813b77d 100644 (file)
--- a/lambda.y
+++ b/lambda.y
@@ -2,10 +2,11 @@
 %{
 #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();
 
@@ -17,6 +18,14 @@ void yyerror(const char *str)
 
 int yywrap()
 {
+       struct decllist *t;
+       while(decls != NULL){
+               free(decls->ident);
+               lambda_free(decls->value);
+               t = decls->next;
+               free(decls);
+               decls = t;
+       }
        return 1;
 }
 
@@ -88,38 +97,11 @@ struct lambda *decls_lookup(char *ident)
        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;
 }
 
 %}
@@ -128,14 +110,23 @@ void decls_free()
 
 %%
 
+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
diff --git a/main.c b/main.c
index 4269ff4..dce43c3 100644 (file)
--- a/main.c
+++ b/main.c
@@ -7,20 +7,9 @@
 #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;
 }