private static ProtoLanguage.CompileStateTracker BuildCompilerState(ProtoLanguage.CompileOptions options)
 {
     ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);
     compileState.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive());
     compileState.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive());
     return(compileState);
 }
Beispiel #2
0
        /// <summary>
        /// TODO Jun : Remove me after the tracker + core swap-out
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string GetDSFullPathName(string fileName, ProtoLanguage.CompileOptions options = null)
        {
            //1.  First search at .exe module directory, in case files of the same name exists in the following directories.
            //    The .exe module directory is of highest priority.
            //    CodeBase is used here because Assembly.Location does not work quite well when the module is shallow-copied in nunit test.
            if (Path.IsPathRooted(fileName))
            {
                if (File.Exists(fileName))
                {
                    return(Path.GetFullPath(fileName));
                }
                fileName = Path.GetFileName(fileName);
            }

            string fullPathName;

            if (GetFullPath(fileName, GetInstallLocation(), out fullPathName))
            {
                return(fullPathName);
            }

            //2. Search relative to the .ds file directory
            string rootModulePath = ".";

            if (null != options && !string.IsNullOrEmpty(options.RootModulePathName))
            {
                rootModulePath = options.RootModulePathName;
            }

            if (GetFullPath(fileName, rootModulePath, out fullPathName))
            {
                return(fullPathName);
            }

            if (null != options)
            {
                //3. Search at include directories.
                //   This will include the import path.
                foreach (string directory in options.IncludeDirectories)
                {
                    fullPathName = Path.Combine(directory, fileName);
                    if (null != fullPathName && File.Exists(fullPathName))
                    {
                        return(fullPathName);
                    }
                }
            }

            //4. Search the absolute path or relative to the current directory
            if (File.Exists(fileName))
            {
                return(Path.GetFullPath(fileName));
            }

            return(fileName);
        }
 public static ProtoLanguage.CompileStateTracker BuildDebuggertCompilerState(Dictionary <string, Object> context = null)
 {
     ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
     options.IDEDebugMode = true;
     ProtoLanguage.CompileStateTracker compileState = BuildCompilerState(options);
     if (null != context)
     {
         compileState.AddContextData(context);
     }
     return(compileState);
 }
 public static ProtoLanguage.CompileStateTracker BuildLiveRunnerCompilerState(Dictionary <string, Object> context = null)
 {
     ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
     options.IsDeltaExecution = true;
     ProtoLanguage.CompileStateTracker compileState = BuildCompilerState(options);
     if (null != context)
     {
         compileState.AddContextData(context);
     }
     return(compileState);
 }
        public static ProtoLanguage.CompileStateTracker BuildDefaultCompilerState(bool isDeltaExecution = false, Dictionary <string, Object> context = null)
        {
            if (isDeltaExecution)
            {
                return(BuildLiveRunnerCompilerState(context));
            }

            ProtoLanguage.CompileOptions      options      = new ProtoLanguage.CompileOptions();
            ProtoLanguage.CompileStateTracker compileState = BuildCompilerState(options);
            if (null != context)
            {
                compileState.AddContextData(context);
            }
            return(compileState);
        }
        public static ProtoLanguage.CompileStateTracker BuildDefaultCompilerState(Dictionary<string, Object> context = null)
        {
            ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();

            ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);

            compileState.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive());
            compileState.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive());

            if (null != context)
            {
                compileState.AddContextData(context);
            }

            return compileState;
        }
        public static ProtoLanguage.CompileStateTracker BuildDefaultCompilerState(Dictionary <string, Object> context = null)
        {
            ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();

            ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);

            compileState.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive());
            compileState.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive());

            if (null != context)
            {
                compileState.AddContextData(context);
            }

            return(compileState);
        }
        private ProtoLanguage.CompileStateTracker CompileCodeSnapshot(AutoCompleteWorkData workData)
        {
            if (null != this.scopeIdentifiers)
            {
                this.scopeIdentifiers.Clear();
                this.scopeIdentifiers = null;
            }

            ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
            options.RootModulePathName = workData.ScriptPath;
            ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);

            compileState.CurrentDSFileName = workData.ScriptPath;
            ProtoFFI.DLLFFIHandler.Register(ProtoFFI.FFILanguage.CSharp, new ProtoFFI.CSModuleHelper());

            // Register a message stream if we do have one.
            if (null != this.MessageHandler)
            {
                compileState.BuildStatus.MessageHandler = this.MessageHandler;
            }

            MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(workData.CodeSnapshot));

            ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(stream);
            ProtoCore.DesignScriptParser.Parser  p = new ProtoCore.DesignScriptParser.Parser(s, compileState);

            try
            {
                p.Parse();
                CoreCodeGen.arrayTypeTable = new ArrayTypeTable();
                AssociativeCodeGen codegen = new AssociativeCodeGen(compileState);

                codegen.Emit(p.root as ProtoCore.AST.AssociativeAST.CodeBlockNode);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception Caught in CodeGen");
                System.Diagnostics.Debug.WriteLine(ex.Message);
                compileState = null;
            }
            finally
            {
                // Do necessary clean-up here.
            }

            return(compileState);
        }
