codea: refactor, teil1
[uebersetzerbau-ss10.git] / codea / parser.y
index a804229a7bf18e0e20eb404e2f38b9232b14af52..bab08bcc61e9a2e434282959fd57eab41159c883 100644 (file)
@@ -5,6 +5,8 @@
        #include "symtable.h"
        #include "chelper.h"
        #include "tree.h"
+
+       #define MAX(A,B) (A > B ? A : B)
 %}
 
 %start Input
@@ -27,7 +29,6 @@
  * gparamges:
  * parms:
  * node:
- * imm:
  * exprcount:
  * sin:
  * sout:
 @attributes { char *name; } IDENT
 @attributes { long val; } NUM
 @attributes { struct symbol *f; int paramges; int parms; } Parms
-@attributes { struct symbol *f; } FeldID Structdef Program
+@attributes { struct symbol *f; } Program Structdef;
+@attributes { struct symbol *f; int offsetcount; } FeldID
 @attributes { struct symbol *s; } Methoddef
 @attributes { struct symbol *s; int gparamges; } Statseq Exprs
-@attributes { struct symbol *s; int gparamges; struct treenode *node; short imm; int exprcount; } Expr Minusterm Term
-@attributes { struct symbol *s; int gparamges; struct treenode *node; } Lexpr Multerm Orterm Feld
+@attributes { struct symbol *s; int gparamges; struct treenode *node; int exprcount; short imm; } Expr Minusterm Multerm Orterm Feld Term
+@attributes { struct symbol *s; int gparamges; struct treenode *node; } Lexpr
 @attributes { struct symbol *sin; int gparamges; struct symbol *sout; struct treenode *node; } Statement
 
 @traversal @postorder c
@@ -86,6 +88,7 @@ Structdef:
          STRUCT FeldID END
          @{
            @i @Structdef.f@ = @FeldID.f@;
+               @i @FeldID.offsetcount@ = 0;
          @}
        ;
 
@@ -93,9 +96,9 @@ Parms:
          /* lokale Vars werden in Statement in die tabelle eingefuegt */
          IDENT Parms
          @{
-           @i @Parms.1.parms@ = @Parms.parms@ + 1;
+           @i @Parms.1.parms@ = @Parms.0.parms@ + 1;
                @i @Parms.0.paramges@ = @Parms.1.paramges@;
-           @i @Parms.0.f@ = tab_add_symbol(@Parms.1.f@, @IDENT.name@, S_PARM, 1, @Parms.parms@);
+           @i @Parms.0.f@ = tab_add_symbol(@Parms.1.f@, @IDENT.name@, S_PARM, 1, @Parms.parms@, -1);
          @}
 
        |
@@ -108,7 +111,8 @@ Parms:
 FeldID:
          IDENT FeldID
          @{
-           @i @FeldID.0.f@ = tab_add_symbol(@FeldID.1.f@, @IDENT.name@, S_FIELD, 1, -1);
+           @i @FeldID.1.offsetcount@ = @FeldID.0.offsetcount@ + 1;
+           @i @FeldID.0.f@ = tab_add_symbol(@FeldID.1.f@, @IDENT.name@, S_FIELD, 1, -1, @FeldID.0.offsetcount@);
          @}
 
        |
@@ -141,7 +145,7 @@ Statement:
          @{
                /* tab_clone ist hier noetig, vgl. folgendes statement
                 * > var x := x - 1; */
-               @i @Statement.sout@ = tab_add_symbol(tab_clone(@Statement.sin@), @IDENT.name@, S_VAR, 1, -1);
+               @i @Statement.sout@ = tab_add_symbol(tab_clone(@Statement.sin@), @IDENT.name@, S_VAR, 1, -1, -1);
                xxputsin(@Expr.s@,)
            @i @Statement.node@ = TREENULL;
          @}
