[svn] r7813: nemerle/trunk/macros: io.n string.n

VladD2 svnadmin at nemerle.org
Thu Oct 18 14:11:14 CEST 2007


Log:
1. Move implementation of sprint and print macro into SprintImpl helper method.
2. Remove not used fprint macro.

Author: VladD2
Date: Thu Oct 18 14:11:12 2007
New Revision: 7813

Modified:
   nemerle/trunk/macros/io.n
   nemerle/trunk/macros/string.n

Modified: nemerle/trunk/macros/io.n
==============================================================================
--- nemerle/trunk/macros/io.n	(original)
+++ nemerle/trunk/macros/io.n	Thu Oct 18 14:11:12 2007
@@ -123,48 +123,15 @@
 
   macro sprint(str : string)
   {
-    if (string.IsNullOrEmpty (str))
-    {
-      Message.Warning ("empty spliced string");
-      <[ string.Empty ]>
+    StringTemplate.Helper.SprintImpl(str, true, expr => expr, Macros.ImplicitCTX().Env)
     }
-    else
-    {
-      def seq = StringTemplate.Helper.make_splice_distribution (str, Macros.ImplicitCTX().Env).Rev ();
-
-      match (seq)
-      {
-        | [StrPart.Lit(val)] =>
-          Message.Warning ($"spliced string without splices: '$str'");
-          <[ $(val : string) ]> //PT.PExpr.Literal(Literal.String(val))
 
-        | _ => 
-          //def indentPresent = seq.Exists(_ is StrPart.IndentedExpr);
-          //def seq = if (indentPresent) StrPart.Expr(<[ def ident = ""; ]>) :: seq;
-          //mutable curIndent = "";
-          def seq = seq;
+  // use writer.Write($"...") instade
+  // macro fprint (writer, str : string)
+  // {
+  //   <[ $writer.Write(sprint($(str : string))) ]>
+  // }
           
-          def res = seq.Map(e => 
-            match (e : StrPart)
-            {
-              | Lit(str)    => <[ $(str : string) ]>
-              // TODO: Try add support of identation.
-              | NewLine     =>   <[ Environment.NewLine ]>
-              | Expr(expr)  => expr
-              | IndentedExpr(indent, expr) => 
-                <[ def indent = $(indent : string);
-                   indent + Convert.ToString($expr) ]>
-            });
-            
-          <[ string.Concat (..$res) ]>
-      }
-    }
-  }
-
-  macro fprint (writer, str : string)
-  {
-    <[ $writer.Write(sprint($(str : string))) ]>
-  }
 
   /// If string literal is supplied, then prints it to System.Console, replacing all
   /// occurences of $id with id.ToString () invocation
@@ -173,46 +140,10 @@
   {
     match (value)
     {
-      | <[ $(str : string) ]> => 
-        if (string.IsNullOrEmpty (str))
-        {
-          Message.Warning ("empty spliced string");
-          <[ string.Empty ]>
-        }
-        else
-        {
-          def seq = StringTemplate.Helper.make_splice_distribution (str, Macros.ImplicitCTX().Env).Rev ();
-
-          match (seq)
-          {
-            | [StrPart.Lit(val)] =>
-              <[ Console.Write ($(val : string)) ]>
-
-            | _ => 
-              //def indentPresent = seq.Exists(_ is StrPart.IndentedExpr);
-              //def seq = if (indentPresent) StrPart.Expr(<[ def ident = ""; ]>) :: seq;
-              //mutable curIndent = "";
-              def seq = seq;
-              
-              def res = seq.Map(e => 
-                match (e : StrPart)
-                {
-                  | Lit(str)    => <[ $(str : string) ]>
-                  // TODO: Try add support of identation.
-                  | NewLine     =>   <[ Environment.NewLine ]>
-                  | Expr(expr)  => expr
-                  | IndentedExpr(indent, expr) => 
-                    <[ def indent = $(indent : string);
-                       indent + Convert.ToString($expr) ]>
-                });
-                
-              <[ Console.Write (string.Concat (..$res)) ]>
-          }
-        }
-
+      | <[ $(str : string) ]> => StringTemplate.Helper.SprintImpl(
+                                   str, false, expr => <[ Console.Write($expr) ]>, Macros.ImplicitCTX().Env)
       | _                     => <[ Console.Write ($value) ]>
     }
-
   }
 
   // module internal to this assembly used for compile time analysis of string formatters, etc.

Modified: nemerle/trunk/macros/string.n
==============================================================================
--- nemerle/trunk/macros/string.n	(original)
+++ nemerle/trunk/macros/string.n	Thu Oct 18 14:11:12 2007
@@ -301,6 +301,53 @@
 
   public module Helper
   {  
+    public SprintImpl (
+      str : string, 
+      warnIfFormatStrIsEmpty : bool, 
+      envelopExpr : PT.PExpr -> PT.PExpr, 
+      env : GlobalEnv
+    )
+      : PT.PExpr
+    {
+      if (string.IsNullOrEmpty (str))
+      {
+        Message.Warning ("empty spliced string");
+        envelopExpr(<[ string.Empty ]>)
+      }
+      else
+      {
+        def seq = StringTemplate.Helper.make_splice_distribution (str, env).Rev (); //Macros.ImplicitCTX().Env
+
+        match (seq)
+        {
+          | [StrPart.Lit(val)] =>
+            when (warnIfFormatStrIsEmpty)
+              Message.Warning ($"spliced string without splices: '$str'");
+            envelopExpr(<[ $(val : string) ]>);
+
+          | _ => 
+            //def indentPresent = seq.Exists(_ is StrPart.IndentedExpr);
+            //def seq = if (indentPresent) StrPart.Expr(<[ def ident = ""; ]>) :: seq;
+            //mutable curIndent = "";
+            def seq = seq;
+            
+            def res = seq.Map(e => 
+              match (e : StrPart)
+              {
+                | Lit(str)    => <[ $(str : string) ]>
+                // TODO: Try add support of identation.
+                | NewLine     =>   <[ Environment.NewLine ]>
+                | Expr(expr)  => expr
+                | IndentedExpr(indent, expr) => 
+                  <[ def indent = $(indent : string);
+                     indent + Convert.ToString($expr) ]>
+              });
+              
+            envelopExpr(<[ string.Concat (..$res) ]>);
+        }
+      }
+    }
+
     /// If we have string like this
     ///     &lt;#
     ///   SomeText1



More information about the svn mailing list