changeset 22:e331e4e816e4

now handling structs to some extend
author johnsen@johnsen-laptop
date Fri, 18 Apr 2008 23:45:45 +0200
parents 0fb2d13dce37
children 2d28b21faad6 f1282c5fe8e3 5e383b3755d6
files ast/Decl.d dang/compiler.d gen/LLVMGen.d lexer/Keyword.d lexer/Token.d parser/Parser.d sema/Declarations.d sema/SymbolTable.d sema/SymbolTableBuilder.d sema/Visitor.d test.td
diffstat 11 files changed, 139 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Decl.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/ast/Decl.d	Fri Apr 18 23:45:45 2008 +0200
@@ -11,6 +11,7 @@
 {
     VarDecl,
     FuncDecl,
+    StructDecl,
 }
 
 class Decl
@@ -56,3 +57,17 @@
     Stmt[] statements;
 }
 
+class StructDecl : Decl
+{
+    this(Identifier identifier, 
+            VarDecl[] vars)
+    {
+        super(DeclType.StructDecl);
+        this.identifier = identifier;
+        this.vars = vars;
+    }
+
+    Identifier identifier;
+    VarDecl[] vars;
+}
+
--- a/dang/compiler.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/dang/compiler.d	Fri Apr 18 23:45:45 2008 +0200
@@ -139,7 +139,15 @@
 
         auto src = DataSource(file);
         auto lexer = new Lexer(src);
-
+/*
+        auto t = lexer.next;
+        while(t.getType != "EOF")
+        {
+            Stdout(t.getType)(" : ")(t.get).newline;
+            t = lexer.next;
+        }
+        lexer = new Lexer(src);
+*/
         postLex(lexer);
 
         preParse(lexer);
--- a/gen/LLVMGen.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/gen/LLVMGen.d	Fri Apr 18 23:45:45 2008 +0200
@@ -53,6 +53,9 @@
         foreach(decl ; decls)
                 genRootDecl(decl);
 
+        foreach(decl ; nestedFunc)
+                genRootDecl(decl);
+
         table.leaveScope;
     }
 
@@ -132,6 +135,25 @@
                     printEndLine("i32 0");
 
                 printEndLine();
+
+                break;
+
+            case DeclType.StructDecl:
+                auto structDecl = cast(StructDecl)decl;
+                printBeginLine("%struct.");
+                genIdentifier(structDecl.identifier);
+                
+                print(" = type { ");
+                foreach (i, var; structDecl.vars)
+                {
+                    print(typeToLLVM[var.type.get]);
+                    if(i+1 < structDecl.vars.length)
+                        print(", ");
+                }
+
+                printEndLine(" }");
+
+                break;
         
             default:
         }
@@ -146,7 +168,10 @@
                 printBeginLine("%");
                 print(table.find(varDecl.identifier.get));
                 print(" = alloca ");
-                printEndLine(typeToLLVM[varDecl.type.get]);
+                if(varDecl.type.get in typeToLLVM)
+                    printEndLine(typeToLLVM[varDecl.type.get]);
+                else
+                    printEndLine("%struct."~varDecl.type.get);
                 if(varDecl.init)
                 {
                     auto assignExp = new AssignExp(varDecl.identifier, varDecl.init);
@@ -154,6 +179,12 @@
                     assignExp.identifier.env = decl.env;
                     genExpression(assignExp);
                 }
+                break;
+
+            case DeclType.FuncDecl:
+                auto func = cast(FuncDecl)decl;
+                nestedFunc[func.identifier.get] = func;
+                break;
         
             default:
         }
@@ -494,6 +525,8 @@
     static char[][BinaryExp.Operator] opToLLVM;
 
     static char[][] intTypes = [ "i1", "i8", "i16", "i32", "i64" ];
+
+    FuncDecl[char[]] nestedFunc;
 }
 
 struct Ref
--- a/lexer/Keyword.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/lexer/Keyword.d	Fri Apr 18 23:45:45 2008 +0200
@@ -25,6 +25,7 @@
         "if"        : Tok.If,
         "else"      : Tok.Else,
         "while"     : Tok.While,
-        "return"    : Tok.Return
+        "return"    : Tok.Return,
+        "struct"    : Tok.Struct
     ];
 }
--- a/lexer/Token.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/lexer/Token.d	Fri Apr 18 23:45:45 2008 +0200
@@ -76,6 +76,8 @@
 
     Bool,
 
+    Struct,
+
     If, Else,
     While,
     Return,
@@ -115,6 +117,7 @@
         Tok.While:"While",
         Tok.Comma:"Comma",
         Tok.Return:"Return",
