[svn] r5988: nemerle/trunk/ncc: external/InternalTypes.n generation/Typer3.n generation/Typer4.n misc/Pret...

malekith svnadmin at nemerle.org
Wed Nov 30 15:47:03 CET 2005


Log:
Start yield support. Will need closure variable moving for this to work.

Author: malekith
Date: Wed Nov 30 15:47:02 2005
New Revision: 5988

Modified:
   nemerle/trunk/ncc/external/InternalTypes.n
   nemerle/trunk/ncc/generation/Typer3.n
   nemerle/trunk/ncc/generation/Typer4.n
   nemerle/trunk/ncc/misc/PrettyPrint.n
   nemerle/trunk/ncc/typing/TypedTree.n
   nemerle/trunk/ncc/typing/Typer.n
   nemerle/trunk/ncc/typing/Typer2.n

Modified: nemerle/trunk/ncc/external/InternalTypes.n
==============================================================================
--- nemerle/trunk/ncc/external/InternalTypes.n	(original)
+++ nemerle/trunk/ncc/external/InternalTypes.n	Wed Nov 30 15:47:02 2005
@@ -382,6 +382,7 @@
   public mutable AssemblyCultureAttribute_tc : TypeInfo;  
   public mutable Nemerle_list_tc : TypeInfo;
   public mutable IEnumerable_tc : TypeInfo;
+  public mutable Generic_IEnumerable_tc : TypeInfo;
   public mutable DllImport_tc : TypeInfo;
   public mutable Serializable_tc : TypeInfo;    
 
@@ -525,6 +526,7 @@
     UInt64_tc = lookup ("System.UInt64"); UInt64 = MType.Class (UInt64_tc, []);
     ValueType_tc = lookup ("System.ValueType"); ValueType = MType.Class (ValueType_tc, []);
     IEnumerable_tc = lookup ("System.Collections.IEnumerable");
+    Generic_IEnumerable_tc = lookup ("System.Collections.Generic.IEnumerable");
     DllImport_tc = lookup ("System.Runtime.InteropServices.DllImportAttribute");
     Serializable_tc = lookup ("System.SerializableAttribute");    
     IObjectReference = MType.Class (lookup ("System.Runtime.Serialization.IObjectReference"), []);

Modified: nemerle/trunk/ncc/generation/Typer3.n
==============================================================================
--- nemerle/trunk/ncc/generation/Typer3.n	(original)
+++ nemerle/trunk/ncc/generation/Typer3.n	Wed Nov 30 15:47:02 2005
@@ -1573,6 +1573,7 @@
         | DefFunctionsIn
         | Match
         | SelfTailCall 
+        | Yield
         | Block =>
           Util.cassert (Message.SeenError);
           null

Modified: nemerle/trunk/ncc/generation/Typer4.n
==============================================================================
--- nemerle/trunk/ncc/generation/Typer4.n	(original)
+++ nemerle/trunk/ncc/generation/Typer4.n	Wed Nov 30 15:47:02 2005
@@ -309,6 +309,7 @@
           | DefFunctionsIn
           | Match
           | Block
+          | Yield
           | SelfTailCall =>
             Util.cassert (Message.SeenError);
             false

Modified: nemerle/trunk/ncc/misc/PrettyPrint.n
==============================================================================
--- nemerle/trunk/ncc/misc/PrettyPrint.n	(original)
+++ nemerle/trunk/ncc/misc/PrettyPrint.n	Wed Nov 30 15:47:02 2005
@@ -1048,6 +1048,12 @@
           recurse_and_indent (body);
 
 
+        | TT.TExpr.Yield (body) =>
+          append ("yield (");
+          recurse (body);
+          append (")");
+
+
         | TT.TExpr.Goto (id, t) =>
           append ($ "goto l$id [$t];");
           

Modified: nemerle/trunk/ncc/typing/TypedTree.n
==============================================================================
--- nemerle/trunk/ncc/typing/TypedTree.n	(original)
+++ nemerle/trunk/ncc/typing/TypedTree.n	Wed Nov 30 15:47:02 2005
@@ -447,6 +447,7 @@
     | StaticEventRef        { from : MType.Class; ev : IEvent; }
     | ConstantObjectRef     { from : MType.Class; mem : IField; }
     | Block                 { jump_out : LocalValue; body : TExpr; }
+    | Yield                 { expr : TExpr; }
     | Delayed               { susp : Typer.DelayedTyping; }
     | Error
 
@@ -751,6 +752,7 @@
         | StaticPropertyRef
         | EventMember
         | Block
+        | Yield
         | StaticEventRef =>
           // this is supposed to be run after Typer2
           Message.Warning (expr.loc, 

Modified: nemerle/trunk/ncc/typing/Typer.n
==============================================================================
--- nemerle/trunk/ncc/typing/Typer.n	(original)
+++ nemerle/trunk/ncc/typing/Typer.n	Wed Nov 30 15:47:02 2005
@@ -257,7 +257,10 @@
               MakeImplicitBlockJumpOut ("_N_return", current_fun.ret_type);
           def e' = TypeExpr (e);
           messenger.CleanLocalError ();
-          def e' = AddCastTo (e', current_fun.ret_type, "function return type");
+          def ret_type =
+            if (UsedYield) InternalType.Void
+            else current_fun.ret_type;
+          def e' = AddCastTo (e', ret_type, "function return type");
           def e' =
             if (skip_n_return) e' 
             else TExpr.Block (e'.Type, local, e');
