Supposedly streameing
[lambda.git] / lambda.y
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