# HG changeset patch # User Anders Halager # Date 1209739111 -7200 # Node ID ad956143dcdc50132d1d2f7d0bb9e921fdbb3e52 # Parent 9171f04dd9ee7fede18861d5787a1afe7c0a69af Parse and gen for dereferences diff -r 9171f04dd9ee -r ad956143dcdc ast/Exp.d --- a/ast/Exp.d Fri May 02 15:48:57 2008 +0200 +++ b/ast/Exp.d Fri May 02 16:38:31 2008 +0200 @@ -15,6 +15,7 @@ { Binary, Negate, + Deref, IntegerLit, MemberReference, ArrayReference, @@ -210,6 +211,26 @@ super(ExpType.Negate); this.exp = exp; } + + Exp simplify() + { + exp = exp.simplify; + return this; + } + + override DType type() { return exp.type(); } + + public Exp exp; +} + +class DerefExp : Exp +{ + this(Exp exp) + { + super(ExpType.Deref); + this.exp = exp; + } + Exp simplify() { exp = exp.simplify; diff -r 9171f04dd9ee -r ad956143dcdc gen/CodeGen.d --- a/gen/CodeGen.d Fri May 02 15:48:57 2008 +0200 +++ b/gen/CodeGen.d Fri May 02 16:38:31 2008 +0200 @@ -270,6 +270,10 @@ auto negateExp = cast(NegateExp)exp; auto target = genExpression(negateExp.exp); return b.buildNeg(target, "neg"); + case ExpType.Deref: + auto derefExp = cast(DerefExp)exp; + auto target = genExpression(derefExp.exp); + return b.buildLoad(target, "deref"); case ExpType.AssignExp: auto assignExp = cast(AssignExp)exp; return buildAssign(assignExp.identifier, assignExp.exp); diff -r 9171f04dd9ee -r ad956143dcdc parser/Parser.d --- a/parser/Parser.d Fri May 02 15:48:57 2008 +0200 +++ b/parser/Parser.d Fri May 02 16:38:31 2008 +0200 @@ -423,7 +423,11 @@ int prec; } - static const UnOp[] _unary = [{Tok.Minus, 4}]; + static const UnOp[] _unary = + [ + {Tok.Minus, 4}, + {Tok.Star, 4} + ]; UnOp* unary(Tok t) { foreach (ref op; _unary) diff -r 9171f04dd9ee -r ad956143dcdc sema/AstAction.d --- a/sema/AstAction.d Fri May 02 15:48:57 2008 +0200 +++ b/sema/AstAction.d Fri May 02 16:38:31 2008 +0200 @@ -128,8 +128,11 @@ override ExprT actOnUnaryOp(Token op, ExprT operand) { Exp target = cast(Exp)operand; - // can only be -x for now - return new NegateExp(target); + if (op.type == Tok.Minus) + return new NegateExp(target); + if (op.type == Tok.Star) + return new DerefExp(target); + assert(0, "Only valid unary expressions are -x and *x"); } override ExprT actOnCallExpr(ExprT fn, ref Token, ExprT[] args, ref Token) diff -r 9171f04dd9ee -r ad956143dcdc sema/Visitor.d --- a/sema/Visitor.d Fri May 02 15:48:57 2008 +0200 +++ b/sema/Visitor.d Fri May 02 16:38:31 2008 +0200 @@ -70,6 +70,8 @@ return visitIntegerLit(cast(IntegerLit)exp); case ExpType.Negate: return visitNegateExp(cast(NegateExp)exp); + case ExpType.Deref: + return visitDerefExp(cast(DerefExp)exp); case ExpType.AssignExp: return visitAssignExp(cast(AssignExp)exp); case ExpType.CallExp: @@ -257,6 +259,15 @@ return ExpT.init; } + ExpT visitDerefExp(DerefExp exp) + { + visitExp(exp.exp); + static if (is(ExpT == void)) + return; + else + return ExpT.init; + } + ExpT visitIntegerLit(IntegerLit exp) { static if (is(ExpT == void)) diff -r 9171f04dd9ee -r ad956143dcdc tools/AstPrinter.d --- a/tools/AstPrinter.d Fri May 02 15:48:57 2008 +0200 +++ b/tools/AstPrinter.d Fri May 02 16:38:31 2008 +0200 @@ -120,6 +120,11 @@ print("-"); printExp(negateExp.exp); break; + case ExpType.Deref: + auto derefExp = cast(DerefExp)exp; + print("*"); + printExp(derefExp.exp); + break; case ExpType.AssignExp: auto assignExp = cast(AssignExp)exp; printExp(assignExp.identifier);