@@ -274,7 +277,7 @@
 
     RunSecondPass (meth : MethodBuilder) : void
     {
-      def t2 = Typer2 (current_type, meth);
+      def t2 = Typer2 (current_type, meth, UsedYield);
       t2.Run ();
       def t3 = Typer3 (meth);
       t3.Run ();
@@ -2923,6 +2926,7 @@
     #region Special macros
     static mutable checked_macro : NamespaceTree.Node;
     static mutable unchecked_macro : NamespaceTree.Node;
+    static mutable yield_macro : NamespaceTree.Node;
     
 
     TypeBlock (name : PT.Name, body : PT.PExpr, expected : TyVar) : TExpr
@@ -2963,6 +2967,7 @@
       when (checked_macro == null) {
         checked_macro = GlobalEnv.ExactPath (["Nemerle", "Core", "checked"]);
         unchecked_macro = GlobalEnv.ExactPath (["Nemerle", "Core", "unchecked"]);
+        yield_macro = GlobalEnv.ExactPath (["Nemerle", "Core", "yield"]);
       }
       
       match (expr) {
@@ -2976,6 +2981,9 @@
             } finally {
               PopLocals ();
             }
+          } else if (ns.Equals (yield_macro)) {
+            _ = Expect (expected, InternalType.Void, "yield ``result''");
+            HandleYield (expr)
           } else null
         
         | _ => null
@@ -2984,6 +2992,54 @@
     #endregion
 
 
+    #region yield handling
+    GetYieldType () : TyVar
+    {
+      match (parent_typer) {
+        | Some (t) => t.GetYieldType ()
+        | None =>
+          match (current_fun.ret_type.Fix ()) {
+            | Class (tc, [t])
+              when tc.Equals (InternalType.Generic_IEnumerable_tc) =>
+              t
+            | t =>
+              ReportError (messenger,
+                           $ "yield used in a function returning $t (it "
+                             "should be System.Collections.Generic."
+                             "IEnumerable[T])");
+              InternalType.Object
+          }
+      }
+    }
+
+    HandleYield (expr : PT.PExpr) : TExpr
+    {
+      when (solver.IsTopLevel)
+        UsedYield = true;
+      TExpr.Yield (InternalType.Void,
+                   AddCastTo (TypeExpr (expr), GetYieldType (), "yielded expression"))
+    }
+
+    UsedYield : bool
+    {
+      mutable used_yield : bool;
+
+      get {
+        match (parent_typer) {
+          | Some (t) => t.UsedYield
+          | None => used_yield
+        }
+      }
+      set {
+        match (parent_typer) {
+          | Some (t) => t.UsedYield = value
+          | None => used_yield = value
+        }
+      }
+    }
+    #endregion
+
+
     #region Partial application
     /** Transform [_.foo] to [fun (x) { x.foo }] and [foo (_, 3)] to
         [fun (x) { foo (x, 3) }].  */
@@ -3083,6 +3139,7 @@
         | TExpr.Goto
         | TExpr.Label
         | TExpr.Block
+        | TExpr.Yield
         | TExpr.MultipleAssign
         | TExpr.MethodAddress => 
           $ "!!!shouldn't happen: $(expr.GetType())!!!"

Modified: nemerle/trunk/ncc/typing/Typer2.n
==============================================================================
--- nemerle/trunk/ncc/typing/Typer2.n	(original)
+++ nemerle/trunk/ncc/typing/Typer2.n	Wed Nov 30 15:47:02 2005
@@ -42,6 +42,7 @@
     mutable current_fun : Fun_header;
     current_type : TypeBuilder;
     messenger : Messenger;
+    used_yield : bool;
 
     [System.Flags]
     enum Context {
@@ -60,11 +61,12 @@
       | TopLevel           = IsTail %| AllowGoto %| AllowTry
     }
     
-    public this (ty : TypeBuilder, fn : MethodBuilder)
+    public this (ty : TypeBuilder, fn : MethodBuilder, used_yield : bool)
     {
       current_fun = fn.GetHeader ();
       top_level_fun = fn;
       current_type = ty;
+      this.used_yield = used_yield;
 
       messenger = Passes.Solver.CurrentMessenger;
     }
@@ -165,11 +167,13 @@
             | _ => false
           }
         | TExpr.StaticRef (Class (tc, _) as from, meth is IMethod, typarms) =>
+          !used_yield &&
           meth.GetHeader ().id == current_fun.id &&
           tc.GetMemType ().Equals (from) &&
           AreSelfMethodTyParms (meth, typarms)
           
         | TExpr.MethodRef (th, meth : IMethod, typarms, _) =>
+          !used_yield &&
           meth.GetHeader ().id == current_fun.id &&
           Unfold (th) is TExpr.This &&
           AreSelfMethodTyParms (meth, typarms)
@@ -651,6 +655,12 @@
     }
 
 
+    [Nemerle.NotImplemented]
+    ExpandYield (expr : TExpr) : TExpr
+    {
+    }
+
+
     /* block (foo) {
          ...
          when (...) foo (33);
@@ -1189,6 +1199,10 @@
           Walk (ctx, ExpandBlock (b))
 
 
+        | TExpr.Yield (e) =>
+          Walk (ctx, ExpandYield (e))
+
+
         | TExpr.If (cond, e1, e2) =>
           TExpr.If (Walk (cond), Walk (ctx, e1), Walk (ctx, e2))
 



More information about the svn mailing list