private static bool _tryConvertToPair( StackContext sctx, PValue inpv, out PValueKeyValuePair result) { PValue res; result = null; if (!inpv.TryConvertTo(sctx, typeof (PValueKeyValuePair), out res)) return false; else result = (PValueKeyValuePair) res.Value; return true; }
private static bool _tryConvertToInt( StackContext sctx, PValue operand, out int value, bool allowNull) { value = -1337; //should never surface as value is only used if the method returns true switch (operand.Type.ToBuiltIn()) { case BuiltIn.Int: value = Convert.ToInt32(operand.Value); return true; /* case BuiltIn.String: string rawRight = operand.Value as string; if (!int.TryParse(rawRight, out value)) value = 0; return true; //*/ case BuiltIn.Object: PValue pvRight; if (operand.TryConvertTo(sctx, Int, out pvRight)) { value = (int) pvRight.Value; return true; } break; case BuiltIn.Null: value = 0; return allowNull; default: break; } return false; }
private bool _tryConvert(StackContext sctx, PValue pv, out char c) { c = '\0'; switch (pv.Type.ToBuiltIn()) { case BuiltIn.Char: c = (char) pv.Value; return true; case BuiltIn.Int: c = (char) (int) pv.Value; return true; case BuiltIn.Null: return true; case BuiltIn.String: var s = (string) pv.Value; if (s.Length == 1) { c = s[0]; return true; } else { return false; } case BuiltIn.Object: if (pv.TryConvertTo(sctx, Char, false, out pv)) return _tryConvert(sctx, pv, out c); else return false; case BuiltIn.Structure: case BuiltIn.Hash: case BuiltIn.List: case BuiltIn.Bool: case BuiltIn.None: case BuiltIn.Real: return false; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Tries to turn a generic PValue object into an <see cref = "IEnumerable{PValue}" /> if possible. Returns null if <paramref /// name = "psource" /> cannot be enumerated over. /// </summary> /// <param name = "sctx"></param> /// <param name = "psource"></param> /// <returns></returns> internal static IEnumerable<PValue> _ToEnumerable(StackContext sctx, PValue psource) { switch (psource.Type.ToBuiltIn()) { case PType.BuiltIn.List: return (IEnumerable<PValue>) psource.Value; case PType.BuiltIn.Object: var clrType = ((ObjectPType) psource.Type).ClrType; if (typeof (IEnumerable<PValue>).IsAssignableFrom(clrType)) goto case PType.BuiltIn.List; else if (typeof (IEnumerable).IsAssignableFrom(clrType)) return _wrapNonGenericIEnumerable(sctx, (IEnumerable) psource.Value); break; } IEnumerable<PValue> set; IEnumerable nset; if (psource.TryConvertTo(sctx, true, out set)) return set; else if (psource.TryConvertTo(sctx, true, out nset)) return _wrapNonGenericIEnumerable(sctx, nset); else return _wrapDynamicIEnumerable(sctx, psource); }
private static EntityRef _getMacroRef(StackContext sctx, PValue rawMacro) { PFunction func; MacroCommand mcmd; if (rawMacro.TryConvertTo(sctx, out func)) return EntityRef.Function.Create(func.Id, func.ParentApplication.Module.Name); else if (rawMacro.TryConvertTo(sctx, out mcmd)) return EntityRef.MacroCommand.Create(mcmd.Id); else return rawMacro.ConvertTo<EntityRef>(sctx); }
private static bool _tryConvertToBool(StackContext sctx, PValue operand, out bool value) { value = false; switch (operand.Type.ToBuiltIn()) { case BuiltIn.Real: case BuiltIn.Int: case BuiltIn.Bool: value = (bool) operand.Value; return true; case BuiltIn.String: if (!bool.TryParse(operand.Value as string, out value)) { value = false; return true; } break; case BuiltIn.Object: PValue asBool; if (operand.TryConvertTo(sctx, Bool, out asBool)) { value = (bool) asBool.Value; return true; } break; case BuiltIn.Null: value = false; return true; } return false; }
public static PValue RunStatically(PValue arg0, PValue arg1, StackContext sctx) { var x = (double) arg0.ConvertTo(sctx, PType.Real, true).Value; int d; PValue pd; if (arg1 != null && arg1.TryConvertTo(sctx, PType.Int, true, out pd)) d = System.Math.Abs((int) pd.Value); else d = 0; return System.Math.Round(x, d, MidpointRounding.AwayFromZero); }