[svn] r7600: vs-plugin/trunk: Nemerle.Compiler.Utils/NemerleCodeParser.n Nemerle.VsIntegration/Project/Nem...

VladD2 svnadmin at nemerle.org
Thu Apr 19 06:26:16 CEST 2007


Log:
Work on CodeDomParser (WinForms designer).

Author: VladD2
Date: Thu Apr 19 06:26:14 2007
New Revision: 7600

Modified:
   vs-plugin/trunk/Nemerle.Compiler.Utils/NemerleCodeParser.n
   vs-plugin/trunk/Nemerle.VsIntegration/Project/NemerleFileNodeCodeDomProvider.cs

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/NemerleCodeParser.n
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/NemerleCodeParser.n	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/NemerleCodeParser.n	Thu Apr 19 06:26:14 2007
@@ -19,6 +19,10 @@
 using Nemerle.Text;
 using Nemerle.Utility;
 
+using System.CodeDom.CodeBinaryOperatorType;
+
+using SCG = System.Collections.Generic;
+
 namespace Nemerle.Compiler.Utils
 {
   // now CodeDomParser only parses files from Project.CompileUnits
@@ -233,11 +237,13 @@
         //.Typed.expr);
 
      _currentMethod = method;
+      method.EnsureCompiled(); // we need PExpr & TExpr
 
       match(methodDecl)
       {
         | m is CodeConstructor => CreateCtorStatements(m, method.BodyTyped);
-        | _                    => CreateStatements(method.BodyTyped, methodDecl.Statements);
+        | _                    => 
+          methodDecl.Statements.AddRange(ToStatements(method.BodyParsed).ToArray());
       }
 
       Debug.Print($"  method.BodyTyped.Location (col = $(method.BodyTyped.Location.Column), line = $(method.BodyTyped.Location.Line))");
@@ -245,66 +251,109 @@
       methodDecl
     }
 
-    protected CreateParamsArray(parms : list [Parm]) : array[CodeExpression]
-    {
-      | [] => array[CodeSnippetExpression("")]
-      | _  => parms.MapToArray(p => CreateExpression(p.expr));
-    }
-
-    protected CreateCtorStatements(ctor : CodeConstructor, expr : TExpr) : void 
+    protected virtual ToExpression(expr : PExpr) : CodeExpression
     {
-      // TODO : now base(..) or this(...) constructor calls are supposed to be the first calls
-      //        in the ctor body. 
-      match(expr)
+      def createBinOper(expr1, op, expr2)
       {
-        | TExpr.Sequence(e1,e2) => 
-          { CreateCtorStatements(ctor,e1); CreateStatements(e2,ctor.Statements); }
-        | TExpr.Call(func,parms,_) when (func is TExpr.Base) => 
-          ctor.BaseConstructorArgs.AddRange(CreateParamsArray(parms));
-        | TExpr.Call(func,parms,_) when (func is TExpr.This) => 
-          ctor.ChainedConstructorArgs.AddRange(CreateParamsArray(parms));
-        | _ => 
-          CreateStatements(expr,ctor.Statements); 
-      }
+        CodeBinaryOperatorExpression(ToExpression(expr1), op, ToExpression(expr2))
     }
 