@@ -182,8 +186,8 @@ Statement:
          @{
                statinout()
                xxputsin(@Expr.s@,)
-               @i @Statement.node@ = new_node(O_RET, @Expr.node@, TREENULL, 0);
-               @reg @Statement.node@->reg = next_reg((char *)NULL, 0, @Expr.gparamges@); @Expr.node@->reg = @Statement.node@->reg;
+               @i @Statement.node@ = new_node(O_RET, @Expr.node@, TREENULL, @Expr.exprcount@+1); fprintf(stderr, "RETURN: %s (gparamges: %i, exprcount: %i, gesamt %i [maximal: 7])\n", @Expr.gparamges@ + @Expr.exprcount@ + 1 > 7 ? "zu hohe tiefe" : "tiefe passt", @Expr.gparamges@, @Expr.exprcount@+1, @Expr.gparamges@+@Expr.exprcount@+1);
+               @reg @Statement.node@->reg = @Expr.node@->reg = next_reg((char *)NULL, @Expr.gparamges@);
          @}
        ;
 
@@ -192,6 +196,8 @@ Lexpr:
          @{
            @i @Lexpr.node@ = TREENULL;
            @c check(@Lexpr.s@, @IDENT.name@, S_VAR|S_PARM);
+               /* this.feldname */
+               /* TODO fuer zuweisungen ... (codeb?) */
          @}
 
        | Feld
@@ -200,7 +206,10 @@ Lexpr:
 Feld: Term '.' IDENT
          @{
            @c check(@Feld.s@, @IDENT.name@, S_FIELD);
-           @i @Feld.node@ = TREENULL;
+           @i @Feld.node@ = new_field(@IDENT.name@, @Term.node@, TREENULL, tab_lookup(@Feld.s@, @IDENT.name@, S_FIELD) == SYMNULL ? -1 : tab_lookup(@Feld.s@, @IDENT.name@, S_FIELD)->soffset, @Feld.exprcount@);
+
+               @i @Feld.exprcount@ = @Term.exprcount@; fprintf(stderr, "(Feld)- Term.IDENT\n");
+               @reg @Term.node@->reg = @Feld.node@->reg;
          @}
        ;
 
