public static string HandleCommands(string str, KScriptContainer container, KScriptBaseObject parent, bool parse_immediately = false) { string temp_string = str; List <ICommand> commands = GetCommands(str, container, parent); if (commands.Count > 0) { foreach (ICommand item in commands) { if (item.IsCommandObject) { if (parse_immediately) { temp_string = ReplaceFirst(temp_string, item.GetCommandObject().CommandParameters, item.GetCommandObject().CalculateValue()); } else { string id = Guid.NewGuid().ToString(); temp_string = ReplaceFirst(temp_string, item.GetCommandObject().CommandParameters, $"[{id}]"); container.GetCommandStore().Add($"[{id}]", item.GetCommandObject()); } } } if (!parse_immediately) { return(ReturnCommandString(temp_string, container, parent)); } } return(temp_string); }
/// <summary> /// Used to retrieve an array with specified ID /// </summary> /// <param name="id">ID to retrieve array</param> /// <returns>Array of strings</returns> public List <string> ArrayGet(KScriptBaseObject obj, string id) { try { if (arrays.ContainsKey(id)) { return(arrays[id]); } throw new KScriptDefNotFound(obj, string.Format("The Array '{0}' does not exist.", id)); } catch (Exception ex) { HandleException(ex); return(null); } }
public ICommandObject(string value, KScriptContainer container, KScriptBaseObject parent) : base(value) { kScriptContainer = container; kScriptBaseObject = parent; }
public KScriptException(KScriptBaseObject obj) : base() { KScriptObject = obj; }
/// <summary> /// Method used to handle exceptions - current purpose is to output the exception message. /// </summary> /// <param name="ex"></param> internal void HandleException(KScriptBaseObject obj, Exception ex) => HandleException(new KScriptException(obj, ex.Message));
/// <summary> /// Sets the Base script object to obj. /// </summary> /// <param name="obj">Base script object</param> public void SetBaseScriptObject(KScriptBaseObject obj) => ScriptObject = obj;
public static KScriptCommand GetCommandObject(Type _type, KScriptContainer container, KScriptBaseObject parent) { if (_type != null) { KScriptCommand obj = (KScriptCommand)Activator.CreateInstance(_type); obj.Init(container, parent); return(obj); } else { return(null); } }
public static List <ICommand> GetCommands(string str, KScriptContainer container, KScriptBaseObject baseObj) { //Count all open brackets, when finding close bracket, length of string to array Stack <ICommand> commands = new Stack <ICommand>(); List <ICommand> All_Commands = new List <ICommand>(); char[] str_cmds = str.ToCharArray(); ICommandObject bracket = new ICommandObject(str, container, baseObj); ParamTracker paramTracker = new ParamTracker(); HashSet <char> allowedChars = new HashSet <char>(new[] { '(', ')', char.Parse("'") }); List <char> stack = new List <char>(str.Where(allowedChars.Contains)); int index = -1; bool ignore = false, encountered_cmd = false; for (int i = 0; i < str_cmds.Length; i++) { if (str_cmds[i].Equals('@') && !ignore) { encountered_cmd = true; } if (encountered_cmd) { paramTracker.Track(str_cmds[i], i); } if (str_cmds[i].Equals(char.Parse("'")) && encountered_cmd) { ignore = !ignore; } if (str_cmds[i].Equals('@') && !ignore && encountered_cmd) { bracket = new ICommandObject(str, container, baseObj); bracket.IndexProperties.Start = i; bracket.Index = ++index; commands.Push(bracket); continue; } else if (str_cmds[i].Equals('$') && !ignore && encountered_cmd) { //ignore = !ignore; } else if (str_cmds[i].Equals('(') && !ignore && encountered_cmd) { if (commands.Peek().IsCommandObject) { commands.Peek().GetCommandObject().EndNameIndex = i; } continue; } else if (str_cmds[i].Equals(',') && !ignore && encountered_cmd && commands.Count > 0) { if (commands.Any()) { ICommand cmd = commands.Peek(); if (cmd.IsCommandObject) { if (paramTracker.HasParams) { IValue variable = new IValue(paramTracker.GetIndexPair(), container); cmd.GetCommandObject().Children.Enqueue(variable); } } } } else if (str_cmds[i].Equals(')') && !ignore && encountered_cmd && commands.Count > 0) { if (!commands.Any()) { All_Commands.Add(bracket); continue; } ICommand cmd = commands.Pop(); cmd.IndexProperties.End = i; if (paramTracker.HasParams) { if (cmd.IsCommandObject) { if (cmd.GetCommandObject().InnerCommand.Length > 0) { IValue variable = new IValue(paramTracker.GetIndexPair(), container); cmd.GetCommandObject().Children.Enqueue(variable); } } else { IValue variable = new IValue(paramTracker.GetIndexPair(), container); cmd.GetCommandObject().Children.Enqueue(variable); } } if (commands.Count == 0) { All_Commands.Add(cmd); } if (commands.Count > 0 && commands.Peek().IsCommandObject&& !commands.Peek().GetCommandObject().IsClosed) { commands.Peek().GetCommandObject().Children.Enqueue(cmd); continue; } All_Commands.Add(cmd); continue; } } if (!(stack.Count % 2 == 0) && encountered_cmd) { throw new KScriptExceptions.KScriptException("KScript Command is incorrectly formatted - check brackets match and that all (') symbols are surrounding strings." + "\nError occurs when parsing string: \n" + "----------------------------------------------------\n" + str); } return(All_Commands); }
public static KScriptCommand GetCommandObject(string[] @params, Type _type, KScriptContainer container, KScriptBaseObject parent) { KScriptCommand obj = (KScriptCommand)Activator.CreateInstance(_type, @params); obj.Init(container, parent); return(obj); }
public static string ReturnCommandString(string str, KScriptContainer container, KScriptBaseObject parent) { if (container.GetCommandStore().Any()) { string temp_string = str; foreach (KeyValuePair <string, ICommandObject> item in container.GetCommandStore().ToList()) { string val = item.Value.GetCommandObject().CalculateValue(); temp_string = temp_string.Replace(item.Key, val); } container.GetCommandStore().Clear(); return(temp_string); } return(str); }
internal void Init(KScriptContainer container, KScriptBaseObject parent) { SetContainer(container); SetBaseScriptObject(parent); }