# HG changeset patch # User Anders Johnsen # Date 1209745413 -7200 # Node ID 110c7e1c4ca2f71a048a787108a86be2a4b23f0d # Parent 682e20aa224ff275f23c8a4db8194137ba33d8ba Now you can declare array diff -r 682e20aa224f -r 110c7e1c4ca2 ast/Exp.d --- 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; } diff -r 682e20aa224f -r 110c7e1c4ca2 gen/CodeGen.d --- 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"); } diff -r 682e20aa224f -r 110c7e1c4ca2 parser/Action.d --- 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. diff -r 682e20aa224f -r 110c7e1c4ca2 parser/Parser.d --- 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; } diff -r 682e20aa224f -r 110c7e1c4ca2 sema/AstAction.d --- 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); } diff -r 682e20aa224f -r 110c7e1c4ca2 sema/DType.d --- 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 diff -r 682e20aa224f -r 110c7e1c4ca2 sema/SymbolTableBuilder.d --- 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); } } diff -r 682e20aa224f -r 110c7e1c4ca2 sema/Visitor.d --- 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);