diff ast/Exp.d @ 56:4ae365eff712 new_gen

Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
author Anders Johnsen <skabet@gmail.com>
date Mon, 28 Apr 2008 21:40:00 +0200
parents 79cb0afafabe
children fc62c5296a1c
line wrap: on
line diff
--- a/ast/Exp.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/ast/Exp.d	Mon Apr 28 21:40:00 2008 +0200
@@ -1,6 +1,10 @@
 module ast.Exp;
 
 import tango.text.Util : jhash;
+import tango.io.Stdout;
+
+import ast.Decl,
+       ast.Stmt;
 
 import lexer.Token;
 
@@ -27,6 +31,12 @@
 
     ExpType expType;
     Scope env;
+    int stmtIndex;
+
+    Exp simplify()
+    {
+        return this;
+    }
 }
 
 class CallExp : Exp
@@ -40,6 +50,43 @@
 
     Exp exp;
     Exp[] args;
+    bool sret = false;
+
+
+    Exp simplify()
+    {
+        auto call = cast(Identifier)exp;
+        FuncDecl f = env.parentFunction;
+        auto i = new Identifier("temp.var");
+        i.env = f.env;
+        f.env.add(i);
+        f.env.find(i).type = f.env.find(call).type;
+        auto var = new VarDecl(f.type, i, null);
+        Exp[] args; 
+        args ~= i;
+        args ~= this.args;
+        auto callExp = new CallExp(exp, args);
+        callExp.env = f.env;
+//        auto ass = new AssignExp(i, callExp);
+        var.env = f.env;
+//        ass.env = f.env;
+        auto stmtVar = new DeclStmt(var);
+        auto stmtCall = new ExpStmt(callExp);
+        Stmt[] stmts;
+        foreach( index, s ; f.statements)
+        {
+            if(stmtIndex == index)
+            {
+                stmts ~= stmtVar;
+                stmts ~= stmtCall;
+            }
+            stmts ~= s;
+        }
+        f.statements = stmts;
+        callExp.sret = true;
+
+        return i;
+    }
 }
 
 class AssignExp : Exp
@@ -50,6 +97,13 @@
         this.identifier = identifier;
         this.exp = exp;
     }
+    Exp simplify()
+    {
+        identifier = identifier.simplify;
+        exp = exp.simplify;
+
+        return this;
+    }
 
     Exp identifier;
     Exp exp;
@@ -84,6 +138,12 @@
             return "bool";
         return null;
     }
+    Exp simplify()
+    {
+        left = left.simplify;
+        right = right.simplify;
+        return this;
+    }
 
     Operator op;
     Exp left, right;
@@ -96,6 +156,11 @@
         super(ExpType.Negate);
         this.exp = exp;
     }
+    Exp simplify()
+    {
+        exp = exp.simplify;
+        return this;
+    }
 
     public Exp exp;
 }
@@ -107,6 +172,10 @@
         super(ExpType.IntegerLit);
         this.token = t;
     }
+    Exp simplify()
+    {
+        return this;
+    }
 
     Token token;
 }
@@ -119,6 +188,12 @@
         this.target = target;
         this.child = child;
     }
+    Exp simplify()
+    {
+        child.simplify;
+        target = target.simplify;
+        return this;
+    }
 
     Identifier child;
     Exp target;
@@ -133,6 +208,13 @@
         this.pos = pos;
     }
 
+    Exp simplify()
+    {
+        target = target.simplify;
+        pos.simplify;
+        return this;
+    }
+
     Exp target;
     IntegerLit pos;
 }
@@ -146,6 +228,12 @@
         name = t.get;
     }
 
+    this(char[] name)
+    {
+        super(ExpType.Identifier);
+        this.name = name;
+    }
+
     char[] get()
     {
         return name;
@@ -170,6 +258,11 @@
         return 0;
     }
 
+    Exp simplify()
+    {
+        return this;
+    }
+
     Token token;
     char[] name;
 }