private GSCore execWhile(GSCoreCollection args) { GSCoreCollection commands = args.getSublist(1); GSCore r = null; while (ExecuteElement(args.getSafe(0)).getBooleanValue()) { foreach (GSCore cmd in commands) { r = ExecuteElement(cmd); if (r is GSReturn) { break; } } if (r is GSReturn) { GSReturn ret = r as GSReturn; if (ret.Type == GSReturn.TYPE_BREAK) { break; } if (ret.Type == GSReturn.TYPE_RETURN) { return(ret); } } } return(GSCore.Void); }
private GSCore ExecuteElementWithoutreport(GSCore E) { if (E is GSList) { GSList L = (GSList)E; if (L.Count == 0) { return(GSCore.Void); } else if (L.Count == 1) { return(ExecuteElement(L[0])); } else { if (L.Parts.IsFirstToken()) { GSCore res = null; try { res = ExecuteMessage(L.Parts.getFirstToken(), L.Parts.getSublist(1)); } catch { res = new GSString(); } finally { } return(res); } else { GSCore result = null; foreach (GSCore item in L.Parts) { result = ExecuteElement(item); if (result is GSReturn) { break; } } if (result == null) { return(new GSString()); } return(result); } } } else if (E is GSToken) { return(EvaluateProperty(((GSToken)E).Token)); } else { return(E); } }
/// <summary> /// Short-cut method for 1 argument /// </summary> /// <param name="token"></param> /// <param name="obj"></param> /// <returns></returns> public GSCore ExecuteMessage(string token, GSCore obj) { GSCoreCollection args = new GSCoreCollection(); args.Add(obj); return(ExecuteMessage(token, args)); }
private GSCore execForeach(GSCoreCollection args) { if (args.Count < 4) { Debugger.Log(0, "", "Insufficient arguments for (FOREACH varName : list commands ) "); return(null); } GSCore t1 = args.getSafe(0); GSCore l1 = ExecuteElement(args.getSafe(2)); if (!(t1 is GSToken)) { Debugger.Log(0, "", "Token shoudl be second argument in FOREACH "); return(null); } if (!(l1 is GSList)) { Debugger.Log(0, "", "List should be fourth argument in FOREACH "); return(null); } GSToken tk = (GSToken)t1; GSList lst = (GSList)l1; GSCore r = null; int ik = 0; foreach (GSCore item in lst.Parts) { SetVariable(tk.Token, item); for (int i = 3; i < args.Count; i++) { r = ExecuteElement(args.getSafe(i)); if (r is GSReturn) { break; } } ik++; if (r is GSReturn) { GSReturn ret = r as GSReturn; if (ret.Type == GSReturn.TYPE_BREAK) { break; } if (ret.Type == GSReturn.TYPE_RETURN) { return(ret); } } } return(new GSNumber(ik)); }
private GSCore execIf(GSCoreCollection args) { GSCore cond = ExecuteElement(args.getSafe(0)); GSCore cmd1 = args.getSafe(1); GSCore cmd2 = args.getSafe(2); GSCore r = null; if (cond.getBooleanValue()) { bool running = false; foreach (GSCore cmd in args) { if (cmd is GSToken && cmd.ToString().Equals("then")) { running = true; } if (cmd is GSToken && cmd.ToString().Equals("else")) { running = false; } if (running) { r = ExecuteElement(cmd); if (r is GSReturn) { return(r); } } } } else { bool running = false; foreach (GSCore cmd in args) { if (cmd is GSToken && cmd.ToString().Equals("else")) { running = true; } if (running) { r = ExecuteElement(cmd); if (r is GSReturn) { return(r); } } } } return(cond); }
/// <summary> /// Sets value for variable. /// </summary> /// <param name="varName"></param> /// <param name="varValue"></param> public void SetVariable(string varName, GSCore varValue) { Dictionary <string, GSCore> vars = getLastVars(); if (vars.ContainsKey(varName)) { vars[varName] = varValue; } else { vars.Add(varName, varValue); } }
private GSCore execDo(GSCoreCollection args) { GSCore last = null; foreach (GSCore item in args) { last = ExecuteElement(item); if (last is GSReturn) { return(last); } } return(last); }
private void SetSystemVariables(string key, GSCore value) { if (key.Equals("escapeChar")) { escapeChar = value.getStringValue(); } else if (key.Equals("markupStart")) { markupStart = value.getStringValue(); } else if (key.Equals("markupEnd")) { markupEnd = value.getStringValue(); } }
private GSCore execPrint(GSCoreCollection arg, bool newLine) { foreach (GSCore argument in arg) { GSCore val = ExecuteElement(argument); string str = val.getStringValue(); str = ReplaceVariables(str); output.Append(str); } if (newLine) { output.AppendLine(); } return(GSCore.Void); }
private GSCore execSet(GSCore keyElem, GSCore valueElem) { string key; if (keyElem is GSToken) { key = (keyElem as GSToken).Token; } else { key = keyElem.getStringValue(); } GSCore value = ExecuteElement(valueElem); SetVariable(key, value); SetSystemVariables(key, value); return(value); }
/// <summary> /// Token can contain dot separator /// We should not normally override this method /// Rather method GetTokenValue shoudl be overriden /// </summary> /// <param name="Token"></param> /// <returns></returns> public GSCore EvaluateProperty(string Token) { int dotPos = Token.IndexOf('.'); if (dotPos >= 0) { string str = Token.Substring(0, dotPos); GSCore obj = GetPropertyValue(str); if (obj == null) { return(GSCore.Void); } return(obj.EvaluateProperty(Token.Substring(dotPos + 1))); } else { return(GetPropertyValue(Token)); } }
/// <summary> /// Executing element. For most of the elements in the program it is element itself, /// but for the list it is result of executing operation that is mentioned in the head /// of the list. /// </summary> /// <param name="E"></param> /// <returns></returns> public GSCore ExecuteElement(GSCore E) { level++; GSCore result = ExecuteElementWithoutreport(E); /*Debugger.Log(0, "", "".PadLeft(level) + "Script: " + E.ToString() + "\r\n"); * Debugger.Log(0, "", "".PadLeft(level) + "Result: " + result.ToString() + "\r\n"); * if (level == 1) * { * foreach(Dictionary<string,GSCore> vd in stackVars) * { * foreach(KeyValuePair<string,GSCore> v in vd) * { * Debugger.Log(0, "", "".PadLeft(level) + "Var: [" + v.Key + "] = " + v.Value.ToString() + "\r\n"); * } * } * }*/ level--; return(result); }
private GSCore execMessage(GSCoreCollection args) { GSCore result = GSCore.Void; // first is token, name of variable, object // second is token, message name // third etc are arguments if (args.Count >= 2 && args.getSafe(0) is GSToken && args.getSafe(1) is GSToken) { // evaluate the remaining portion of list GSCoreCollection subArgs = getNativeValues(args.getSublist(2)); // first and second tokens GSToken t1 = args.getSafe(0) as GSToken; GSToken t2 = args.getSafe(1) as GSToken; // evaluate reference to object GSCore obj = ExecuteElement(t1); // execute message in the object result = obj.ExecuteMessage(t2.Token, subArgs); } return(result); }
// this function is also in GSCore object // evaluates property into value public override GSCore GetPropertyValue(string Token) { // find in variables GSCore obj = GetVariable(Token); if (obj != null) { return(obj); } // find built-in property if (Token.Equals("name")) { return new GSString() { Value = "Executor" } } ; // return default empty string return(new GSString()); }
public GSReturn(GSCore val) { Value = val; }
public void Add(GSCore obj) { Parts.Add(obj); }
public override GSCore ExecuteMessage(string token, GSCoreCollection args) { GSCore result = null; if (token.Equals("add") || token.Equals("+")) { result = execAdd(getNativeValues(args)); } else if (token.Equals("sub") || token.Equals("-")) { result = execSub(getNativeValues(args)); } else if (token.Equals("mul") || token.Equals("*")) { result = execMul(getNativeValues(args)); } else if (token.Equals("div") || token.Equals("/")) { result = execDiv(getNativeValues(args)); } else if (token.Equals("and") || token.Equals("&")) { result = execAnd(getNativeValues(args)); } else if (token.Equals("or") || token.Equals("|")) { result = execOr(getNativeValues(args)); } else if (token.Equals("not") || token.Equals("!")) { result = execNot(args); } else if (token.Equals("set") && args.Count > 1) { result = execSet(args[0], args[1]); } else if ((token.Equals("gt") || token.Equals(">")) && args.Count > 1) { result = execGt(getNativeValues(args)); } else if ((token.Equals("ge") || token.Equals(">=")) && args.Count > 1) { result = execGe(getNativeValues(args)); } else if ((token.Equals("eq") || token.Equals("==")) && args.Count > 1) { result = execEq(getNativeValues(args)); } else if ((token.Equals("ne") || token.Equals("!=")) && args.Count > 1) { result = execNe(getNativeValues(args)); } else if ((token.Equals("le") || token.Equals("<=")) && args.Count > 1) { result = execLe(getNativeValues(args)); } else if ((token.Equals("lt") || token.Equals("<")) && args.Count > 1) { result = execLt(getNativeValues(args)); } else if (token.Equals("print")) { result = execPrint(args, false); } else if (token.Equals("println")) { result = execPrint(args, true); } else if (token.Equals("if")) { result = execIf(args); } else if (token.Equals("while")) { result = execWhile(args); } else if (token.Equals("foreach")) { result = execForeach(args); } else if (token.Equals("x")) { result = execMessage(args); } else if (token.Equals("do")) { result = execDo(args); } else if (token.Equals("return")) { result = new GSReturn(args.getSafe(0)); } else if (token.Equals("break")) { result = new GSReturn(GSReturn.TYPE_BREAK); } else if (token.Equals("continue")) { result = new GSReturn(GSReturn.TYPE_CONTINUE); } else { Debugger.Log(0, "", "UNKNOWN MESSAGE: " + token + " "); } return(result); }
/// <summary> /// Formating string: /// 20s - string with padding to 20 chars, left align /// -20s - string with padding to 20 chars, right align /// 2d - integer number padded to 2 chars with spaces, right align /// 02d - integer number padded to 2 chars with zero, right align /// 1.7f - floating point value with at least 1 digit before point and 7 digits after point /// </summary> /// <param name="ph">Input placeholder (without markup substrings, /// this can contain also some formatting after : character</param> /// <returns></returns> public string ReplaceVariablePlaceholder(string ph) { string fmt = ""; int phi = ph.IndexOf(':'); if (phi >= 0) { fmt = ph.Substring(phi + 1); ph = ph.Substring(0, phi); } GSCore cs = EvaluateProperty(ph); if (fmt.EndsWith("s")) { string value = cs.getStringValue(); int places; if (int.TryParse(fmt.Substring(0, fmt.Length - 1), out places)) { if (places > 0) { value = value.PadRight(places); } else { value = value.PadLeft(-places); } } return(value); } else if (fmt.EndsWith("m")) { string value = cs.getStringValue(); int places; if (int.TryParse(fmt.Substring(0, fmt.Length - 1), out places)) { if (value.Length > places) { value = value.Substring(0, places - 3) + "..."; } else { if (places > 0) { value = value.PadRight(places); } else { value = value.PadLeft(-places); } } } return(value); } else if (fmt.EndsWith("d")) { bool padWithZero = false; int places; string result = ""; long ival = cs.getIntegerValue(); if (int.TryParse(fmt.Substring(0, fmt.Length - 1), out places)) { if (fmt.StartsWith("0")) { padWithZero = true; } if (padWithZero) { result = string.Format("{0:0".PadRight(places - 1, '0') + "}", ival); result = result.PadLeft(places, '0'); } else { result = string.Format("{0:#".PadRight(places - 1, '#') + "}", ival); result = result.PadLeft(places, ' '); } } else { result = ival.ToString(); } return(result); } else if (fmt.EndsWith("f")) { string a, b; fmt = fmt.Substring(0, fmt.Length - 1); int i = fmt.IndexOf('.'); if (i >= 0) { a = fmt.Substring(0, i); b = fmt.Substring(i + 1); } else { a = fmt; b = "0"; } int ia, ib; double d = cs.getDoubleValue(); string result; if (int.TryParse(a, out ia) && int.TryParse(b, out ib)) { result = string.Format("{0:" + string.Format("F{0}", ib) + "}", d); result = result.PadLeft(ia + ib + 1); } else { result = d.ToString(); } return(result); } else { return(cs.getStringValue()); } }
public GSReturn(int type, GSCore val) { Value = val; Type = type; }