private void EmitImportNode(ImportNode node, out AssociativeNode outnode)
        {
            Validity.Assert(node != null);

            ProtoCore.AST.AssociativeAST.ImportNode importNode = null;

            importNode            = new ProtoCore.AST.AssociativeAST.ImportNode();
            importNode.ModuleName = node.ModuleName;

            //(AstRootNode as CodeBlockNode).Body.Add(importNode);
            outnode = importNode;
        }
Beispiel #2
0
        private bool TryGetImportNode(string moduleName, string typeName, out ProtoCore.AST.AssociativeAST.ImportNode node)
        {
            if (mModuleTable.TryGetValue(moduleName, out node))
            {
                if (typeName.Length == 0) //All types
                {
                    return(true);
                }

                ProtoCore.AST.AssociativeAST.ClassDeclNode classNode;
                if (TryGetClassNode(node.CodeNode, typeName, out classNode) && !IsEmptyClassNode(classNode))
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #3
0
        private ProtoCore.AST.AssociativeAST.CodeBlockNode MergeCodeBlockNode(ref ProtoCore.AST.AssociativeAST.ImportNode importedNode,
                                                                              ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockNode)
        {
            if (codeBlockNode == null || codeBlockNode.Body == null || importedNode == null)
            {
                return(codeBlockNode);
            }

            ProtoCore.AST.AssociativeAST.CodeBlockNode importedCodeBlock = importedNode.CodeNode;
            if (importedCodeBlock == null)
            {
                importedNode.CodeNode = codeBlockNode;
                return(codeBlockNode);
            }

            foreach (var item in codeBlockNode.Body)
            {
                ProtoCore.AST.AssociativeAST.ClassDeclNode classNode = item as ProtoCore.AST.AssociativeAST.ClassDeclNode;
                if (classNode != null)
                {
                    ProtoCore.AST.AssociativeAST.ClassDeclNode importedClass = null;
                    if (TryGetClassNode(importedCodeBlock, classNode.className, out importedClass))
                    {
                        bool dummyClassNode   = IsEmptyClassNode(classNode);
                        bool dummyImportClass = IsEmptyClassNode(importedClass);

                        Validity.Assert(dummyImportClass || dummyClassNode, string.Format("{0} is imported more than once!!", classNode.className));
                        if (dummyImportClass && !dummyClassNode)
                        {
                            importedNode.CodeNode.Body.Remove(importedClass);
                            importedNode.CodeNode.Body.Add(classNode);
                        }
                    }
                    else
                    {
                        importedNode.CodeNode.Body.Add(classNode);
                    }
                }
                else
                {
                    importedNode.CodeNode.Body.Add(item); //TODO other conflict resolution needs to be done here.
                }
            }
            return(importedNode.CodeNode);
        }
Beispiel #4
0
        /// <summary>
        /// Resets the VM whenever a new library is imported and re-imports them
        /// Returns the list of new Library Mirrors for reflection
        /// TODO: It should not be needed once we have language support to insert import statements arbitrarily
        /// </summary>
        /// <param name="libraries"></param>
        /// <returns></returns>
        public List<LibraryMirror> ResetVMAndImportLibrary(List<string> libraries)
        {
            List<LibraryMirror> libs = new List<LibraryMirror>();

            // Reset VM
            ReInitializeLiveRunner();

            // generate import node for each library in input list
            List<AssociativeNode> importNodes = null;
            foreach (string lib in libraries)
            {
                importNodes = new List<AssociativeNode>();

                ProtoCore.AST.AssociativeAST.ImportNode importNode = new ProtoCore.AST.AssociativeAST.ImportNode();
                importNode.ModuleName = lib;

                importNodes.Add(importNode);

                ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(importNodes);
                string code = codeGen.GenerateCode();

                int currentCI = runnerCore.ClassTable.ClassNodes.Count;

                UpdateCmdLineInterpreter(code);

                int postCI = runnerCore.ClassTable.ClassNodes.Count;

                IList<ProtoCore.DSASM.ClassNode> classNodes = new List<ProtoCore.DSASM.ClassNode>();
                for (int i = currentCI; i < postCI; ++i)
                {
                    classNodes.Add(runnerCore.ClassTable.ClassNodes[i]);
                }

                ProtoCore.Mirror.LibraryMirror libraryMirror = ProtoCore.Mirror.Reflection.Reflect(lib, classNodes, runnerCore);
                libs.Add(libraryMirror);
            }

            return libs;
        }
Beispiel #5
0
        /// <summary>
        /// This is called temporarily to reset the VM and recompile the entire graph with new import 
        /// statements whenever a node from a new library is added to the graph.
        /// TODO: It should not be needed once we have language support to insert import statements arbitrarily
        /// </summary>
        /// <param name="libraries"></param>
        /// <param name="syncData"></param>
        public void ResetVMAndResyncGraph(IEnumerable<string> libraries)
        {
            // Reset VM
            ReInitializeLiveRunner();

            if (!libraries.Any())
            {
                return;
            }

            // generate import node for each library in input list
            List<AssociativeNode> importNodes = new List<AssociativeNode>();
            foreach (string lib in libraries)
            {
                ProtoCore.AST.AssociativeAST.ImportNode importNode = new ProtoCore.AST.AssociativeAST.ImportNode();
                importNode.ModuleName = lib;

                importNodes.Add(importNode);
            }
            ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(importNodes);
            string code = codeGen.GenerateCode();

            UpdateCmdLineInterpreter(code);
        }
 internal ImportNode Compile(ImportModuleHandler importer)
 {
     ImportNode impNode = null;
     ProtoCore.AST.AssociativeAST.CodeBlockNode code = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
     foreach (var item in mData)
     {
         SortedSet<Type> types = GetTypesForImport(item.Value.Data);
         foreach (var type in types)
         {
             if (CLRObjectMarshler.IsMarshaledAsNativeType(type))
                 continue;
             ImportNode node = importer.Import(type.Assembly.Location, type.FullName, "");
             if (impNode != null && node != null)
                 impNode.CodeNode.Body.AddRange(node.CodeNode.Body);
             else
                 impNode = node;
         }
         if (impNode == null)
             impNode = new ImportNode() { ModuleName="ExternalContext", CodeNode = new ProtoCore.AST.AssociativeAST.CodeBlockNode() };
         impNode.CodeNode.Body.Add(ContextDataMethodCallNode(item.Value));
     }
     return impNode;
 }
Beispiel #7
0
        public ProtoCore.AST.AssociativeAST.ImportNode Import(string moduleName, string typeName, string alias, int line = -1, int col = -1)
        {
            curLine = line;
            curCol = col;

            moduleName = moduleName.Replace("\"", String.Empty);
            ProtoCore.AST.AssociativeAST.ImportNode node = new ProtoCore.AST.AssociativeAST.ImportNode();
            node.ModuleName = moduleName;

            string modulePathFileName = FileUtils.GetDSFullPathName(moduleName, _coreObj.Options);

            // Tracking directory paths for all imported DS files during preload assembly stage so that they can be accessed by Graph compiler before execution - pratapa
            if (_coreObj.IsParsingPreloadedAssembly)
            {
                string dirName = Path.GetDirectoryName(modulePathFileName);
                if (!string.IsNullOrEmpty(dirName) && !_coreObj.Options.IncludeDirectories.Contains(dirName))
                    _coreObj.Options.IncludeDirectories.Add(dirName);
            }

            if (string.IsNullOrEmpty(typeName))
            {
                //Check if moduleName is a type name with namespace.
                Type type = Type.GetType(moduleName);
                if (type != null)
                {
                    typeName = type.FullName;
                    modulePathFileName = type.Assembly.Location;
                }
            }

            if (modulePathFileName == null || !File.Exists(modulePathFileName))
            {
                System.Diagnostics.Debug.Write(@"Cannot import file: '" + modulePathFileName);
                _coreObj.LogWarning(ProtoCore.BuildData.WarningID.FileNotFound, string.Format(Resources.kFileNotFound, modulePathFileName));
                return null;
            }

            node.ModulePathFileName = modulePathFileName;
            node.CodeNode = null;
            if (typeName.Length > 0)
                node.Identifiers.Add(typeName);

            ProtoCore.AST.AssociativeAST.ImportNode importedNode = null;
            if (TryGetImportNode(modulePathFileName, typeName, out importedNode))
            {
                node.HasBeenImported = true;
                return node;
            }

            ProtoCore.AST.AssociativeAST.CodeBlockNode codeNode = null;
            if (importedNode == null)
                codeNode = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            else
                codeNode = importedNode.CodeNode;

            try
            {
                codeNode = ImportCodeBlock(modulePathFileName, typeName, alias, codeNode);
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                    _coreObj.BuildStatus.LogSemanticError(ex.InnerException.Message);
                _coreObj.BuildStatus.LogSemanticError(ex.Message);
            }

            //Cache the codeblock of root import node.
            CodeBlockNode rootImportCodeBlock = mRootImportNode.CodeNode;
            mRootImportNode.CodeNode = new CodeBlockNode(); //reset with the new one.

            //Remove all empty nodes and add to root import node.
            codeNode.Body.RemoveAll(AddToRootImportNodeIfEmpty);

            if (mRootImportNode.CodeNode.Body.Count == 0) //empty
                mRootImportNode.CodeNode = rootImportCodeBlock; //reset the old one.

            if (importedNode != null)
            {
                //module has import node, but type is not imported yet.
                //MergeCodeBlockNode(ref importedNode, codeBlockNode);

                //update existing import node and return null.
                importedNode.CodeNode = codeNode;
                return null; 
            }

            node.CodeNode = codeNode;
            mModuleTable.Add(modulePathFileName, node);
            return node;
        }
        private void EmitImportNode(ImportNode node, out AssociativeNode outnode)
        {
            Validity.Assert(node != null);

            ProtoCore.AST.AssociativeAST.ImportNode importNode = null;

            importNode = new ProtoCore.AST.AssociativeAST.ImportNode();
            importNode.ModuleName = node.ModuleName;

            //(AstRootNode as CodeBlockNode).Body.Add(importNode);
            outnode = importNode;
        }
Beispiel #9
0
        public ProtoCore.AST.AssociativeAST.ImportNode Import(string moduleName, string typeName, string alias, int line = -1, int col = -1)
        {
            curLine = line;
            curCol  = col;

            moduleName = moduleName.Replace("\"", String.Empty);
            ProtoCore.AST.AssociativeAST.ImportNode node = new ProtoCore.AST.AssociativeAST.ImportNode();
            node.ModuleName = moduleName;

            string modulePathFileName = FileUtils.GetDSFullPathName(moduleName, _coreObj.Options);

            // Tracking directory paths for all imported DS files during preload assembly stage so that they can be accessed by Graph compiler before execution - pratapa
            if (_coreObj.IsParsingPreloadedAssembly)
            {
                string dirName = Path.GetDirectoryName(modulePathFileName);
                if (!string.IsNullOrEmpty(dirName) && !_coreObj.Options.IncludeDirectories.Contains(dirName))
                {
                    _coreObj.Options.IncludeDirectories.Add(dirName);
                }
            }

            if (string.IsNullOrEmpty(typeName))
            {
                //Check if moduleName is a type name with namespace.
                Type type = Type.GetType(moduleName);
                if (type != null)
                {
                    typeName           = type.FullName;
                    modulePathFileName = type.Assembly.Location;
                }
            }

            if (modulePathFileName == null || !File.Exists(modulePathFileName))
            {
                System.Diagnostics.Debug.Write(@"Cannot import file: '" + modulePathFileName);
                _coreObj.LogWarning(ProtoCore.BuildData.WarningID.kFileNotFound, string.Format(Resources.kFileNotFound, modulePathFileName));
                return(null);
            }

            node.ModulePathFileName = modulePathFileName;
            node.CodeNode           = null;
            if (typeName.Length > 0)
            {
                node.Identifiers.Add(typeName);
            }

            ProtoCore.AST.AssociativeAST.ImportNode importedNode = null;
            if (TryGetImportNode(modulePathFileName, typeName, out importedNode))
            {
                node.HasBeenImported = true;
                return(node);
            }

            ProtoCore.AST.AssociativeAST.CodeBlockNode codeNode = null;
            if (importedNode == null)
            {
                codeNode = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
            }
            else
            {
                codeNode = importedNode.CodeNode;
            }

            try
            {
                codeNode = ImportCodeBlock(modulePathFileName, typeName, alias, codeNode);
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    _coreObj.BuildStatus.LogSemanticError(ex.InnerException.Message);
                }
                _coreObj.BuildStatus.LogSemanticError(ex.Message);
            }

            //Cache the codeblock of root import node.
            CodeBlockNode rootImportCodeBlock = mRootImportNode.CodeNode;

            mRootImportNode.CodeNode = new CodeBlockNode(); //reset with the new one.

            //Remove all empty nodes and add to root import node.
            codeNode.Body.RemoveAll(AddToRootImportNodeIfEmpty);

            if (mRootImportNode.CodeNode.Body.Count == 0)       //empty
            {
                mRootImportNode.CodeNode = rootImportCodeBlock; //reset the old one.
            }
            if (importedNode != null)
            {
                //module has import node, but type is not imported yet.
                //MergeCodeBlockNode(ref importedNode, codeBlockNode);

                //update existing import node and return null.
                importedNode.CodeNode = codeNode;
                return(null);
            }

            node.CodeNode = codeNode;
            mModuleTable.Add(modulePathFileName, node);
            return(node);
        }