public static List <ESD.CommandCall> AssembleCommandScript(EzSembleContext context, Block block) { var result = new List <ESD.CommandCall>(); foreach (Statement st in block.Cmds) { var cmdId = context.GetCommandID(st.Name); result.Add(new ESD.CommandCall() { CommandBank = cmdId.Bank, CommandID = cmdId.ID, Arguments = st.Args.Select(x => AssembleExpression(context, x)).ToList(), }); } return(result); }
/// <summary> /// Assembles a plain text "EzLanguage" command call into a CommandCall object. /// </summary> public static ESD.CommandCall AssembleCommandCall(EzSembleContext context, string plaintext) { var regex = System.Text.RegularExpressions.Regex.Match(plaintext, @"^([A-Za-z0-9_\-:]+)\((.*)\)$"); if (regex.Groups.Count != 3) { throw new Exception($"Invalid EzLanguage command call text: \"{plaintext}\""); } var command = regex.Groups[1].Value; var cmdId = context.GetCommandID(command); var argsText = regex.Groups[2].Value.Trim(); var finalArgs = new List <string>(); if (!string.IsNullOrWhiteSpace(argsText)) { // If <= 2 chars theres no way there can be more than 1 arg // And obviously if there's no commas if (argsText.Length <= 2 || !argsText.Contains(",")) { finalArgs = new List <string>() { argsText }; } else { int thisArgStartIndex = 0; int innerParenthesisLevel = 0; for (int i = 0; i < argsText.Length; i++) { if (i < argsText.Length - 1) { if (argsText[i] == '(') { innerParenthesisLevel++; } else if (argsText[i] == ')') { if (innerParenthesisLevel > 0) { innerParenthesisLevel--; } else { throw new Exception("Extra parenthesis found in command call args."); } } if (argsText[i] == ',') { if (innerParenthesisLevel == 0) { finalArgs.Add(argsText.Substring(thisArgStartIndex, i - thisArgStartIndex)); thisArgStartIndex = i + 1; } } } else //Very last char { if (argsText[i] == ',' || argsText[i] == '(') { throw new Exception("Very last char in command args cannot be a '(' or ','"); } else if (argsText[i] == ')') { if (innerParenthesisLevel > 0) { innerParenthesisLevel--; } else { throw new Exception("Extra parenthesis found in command call args."); } } } } if (thisArgStartIndex < argsText.Length - 1) { finalArgs.Add(argsText.Substring(thisArgStartIndex, (argsText.Length) - thisArgStartIndex)); } if (innerParenthesisLevel != 0) { throw new Exception("Unclosed parenthesis found in command call args."); } } } return(new ESD.CommandCall() { CommandBank = cmdId.Bank, CommandID = cmdId.ID, Arguments = finalArgs.Select(x => AssembleExpression(context, x)).ToList(), }); }