public override Value Execute(Args args, Environment environment, SourcePos pos) { var a = args.GetInt("a").GetValue(); var n = (int)args.GetInt("n").GetValue() % 32; return(new ValueInt((a >> n) & 4294967295L)); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("str")) { return(ValueNull.NULL); } var value = args.GetString("str").GetValue(); var start = (int)args.GetInt("startidx").GetValue(); if (start < 0) { start = value.Length + start; } if (start > value.Length) { return(new ValueString("")); } var end = (int)args.GetInt("endidx", value.Length).GetValue(); if (end < 0) { end = value.Length + end; } if (end > value.Length) { end = value.Length; } return(new ValueString(value.Substring(start, end - start))); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { var a = args.GetInt("a").GetValue(); var b = args.GetInt("b").GetValue(); return(new ValueInt((a & b) & 4294967295L)); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("lst")) { return(ValueNull.NULL); } var value = args.GetList("lst").GetValue(); var start = (int)args.GetInt("startidx").GetValue(); if (start < 0) { start = value.Count + start; } if (start > value.Count) { return(new ValueList()); } var end = (int)args.GetInt("endidx", value.Count).GetValue(); if (end < 0) { end = value.Count + end; } if (end > value.Count) { end = value.Count; } var result = new ValueList(); for (var i = start; i < end; i++) { result.AddItem(value[i]); } return(result); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.HasArg("a") && !args.HasArg("b")) { return(new ValueInt(random.Next((int)args.GetInt("a").GetValue()))); } if (args.HasArg("a") && args.HasArg("b")) { return(new ValueInt(random.Next((int)args.GetInt("a").GetValue(), (int)args.GetInt("b").GetValue()))); } return(new ValueDecimal((decimal)random.NextDouble())); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { var seed = (int)args.GetInt("n").GetValue(); FuncRandom.random = new Random(seed); return(new ValueInt(seed)); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { var lst = args.Get("lst"); if (!lst.IsList()) { throw new ControlErrorException(new ValueString("ERROR"), "Cannot insert into object of type " + lst.Type(), pos); } var index = (int)args.GetInt("index").GetValue(); if (index < 0) { index = lst.AsList().GetValue().Count + index + 1; } var value = args.Get("value"); var list = lst.AsList().GetValue(); if (index < 0 || index > list.Count) { return(lst); } if (index == list.Count) { list.Add(value); } else { list.Insert(index, value); } return(lst); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("n")) { return(ValueNull.NULL); } return(new ValueString(char.ConvertFromUtf32((int)args.GetInt("n").GetValue()).ToString())); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("obj")) { return(ValueNull.NULL); } var obj = args.Get("obj"); var key = args.HasArg("key") ? args.GetFunc("key") : null; if (obj.IsString()) { var str = obj.AsString().GetValue(); var start = (int)args.GetInt("start", str.Length - 1).GetValue(); var part = args.GetString("part").GetValue(); return(new ValueInt(str.LastIndexOf(part, start, StringComparison.Ordinal))); } if (obj.IsList()) { var env = environment; if (key != null) { env = environment.NewEnv(); } var item = args.Get("part"); var list = obj.AsList().GetValue(); var start = (int)args.GetInt("start", list.Count - 1).GetValue(); for (var idx = start; idx >= 0; idx--) { var elem = list[idx]; if (key != null) { elem = key.Execute(new Args(key.GetArgNames()[0], elem, pos), env, pos); } if (elem.IsEquals(item)) { return(new ValueInt(idx)); } } return(new ValueInt(-1)); } throw new ControlErrorException(new ValueString("ERROR"), "Find_last only works with strings and lists", pos); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { var start = 0; var end = 0; var step = 1; if (args.HasArg("a") && !args.HasArg("b")) { end = (int)args.GetInt("a").GetValue(); } else if (args.HasArg("a") && args.HasArg("b")) { start = (int)args.GetInt("a").GetValue(); end = (int)args.GetInt("b").GetValue(); } if (args.HasArg("step")) { step = (int)args.GetInt("step").GetValue(); } var result = new ValueList(); var i = start; if (step > 0) { while (i < end) { result.AddItem(new ValueInt(i)); i += step; } } else if (step < 0) { while (i > end) { result.AddItem(new ValueInt(i)); i += step; } } return(result); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("x")) { return(ValueNull.NULL); } if (args.IsNull("y")) { return(ValueNull.NULL); } if (args.Get("y").IsInt()) { if (args.Get("x").IsInt()) { var x = args.GetInt("x").GetValue(); var y = args.GetInt("y").GetValue(); long result = 1; for (int i = 0; i < y; i++) { result *= x; } return(new ValueInt(result)); } else { var x = args.GetDecimal("x").GetValue(); var y = args.GetInt("y").GetValue(); decimal result = 1; for (int i = 0; i < y; i++) { result *= x; } return(new ValueDecimal(result)); } } else { return(new ValueDecimal((decimal)Math.Pow((double)args.GetNumerical("x").GetValue(), (double)args.GetNumerical("y").GetValue()))); } }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("x")) { return(ValueNull.NULL); } var digits = 0; if (args.HasArg("digits")) { digits = (int)args.GetInt("digits").GetValue(); } return(new ValueDecimal((decimal)Math.Round((double)args.GetNumerical("x").GetValue(), digits))); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("s")) { return(ValueNull.NULL); } var s = args.GetString("s").GetValue(); var a = args.GetString("a").GetValue(); var b = args.GetString("b").GetValue(); var start = args.GetInt("start", 0L).GetValue(); if (start >= s.Length) { return(args.GetString("s")); } if (start == 0) { return(new ValueString(s.Replace(a, b))); } var prefix = s.Substring(0, (int)start); return(new ValueString(prefix + s.Substring((int)start).Replace(a, b))); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { var lst = args.Get("lst"); var index = (int)args.GetInt("index").GetValue(); if (lst.IsList()) { var list = lst.AsList().GetValue(); if (index < 0) { index = list.Count + index; } if (index >= list.Count) { return(ValueNull.NULL); } var result = list[index]; list.RemoveAt(index); return(result); } throw new ControlErrorException(new ValueString("ERROR"), "Cannot delete from " + lst.Type(), pos); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("str")) { return(ValueNull.NULL); } var str = args.GetString("str").GetValue(); var start_ = (int)args.GetInt("start", 0).GetValue(); if (start_ < 0) { start_ = str.Length + start_; } var result = ""; if (start_ > 0) { result = str.Substring(0, start_); str = str.Substring(start_); } var lastidx = 0; var pattern = "\\{([^#{}]+)(#-?[0-9.]*x?)?\\}"; foreach (Match match in Regex.Matches(str, pattern)) { var start = match.Groups[0].Index; var end = start + match.Groups[0].Length; var expr = match.Groups[1].Value; var spec = match.Groups[2].Value; var width = 0; var zeroes = false; var leading = true; int numbase = 10; var digits = -1; if (spec != null && spec != "") { spec = spec.Substring(1); // skip # if (spec.StartsWith("-")) { leading = false; spec = spec.Substring(1); } if (spec.StartsWith("0")) { zeroes = true; leading = false; spec = spec.Substring(1); } if (spec.EndsWith("x")) { numbase = 16; spec = spec.Substring(0, spec.Length - 1); } int idx = spec.IndexOf('.'); if (idx == -1) { digits = -1; width = int.Parse(spec == "" ? "0" : spec); } else { digits = int.Parse(spec.Substring(idx + 1)); width = idx == 0 ? 0 : int.Parse(spec.Substring(0, idx)); } } string value = ""; try { var node = Parser.Parse(expr, pos.filename); var val = node.Evaluate(environment); if (numbase != 10) { value = string.Format("{0:x}", val.AsInt().GetValue()); } else if (digits != -1) { value = string.Format("{0:f" + digits + "}", val.AsDecimal().GetValue()); } else { value = val.AsString().GetValue(); } while (value.Length < width) { if (leading) { value = ' ' + value; } else if (zeroes) { value = '0' + value; } else { value = value + ' '; } } } catch (Exception) { // ignore } if (lastidx < start) { result += str.Substring(lastidx, start - lastidx); } result += value; lastidx = end; } if (lastidx < str.Length) { result += str.Substring(lastidx); } return(new ValueString(result)); }
public override Value Execute(Args args, Environment environment, SourcePos pos) { if (args.IsNull("str")) { return(ValueNull.NULL); } var str = args.GetString("str").GetValue(); var start = (int)args.GetInt("start", 0).GetValue(); if (start < 0) { start = str.Length + start; } while (true) { var idx1 = str.IndexOf('{', start); if (idx1 == -1) { return(new ValueString(str)); } var idx2 = str.IndexOf('}', idx1 + 1); if (idx2 == -1) { return(new ValueString(str)); } var variable = str.Substring(idx1 + 1, idx2 - idx1 - 1); var width = 0; var zeroes = false; var leading = true; var digits = -1; int idx3 = variable.IndexOf('#'); if (idx3 != -1) { var spec = variable.Substring(idx3 + 1); variable = variable.Substring(0, idx3); if (spec.StartsWith("-")) { leading = false; spec = spec.Substring(1); } if (spec.StartsWith("0")) { zeroes = true; leading = false; spec = spec.Substring(1); } int idx4 = spec.IndexOf('.'); if (idx4 == -1) { digits = -1; width = int.Parse(spec); } else { digits = int.Parse(spec.Substring(idx4 + 1)); width = idx4 == 0 ? 0 : int.Parse(spec.Substring(0, idx4)); } } var node = Parser.Parse(variable, pos.filename); var value = node.Evaluate(environment).AsString().GetValue(); if (digits != -1) { value = string.Format("{0:f" + digits + "}", decimal.Parse(value)); } while (value.Length < width) { if (leading) { value = ' ' + value; } else if (zeroes) { value = '0' + value; } else { value = value + ' '; } } str = str.Substring(0, idx1) + value + str.Substring(idx2 + 1); start = idx1 + value.Length; } }