/// <summary> /// Generate code for all blocks in the workspace to the specified language. /// </summary> public string WorkspaceToCode(Workspace workspace) { Init(workspace); StringBuilder codeSB = new StringBuilder(); var blocks = workspace.GetTopBlocks(true); foreach (var block in blocks) { var line = this.BlockToCode(block).code; if (!string.IsNullOrEmpty(line)) { if (block.OutputConnection != null) { line = this.ScrubNakedValue(line); } codeSB.Append(line + "\n"); } } string code = this.Finish(codeSB.ToString()); // Final scrubbing of whitespace. Regex rgx = new Regex(@"^\s+\n"); code = rgx.Replace(code, ""); rgx = new Regex(@"\n\s+$"); code = rgx.Replace(code, "\n"); rgx = new Regex(@"[ \t]+\n"); code = rgx.Replace(code, "\n"); return(code); }
/// <summary> /// Encode a block tree as XML. /// </summary> /// <param name="workspace"> The workspace containing blocks.</param> /// <param name="optNoId"> Ture if the encoder should skip the block ids.</param> /// <returns> XML document.</returns> public static XmlNode WorkspaceToDom(Workspace workspace, bool optNoId = false) { var xml = XmlUtil.CreateDom("xml"); xml.AppendChild(VariablesToDom(workspace.GetAllVariables())); var blocks = workspace.GetTopBlocks(true); foreach (var block in blocks) { xml.AppendChild(BlockToDomWithXY(block, optNoId)); } return(xml); }
/// <summary> /// coroutine run code for workspace /// todo: execute topblocks in order or synchronously /// </summary> IEnumerator RunWorkspace(Workspace workspace) { //traverse all blocks in the workspace and run code for the blocks List <Block> blocks = workspace.GetTopBlocks(true); foreach (Block block in blocks) { //exclude the procedure definition blocks if (ProcedureDB.IsDefinition(block)) { continue; } yield return(RunBlock(block)); } mRunningProcess = null; CSharp.Interpreter.FireUpdate(new InterpreterUpdateState(InterpreterUpdateState.Stop)); }