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 GSInt64(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); }
private GSCore execRandom(GSCoreCollection args) { int count = args.Count; string exceptValue = ""; for (int i = 0; i < count; i++) { if (args[i].getStringValue().Equals("#")) { if (i < count - 1) { exceptValue = args[i + 1].getStringValue(); } count = i; break; } } Random rnd = new Random(DateTime.Now.Millisecond); GSCore rv = null; for (int i = 0; i < 20; i++) { rv = args.getSafe(rnd.Next(count)); if (exceptValue.Equals("") || !rv.getStringValue().Equals(exceptValue)) { return(rv); } } return(rv); }
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(GSVoid.Void); }
private GSCore execMessage(GSCoreCollection args) { GSCore result = GSVoid.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); }
public override GSCore ExecuteMessage(string token, GSCoreCollection args) { if (token.Equals("replace")) { string a1 = args.getSafe(0).getStringValue(); string a2 = args.getSafe(1).getStringValue(); if (a1.Length > 0) { return(new GSString(Value.Replace(a1, a2))); } else { return(this); } } else if (token.Equals("substring")) { int a = (int)args.getSafe(0).getIntegerValue(); int b = (int)args.getSafe(1).getIntegerValue(); if (b == 0) { return(new GSString(Value.Substring(a))); } else { return(new GSString(Value.Substring(a, b))); } } else if (token.Equals("padLeft")) { int a = (int)args.getSafe(0).getIntegerValue(); if (a > Value.Length) { return(new GSString(Value.PadLeft(a))); } else { return(this); } } else if (token.Equals("padRight")) { int a = (int)args.getSafe(0).getIntegerValue(); if (a > Value.Length) { return(new GSString(Value.PadRight(a))); } else { return(this); } } else { return(base.ExecuteMessage(token, args)); } }
public override GSCore ExecuteMessage(string token, GSCoreCollection args) { GSCore result = null; 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 if (token.Equals("set") && args.Count > 1) { result = execSet(args[0], args[1]); } else if (token.Equals("random")) { result = execRandom(args); } else { if (token.IndexOf('.') >= 0) { string[] tp = token.Split('.'); GSCore obj = this; for (int a = 0; a < tp.Length - 1; a++) { if (obj == null) { break; } obj = obj.GetPropertyValue(tp[a]); } if (obj != null) { return(obj.ExecuteMessage(tp[tp.Length - 1], args)); } } Debugger.Log(0, "", "UNKNOWN MESSAGE: " + token + " "); } return(result); }