# HG changeset patch # User Anders Johnsen # Date 1209676121 -7200 # Node ID 628cb46ab13b396bf30e56c0e906017d2f82572f # Parent 010f46b6641c7ef6c0a549e209e0255197e57748 First update on the way to Arrays! :) Lexer and ast/Exp now have the types needed. All that needs to be updated now is the Parser. I suggest two options: 1) Making the Id a class that we can make a ArrayId and PointerId for at some point. 2) Use a struct as now, but adding some bools / ints to give a more abstract description of the type 2) is messy and 1) is slower... :/ diff -r 010f46b6641c -r 628cb46ab13b ast/Exp.d --- a/ast/Exp.d Thu May 01 19:51:22 2008 +0200 +++ b/ast/Exp.d Thu May 01 23:08:41 2008 +0200 @@ -17,8 +17,9 @@ Negate, IntegerLit, MemberReference, - ArrayLookup, + ArrayReference, Identifier, + ArrayIdentifier, AssignExp, CallExp, CastExp, @@ -280,11 +281,11 @@ private DType myType; } -class ArrayLookup : Exp +class ArrayReference : Exp { this(Exp target, IntegerLit pos) { - super(ExpType.ArrayLookup); + super(ExpType.ArrayReference); this.target = target; this.pos = pos; } @@ -327,6 +328,27 @@ Exp exp; } +class ArrayIdentifier : Identifier +{ + this(Identifier arrayOf) + { + super(ExpType.ArrayIdentifier); + this.arrayOf = arrayOf; + } + + override DType type() + { + if (myType !is null) + return myType; + myType = new DArray(arrayOf.type); + return myType; + } + + Identifier arrayOf; + + private DType myType; +} + class Identifier : Exp { this(Token t) @@ -336,6 +358,11 @@ name = t.get; } + protected this(ExpType t) + { + super(t); + } + override DType type() { if (myType !is null) diff -r 010f46b6641c -r 628cb46ab13b gen/CodeGen.d --- a/gen/CodeGen.d Thu May 01 19:51:22 2008 +0200 +++ b/gen/CodeGen.d Thu May 01 23:08:41 2008 +0200 @@ -480,6 +480,7 @@ Value getPointer(Exp exp) { + Stdout("lala ")(exp).newline; switch(exp.expType) { case ExpType.Identifier: @@ -488,6 +489,7 @@ return table.find(sym.id.get); case ExpType.MemberReference: auto mem = cast(MemberReference)exp; + Stdout(mem.target).newline; switch(mem.target.expType) { case ExpType.Identifier: @@ -508,6 +510,22 @@ Value val = b.buildGEP(v, vals, sym.id.get~"."~child.get); return val; + case ExpType.MemberReference: + auto v = getPointer(mem.target); + auto child = mem.child; + auto symChild = child.env.find(child); + DType t = mem.target.type; + auto st = t.asStruct; + + int i = st.indexOf(child.get); + + Value[] vals; + vals ~= ConstantInt.Get(IntegerType.Int32, 0, false); + vals ~= ConstantInt.Get(IntegerType.Int32, i, false); + + Value val = b.buildGEP(v, vals, "."~child.get); + return val; + default: Value val = genExpression(exp); auto AI = b.buildAlloca(val.type, ".s"); diff -r 010f46b6641c -r 628cb46ab13b lexer/Lexer.d --- a/lexer/Lexer.d Thu May 01 19:51:22 2008 +0200 +++ b/lexer/Lexer.d Thu May 01 23:08:41 2008 +0200 @@ -39,7 +39,7 @@ foreach( char c ; "0123456789") charTable[c] = CharType.Number; - foreach( char c ; "(){};:.,=!<>+-*/%") + foreach( char c ; "(){}[];:.,=!<>+-*/%") charTable[c] = CharType.Symbol; foreach( char c ; " \n") @@ -51,6 +51,8 @@ symbolFunctions[')'] = &closeParentheses; symbolFunctions['{'] = &openBrace; symbolFunctions['}'] = &closeBrace; + symbolFunctions['['] = &openBracket; + symbolFunctions[']'] = &closeBracket; symbolFunctions[';'] = &seperator; symbolFunctions[':'] = : symbolFunctions['.'] = ˙ @@ -134,6 +136,10 @@ { return Token(Tok.OpenBrace, Location(position - 1, source), 1); } + Token closeBrace() + { + return Token(Tok.CloseBrace, Location(position - 1, this.source), 1); + } Token openParentheses() { return Token(Tok.OpenParentheses, Location(position - 1, this.source), 1); @@ -142,14 +148,17 @@ { return Token(Tok.CloseParentheses, Location(position - 1, this.source), 1); } - Token closeBrace() + Token openBracket() { - return Token(Tok.CloseBrace, Location(position - 1, this.source), 1); + return Token(Tok.OpenBracket, Location(position - 1, this.source), 1); + } + Token closeBracket() + { + return Token(Tok.CloseBracket, Location(position - 1, source), 1); } Token seperator() { - Token t = Token(Tok.Seperator, Location(position - 1, source), 1); - return t; + return Token(Tok.Seperator, Location(position - 1, source), 1); } Token colon() { diff -r 010f46b6641c -r 628cb46ab13b lexer/Token.d --- a/lexer/Token.d Thu May 01 19:51:22 2008 +0200 +++ b/lexer/Token.d Thu May 01 23:08:41 2008 +0200 @@ -109,6 +109,8 @@ CloseParentheses, OpenBrace, CloseBrace, + OpenBracket, + CloseBracket, Seperator, Colon, Dot, @@ -170,6 +172,8 @@ Tok.CloseParentheses:"CloseParentheses", Tok.OpenBrace:"OpenBrace", Tok.CloseBrace:"CloseBrace", + Tok.OpenBracket:"OpenBracket", + Tok.CloseBracket:"CloseBracket", Tok.Dot:"Dot", Tok.Assign:"Assign", Tok.Add:"Add", diff -r 010f46b6641c -r 628cb46ab13b sema/DType.d --- a/sema/DType.d Thu May 01 19:51:22 2008 +0200 +++ b/sema/DType.d Thu May 01 23:08:41 2008 +0200 @@ -30,6 +30,11 @@ /// Return a DStruct if this is one, otherwise return null DStruct asStruct() { return null; } + /// Is this type a DArray + bool isArray() { return false; } + /// Return a DArray if this is one, otherwise return null + DArray asArray() { return null; } + /// Is this type a DFunction bool isFunction() { return false; } /// Return a DFunction if this is one, otherwise return null @@ -177,6 +182,22 @@ } } +class DArray : DType +{ + this(DType arrayOf, DType actual = null) + { + super(id, actual); + this.arrayOf = arrayOf; + } + + override bool isArray() { return true; } + override DArray asArray() { return this; } + + int byteSize() { return arrayOf.byteSize; } + + DType arrayOf; +} + class DFunction : DType { this(Identifier id, DType actual = null)