@@ -208,72 +217,108 @@ Expr:
          Term
          @{
            @reg @Term.node@->reg = @Expr.node@->reg;
-               @i @Expr.exprcount@ = 1 + @Term.exprcount@; fprintf(stderr, "(Expr)- Term\n");
+               @i @Expr.exprcount@ = @Term.exprcount@; fprintf(stderr, "(Expr)- Term\n");
          @}
 
        | NOT Term
          @{
-           @i @Expr.node@ = TREENULL;
-               @i @Expr.exprcount@ = 1 + @Term.exprcount@; fprintf(stderr, "(Expr)- NOT Term\n");
+               @i @Expr.exprcount@ = @Term.exprcount@; fprintf(stderr, "(Expr)- NOT Term\n");
+               @i @Expr.node@ = new_node(O_EQ, @Term.node@, new_node(O_NULL, TREENULL, TREENULL, @Expr.exprcount@), @Expr.exprcount@);
+
+               @reg @Term.node@->reg = @Expr.node@->reg;
          @}
 
        | Term Minusterm
          @{
-               @i @Expr.exprcount@ = 1 + @Term.exprcount@ + @Minusterm.exprcount@; fprintf(stderr, "(Expr)- Term Minusterm\n");
-           @i @Expr.node@ = new_node(O_SUB, @Minusterm.node@, @Term.node@, @Expr.exprcount@);
+               @i @Expr.exprcount@ = @Term.exprcount@ + @Minusterm.exprcount@; fprintf(stderr, "(Expr)- Term Minusterm\n");
+           @i @Expr.node@ = new_node(O_SUB, @Term.node@, @Minusterm.node@, @Expr.exprcount@);
                @i @Expr.imm@ = @Term.imm@ && @Minusterm.imm@;
 
                @reg {
-                       /* TODO */
-                       @Term.node@->reg = @Expr.node@->reg;
-                       @Term.node@->skip = 1;
-                       @Minusterm.node@->reg = next_reg(@Term.node@->reg, @Expr.node@->skip, @Expr.gparamges@);
+                       if(!(@Expr.node@->kids[0] == TREENULL && @Expr.node@->kids[1] == TREENULL)) {
+                               @Term.node@->reg = @Expr.node@->reg;
+                               if(@Minusterm.imm@) {
+                                       @Minusterm.node@->reg = @Expr.node@->reg;
+                               } else {
+                                       @Minusterm.node@->reg = next_reg(@Term.node@->reg, @Expr.gparamges@);
+                               }
+                       }
                }
          @}
 
        | Term Multerm
          @{
-           @i @Expr.node@ = TREENULL;
-               @i @Expr.exprcount@ = 1 + @Term.exprcount@; fprintf(stderr, "(Expr)- Term Multerm\n");
+               @i @Expr.exprcount@ = @Term.exprcount@ + @Multerm.exprcount@; fprintf(stderr, "(Expr)- Term Multerm\n");
+           @i @Expr.node@ = new_node(O_MUL, @Term.node@, @Multerm.node@, @Expr.exprcount@);
+               @i @Expr.imm@ = @Term.imm@ && @Multerm.imm@;
+
+               @reg {
+                       @Term.node@->reg = @Expr.node@->reg;
+                       if(@Term.imm@) {
+                               @Multerm.node@->reg = @Expr.node@->reg;
+                       } else {
+                               @Multerm.node@->reg = next_reg(@Term.node@->reg, @Expr.gparamges@);
+                       }
+               }
          @}
 
        | Term Orterm
          @{
-           @i @Expr.node@ = TREENULL;
-               @i @Expr.exprcount@ = 1 + @Term.exprcount@; fprintf(stderr, "(Expr)- Term OrTerm\n");
+               @i @Expr.exprcount@ = @Term.exprcount@ + @Orterm.exprcount@; fprintf(stderr, "(Expr)- Term Orterm\n");
+           @i @Expr.node@ = new_node(O_OR, @Term.node@, @Orterm.node@, @Expr.exprcount@);
+               @i @Expr.imm@ = @Term.imm@ && @Orterm.imm@;
+
+               @reg {
+                       @Term.node@->reg = @Expr.node@->reg;
+                       @Orterm.node@->reg = next_reg(@Term.node@->reg, @Expr.gparamges@);
+               }
          @}
 
        | Term '<' Term
          @{
-           @i @Expr.node@ = TREENULL;
-               /* das is bloedsinn atm */ @i @Expr.imm@ = @Term.0.imm@ && @Term.1.imm@;
-               @i @Expr.exprcount@ = 1 + @Term.0.exprcount@ + @Term.1.exprcount@; fprintf(stderr, "(Expr)- Term < Term\n");
+               @i @Expr.exprcount@ = @Term.0.exprcount@ + @Term.1.exprcount@; fprintf(stderr, "(Expr)- Term < Term\n");
+           @i @Expr.node@ = new_node(O_LESS, @Term.0.node@, @Term.1.node@, @Expr.exprcount@);
+               @i @Expr.imm@ = @Term.0.imm@ && @Term.0.imm@;
+
+               @reg {
+                       @Term.0.node@->reg = @Expr.node@->reg;
+                       @Term.1.node@->reg = next_reg(@Term.0.node@->reg, @Expr.gparamges@);
+               }
          @}
 
        | Term '=' Term
          @{
-           @i @Expr.node@ = TREENULL;
-               /* das is bloedsinn atm */ @i @Expr.imm@ = @Term.0.imm@ && @Term.1.imm@;
-               @i @Expr.exprcount@ = 1 + @Term.0.exprcount@ + @Term.1.exprcount@; fprintf(stderr, "(Expr)- Term = Term\n");
+               @i @Expr.exprcount@ = @Term.0.exprcount@ + @Term.1.exprcount@; fprintf(stderr, "(Expr)- Term = Term\n");
+           @i @Expr.node@ = new_node(O_EQ, @Term.0.node@, @Term.1.node@, @Expr.exprcount@);
+               @i @Expr.imm@ = @Term.0.imm@ && @Term.0.imm@;
+
+               @reg {
+                       @Term.0.node@->reg = @Expr.node@->reg;
+                       @Term.1.node@->reg = next_reg(@Term.0.node@->reg, @Expr.gparamges@);
+               }
          @}
        ;
 
 Minusterm:
          '-' Term Minusterm
          @{
-               @i @Minusterm.0.exprcount@ = 1 + @Term.exprcount@ + @Minusterm.1.exprcount@; fprintf(stderr, "(Minusterm)- - Term Minusterm\n");
+               @i @Minusterm.0.exprcount@ = @Term.exprcount@ + @Minusterm.1.exprcount@; fprintf(stderr, "(Minusterm)- - Term Minusterm\n");
            @i @Minusterm.node@ = new_node(O_ADD, @Minusterm.1.node@, @Term.node@, @Minusterm.0.exprcount@);
-               @i @Minusterm.imm@ = @Term.imm@ && @Minusterm.1.imm@;
+               @i @Minusterm.0.imm@ = @Term.imm@ && @Minusterm.1.imm@;
 
            @reg {
                        @Minusterm.1.node@->reg = @Minusterm.node@->reg;
-                       @Term.node@->reg = next_reg(@Minusterm.1.node@->reg, @Minusterm.node@->skip, @Minusterm.gparamges@);
+                       if(@Minusterm.1.imm@) {
+                               @Term.node@->reg = @Minusterm.node@->reg;
+                       } else {
+                               @Term.node@->reg = next_reg(@Minusterm.1.node@->reg, @Minusterm.gparamges@);
+                       }
                }
          @}
 
        | '-' Term
          @{
-               @i @Minusterm.exprcount@ = 1 + @Term.exprcount@; fprintf(stderr, "(Minusterm)- - Term\n");
+               @i @Minusterm.exprcount@ = @Term.exprcount@; fprintf(stderr, "(Minusterm)- - Term\n");
            @reg @Term.node@->reg = @Minusterm.node@->reg;
          @}
        ;
