Ejemplo n.º 1
0
        public void LookupResolvedName_ForPartialNestedExpression_RewriteAst()
        {
            var astNodes = CoreUtils.BuildASTList(core, "p = Autodesk.Point.ByCoordinates(Autodesk.Point.ByCoordinates(x, y, z).X, y, z).X;");

            var elementResolver = new ElementResolver();

            elementResolver.AddToResolutionMap("Autodesk.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p = Autodesk.DS.Geometry.Point.ByCoordinates(Autodesk.DS.Geometry.Point.ByCoordinates(x, y, z).X, y, z).X;\n", newNodes.ElementAt(0).ToString());

            /////////////////////////////////////
            astNodes = CoreUtils.BuildASTList(core, "p = Autodesk.DS.Point.ByCoordinates(Autodesk.Point.ByCoordinates(x, y, z).X, y, z).X;");

            elementResolver = new ElementResolver();
            elementResolver.AddToResolutionMap("Autodesk.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");
            elementResolver.AddToResolutionMap("Autodesk.DS.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p = Autodesk.DS.Geometry.Point.ByCoordinates(Autodesk.DS.Geometry.Point.ByCoordinates(x, y, z).X, y, z).X;\n", newNodes.ElementAt(0).ToString());

            //////////////////////////////////////
            astNodes = CoreUtils.BuildASTList(core, "p = Autodesk.DS.Geometry.Point.ByCoordinates(Autodesk.Point.ByCoordinates(x, y, z).X, y, z).X;");

            elementResolver = new ElementResolver();
            elementResolver.AddToResolutionMap("Autodesk.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");
            elementResolver.AddToResolutionMap("Autodesk.DS.Geometry.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p = Autodesk.DS.Geometry.Point.ByCoordinates(Autodesk.DS.Geometry.Point.ByCoordinates(x, y, z).X, y, z).X;\n", newNodes.ElementAt(0).ToString());
        }
Ejemplo n.º 2
0
        public void LookupResolvedName_ForAssociativeBlock_RewriteAst()
        {
            var code =
                "c = [Associative]" +
                "{" +
                "   a = [1, 2, 3, 4];" +
                "   b = Autodesk.Point.ByCoordinates(a);" +
                "   return = b;" +
                "}";

            var elementResolver = new ElementResolver();

            elementResolver.AddToResolutionMap("Autodesk.Point", "Autodesk.DesignScript.Geometry.Point",
                                               "Protogeometry.dll");

            var astNodes = CoreUtils.BuildASTList(core, code);
            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes).ToList();

            var lbn = ((BinaryExpressionNode)newNodes[0]).RightNode as LanguageBlockNode;

            Assert.IsNotNull(lbn);

            var cbn = lbn.CodeBlockNode as CodeBlockNode;

            Assert.IsNotNull(cbn);

            Assert.AreEqual("b = Autodesk.DesignScript.Geometry.Point.ByCoordinates(a);\n",
                            cbn.Body[1].ToString());
        }
Ejemplo n.º 3
0
        public void LookupResolvedName_ForGlobalClass_RewriteAst()
        {
            const string code   = @"import (""FFITarget.dll"");";
            var          mirror = thisTest.RunScriptSource(code);

            var testCore = thisTest.GetTestCore();
            var astNodes = CoreUtils.BuildASTList(testCore, "a = GlobalClass.GlobalClass(a);");

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes).ToList();

            Assert.AreEqual("a = GlobalClass.GlobalClass(a);\n", newNodes[0].ToString());

            astNodes = CoreUtils.BuildASTList(testCore, "a : GlobalClass;");

            newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes).ToList();

            Assert.AreEqual("a : GlobalClass", newNodes[0].ToString());

            // Add verification for contents of element resolver resolution map
            Assert.AreEqual(1, elementResolver.ResolutionMap.Count);

            var assembly     = elementResolver.LookupAssemblyName("GlobalClass");
            var resolvedName = elementResolver.LookupResolvedName("GlobalClass");

            Assert.AreEqual("FFITarget.dll", assembly);
            Assert.AreEqual("GlobalClass", resolvedName);
        }
