/// <summary> /// Class for a workspace. This is a data structure tha contains blocks. /// There is no UI,and can be created headlessly. /// </summary> public Workspace(WorkspaceOptions options = null, string optId = null) { if (string.IsNullOrEmpty(optId)) { Id = Utils.GenUid(); } else { Id = optId; } if (mWorkspaceDB.ContainsKey(Id)) { mWorkspaceDB[Id] = this; Debug.LogWarning("Already contains workspace id:" + Id); } else { mWorkspaceDB.Add(Id, this); } Options = options ?? new WorkspaceOptions(); TopBlocks = new List <Block>(); BlockDB = new Dictionary <string, Block>(); VariableMap = new VariableMap(this); ConnectionDBList = ConnectionDB.Build(); ProcedureDB = new ProcedureDB(this); }
private object Procedure_DefReturn(Block block) { string funcName = Lua.VariableNames.GetName(block.GetFieldValue("NAME"), Define.PROCEDURE_CATEGORY_NAME); string branch = Lua.Generator.StatementToCode(block, "STACK", ""); string returnValue = Lua.Generator.ValueToCode(block, "RETURN", Lua.ORDER_NONE); if (!string.IsNullOrEmpty(returnValue)) { returnValue = " return " + returnValue + "\n"; } StringBuilder argumentSB = new StringBuilder(); List <string> arguments = ProcedureDB.GetProcedureArguments(block); for (int i = 0; i < arguments.Count; i++) { argumentSB.Append(Lua.VariableNames.GetName(arguments[i], Define.VARIABLE_CATEGORY_NAME)); if (i < arguments.Count - 1) { argumentSB.Append(", "); } } string code = string.Format("function {0}({1})\n{2}{3}end\n", funcName, argumentSB.ToString(), branch, returnValue); code = Lua.Generator.Scrub(block, code); // Add % so as not to collide with helper functions in definitions list. Lua.Generator.AddFunction('%' + funcName, code); return(null); }
/// <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)); }
private CodeStruct Procedure_CallReturn(Block block) { // Call a procedure with a return value. string funcName = Lua.VariableNames.GetName(block.GetFieldValue("NAME"), Define.PROCEDURE_CATEGORY_NAME); StringBuilder argumentSB = new StringBuilder(); List <string> arguments = ProcedureDB.GetProcedureArguments(block); for (int i = 0; i < arguments.Count; i++) { argumentSB.Append(Lua.Generator.ValueToCode(block, "ARG" + i, Lua.ORDER_NONE, "nil")); if (i < arguments.Count - 1) { argumentSB.Append(", "); } } string code = string.Format("{0}({1})", funcName, argumentSB.ToString()); return(new CodeStruct(code, Lua.ORDER_HIGH)); }