changeset 60:2451f0904bf6 new_gen

Dumping Ast with AstPrinter is now possible again! :)
author Anders Johnsen <skabet@gmail.com>
date Tue, 29 Apr 2008 15:00:11 +0200
parents 1d6f4ad38a91
children 575c267bdd1f 9f8131676242
files ast/Decl.d ast/Exp.d dang/compiler.d tools/AstPrinter.d
diffstat 4 files changed, 115 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Decl.d	Tue Apr 29 00:31:56 2008 +0200
+++ b/ast/Decl.d	Tue Apr 29 15:00:11 2008 +0200
@@ -71,20 +71,25 @@
 
     void simplify()
     {
-        if(auto t = cast(DStruct)env.find(identifier).type)
+        if(auto t = cast(DFunction)env.find(identifier).type)
         {
-            VarDecl[] funcArgs;
-            auto i = new Identifier("ret.val");
-            i.env = env;
-            i.env.add(i);
-            i.env.find(i).type = i.env.find(identifier).type;
-            auto var = new VarDecl(type, i);
-            var.env = env;
-            funcArgs ~= var;
-            funcArgs ~= this.funcArgs;
-            this.funcArgs = funcArgs;
-            env.find(this.identifier).type = DType.Void;
-            sret = true;
+            if(auto s = cast(DStruct)t.return_type)
+            {
+                VarDecl[] funcArgs;
+                auto i = new Identifier("ret.val");
+                i.env = env;
+                i.env.add(i);
+                i.env.find(i).type = s;
+                auto var = new VarDecl(type, i);
+                var.env = env;
+                funcArgs ~= var;
+                funcArgs ~= this.funcArgs;
+                this.funcArgs = funcArgs;
+                t.return_type = DType.Void;
+                this.type = new Identifier("void");
+                env.find(identifier).type = t;
+                sret = true;
+            }
         }
 
         foreach ( funcArg ; funcArgs )
--- a/ast/Exp.d	Tue Apr 29 00:31:56 2008 +0200
+++ b/ast/Exp.d	Tue Apr 29 15:00:11 2008 +0200
@@ -65,42 +65,46 @@
 
     Exp simplify()
     {
-        DFunction func_t = cast(DFunction)exp.type();
-        assert(func_t !is null, "Calling on something that isn't a function");
-        if (cast(DStruct)func_t.return_type is null)
-            return this;
+        if(auto t = cast(DStruct)type)
+        {
+            DFunction func_t = cast(DFunction)exp.type();
+            assert(func_t !is null, "Calling on something that isn't a function");
+            if (cast(DStruct)func_t.return_type is null)
+                return this;
 
-        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)
+            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 = t;
+            auto var = new VarDecl(new Identifier(t.name), 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)
             {
-                stmts ~= stmtVar;
-                stmts ~= stmtCall;
+                if(stmtIndex == index)
+                {
+                    stmts ~= stmtVar;
+                    stmts ~= stmtCall;
+                }
+                stmts ~= s;
             }
-            stmts ~= s;
+            f.statements = stmts;
+            callExp.sret = true;
+
+            return i;
         }
-        f.statements = stmts;
-        callExp.sret = true;
-
-        return i;
+        return this;
     }
 }
 
--- a/dang/compiler.d	Tue Apr 29 00:31:56 2008 +0200
+++ b/dang/compiler.d	Tue Apr 29 15:00:11 2008 +0200
@@ -89,6 +89,7 @@
                     (Decl[] decls, DataSource src) {
                         auto print = new DotPrinter();
                         print.print(decls);
+                        exit(0);
                     });
             }
     ).help("Output the AST as dot-graphicz");
@@ -99,6 +100,7 @@
                     (Decl[] decls, DataSource src) {
                         auto print = new AstPrinter(src);
                         print.print(decls);
+                        exit(0);
                     });
             }
     ).help("Output the AST as dot-graphicz");
