changeset 81:110c7e1c4ca2 new_gen

Now you can declare array
author Anders Johnsen <skabet@gmail.com>
date Fri, 02 May 2008 18:23:33 +0200
parents 682e20aa224f
children 06dda301ea61
files ast/Exp.d gen/CodeGen.d parser/Action.d parser/Parser.d sema/AstAction.d sema/DType.d sema/SymbolTableBuilder.d sema/Visitor.d
diffstat 8 files changed, 68 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Fri May 02 17:33:50 2008 +0200
+++ b/ast/Exp.d	Fri May 02 18:23:33 2008 +0200
@@ -372,21 +372,21 @@
 
 class ArrayIdentifier : Identifier
 {
-    this(Identifier arrayOf)
+    this(Identifier arrayOf, IntegerLit size)
     {
         super(ExpType.ArrayIdentifier);
         this.arrayOf = arrayOf;
+        this.size = Integer.parse(size.get);
+        this.name = arrayOf.name;
     }
 
     override DType type()
     {
-        if (myType !is null)
-            return myType;
-        myType = new DArray(arrayOf.type);
-        return myType;
+        return arrayOf.type.getAsArray(size);
     }
 
     Identifier arrayOf;
+    int size;
 
     private DType myType;
 }
--- a/gen/CodeGen.d	Fri May 02 17:33:50 2008 +0200
+++ b/gen/CodeGen.d	Fri May 02 18:23:33 2008 +0200
@@ -675,6 +675,12 @@
             type_map[t] = res;
             return res;
         }
+        else if (auto f = t.asArray)
+        {
+            Type res = ArrayType.Get(llvm(f.arrayOf), f.size);
+            type_map[t] = res;
+            return res;
+        }
         assert(0, "Only integers, structs and functions are supported");
     }
 
--- a/parser/Action.d	Fri May 02 17:33:50 2008 +0200
+++ b/parser/Action.d	Fri May 02 18:23:33 2008 +0200
@@ -50,6 +50,20 @@
     Id id;
 }
 
+class ArrayId : Id
+{
+    public static ArrayId opCall(Id id, Object number)
+    {
+        auto a = new ArrayId();
+        a.id = id;
+        a.number = number;
+        return a;
+    }
+
+    Id id;
+    Object number;
+}
+
 /**
   All methods are optional.
 
--- a/parser/Parser.d	Fri May 02 17:33:50 2008 +0200
+++ b/parser/Parser.d	Fri May 02 18:23:33 2008 +0200
@@ -182,7 +182,7 @@
                 // identifiers in a row
                 if (iden.isBasicType() || iden.isIdentifier())
                 {
-                    if ( n.type == Tok.Star)
+                    if ( n.type == Tok.Star || n.type == Tok.OpenBracket)
                         return action.actOnDeclStmt(parseVarDecl());
                         
                     if ( n.isIdentifier())
@@ -319,10 +319,20 @@
         currentType = Id(type);
         type = lexer.peek;
 
-        while(type.type == Tok.Star)
+        while(type.type == Tok.Star || type.type == Tok.OpenBracket)
         {
-            currentType = PointerId(currentType);
-            lexer.next;
+            if(type.type == Tok.Star)
+            {
+                currentType = PointerId(currentType);
+                lexer.next;
+            }
+            else
+            {
+                lexer.next;
+                currentType = ArrayId(currentType, action.actOnNumericConstant(require(Tok.Integer)));
+                require(Tok.CloseBracket);
+                
+            }
             type = lexer.peek;
         }
 
--- a/sema/AstAction.d	Fri May 02 17:33:50 2008 +0200
+++ b/sema/AstAction.d	Fri May 02 18:23:33 2008 +0200
@@ -23,6 +23,8 @@
     {
         if(auto t = cast(PointerId)type)
             return new PointerIdentifier(handleType(t.id));
+        if(auto t = cast(ArrayId)type)
+            return new ArrayIdentifier(handleType(t.id), cast(IntegerLit)t.number);
         else
             return new Identifier(type.tok);
     }
--- a/sema/DType.d	Fri May 02 17:33:50 2008 +0200
+++ b/sema/DType.d	Fri May 02 18:23:33 2008 +0200
@@ -98,6 +98,14 @@
         return myPointer;
     }
 
+    DArray getAsArray(int size)
+    {
+        if(size in myArray)
+            return myArray[size];
+        myArray[size] = new DArray(this, size);
+        return myArray[size];
+    }
+
     static DInteger
         Bool,
         Byte, UByte, Short, UShort,
@@ -106,6 +114,7 @@
     static DType Void;
 
     private DPointer myPointer;
+    private DArray[int] myArray;
 
     static this()
     {
@@ -199,18 +208,20 @@
 
 class DArray : DType
 {
-    this(DType arrayOf, DType actual = null)
+    this(DType arrayOf, int size, DType actual = null)
     {
         super(id, actual);
         this.arrayOf = arrayOf;
+        this.size = size;
     }
 
     override bool isArray() { return true; }
     override DArray asArray() { return this; }
 
-    int byteSize() { return arrayOf.byteSize; }
+    int byteSize() { return arrayOf.byteSize * size; }
 
     DType arrayOf;
+    const int size;
 }
 
 class DPointer : DType
--- a/sema/SymbolTableBuilder.d	Fri May 02 17:33:50 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Fri May 02 18:23:33 2008 +0200
@@ -57,6 +57,8 @@
     {
         if(auto i = cast(PointerIdentifier)id)
             return new DPointer(typeOf(i.pointerOf, sc));
+        if(auto i = cast(ArrayIdentifier)id)
+            return new DArray(typeOf(i.arrayOf, sc), i.size);
         return sc.findType(id);
     }
 }
--- a/sema/Visitor.d	Fri May 02 17:33:50 2008 +0200
+++ b/sema/Visitor.d	Fri May 02 18:23:33 2008 +0200
@@ -82,6 +82,8 @@
                 return visitIdentifier(cast(Identifier)exp);
             case ExpType.PointerIdentifier:
                 return visitPointerIdentifier(cast(PointerIdentifier)exp);
+            case ExpType.ArrayIdentifier:
+                return visitArrayIdentifier(cast(ArrayIdentifier)exp);
             case ExpType.MemberReference:
                 return visitMemberReference(cast(MemberReference)exp);
             default:
@@ -296,6 +298,16 @@
             return ExpT.init;
     }
 
+    ExpT visitArrayIdentifier(ArrayIdentifier exp)
+    {
+        visitExp(exp.arrayOf);
+
+        static if (is(ExpT == void))
+            return;
+        else
+            return ExpT.init;
+    }
+
     ExpT visitMemberReference(MemberReference mem)
     {
         visitExp(mem.target);