Beispiel #9
0
        public static bool Compile(string src, string entryFile)
        {
            ProtoLanguage.CompileOptions ops = new ProtoLanguage.CompileOptions();
            ops.RootModulePathName = entryFile;
            if (null != AutoCompletionHelper.searchPaths)
            {
                ops.IncludeDirectories.AddRange(AutoCompletionHelper.searchPaths);
            }

            compileState = new ProtoLanguage.CompileStateTracker(ops);

            compileState.CurrentDSFileName = entryFile;

            CoreCodeGen.ResetInternalStates();
            ProtoFFI.DLLFFIHandler.Register(ProtoFFI.FFILanguage.CSharp, new ProtoFFI.CSModuleHelper());

            // Register a message stream if we do have one.
            if (null != AutoCompletionHelper.MessageHandler)
            {
                compileState.BuildStatus.MessageHandler = MessageHandler;
            }

            MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(src));

            ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(ms);
            ProtoCore.DesignScriptParser.Parser  p = new ProtoCore.DesignScriptParser.Parser(s, compileState);

            try
            {
                p.Parse();

                CoreCodeGen.arrayTypeTable = new ArrayTypeTable();
                AssociativeCodeGen codegen = new AssociativeCodeGen(compileState);
                codegen.Emit(p.root as ProtoCore.AST.AssociativeAST.CodeBlockNode);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception Caught in CodeGen");
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return(false);
            }

            return(true);
        }
        public static List<SnapshotNode> NodeToCodeBlocks(List<SnapshotNode> inputs, GraphToDSCompiler.GraphCompiler originalGC)
        {
            List<SnapshotNode> codeBlocks = new List<SnapshotNode>();
            GraphToDSCompiler.GraphCompiler newGC = GraphCompiler.CreateInstance();

            newGC.SetCore(compileState);
            GraphToDSCompiler.SynchronizeData newSyncData = new SynchronizeData();
            newSyncData.AddedNodes = inputs;
            newSyncData.ModifiedNodes = new List<SnapshotNode>();
            newSyncData.RemovedNodes = new List<uint>();
            GraphToDSCompiler.GraphBuilder GB = new GraphBuilder(newSyncData, newGC);

            #region fix connection for multi-line CBN
            /*for multi-line code blocks*/
            List<Node> completeList = originalGC.Graph.nodeList;
            List<uint> originalNodeUIDList = new List<uint>();
            foreach (Node oriGcNode in completeList)
            {
                originalNodeUIDList.Add(oriGcNode.Guid);
            }

            GB.AddNodesToAST();

            //List<SnapshotNode> inputsCopy = new List<SnapshotNode>(inputs);
            for (int i = 0; i < inputs.Count; i++)
            {
                SnapshotNode inputSnapshotNode = inputs[i];
                for (int j = 0; j < inputSnapshotNode.InputList.Count; j++)
                {
                    Connection inputConnection = inputSnapshotNode.InputList[j];
                    if (!originalNodeUIDList.Contains(inputConnection.OtherNode))
                    {
                        Connection correctedInputConnection = new Connection();
                        correctedInputConnection.LocalName = inputConnection.LocalName;
                        correctedInputConnection.LocalIndex = inputConnection.LocalIndex;
                        correctedInputConnection.IsImplicit = inputConnection.IsImplicit;
                        correctedInputConnection.OtherIndex = inputConnection.OtherIndex;
                        if (newGC.codeBlockUIDMap.ContainsKey(inputConnection.OtherNode))
                        {
                            correctedInputConnection.OtherNode = newGC.codeBlockUIDMap[inputConnection.OtherNode][inputConnection.OtherIndex];
                        }
                        else
                        {
                            correctedInputConnection.OtherNode = originalGC.codeBlockUIDMap[inputConnection.OtherNode][inputConnection.OtherIndex];
                        }
                        inputSnapshotNode.InputList.Remove(inputConnection);
                        inputSnapshotNode.InputList.Insert(j, correctedInputConnection);
                    }
                }
                for (int j = 0; j < inputSnapshotNode.OutputList.Count; j++)
                {
                    Connection outputConnection = inputSnapshotNode.OutputList[j];
                    if (!originalNodeUIDList.Contains(outputConnection.OtherNode))                       // if the other node is split
                    {
                        Connection correctedInputConnection = new Connection();
                        correctedInputConnection.LocalName = outputConnection.LocalName;
                        correctedInputConnection.LocalIndex = outputConnection.LocalIndex;
                        correctedInputConnection.IsImplicit = outputConnection.IsImplicit;
                        correctedInputConnection.OtherIndex = outputConnection.OtherIndex;
                        if (newGC.codeBlockUIDMap.ContainsKey(outputConnection.OtherNode))
                        {
                            correctedInputConnection.OtherNode = newGC.GetUidOfRHSIdentifierInCodeBlock(
                                outputConnection.OtherNode, outputConnection.OtherIndex, outputConnection.LocalName);
                            //correctedInputConnection.OtherNode = newGC.codeBlockUIDMap[outputConnection.OtherNode][outputConnection.OtherIndex];
                        }
                        else
                        {
                            correctedInputConnection.OtherNode = originalGC.codeBlockUIDMap[outputConnection.OtherNode][outputConnection.OtherIndex];
                        }
                        inputSnapshotNode.OutputList.Remove(outputConnection);
                        inputSnapshotNode.OutputList.Insert(j, correctedInputConnection);
                    }
                }
            }
            GB.nodesToAdd = inputs;
            GB.MakeConnectionsForAddedNodes_NodeToCode(GB.nodesToAdd);
            newGC.PrintGraph();
            #endregion

            //GB.BuildGraphForCodeBlock();

            List<uint> nodesToBeAdded = new List<uint>();
            List<Node> nodesToBeReplaced = new List<Node>();

            //adding children node from the originalGC to the newGC needed for the newGC to generate code
            foreach (Node n in completeList)
            {
                foreach (Node child in n.GetChildren())
                {
                    if (newGC.Graph.GetNode(child.Guid) != null)
                    {
                        if (child.Name != newGC.Graph.GetNode(child.Guid).Name)
                        {
                            nodesToBeReplaced.Add(child);
                        }
                    }
                }
            }

            foreach (uint n in nodesToBeAdded)
            {
                Node n1 = completeList.FirstOrDefault(q => q.Guid == n);
                //n1.children.Clear();
                nodesToBeReplaced.Add(n1);
                //newSyncData.RemovedNodes.Add(n);
            }

            List<uint> nodeToCodeUIDs = new List<uint>();
            foreach (SnapshotNode ssn in inputs)
                nodeToCodeUIDs.Add(ssn.Id);
            newGC.nodeToCodeUIDs = nodeToCodeUIDs;

            /*create output snapshot nodes*/
            List<Connection> inputNodeInputConnections = new List<Connection>();
            List<Connection> inputNodeOutputConnections = new List<Connection>();
            foreach (SnapshotNode ssn in inputs)
            {
                foreach (Connection inputConnection in ssn.InputList)
                {
                    if (!nodeToCodeUIDs.Contains(inputConnection.OtherNode))
                        inputNodeInputConnections.Add(inputConnection);
                }
                foreach (Connection outputConnection in ssn.OutputList)
                {
                    if (!nodeToCodeUIDs.Contains(outputConnection.OtherNode))
                        inputNodeOutputConnections.Add(outputConnection);
                }
            }

            newGC.ReplaceNodesFromAList(nodesToBeReplaced);
            newSyncData.AddedNodes = new List<SnapshotNode>();
            newSyncData.ModifiedNodes = new List<SnapshotNode>();
            newSyncData.RemovedNodes = new List<uint>();
            GB = new GraphBuilder(newSyncData, newGC);
            //string result = GB.BuildGraphDAG();
            List<SnapshotNode> nodeToCodeBlocks = GB.PrintCodeForSelectedNodes(originalGC, inputs);

            /*for now, only connected nodes are supported: return all connections that are not internal connections (connections between the selected nodes)*/
            //uint id = 0;
            //foreach (string content in toCode)
            //{
            //    SnapshotNode ssn = new SnapshotNode();
            //    ssn.Type = SnapshotNodeType.CodeBlock;
            //    ssn.Content = content;
            //    ssn.Id = id++;
            //    ssn.InputList = new List<Connection>();
            //    //stupid stub
            //    foreach (Connection inputConnection in inputNodeInputConnections)
            //    {
            //        Connection newInputConnection = new Connection();
            //        newInputConnection.OtherNode = inputConnection.OtherNode;
            //        newInputConnection.OtherIndex = inputConnection.OtherIndex;
            //        newInputConnection.IsImplicit = inputConnection.IsImplicit;
            //        string[] tokens = newGC.Graph.GetNode(inputConnection.OtherNode).Name.Split('=');
            //        newInputConnection.LocalName = tokens[0];
            //        ssn.InputList.Add(newInputConnection);
            //    }
            //    //ssn.InputList = inputNodeInputConnections;
            //    ssn.OutputList = new List<Connection>();
            //    foreach (Connection outputConnection in inputNodeOutputConnections)
            //    {
            //        Connection newOutputConnection = new Connection();
            //        newOutputConnection.OtherNode = outputConnection.OtherNode;
            //        newOutputConnection.OtherIndex = outputConnection.OtherIndex;
            //        newOutputConnection.IsImplicit = outputConnection.IsImplicit;
            //        //string[] tokens = originalGC.Graph.GetNode(outputConnection.OtherNode).Name.Split('=');
            //        newOutputConnection.LocalName = outputConnection.LocalName;
            //        ssn.OutputList.Add(newOutputConnection);
            //    }
            //    //ssn.OutputList = inputNodeOutputConnections;
            //    codeBlocks.Add(ssn);
            //}

            /*update the original GC*/
            foreach (SnapshotNode inputNode in inputs)
            {
                if (originalNodeUIDList.Contains(inputNode.Id))
                    originalGC.RemoveNodes(inputNode.Id, false);
                else
                {
                    foreach (KeyValuePair<uint, Dictionary<int, uint>> kvp in originalGC.codeBlockUIDMap)
                    {
                        if (kvp.Value.ContainsValue(inputNode.Id))
                        {
                            originalGC.RemoveNodes(kvp.Key, false);
                        }
                    }
                }
            }
            foreach (Node node in newGC.Graph.nodeList)
            {
                node.Name = node.Name.TrimEnd(';') + ";";
            }
            originalGC.Graph.nodeList.Union<Node>(newGC.Graph.nodeList);
            //originalGC = newGC;
            /**/

            return nodeToCodeBlocks;
            //return codeBlocks;
        }

        /// <summary>
        /// This is called to Parse individual assignment statements in Codeblock nodes in GraphUI
        /// and return the resulting ProtoAST node
        /// </summary>
        /// <param name="statement"></param>
        public static ProtoCore.AST.Node Parse(string statement, out ProtoCore.AST.AssociativeAST.CodeBlockNode commentNode)
        {
            commentNode = null;
            BuildCompileState(true);

            Validity.Assert(compileState != null);
            Validity.Assert(statement != null);

            if (string.IsNullOrEmpty(statement))
                return null;

            System.IO.MemoryStream memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(statement));
            ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(memstream);
            ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, compileState);

            p.Parse();
            commentNode = p.commentNode;

            return p.root;
        }

        public static List<ProtoCore.AST.Node> ParseCodeBlock(string code)
        {
            Validity.Assert(code != null);

            if (string.IsNullOrEmpty(code))
                return null;

            // TODO: Change the logic to ignore Import statements in this case using parser - pratapa
            // Check if this will work with modifier blocks as well
            /*string[] stmts = code.Split(';');
            string source = "";
            for (int i=0; i < stmts.Length; ++i)
            {
                if (!stmts[i].Contains("import"))
                    source += stmts[i] + ";";
            }*/

            ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
            ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);

            //compileState.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive(compileState));
            //compileState.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive(compileState));

            System.IO.MemoryStream memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(code));
            ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(memstream);
            ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, compileState);

            p.Parse();
            ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = p.root as ProtoCore.AST.AssociativeAST.CodeBlockNode;

            Validity.Assert(cbn != null);
            return p.GetParsedASTList(cbn);
        }
        private static bool LocateAssembly(string assembly, out string assemblyPath)
        {
            ProtoLanguage.CompileOptions options = null;
            if (null == compileState)
                options = new ProtoLanguage.CompileOptions() { RootModulePathName = GraphUtilities.rootModulePath };
            else
                options = compileState.Options;

            assemblyPath = FileUtils.GetDSFullPathName(assembly, options);
            string dirName = Path.GetDirectoryName(assemblyPath);
            if (!string.IsNullOrEmpty(dirName) && !compileState.Options.IncludeDirectories.Contains(dirName))
            {
                compileState.Options.IncludeDirectories.Add(dirName);
            }

            return File.Exists(assemblyPath);
        }
        private static void BuildCompileState(bool isCodeBlockNode = false, bool isPreloadedAssembly = false)
        {
            if (compileState == null)
            {
                ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
                options.RootModulePathName = rootModulePath;
                compileState = new ProtoLanguage.CompileStateTracker(options);
                compileState.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive());
                compileState.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive());

            }
            else
            {
                compileState.ResetForPrecompilation();
            }

            compileState.IsParsingPreloadedAssembly = isPreloadedAssembly;
            compileState.IsParsingCodeBlockNode = isCodeBlockNode;
            compileState.ParsingMode = ProtoCore.ParseMode.AllowNonAssignment;
        }
        public static List<ProtoCore.AST.Node> ParseCodeBlock(string code)
        {
            Validity.Assert(code != null);

            if (string.IsNullOrEmpty(code))
                return null;

            // TODO: Change the logic to ignore Import statements in this case using parser - pratapa
            // Check if this will work with modifier blocks as well
            /*string[] stmts = code.Split(';');
            string source = "";
            for (int i=0; i < stmts.Length; ++i)
            {
                if (!stmts[i].Contains("import"))
                    source += stmts[i] + ";";
            }*/


            ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
            ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);
            
            //compileState.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive(compileState));
            //compileState.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive(compileState));

            System.IO.MemoryStream memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(code));
            ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(memstream);
            ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, compileState);

            p.Parse();
            ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = p.root as ProtoCore.AST.AssociativeAST.CodeBlockNode;

            Validity.Assert(cbn != null);
            return p.GetParsedASTList(cbn);
        }
        private ProtoLanguage.CompileStateTracker CompileCodeSnapshot(AutoCompleteWorkData workData)
        {
            if (null != this.scopeIdentifiers)
            {
                this.scopeIdentifiers.Clear();
                this.scopeIdentifiers = null;
            }

            ProtoLanguage.CompileOptions options = new ProtoLanguage.CompileOptions();
            options.RootModulePathName = workData.ScriptPath;
            ProtoLanguage.CompileStateTracker compileState = new ProtoLanguage.CompileStateTracker(options);

            compileState.CurrentDSFileName = workData.ScriptPath;
            ProtoFFI.DLLFFIHandler.Register(ProtoFFI.FFILanguage.CSharp, new ProtoFFI.CSModuleHelper());

            // Register a message stream if we do have one.
            if (null != this.MessageHandler)
                compileState.BuildStatus.MessageHandler = this.MessageHandler;

            MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(workData.CodeSnapshot));
            ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(stream);
            ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, compileState);

            try
            {
                p.Parse();
                CoreCodeGen.arrayTypeTable = new ArrayTypeTable();
                AssociativeCodeGen codegen = new AssociativeCodeGen(compileState);

                codegen.Emit(p.root as ProtoCore.AST.AssociativeAST.CodeBlockNode);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception Caught in CodeGen");
                System.Diagnostics.Debug.WriteLine(ex.Message);
                compileState = null;
            }
            finally
            {
                // Do necessary clean-up here.
            }

            return compileState;
        }