@@ -281,23 +326,47 @@ Minusterm:
 Multerm:
          '*' Term Multerm
          @{
-           @i @Multerm.node@ = TREENULL;
+               @i @Multerm.0.exprcount@ = @Term.exprcount@ + @Multerm.1.exprcount@; fprintf(stderr, "(Multerm)- - Term Multerm\n");
+           @i @Multerm.node@ = new_node(O_MUL, @Multerm.1.node@, @Term.node@, @Multerm.0.exprcount@);
+               @i @Multerm.0.imm@ = @Term.imm@ && @Multerm.1.imm@;
+
+           @reg {
+                       @Multerm.1.node@->reg = @Multerm.node@->reg;
+                       if(@Multerm.1.imm@) {
+                               @Term.node@->reg = @Multerm.node@->reg;
+                       } else {
+                               @Term.node@->reg = next_reg(@Multerm.1.node@->reg, @Multerm.gparamges@);
+                       }
+               }
          @}
 
        | '*' Term
          @{
-           @i @Multerm.node@ = TREENULL;
+               @i @Multerm.exprcount@ = @Term.exprcount@; fprintf(stderr, "(Multerm)- - Term\n");
+           @reg @Term.node@->reg = @Multerm.node@->reg;
          @}
        ;
 
 Orterm:
          OR Term Orterm
          @{
-           @i @Orterm.node@ = TREENULL;
+               @i @Orterm.0.exprcount@ = @Term.exprcount@ + @Orterm.1.exprcount@; fprintf(stderr, "(Orterm)- - Term Orterm\n");
+           @i @Orterm.node@ = new_node(O_OR, @Orterm.1.node@, @Term.node@, @Orterm.0.exprcount@);
+               @i @Orterm.0.imm@ = @Term.imm@ && @Orterm.1.imm@;
+
+           @reg {
+                       @Orterm.1.node@->reg = @Orterm.node@->reg;
+                       if(@Orterm.1.imm@) {
+                               @Term.node@->reg = @Orterm.node@->reg;
+                       } else {
+                               @Term.node@->reg = next_reg(@Orterm.1.node@->reg, @Orterm.gparamges@);
+                       }
+               }
          @}
        | OR Term
          @{
-           @i @Orterm.node@ = TREENULL;
+               @i @Orterm.exprcount@ = @Term.exprcount@; fprintf(stderr, "(Orterm)- - Term\n");
+           @reg @Term.node@->reg = @Orterm.node@->reg;
          @}
 
        ;
