public override bool Execute() { if (function == null) { function = (MultiActionCommand)FUNCTIONS[functionDefinition.functionName].function.Copy(); foreach (string key in inputParameters.Keys) { Program.memoryVariables[key] = new StaticVariable(inputParameters[key].GetValue()); } } STATE = ProgramState.RUNNING; switch (type) { case FunctionType.GOSUB: return(function.Execute()); case FunctionType.GOTO: RUNNING_COMMANDS = function; RUNNING_FUNCTION = functionDefinition.functionName; return(false); case FunctionType.SWITCH: RUNNING_COMMANDS = function; RUNNING_FUNCTION = functionDefinition.functionName; STATE = ProgramState.STOPPED; return(true); default: throw new Exception("Unsupported Function Type: " + type); } }
void ParseFunctions(List <String> commandStrings) { List <int> functionIndices = new List <int>(); int implicitMainOffset = 1; if (!commandStrings[0].StartsWith(":")) { commandStrings.Insert(0, ":main"); implicitMainOffset--; } for (int i = commandStrings.Count - 1; i >= 0; i--) { Trace("Command String: " + commandStrings[i]); if (commandStrings[i].StartsWith(":")) { functionIndices.Add(i); } } Trace("Function Indices: "); Trace(String.Join(" | ", functionIndices)); //Parse Function Definitions foreach (int i in functionIndices) { String functionString = commandStrings[i].Remove(0, 1).Trim(); List <Token> nameAndParams = ParseTokens(functionString); String functionName = nameAndParams[0].original; nameAndParams.RemoveAt(0); FunctionDefinition definition = new FunctionDefinition(functionName, nameAndParams.Select(t => t.original).ToList()); functions[functionName] = definition; } //Parse Function Commands and add to Definitions int toParse = functionParseAmount; foreach (int i in functionIndices) { int startingLineNumber = i + 1 + implicitMainOffset; String functionString = commandStrings[i].Remove(0, 1).Trim(); List <Token> nameAndParams = ParseTokens(functionString); String functionName = nameAndParams[0].original; Info("Parsing Function: " + functionName); Command command = ParseCommand(commandStrings.GetRange(i + 1, commandStrings.Count - (i + 1)).Select(str => new CommandLine(str, this)).ToList(), 0, true, ref startingLineNumber); commandStrings.RemoveRange(i, commandStrings.Count - i); if (!(command is MultiActionCommand)) { command = new MultiActionCommand(new List <Command> { command }); } functions[functionName].function = (MultiActionCommand)command; defaultFunction = functionName; toParse--; if (toParse == 0) { break; //Exceeded # of Functions to parse for 1 tick } } }
public FunctionCommand(FunctionType type, FunctionDefinition functionDefinition, Dictionary <string, Variable> inputParameters) { this.type = type; this.functionDefinition = functionDefinition; this.inputParameters = inputParameters; function = null; }
static bool ParseCommands() { if ((RUNNING_COMMANDS == null && COMMAND_STRINGS.Count == 0) || !CUSTOM_DATA.Equals(PROGRAM.Me.CustomData)) { CUSTOM_DATA = PROGRAM.Me.CustomData; COMMAND_STRINGS = CUSTOM_DATA.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries) .Where(line => !line.StartsWith("#")) .ToList(); if (COMMAND_STRINGS.Count == 0) { Info("Welcome to EasyCommands!"); Info("Add Commands to Custom Data"); return(false); } Info("Parsing Custom Data"); FUNCTIONS.Clear(); } if (COMMAND_STRINGS.Count == 0) { return(true); } Info("Parsing Commands. Lines Left: " + COMMAND_STRINGS.Count); ParseFunctions(COMMAND_STRINGS); if (COMMAND_STRINGS.Count > 0) { return(false); } RUNNING_COMMANDS = (MultiActionCommand)(FUNCTIONS[DEFAULT_FUNCTION].function).Copy(); return(true); }
public override bool Execute() { Thread currentThread = PROGRAM.GetCurrentThread(); if (function == null) { function = (MultiActionCommand)PROGRAM.functions[functionDefinition.functionName].function.Clone(); foreach (string key in inputParameters.Keys) { currentThread.threadVariables[key] = new StaticVariable(inputParameters[key].GetValue().DeepCopy()); } } switch (type) { case Function.GOSUB: return(function.Execute()); case Function.GOTO: currentThread.Command = function; currentThread.SetName(functionDefinition.functionName); return(false); default: throw new Exception("Unsupported Function Type: " + type); } }
public void IterateSimpleCommandAfter() { var program = MDKFactory.CreateProgram <Program>(); var command = program.ParseCommand("for 3 times print \"hello world\""); Assert.IsTrue(command is MultiActionCommand); MultiActionCommand iterateCommand = (MultiActionCommand)command; Assert.AreEqual(3f, iterateCommand.loopCount.GetValue().GetValue()); List <Command> commands = iterateCommand.commandsToExecute; Assert.AreEqual(1, commands.Count); Assert.IsTrue(commands[0] is PrintCommand); PrintCommand printCommand = (PrintCommand)commands[0]; Assert.AreEqual("hello world", printCommand.variable.GetValue().GetValue()); }
public FunctionDefinition(string functionName, MultiActionCommand function, List <string> parameterNames) { this.functionName = functionName; this.function = function; this.parameterNames = parameterNames; }
public FunctionDefinition(string functionName, List <string> parameterNames) { this.functionName = functionName; this.parameterNames = parameterNames; this.function = null; }
public override void Reset() => function = null;