Ejemplo n.º 4
0
        public void LookupResolvedName_ForPartialTypedIdentifier_RewriteAst()
        {
            var astNodes = CoreUtils.BuildASTList(core, "p : Autodesk.Point;");

            var elementResolver = new ElementResolver();

            elementResolver.AddToResolutionMap("Autodesk.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p : Autodesk.DS.Geometry.Point", newNodes.ElementAt(0).ToString());

            astNodes = CoreUtils.BuildASTList(core, "p : Autodesk.DS.Point;");

            elementResolver = new ElementResolver();
            elementResolver.AddToResolutionMap("Autodesk.DS.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p : Autodesk.DS.Geometry.Point", newNodes.ElementAt(0).ToString());

            astNodes = CoreUtils.BuildASTList(core, "p : Autodesk.DS.Geometry.Point;");

            elementResolver = new ElementResolver();
            elementResolver.AddToResolutionMap("Autodesk.DS.Geometry.Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p : Autodesk.DS.Geometry.Point", newNodes.ElementAt(0).ToString());
        }
Ejemplo n.º 5
0
        public void SkipResolvingName_ForPrimitiveTypedIdentifier_RetainAst()
        {
            var astNodes = CoreUtils.BuildASTList(core, "p : int;");

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, new ElementResolver(), astNodes);

            Assert.AreEqual("p : int", newNodes.ElementAt(0).ToString());
        }
Ejemplo n.º 6
0
        public void LookupResolvedName_ForGlobalFunction_RewriteAst()
        {
            var astNodes = CoreUtils.BuildASTList(core, "a = Flatten(a).DifferenceAll(Flatten(b));");

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes).ToList();

            Assert.AreEqual("a = Flatten(a).DifferenceAll(Flatten(b));\n", newNodes[0].ToString());
        }
Ejemplo n.º 7
0
        public void LookupResolvedName_FromElementResolver_RewriteAst()
        {
            var astNodes = CoreUtils.BuildASTList(core, "p = Point.ByCoordinates(0,0,0);");

            var elementResolver = new ElementResolver();

            elementResolver.AddToResolutionMap("Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p = Autodesk.DS.Geometry.Point.ByCoordinates(0, 0, 0);\n", newNodes.ElementAt(0).ToString());
        }
Ejemplo n.º 8
0
        public void LookupResolvedName_FromElementResolver_RewriteAst2()
        {
            // TODO pratapa: Return to fix test with Dictionary.ValueAtKey method

            var astNodes = CoreUtils.BuildASTList(core, "p = Geometry.Point.ByCoordinates(0,0,0)[0];");

            var elementResolver = new ElementResolver();

            elementResolver.AddToResolutionMap("Point", "Autodesk.DS.Geometry.Point", "Protogeometry.dll");

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("p = Autodesk.DS.Geometry.Point.ByCoordinates(0, 0, 0)[0];\n", newNodes.ElementAt(0).ToString());
        }
Ejemplo n.º 9
0
        public void LookupResolvedName_ForNestedNamespacesExpressionFromCompiler_RewriteAst()
        {
            const string code   = @"import (""FFITarget.dll"");";
            var          mirror = thisTest.RunScriptSource(code);

            var    testCore  = thisTest.GetTestCore();
            string class1    = "NestedResolverTarget";
            string class2    = "ElementResolverTarget";
            string fullName1 = "FFITarget.NameSpaceA.NameSpaceB.NameSpaceC.NestedResolverTarget";
            string fullName2 = "FFITarget.ElementResolverTarget";
            var    astNodes  = CoreUtils.BuildASTList(testCore, string.Format("d = {0}.Property.Method({1}.Create().Property.Method({0}.Property.Property));", class1, class2));

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes);

            Assert.AreEqual(
                string.Format("d = {0}.Property.Method({1}.Create().Property.Method({0}.Property.Property));\n", fullName1, fullName2),
                newNodes.ElementAt(0).ToString());

            // Add verification for contents of element resolver resolution map
            var assembly     = elementResolver.LookupAssemblyName(class2);
            var resolvedName = elementResolver.LookupResolvedName(class2);

            Assert.AreEqual("FFITarget.dll", assembly);
            Assert.AreEqual(fullName2, resolvedName);

            ///////////////////////////////////////////////
            astNodes = CoreUtils.BuildASTList(testCore, string.Format("d = {0}.Property.Method({1}.Create().Property.Method({0}.Property.Property));", fullName1, fullName2));

            elementResolver = new ElementResolver();

            newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes);

            Assert.AreEqual(
                string.Format("d = {0}.Property.Method({1}.Create().Property.Method({0}.Property.Property));\n", fullName1, fullName2),
                newNodes.ElementAt(0).ToString());

            // Add verification for contents of element resolver resolution map
            assembly     = elementResolver.LookupAssemblyName(fullName2);
            resolvedName = elementResolver.LookupResolvedName(fullName2);

            Assert.AreEqual("FFITarget.dll", assembly);
            Assert.AreEqual(fullName2, resolvedName);
        }