@@ -306,26 +375,26 @@ Term:
          '(' Expr ')'
          @{
            @i @Term.node@ = @Expr.node@;
-               @i @Term.exprcount@ = @Expr.exprcount@ + 1; fprintf(stderr, "(Term)- ( Expr )\n");
+               @i @Term.exprcount@ = 1 + @Expr.exprcount@; fprintf(stderr, "(Term)- ( Expr )\n");
          @}
 
        | NUM
          @{
-               @i @Term.exprcount@ = 1; fprintf(stderr, "(Term)- NUM\n");
+               @i @Term.exprcount@ = 0; fprintf(stderr, "(Term)- NUM\n");
            @i @Term.node@ = new_number(@NUM.val@, @Term.exprcount@);
                @i @Term.imm@ = 1;
          @}
 
        | '-' NUM
          @{
-               @i @Term.exprcount@ = 1; fprintf(stderr, "(Term)- - NUM\n");
+               @i @Term.exprcount@ = 0; fprintf(stderr, "(Term)- - NUM\n");
            @i @Term.node@ = new_number(-1 * (@NUM.val@), @Term.exprcount@);
                @i @Term.imm@ = 1;
          @}
 
        | THIS
          @{
-               @i @Term.exprcount@ = 1; fprintf(stderr, "(Term)- THIS\n");
+               @i @Term.exprcount@ = 0; fprintf(stderr, "(Term)- THIS\n");
            @i @Term.node@ = new_param(O_ID, strdup("this"), TREENULL, TREENULL, 0, @Term.exprcount@);
                @i @Term.imm@ = 0;
          @}
@@ -333,36 +402,51 @@ Term:
        | IDENT
          @{
            @c check(@Term.s@, @IDENT.name@, S_VAR|S_PARM);
-               @i @Term.exprcount@ = 1; fprintf(stderr, "(Term)- IDENT\n");
-           @i @Term.node@ = new_param(O_ID, @IDENT.name@, TREENULL, TREENULL, tab_lookup(@Term.s@, @IDENT.name@, S_PARM) == SYMNULL ? -1 : tab_lookup(@Term.s@, @IDENT.name@, S_PARM)->param_index, @Term.exprcount@);
+
+               @i @Term.exprcount@ = 0; fprintf(stderr, "(Term)- IDENT\n");
                @i @Term.imm@ = 0;
+           @i {
+                       @Term.node@ = TREENULL;
+                       if(tab_lookup(@Term.s@, @IDENT.name@, S_VAR|S_PARM) == SYMNULL) {
+                               /* es handelt sich um ein feldzugriff auf this */
+                               @Term.node@ = new_field(@IDENT.name@, new_param(O_ID, strdup("this"), TREENULL, TREENULL, 0, @Term.exprcount@), TREENULL, tab_lookup(@Term.s@, @IDENT.name@, S_FIELD) == SYMNULL ? -1 : tab_lookup(@Term.s@, @IDENT.name@, S_FIELD)->soffset, @Term.exprcount@);
+                       } else { /* param oder var */
+                               @Term.node@ = new_param(O_ID, @IDENT.name@, TREENULL, TREENULL, tab_lookup(@Term.s@, @IDENT.name@, S_PARM) == SYMNULL ? -1 : tab_lookup(@Term.s@, @IDENT.name@, S_PARM)->param_index, @Term.exprcount@);
+                       }
+               }
+
+               @reg if(tab_lookup(@Term.s@, @IDENT.name@, S_VAR|S_PARM) == SYMNULL) {
+                       /* TODO: kein schoener hack? */
+                       @Term.node@->kids[0]->reg = @Term.node@->reg;
+               }
          @}
 
        | Feld
          @{
-           @i @Term.node@ = TREENULL;
+           @i @Term.node@ = @Feld.node@;
+               /*TODO*/@i @Term.exprcount@ = 0;
                @i @Term.imm@ = 0;
-               /*TODO*/@i @Term.exprcount@ = 1;
          @}
 
        | IDENT '(' Exprs ')'
          @{
            @i @Term.node@ = TREENULL;
+               /*TODO*/@i @Term.exprcount@ = 0;
                @i @Term.imm@ = 0;
-               /*TODO*/@i @Term.exprcount@ = 1;
          @}
 
        | Term '.' IDENT '(' Exprs ')'
          @{
            @i @Term.node@ = TREENULL;
+               /*TODO*/@i @Term.exprcount@ = 0;
                @i @Term.imm@ = 0;
-               /*TODO*/@i @Term.exprcount@ = 1;
          @}
 
        ;
 
 Exprs:
          Expr ',' Exprs
+         /* TODO: exprcount uppen */
        | Expr
        |
        ;