private static void WriteToConsole(ExecutionContext ctx, DyObject value, string?color, string?backColor, bool newLine) { var str = value.ToString(ctx).Value; if (ctx.HasErrors) { return; } var oldColor = Console.ForegroundColor; var oldBackColor = Console.BackgroundColor; if (color is not null) { Console.ForegroundColor = GetColor(color); } if (backColor is not null) { Console.BackgroundColor = GetColor(backColor); } if (newLine) { Console.Write(str + Environment.NewLine); } else { Console.Write(str); } Console.ForegroundColor = oldColor; Console.BackgroundColor = oldBackColor; }
public void Write(ExecutionContext ctx, DyObject obj) { Reset(); switch (obj.TypeId) { case Dy.Integer: Write(((DyInteger)obj).Value); break; case Dy.Float: Write(((DyFloat)obj).Value); break; case Dy.Bool: Write(obj.IsTrue()); break; case Dy.Char: case Dy.String: Write(obj.ToString()); break; default: ctx.InvalidType(obj); break; } }
private static DyObject IsIn(ExecutionContext ctx, DyObject self, DyObject field) { if (field.TypeId is not Dy.String and not Dy.Char) { return(False); } return(((DyClass)self).Fields.GetOrdinal(field.ToString()) is not - 1 ? True : False); }
public DyObject Assert(ExecutionContext ctx, DyObject expected, DyObject got) { if (!Eq(expected?.ToObject(), got?.ToObject())) { return(ctx.AssertFailed($"Expected {expected?.ToString(ctx)}, got {got?.ToString(ctx)}")); } return(DyNil.Instance); }
protected override DyObject InOp(ExecutionContext ctx, DyObject self, DyObject field) { if (field.TypeId is not Dy.String and not Dy.Char) { return(ctx.InvalidType(field)); } return(((DyTuple)self).GetOrdinal(field.ToString()) is not - 1 ? True : False); }
public static DyObject Concat(this DyObject left, DyObject right, ExecutionContext ctx) { var self = left.ToString(ctx); var type = ctx.RuntimeContext.String; var ret = type.Add(ctx, self, right); if (ReferenceEquals(ctx.Error, DyVariant.Eta)) { ret = ctx.InvokeCallBackFunction(right); } return(ret); }
protected override DyObject ToStringOp(ExecutionContext ctx, DyObject self, DyObject format) { if (format.TypeId is not Dy.String and not Dy.Char and not Dy.Nil) { return(ctx.InvalidType(format)); } try { var value = ((DyInteger)self).Value; return(new DyString(format.TypeId is Dy.Nil ? value.ToString(SystemCulture.NumberFormat) : value.ToString(format.ToString(), SystemCulture.NumberFormat))); } catch (FormatException) { return(ctx.ParsingFailed()); } }
protected override DyObject ToStringOp(ExecutionContext ctx, DyObject arg, DyObject format) { if (format.Is(Dy.Nil)) { return(new DyString(arg.ToString())); } if (format.TypeId is not Dy.String and not Dy.Char) { return(Nil); } try { return(new DyString(((T)arg).ToString(format.ToString(), null))); } catch (FormatException) { return(ctx.ParsingFailed()); } }
public DyObject Print(ExecutionContext ctx, [VarArg] DyObject values, [Default(",")] DyObject separator, [Default("\n")] DyObject terminator) { var fst = true; foreach (var a in (DyTuple)values) { if (!fst) { Console.Write(separator.TypeId == DyType.String ? separator.GetString() : separator.ToString(ctx).ToString()); } if (a.TypeId == DyType.String) { Console.Write(a.GetString()); } else { Console.Write(a.ToString(ctx)); } fst = false; if (ctx.Error != null) { break; } } Console.Write(terminator.TypeId == DyType.String ? terminator.GetString() : terminator.ToString(ctx).ToString()); return(DyNil.Instance); }