-    protected virtual CreateStatements(expr : TExpr, statements: CodeStatementCollection) : void 
+      match (expr)
     {
-      match(expr)
+        | <[ $expr1 && $expr2 ]> => createBinOper(expr1, BooleanAnd, expr2);
+        | <[ $expr1 || $expr2 ]> => createBinOper(expr1, BooleanOr, expr2);
+        | <[ $expr1 != $expr2 ]> => createBinOper(expr1, IdentityInequality, expr2);
+        | <[ $expr1 == $expr2 ]> => createBinOper(expr1, IdentityEquality, expr2);
+        | <[ $expr1 &  $expr2 ]> => createBinOper(expr1, BitwiseAnd, expr2);
+        | <[ $expr1 |  $expr2 ]> => createBinOper(expr1, BitwiseOr, expr2);
+        | <[ $expr1 +  $expr2 ]> => createBinOper(expr1, Add, expr2);
+        | <[ $expr1 -  $expr2 ]> => createBinOper(expr1, Subtract, expr2);
+        | <[ $expr1 /  $expr2 ]> => createBinOper(expr1, Divide, expr2);
+        | <[ $expr1 *  $expr2 ]> => createBinOper(expr1, Multiply, expr2);
+
+        | <[ $obj.$func(..$parms) ]> as call => 
+          _ = call;
+          match (call.func.TypedObject)
       {
-        | TExpr.Sequence(e1,e2) => 
-          { CreateStatements(e1,statements); CreateStatements(e2,statements); }
+            | TExpr.StaticRef(from, mem, type_parms) when mem.MemberKind == MemberTypes.Constructor =>
+              def codeParams = parms.MapToArray(ToExpression);
 
-        | TExpr.MacroEnvelope(original,the_macro,_) as expr => // original : Parsetree.PExpr; the_macro : IMacro; expanded : TExpr; }
-          // TODO: may be ignore or make code snippet?
-          Debug.Print($"--Expanding macro $(CodeDomHelper.PrintMacro(the_macro)),\n--- original:$original");
-          ProcessMacroStatements(expr,statements)
-          //CreateStatements(expanded,statements); 
+              def ctor = if (from.tycon.SystemType != null)
+                CodeObjectCreateExpression(from.tycon.SystemType, codeParams);
+              else
+                CodeObjectCreateExpression(from.tycon.FullName, codeParams);
 
-        | TExpr.DefValIn(name,val,body) => // name : LocalValue; val : TExpr; mutable body : TExpr; }
-          _ = statements.Add( CodeVariableDeclarationStatement(name.Type.SystemType, name.Name, CreateExpression(val)) );
-          CreateStatements(body,statements); 
+              unless (type_parms.IsEmpty)
+              {
+                //ctor.TypeArguments.AddRange(type_parms.MapToArray());
+                Trace.Assert(false);
+              }
+
+              ctor
           
         | _ => 
-          _ = statements.Add(CreateStatement(expr))
-      }
+              CodeMethodInvokeExpression(
+                CodeMethodReferenceExpression(ToExpression(obj), func.ToString()),
+                parms.MapToArray(ToExpression));
     }
 
-    protected virtual ProcessMacroStatements(macroEnvelope : TExpr.MacroEnvelope, statements: CodeStatementCollection) : void
+        | <[ $func(..$parms) ]> => 
+          CodeMethodInvokeExpression(
+            CodeMethodReferenceExpression(null, func.ToString()),
+            parms.MapToArray(ToExpression));
+            
+        | <[ $obj.$field ]> when field.TypedObject is IField =>
+          CodeFieldReferenceExpression(ToExpression(obj), field.TypedObject.Name);
+
+        | <[ $obj.$prop ]> when prop.TypedObject is IProperty =>
+          CodePropertyReferenceExpression(ToExpression(obj), prop.TypedObject.Name);
+
+        | PExpr.Ref(name) => CodeVariableReferenceExpression(name.ToString())
+        
+        | PExpr.Literal(literal) =>
+          match (literal)
     {
-      | (TExpr.MacroEnvelope(original,the_macro,expanded),_) => // original : Parsetree.PExpr; the_macro : IMacro; expanded : TExpr; }
-        Debug.Print($"  ProcessMacroStatements ($(the_macro.GetName())) :\n original=$original,\n"
-                    "expanded = $expanded\n");
-        CreateStatements(expanded,statements)
+            | Void => CodeTypeReferenceExpression("void")
+            | Null with val = null : object | String(val) | Float (val) | Double (val)
+            | Decimal (val) | Char (val) | Bool (val) 
+                        => CodePrimitiveExpression(val)
+            | Integer(val, is_negative, _treat_as) => 
+              def val = val :> long;
+              def val = if (is_negative) -val else val;
+              CodePrimitiveExpression(val)
+              
+            | Enum (_val : Literal.Integer, ty : TypeInfo, field : IField) =>
+              CodeFieldReferenceExpression(CodeTypeReferenceExpression(ty.FullName), field.Name)
     }
 
-    protected CreateStatements(expr : TExpr) : array[CodeStatement]
+        | <[ base ]> => CodeBaseReferenceExpression()
+        | <[ this ]> => CodeThisReferenceExpression()
+        | null       => CodeSnippetExpression("");
+        | _          => CodeSnippetExpression(expr.ToString())
+      }
+    }
+
+    protected ToStatements(expr : PExpr) : SCG.IEnumerable[CodeStatement]
     {
-      def statements = CodeStatementCollection();
-      CreateStatements(expr,statements);
-      def statementsArray = array(statements.Count);
-      statements.CopyTo(statementsArray, 0);
-      statementsArray
+      match (expr)
+      {
+        | PExpr.Sequence(exprs) =>
+          foreach (expr in exprs)
+            foreach (codeStatement in ToStatements(expr))
+              yield codeStatement;
+              
+        | PExpr.Assign(target, source) => 
+          yield CodeAssignStatement(ToExpression(target), ToExpression(source))
+          
+        | <[ when ($cond) $expr ]> =>
+          yield CodeConditionStatement(ToExpression(cond), ToStatements(expr).ToArray())
+
+        | <[ if ($cond) $trueExpr else $falseExpr ]> =>
+          yield CodeConditionStatement(ToExpression(cond),
+            ToStatements(trueExpr).ToArray(), ToStatements(falseExpr).ToArray())
+          
+        | _ => yield CodeExpressionStatement(ToExpression(expr))
+      }
     }
 
     protected virtual CreateStatement(expr : TExpr) : CodeStatement 
