diff parser/Action.d @ 48:b6c1dc30ca4b new_gen

Only tests that dont pass now are structs and switches As far as the parser is concerned assignments are binary expressions Fixed a bug in codegen of if's - it is important to remember that the builder might be positioned at a new block after generating sub-statements
author Anders Halager <halager@gmail.com>
date Thu, 24 Apr 2008 19:42:53 +0200
parents 90fb4fdfefdd
children c7cde6af0095
line wrap: on
line diff
--- a/parser/Action.d	Wed Apr 23 17:09:44 2008 +0200
+++ b/parser/Action.d	Thu Apr 24 19:42:53 2008 +0200
@@ -17,6 +17,8 @@
  */
 public enum Operator
 {
+    Assign,
+
     Eq, Ne,
 
     Lt, Le,
@@ -121,6 +123,19 @@
     }
 
     /**
+      An expression was used as a statement - this includes assignments,
+      function calls.
+
+      Additionally the D spec dictates that expressions with no effect are not
+      legal as statements, but the parser can't test for this so it has to be
+      done in the later stages.
+     */
+    StmtT actOnExprStmt(ExprT exp)
+    {
+        return null;
+    }
+
+    /**
       Called after parsing return statements.
 
       loc is the return token.
@@ -145,6 +160,13 @@
         return null;
     }
 
+    /**
+     */
+    StmtT actOnDeclStmt(DeclT decl)
+    {
+        return null;
+    }
+
     StmtT actOnStartOfSwitchStmt()
     {
         return null;
@@ -207,6 +229,18 @@
     {
         return null;
     }
+
+    /**
+      Called when function calls are encountered.
+
+      Note that args is temporary and might point into the stack. Remember to
+      copy before saving a reference to it.
+     */
+    ExprT actOnCallExpr(ExprT func, ref Token left_paren, ExprT[] args,
+                        ref Token right_paren)
+    {
+        return null;
+    }
 }
 
 /**
@@ -245,7 +279,12 @@
     {
         StmtT[] array = stmts.dup;
         Stmt[] statements = cast(Stmt[])array;
-        return new CompoundStatement(cast(Stmt[])array);
+        return new CompoundStatement(statements);
+    }
+
+    override StmtT actOnExprStmt(ExprT exp)
+    {
+        return new ExpStmt(cast(Exp)exp);
     }
 
     override StmtT actOnReturnStmt(ref Token loc, ExprT exp)
@@ -272,6 +311,12 @@
         return new WhileStmt(c, b);
     }
 
+    override StmtT actOnDeclStmt(DeclT decl)
+    {
+        Decl d = cast(Decl)decl;
+        return new DeclStmt(d);
+    }
+
     // -- Expressions --
     override ExprT actOnNumericConstant(Token c)
     {
@@ -287,7 +332,10 @@
     {
         Exp left = cast(Exp)l;
         Exp right = cast(Exp)r;
-        return new BinaryExp(cast(BinaryExp.Operator)op, left, right);
+        if (op == Operator.Assign)
+            return new AssignExp(left, right);
+        else
+            return new BinaryExp(cast(BinaryExp.Operator)op, left, right);
     }
 
     override ExprT actOnUnaryOp(Token op, ExprT operand)
@@ -296,5 +344,12 @@
         // can only be -x for now
         return new NegateExp(target);
     }
+
+    override ExprT actOnCallExpr(ExprT fn, ref Token, ExprT[] args, ref Token)
+    {
+        Exp f = cast(Exp)fn;
+        Exp[] arguments = cast(Exp[])args.dup;
+        return new CallExp(f, arguments);
+    }
 }