+        Tok.Struct:"Struct",
         Tok.Seperator:"Seperator"
     ];
 }
--- a/parser/Parser.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/parser/Parser.d	Fri Apr 18 23:45:45 2008 +0200
@@ -75,7 +75,17 @@
                         error("Unexpexted token "~c~" at line "~Integer.toString(__LINE__));
                 }
                 break;
-
+            case Tok.Struct:
+                Token iden = lexer.next;
+                switch(iden.type)
+                {
+                    case Tok.Identifier:
+                        Identifier identifier = new Identifier(iden);
+                        return new StructDecl (identifier, parseStruct());
+                    default:
+                        throw new Error("Expected struct identifier, but got "~iden.getType, 
+                            iden.location);
+                }
             case Tok.EOF:
                 return null;
             default:
@@ -84,6 +94,19 @@
         }
     }
 
+    VarDecl[] parseStruct()
+    {
+        VarDecl[] varDecls;
+        require(Tok.OpenBrace);
+        while(lexer.peek.type != Tok.CloseBrace)
+        {
+            varDecls ~= cast(VarDecl)parseDecl;
+        }
+
+        require(Tok.CloseBrace);
+        return varDecls;
+    }
+
     Stmt parseStatement()
     {
         Token t = lexer.peek;
@@ -132,6 +155,9 @@
                         require(Tok.Seperator);
                         return stmt;
                         break;
+                    case Tok.Identifier:
+                        auto decl = new DeclStmt(parseDecl());
+                        return decl;
 
                     default:
                         auto e = new ExpStmt(parseExpression());
--- a/sema/Declarations.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/sema/Declarations.d	Fri Apr 18 23:45:45 2008 +0200
@@ -36,7 +36,7 @@
 
     override void visitVarDecl(VarDecl d)
     {
-        if(!isType(d.type.get))
+        if(!isType(d.type.get) && d.env.findType(d.identifier))
             throw new Error("Undefined type: '"~d.type.get~"'",d.type.token.location);
 
         visitExp(d.type);
@@ -52,3 +52,4 @@
     }
 
 }
+
--- a/sema/SymbolTable.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/sema/SymbolTable.d	Fri Apr 18 23:45:45 2008 +0200
@@ -38,6 +38,16 @@
         return null;
     }
 
+    Symbol findType(Identifier id)
+    {
+        if (auto sym = id in symbols)
+            if(symbols[id].type == null)
+                return *sym;
+        if (enclosing !is null)
+            return enclosing.find(id);
+        return null;
+    }
+
     char[][] names()
     {
         char[][] res;
--- a/sema/SymbolTableBuilder.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Fri Apr 18 23:45:45 2008 +0200
@@ -62,6 +62,14 @@
         super.visitVarDecl(d);
     }
 
+    override void visitStructDecl(StructDecl s)
+    {
+        auto sc = current();
+        auto sym = sc.add(s.identifier);
+//        sym.type = Tok.Struct;
+        super.visitStructDecl(s);
+    }
+
     override void visitDeclStmt(DeclStmt d)
     {
         super.visitDeclStmt(d);
--- a/sema/Visitor.d	Fri Apr 18 21:39:17 2008 +0200
+++ b/sema/Visitor.d	Fri Apr 18 23:45:45 2008 +0200
@@ -30,6 +30,8 @@
                 return visitFuncDecl(cast(FuncDecl)decl);
             case DeclType.VarDecl:
                 return visitVarDecl(cast(VarDecl)decl);
+            case DeclType.StructDecl:
+                return visitStructDecl(cast(StructDecl)decl);
             default:
                 throw new Exception("Unknown declaration type");
         }
@@ -104,6 +106,19 @@
             return DeclT.init;
     }
 
+    DeclT visitStructDecl(StructDecl s)
+    {
+        visitExp(s.identifier);
+
+        foreach (arg; s.vars)
+            visitDecl(arg);
+
+        static if (is(DeclT == void))
+            return;
+        else
+            return DeclT.init;
+    }
+
     // Statements:
     StmtT visitReturnStmt(ReturnStmt s)
     {
--- a/test.td	Fri Apr 18 21:39:17 2008 +0200
+++ b/test.td	Fri Apr 18 23:45:45 2008 +0200
@@ -1,9 +1,22 @@
 
 int x = 4;
 
+struct mystruct
+{
+   int x;
+   int y;
+}
+
 int main()
 {
-    return fib(10);
+    mystruct my;
+    int x = 5;
+    int y = 4;
+    return add(6, 7);
+}
+int add(int x, int y)
+{
+    return x + y;
 }
 
 int fib(int n)