--- a/tools/AstPrinter.d	Tue Apr 29 00:31:56 2008 +0200
+++ b/tools/AstPrinter.d	Tue Apr 29 15:00:11 2008 +0200
@@ -34,8 +34,9 @@
                 auto funcDecl = cast(FuncDecl)decl;
                 printBeginLine();
                 printIdentifier(funcDecl.type);
+                space;
                 printIdentifier(funcDecl.identifier);
-                printFuncArgs(funcDecl.funcArgs);
+                printFuncArgs(funcDecl);
                 printOpenBrace();
                 foreach(stmt ; funcDecl.statements)
                     printStatement(stmt);
@@ -46,14 +47,26 @@
                 auto varDecl = cast(VarDecl)decl;
                 printBeginLine();
                 printIdentifier(varDecl.type);
+                space;
                 printIdentifier(varDecl.identifier);
                 if(varDecl.init)
                 {
-                    print("= ");
+                    print(" = ");
                     printExp(varDecl.init);
                 }
                 printEndLine(";");
                 break;
+
+            case DeclType.StructDecl:
+                auto structDecl = cast(StructDecl)decl;
+                printBeginLine("struct ");
+                printIdentifier(structDecl.identifier);
+                printEndLine;
+                printOpenBrace;
+                foreach( var ; structDecl.vars)
+                    printDecl(var);
+                printCloseBrace;
+                break;
         }
     }
 
@@ -63,8 +76,12 @@
         {
             case StmtType.Return:
                 auto ret = cast(ReturnStmt)stmt;
-                printBeginLine("return ");
-                printExp(ret.exp);
+                printBeginLine("return");
+                if(ret.exp)
+                {
+                    space;
+                    printExp(ret.exp);
+                }
                 printEndLine(";");
                 break;
             case StmtType.Decl:
@@ -94,7 +111,7 @@
             case ExpType.IntegerLit:
                 auto integetLit = cast(IntegerLit)exp;
                 auto t = integetLit.token;
-                print(t.get ~ " ");
+                print(t.get);
                 break;
             case ExpType.Negate:
                 auto negateExp = cast(NegateExp)exp;
@@ -104,22 +121,47 @@
             case ExpType.AssignExp:
                 auto assignExp = cast(AssignExp)exp;
                 printExp(assignExp.identifier);
-                print("= ");
+                print(" = ");
                 printExp(assignExp.exp);
                 break;
+            case ExpType.MemberReference:
+                auto mrExp = cast(MemberReference)exp;
+                printExp(mrExp.target);
+                print(".");
+                printIdentifier(mrExp.child);
+                break;
+            case ExpType.Identifier:
+                auto iden = cast(Identifier)exp;
+                printIdentifier(iden);
+                break;
+            case ExpType.CallExp:
+                auto callExp = cast(CallExp)exp;
+                printExp(callExp.exp);
+                print("(");
+                foreach(i, e; callExp.args)
+                {
+                    printExp(e);
+                    if(i+1 < callExp.args.length)
+                        print(", ");
+                }
+                print(")");
+                break;
         }
         
     }
 
-    void printFuncArgs(VarDecl[] decls)
+    void printFuncArgs(FuncDecl decl)
     {
         print("(");
      
-        foreach(i, decl; decls)
+        foreach(i, d; decl.funcArgs)
         {
-            printIdentifier(decl.type);
-            printIdentifier(decl.identifier);
-            if(i+1 < decls.length)
+            printIdentifier(d.type);
+            if(i == 0 && decl.sret)
+                print("*");
+            space;
+            printIdentifier(d.identifier);
+            if(i+1 < decl.funcArgs.length)
                 print(",");
         }
 
@@ -128,8 +170,7 @@
 
     void printIdentifier(Identifier identifier)
     {
-        auto t = identifier.token;
-        print(t.get ~ " ");
+        print(identifier.get);
     }
 
     void printOpenBrace()
@@ -158,6 +199,11 @@
     {
         Stdout(line);
     }
+
+    void space()
+    {
+        print(" ");
+    }
 private:
     DataSource ds;
     char[] tabIndex;