Ejemplo n.º 10
0
        public void LookupResolvedName_ForFunctionDefinition_RewriteAst()
        {
            var code =
                "def foo()" +
                "{" +
                "   return = Autodesk.DesignScript.Geometry.Point.ByCoordinates();" +
                "}" +
                "a = foo();";

            var elementResolver = new ElementResolver();

            elementResolver.AddToResolutionMap("Point", "Autodesk.DesignScript.Geometry.Point", "Protogeometry.dll");

            var astNodes = CoreUtils.BuildASTList(core, code);
            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes).ToList();

            Assert.AreEqual("return = Autodesk.DesignScript.Geometry.Point.ByCoordinates();\n",
                            ((FunctionDefinitionNode)newNodes[0]).FunctionBody.ToString());
        }
Ejemplo n.º 11
0
        public void LookupResolvedName_ForTypedIdentifierFromCompiler_RewriteAst()
        {
            const string code   = @"import (""FFITarget.dll"");";
            var          mirror = thisTest.RunScriptSource(code);

            var testCore = thisTest.GetTestCore();
            var astNodes = CoreUtils.BuildASTList(testCore, "d : ElementResolverTarget;");

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("d : FFITarget.ElementResolverTarget", newNodes.ElementAt(0).ToString());

            // Add verification for contents of element resolver resolution map
            var assembly     = elementResolver.LookupAssemblyName("ElementResolverTarget");
            var resolvedName = elementResolver.LookupResolvedName("ElementResolverTarget");

            Assert.AreEqual("FFITarget.dll", assembly);
            Assert.AreEqual("FFITarget.ElementResolverTarget", resolvedName);
        }
Ejemplo n.º 12
0
        public void LookupResolvedName_AfterAddingHiddenClass_DoNotRewriteAst()
        {
            const string code   = @"import (""FFITarget.dll"");";
            var          mirror = thisTest.RunScriptSource(code);

            var core = thisTest.GetTestCore();

            // Call method on class hidden in library.
            var astNodes = CoreUtils.BuildASTList(core, "p = C.DupTargetTest.DupTargetTest();");

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(core.ClassTable, elementResolver, astNodes);

            // Assert for fully resolved name of hidden class returned from ElementRewriter
            Assert.AreEqual("p = FFITarget.C.B.DupTargetTest.DupTargetTest();\n", newNodes.ElementAt(0).ToString());

            // Assert that element resolver does not hold namespace mapping for hidden class.
            KeyValuePair <string, string> resolvedName;

            Assert.IsFalse(elementResolver.ResolutionMap.TryGetValue("FFITarget.C.B.DupTargetTest", out resolvedName));
        }
Ejemplo n.º 13
0
        private void VerifyResult(string fullName, string partialName, string functionOrProperty, bool isProperty = false)
        {
            var testCore = thisTest.GetTestCore();

            var astNodes = CoreUtils.BuildASTList(testCore, string.Format("d = {0}.{1};", partialName, functionOrProperty));

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes);

            Assert.AreEqual(string.Format("d = {0}.{1};\n", fullName, functionOrProperty), newNodes.ElementAt(0).ToString());

            if (!isProperty)
            {
                // Add verification for contents of element resolver resolution map
                var assembly     = elementResolver.LookupAssemblyName(partialName);
                var resolvedName = elementResolver.LookupResolvedName(partialName);

                Assert.AreEqual("FFITarget.dll", assembly);
                Assert.AreEqual(fullName, resolvedName);
            }
        }
