public DefaultAttribute(string value) { Value = new DyString(value); }
private Par[] CompileFunctionParameters(List <DParameter> pars) { var arr = new Par[pars.Count]; var hasVarArg = false; for (var i = 0; i < pars.Count; i++) { var p = pars[i]; if (p.IsVarArgs) { if (hasVarArg) { AddError(CompilerError.VarArgOnlyOne, p.Location); } hasVarArg = true; } if (p.DefaultValue != null) { if (p.IsVarArgs) { AddError(CompilerError.VarArgNoDefaultValue, p.Location); } DyObject val = null; switch (p.DefaultValue.NodeType) { case NodeType.Integer: val = new DyInteger(((DIntegerLiteral)p.DefaultValue).Value); break; case NodeType.Float: val = new DyFloat(((DFloatLiteral)p.DefaultValue).Value); break; case NodeType.Char: val = new DyChar(((DCharLiteral)p.DefaultValue).Value); break; case NodeType.Boolean: val = ((DBooleanLiteral)p.DefaultValue).Value ? DyBool.True : DyBool.False; break; case NodeType.String: val = new DyString(((DStringLiteral)p.DefaultValue).Value); break; case NodeType.Nil: val = DyNil.Instance; break; default: AddError(CompilerError.InvalidDefaultValue, p.DefaultValue.Location, p.Name); break; } arr[i] = new Par(p.Name, val, false); } else { arr[i] = new Par(p.Name, null, p.IsVarArgs); } } return(arr); }
public string ToString(string?format, IFormatProvider?formatProvider) => Object.ToString(Context, DyString.Get(format)).ToString();
internal static DyObject ConvertFrom(object?obj, Type type) { if (obj is null) { return(DyNil.Instance); } if (obj is DyObject retval) { return(retval); } switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: return((bool)obj ? True : False); case TypeCode.Byte: return(new DyInteger((byte)obj)); case TypeCode.Int16: return(new DyInteger((short)obj)); case TypeCode.Int32: return(new DyInteger((int)obj)); case TypeCode.Int64: return(new DyInteger((long)obj)); case TypeCode.SByte: return(new DyInteger((sbyte)obj)); case TypeCode.UInt16: return(new DyInteger((ushort)obj)); case TypeCode.UInt32: return(new DyInteger((uint)obj)); case TypeCode.UInt64: return(new DyInteger((long)(ulong)obj)); case TypeCode.String: case TypeCode.Char: return(DyString.Get(obj.ToString())); case TypeCode.Single: return(new DyFloat((float)obj)); case TypeCode.Double: return(new DyFloat((double)obj)); case TypeCode.Decimal: return(new DyFloat((double)(decimal)obj)); case TypeCode.Empty: return(DyNil.Instance); default: if (obj is IDictionary map) { var dict = new Dictionary <DyObject, DyObject>(); foreach (DictionaryEntry kv in map) { dict[ConvertFrom(kv.Key)] = kv.Value is null ? DyNil.Instance : ConvertFrom(kv.Value); } return(new DyDictionary(dict)); } else if (obj is IEnumerable seq) { return(new DyArray(seq.OfType <object>().Select(o => ConvertFrom(o)).ToArray())); } else if (type.IsArray) { var arr = (Array)obj; var newArr = new DyObject[arr.Length]; for (var i = 0; i < arr.Length; i++) { newArr[i] = ConvertFrom(arr.GetValue(i)); } return(new DyArray(newArr)); } else if (BCL.Type.IsAssignableFrom(type)) { return(new DyInterop((Type)obj)); } else { return(new DyInterop(type, obj)); } } }