diff sema/AstAction.d @ 88:eb5b2c719a39 new_gen

Major change to locations, tokens and expressions. A location (now SourceLocation or SLoc) is only 32 bit in size - disadvantage is that it can't find its own text. You have to go through the new SourceManager to do that. This has caused changes to a lot of stuff and removal of DataSource and the old Location Additionally Exp has gotten some location stuff, so we can give proper error messages. Not in Decl and Stmt yet, but thats coming too.
author Anders Halager <halager@gmail.com>
date Sun, 04 May 2008 18:13:46 +0200
parents 29f486ccc203
children 48bb2287c035
line wrap: on
line diff
--- a/sema/AstAction.d	Sun May 04 12:58:02 2008 +0200
+++ b/sema/AstAction.d	Sun May 04 18:13:46 2008 +0200
@@ -4,7 +4,8 @@
 
 import lexer.Token;
 
-import misc.Error;
+import misc.Error,
+       basic.SourceManager;
 
 import ast.Exp,
        ast.Stmt,
@@ -19,14 +20,25 @@
  */
 class AstAction : Action
 {
-    Identifier handleType(Id type)
+    this(SourceManager sm)
+    {
+        this.sm = sm;
+    }
+    private SourceManager sm;
+
+    private Identifier handleType(Id type)
     {
         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);
+            return identifierFromTok(type.tok);
+    }
+
+    private Identifier identifierFromTok(Token t)
+    {
+        return new Identifier(t.location, sm.getText(t.asRange));
     }
 
 
@@ -35,9 +47,9 @@
     {
         Exp exp = cast(Exp)init;
         if(type.tok.type == Tok.Struct)
-            return new StructDecl(new Identifier(id.tok));
+            return new StructDecl(identifierFromTok(id.tok));
         else
-            return new VarDecl(handleType(type), new Identifier(id.tok), exp);
+            return new VarDecl(handleType(type), identifierFromTok(id.tok), exp);
     }
     
     override void actOnStructMember(DeclT decl, ref Id type, ref Id name, ExprT init)
@@ -45,28 +57,30 @@
         Exp exp = cast(Exp)init;
         StructDecl st = cast(StructDecl)decl;
         st.addMember(
-                new Identifier(type.tok), 
-                new Identifier(name.tok), 
+                identifierFromTok(type.tok), 
+                identifierFromTok(name.tok), 
                 exp);
     }
 
-    override ExprT actOnMemberReference(ExprT lhs, Location op, Id member)
+    override ExprT actOnMemberReference(ExprT lhs, SLoc op, Id member)
     {
-        return new MemberReference(cast(Exp)lhs, new Identifier(member.tok));
+        Exp exp = cast(Exp)lhs;
+        Identifier id = identifierFromTok(member.tok);
+        return new MemberReference(op, exp, id);
     }
 
     override DeclT actOnStartOfFunctionDef(ref Id type, ref Id name)
     {
-        return new FuncDecl(new Identifier(type.tok), new Identifier(name.tok));
+        return new FuncDecl(identifierFromTok(type.tok), identifierFromTok(name.tok));
     }
 
     override void addFuncArg(DeclT func, Id type, Id name)
     {
         FuncDecl fd = cast(FuncDecl)func;
         if(name)
-            fd.addParam(handleType(type), new Identifier(name.tok));
+            fd.addParam(handleType(type), identifierFromTok(name.tok));
         else
-            fd.addParam(new Identifier(type.tok));
+            fd.addParam(identifierFromTok(type.tok));
     }
 
     override DeclT actOnEndOfFunction(DeclT func, StmtT stmts)
@@ -121,31 +135,34 @@
     // -- Expressions --
     override ExprT actOnNumericConstant(Token c)
     {
-        return new IntegerLit(c);
+        return new IntegerLit(c.location, sm.getText(c.asRange));
     }
 
     override ExprT actOnIdentifierExp(Id id)
     {
-        return new Identifier(id.tok);
+        return identifierFromTok(id.tok);
     }
 
-    override ExprT actOnBinaryOp(Operator op, ExprT l, ExprT r)
+    override ExprT actOnBinaryOp(SLoc op_loc, Operator op, ExprT l, ExprT r)
     {
         Exp left = cast(Exp)l;
         Exp right = cast(Exp)r;
         if (op == Operator.Assign)
-            return new AssignExp(left, right);
+            return new AssignExp(op_loc, left, right);
         else
-            return new BinaryExp(cast(BinaryExp.Operator)op, left, right);
+        {
+            BinaryExp.Operator bin_op = cast(BinaryExp.Operator)op;
+            return new BinaryExp(op_loc, bin_op, left, right);
+        }
     }
 
     override ExprT actOnUnaryOp(Token op, ExprT operand)
     {
         Exp target = cast(Exp)operand;
         if (op.type == Tok.Minus)
-            return new NegateExp(target);
+            return new NegateExp(op.location, target);
         if (op.type == Tok.Star)
-            return new DerefExp(target);
+            return new DerefExp(op.location, target);
         assert(0, "Only valid unary expressions are -x and *x");
     }
 
@@ -156,14 +173,19 @@
         return new CallExp(f, arguments);
     }
 
-    override ExprT actOnCastExpr(Id id, ExprT exp)
+    override ExprT actOnCastExpr(ref Token _cast, Id id, ExprT exp)
     {
-        return new CastExp(new Identifier(id.tok), cast(Exp)exp );
+        Exp target = cast(Exp)exp;
+        Identifier target_type = identifierFromTok(id.tok);
+        return new CastExp(_cast.location, target_type, target);
     }
 
-    override ExprT actOnIndexEpr(ExprT arr, ref Token, ExprT index, ref Token)
+    override ExprT
+        actOnIndexEpr(ExprT arr, ref Token lb, ExprT index, ref Token rb)
     {
-        return new IndexExp(cast(Exp)arr, cast(Exp)index);
+        Exp target = cast(Exp)arr;
+        Exp idx = cast(Exp)index;
+        return new IndexExp(target, lb.location, idx, rb.location);
     }
 }