Ejemplo n.º 14
0
        public void LookupResolvedName_ForNestedExpressionFromCompiler_RewriteAst()
        {
            const string code   = @"import (""FFITarget.dll"");";
            var          mirror = thisTest.RunScriptSource(code);

            var testCore = thisTest.GetTestCore();
            var astNodes = CoreUtils.BuildASTList(testCore, "d = ElementResolverTarget.Create().Property.Method(ElementResolverTarget.Create().Property);");

            var elementResolver = new ElementResolver();

            var newNodes = ElementRewriter.RewriteElementNames(testCore.ClassTable, elementResolver, astNodes);

            Assert.AreEqual("d = FFITarget.ElementResolverTarget.Create().Property.Method(FFITarget.ElementResolverTarget.Create().Property);\n", newNodes.ElementAt(0).ToString());

            // Add verification for contents of element resolver resolution map
            Assert.AreEqual(1, elementResolver.ResolutionMap.Count);

            var assembly     = elementResolver.LookupAssemblyName("ElementResolverTarget");
            var resolvedName = elementResolver.LookupResolvedName("ElementResolverTarget");

            Assert.AreEqual("FFITarget.dll", assembly);
            Assert.AreEqual("FFITarget.ElementResolverTarget", resolvedName);
        }
Ejemplo n.º 15
0
        private static bool CompileCodeBlockAST(Core core, ParseParam parseParams, IDictionary <string, string> priorNames)
        {
            var unboundIdentifiers = new Dictionary <int, List <VariableLine> >();

            ProtoCore.BuildStatus buildStatus = null;
            try
            {
                int blockId = ProtoCore.DSASM.Constants.kInvalidIndex;


                bool parsingPreloadFlag = core.IsParsingPreloadedAssembly;
                bool parsingCbnFlag     = core.IsParsingPreloadedAssembly;
                core.IsParsingPreloadedAssembly = false;
                core.IsParsingCodeBlockNode     = true;

                core.ResetForPrecompilation();

                var astNodes = parseParams.ParsedNodes;

                // Lookup namespace resolution map in elementResolver to rewrite
                // partial classnames with their fully qualified names in ASTs
                // before passing them for pre-compilation. If partial class is not found in map,
                // update Resolution map in elementResolver with fully resolved name from compiler.
                var reWrittenNodes = ElementRewriter.RewriteElementNames(core.ClassTable,
                                                                         parseParams.ElementResolver, astNodes, core.BuildStatus.LogSymbolConflictWarning);

                if (priorNames != null)
                {
                    // Use migration rewriter to migrate old method names to new names based on priorNameHints from LibraryServices
                    reWrittenNodes = MigrationRewriter.MigrateMethodNames(reWrittenNodes, priorNames, core.BuildStatus.LogDeprecatedMethodWarning);
                }

                // Clone a disposable copy of AST nodes for PreCompile() as Codegen mutates AST's
                // while performing SSA transforms and we want to keep the original AST's
                var codeblock = new CodeBlockNode();
                var nodes     = reWrittenNodes.OfType <AssociativeNode>().Select(NodeUtils.Clone).ToList();
                codeblock.Body.AddRange(nodes);

                buildStatus = PreCompile(string.Empty, core, codeblock, out blockId);

                core.IsParsingCodeBlockNode     = parsingCbnFlag;
                core.IsParsingPreloadedAssembly = parsingPreloadFlag;

                parseParams.AppendErrors(buildStatus.Errors);
                parseParams.AppendWarnings(buildStatus.Warnings);

                if (buildStatus.ErrorCount > 0)
                {
                    return(false);
                }
                IEnumerable <BuildData.WarningEntry> warnings = buildStatus.Warnings;

                // Get the unboundIdentifiers from the warnings
                GetInputLines(parseParams.ParsedNodes, warnings, unboundIdentifiers);
                foreach (KeyValuePair <int, List <VariableLine> > kvp in unboundIdentifiers)
                {
                    foreach (VariableLine vl in kvp.Value)
                    {
                        parseParams.AppendUnboundIdentifier(vl.displayName, vl.variable);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                buildStatus = null;
                return(false);
            }
        }