@@ -363,6 +412,68 @@
       //CodeCommentStatement("dummy")
     }
 
+    protected CreateParamsArray(parms : list [Parm]) : array[CodeExpression]
+    {
+      | [] => array[CodeSnippetExpression("")]
+      | _  => parms.MapToArray(p => CreateExpression(p.expr));
+    }
+
+    protected CreateCtorStatements(ctor : CodeConstructor, expr : TExpr) : void 
+    {
+      // TODO : now base(..) or this(...) constructor calls are supposed to be the first calls
+      //        in the ctor body. 
+      match(expr)
+      {
+        | TExpr.Sequence(e1,e2) => 
+          { CreateCtorStatements(ctor,e1); CreateStatements(e2,ctor.Statements); }
+        | TExpr.Call(func,parms,_) when (func is TExpr.Base) => 
+          ctor.BaseConstructorArgs.AddRange(CreateParamsArray(parms));
+        | TExpr.Call(func,parms,_) when (func is TExpr.This) => 
+          ctor.ChainedConstructorArgs.AddRange(CreateParamsArray(parms));
+        | _ => 
+          CreateStatements(expr,ctor.Statements); 
+      }
+    }
+
+    protected virtual CreateStatements(expr : TExpr, statements: CodeStatementCollection) : void 
+    {
+      match(expr)
+      {
+        | TExpr.Sequence(e1,e2) => 
+          { CreateStatements(e1,statements); CreateStatements(e2,statements); }
+
+        | TExpr.MacroEnvelope(original,the_macro,_) as expr => // original : Parsetree.PExpr; the_macro : IMacro; expanded : TExpr; }
+          // TODO: may be ignore or make code snippet?
+          Debug.Print($"--Expanding macro $(CodeDomHelper.PrintMacro(the_macro)),\n--- original:$original");
+          ProcessMacroStatements(expr,statements)
+          //CreateStatements(expanded,statements); 
+
+        | TExpr.DefValIn(name,val,body) => // name : LocalValue; val : TExpr; mutable body : TExpr; }
+          _ = statements.Add( CodeVariableDeclarationStatement(name.Type.SystemType, name.Name, CreateExpression(val)) );
+          CreateStatements(body,statements); 
+          
+        | _ => 
+          _ = statements.Add(CreateStatement(expr))
+      }
+    }
+
+    protected virtual ProcessMacroStatements(macroEnvelope : TExpr.MacroEnvelope, statements: CodeStatementCollection) : void
+    {
+      | (TExpr.MacroEnvelope(original,the_macro,expanded),_) => // original : Parsetree.PExpr; the_macro : IMacro; expanded : TExpr; }
+        Debug.Print($"  ProcessMacroStatements ($(the_macro.GetName())) :\n original=$original,\n"
+                    "expanded = $expanded\n");
+        CreateStatements(expanded,statements)
+    }
+
+    protected CreateStatements(expr : TExpr) : array[CodeStatement]
+    {
+      def statements = CodeStatementCollection();
+      CreateStatements(expr,statements);
+      def statementsArray = array(statements.Count);
+      statements.CopyTo(statementsArray, 0);
+      statementsArray
+    }
+
     protected TryCreateEventAction(func : TExpr, parms : list[Parm]) : CodeStatement
     {
       def eventActionAndName(name)

Modified: vs-plugin/trunk/Nemerle.VsIntegration/Project/NemerleFileNodeCodeDomProvider.cs
==============================================================================
--- vs-plugin/trunk/Nemerle.VsIntegration/Project/NemerleFileNodeCodeDomProvider.cs	(original)
+++ vs-plugin/trunk/Nemerle.VsIntegration/Project/NemerleFileNodeCodeDomProvider.cs	Thu Apr 19 06:26:14 2007
@@ -198,11 +198,18 @@
 			// AKhropov - in fact codeStream is ignored for now
 			string mainFilePath = PathOfMainFile();
 
+			ProjectInfo projectInfo = ProjectInfo.FindProject(mainFilePath);
+
+			if (projectInfo != null)
+			{
 			// TODO : can _project change for _fileNode?
 			return _codeDomParser.CreateCodeCompileUnit(
-				ProjectInfo.FindProject(mainFilePath).Project, mainFilePath,
+					projectInfo.Project, mainFilePath,
 				(IsFormSubType) ? PathOfDesignerFile() : null);
 		}
+			else
+				return null;
+		}
 		
 		#endregion
 



More information about the svn mailing list