public PartialStaticCall(int[] mappings, PValue[] closedArguments, PCall call, string memberId, PType ptype) : base(mappings, closedArguments, 0) { _ptype = ptype; _call = call; _memberId = memberId; }
public static AstGetSet Call(this IAstFactory factory, ISourcePosition position, EntityRef entity, PCall call = PCall.Get, params AstExpr[] arguments) { var c = factory.IndirectCall(position, factory.Reference(position, entity), call); c.Arguments.AddRange(arguments); return c; }
public AstGetSetStatic( string file, int line, int col, PCall call, AstTypeExpr typeExpr, string memberId) : base(file, line, col, call) { if (typeExpr == null) throw new ArgumentNullException("typeExpr"); if (memberId == null) throw new ArgumentNullException("memberId"); TypeExpr = typeExpr; _memberId = memberId; }
public AstGetSetMemberAccess( string file, int line, int column, PCall call, AstExpr subject, string id) : base(file, line, column, call) { if (subject == null) throw new ArgumentNullException("subject"); if (id == null) id = ""; Subject = subject; Id = id; }
public void Expect(string memberId, PValue[] args, PCall call = PCall.Get, PValue returns = null) { _expectations.Add( memberId, new CallExpectation { ExpectedArguments = args, ExpectedCall = call, ReturnValue = returns }); }
public bool TryDynamicCall(StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { CallExpectation expectation; Assert.IsTrue(_expectations.TryGetValue(id, out expectation), String.Format("A call to member {0} on object {1} is not expected.", id, Name)); Assert.AreEqual(expectation.ExpectedCall, call, "Call type (get/set)"); Assert.AreEqual(expectation.ExpectedArguments.Length, args.Length, "Number of arguments do not match. Called with " + args.ToEnumerationString()); for (var i = 0; i < expectation.ExpectedArguments.Length; i++) Assert.AreEqual(expectation.ExpectedArguments[i], args[i], String.Format("Arguments at position {0} don't match", i)); result = expectation.ReturnValue ?? PType.Null; expectation.WasCalled = true; return true; }
public bool TryDynamicCall(StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { result = null; switch (id.ToUpperInvariant()) { case "RECEIVE": result = Receive(); break; case "SEND": Send((args.Length > 0 ? args[0] : null) ?? PType.Null); result = PType.Null; break; case "TOSTRING": result = ToString(); break; case "TRYRECEIVE": PValue datum; var refVar = (args.Length > 0 ? args[0] : null) ?? PType.Null; if (TryReceive(out datum)) { refVar.IndirectCall(sctx, new[] {datum}); result = true; } else { refVar.IndirectCall(sctx, new PValue[] {PType.Null}); result = false; } break; default: return false; } return result != null; }
public override bool TryStaticCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { if (args.Length >= 1 && Engine.StringsAreEqual(id, "unescape")) { result = Unescape(args[0].ConvertTo(sctx, String).Value as string); return true; } if (args.Length >= 1 && Engine.StringsAreEqual(id, "escape")) { result = Escape(args[0].ConvertTo(sctx, String).Value as string); return true; } if (args.Length > 1 && Engine.StringsAreEqual(id, "format")) { var oargs = new object[args.Length - 1]; var format = args[0].CallToString(sctx); for (var i = 0; i < oargs.Length; i++) oargs[i] = args[i + 1].CallToString(sctx); result = System.String.Format(format, oargs); return true; } return Object[typeof (string)].TryStaticCall(sctx, args, call, id, out result); }
protected AstGetSetImplBase(string file, int line, int column, PCall call) : this(new SourcePosition(file,line,column), call) { }
public override bool TryStaticCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { Object[typeof (double)].TryStaticCall(sctx, args, call, id, out result); return result != null; }
public override bool TryDynamicCall( StackContext sctx, PValue subject, PValue[] args, PCall call, string id, out PValue result) { result = null; var str = (string) subject.Value; switch ((id == null) ? "" : id.ToLowerInvariant()) { case "": if (args.Length < 1) return false; var nArg = args[0]; PValue rArg; if (!nArg.TryConvertTo(sctx, Int, out rArg)) return false; result = str[(int) rArg.Value].ToString(); break; case "unescape": result = Unescape(str); break; case "format": var objs = new object[args.Length]; for (var i = 0; i < args.Length; i++) objs[i] = args[i].Value; result = System.String.Format(str, objs); break; case "escape": result = Escape(str); break; case "isreservedword": result = IsReservedWord(str); break; case "toidorliteral": result = ToIdOrLiteral(str); break; case "tostring": result = subject; break; case "tolower": result = str.ToLower(); break; case "toupper": result = str.ToUpper(); break; case "substring": switch (args.Length) { case 0: return false; case 1: result = str.Substring( (int) args[0].ConvertTo(sctx, Int).Value); break; default: result = str.Substring( (int) args[0].ConvertTo(sctx, Int).Value, (int) args[1].ConvertTo(sctx, Int).Value); break; } break; case "split": if (args.Length == 0 || args[0].IsNull) { result = (PValue) _wrap_strings(str.Split(null)); return true; } //Try to interpret as params char[] or fall back to params string[] var sch = new List<char>(); List<string> sst = null; var isParams = true; _resolve_params(sctx, args, ref isParams, sch, ref sst, false); if (isParams) { if (sst != null) { result = (PValue) _wrap_strings( str.Split(sst.ToArray(), StringSplitOptions.None)); } else { result = (PValue) _wrap_strings( str.Split(sch.ToArray(), StringSplitOptions.None)); } return true; } PValue list; if (!args[0].TryConvertTo(sctx, List, true, out list)) throw new PrexoniteException( "String.Split requires a list as its first argument."); sch.Clear(); sst = null; var isValid = true; _resolve_params(sctx, ((List<PValue>) list.Value).ToArray(), ref isValid, sch, ref sst, true); if (!isValid) throw new PrexoniteException( "String.Split only accepts lists of strings or chars."); if (sst != null) { result = (PValue) _wrap_strings( str.Split(sst.ToArray(), StringSplitOptions.None)); } else { result = (PValue) _wrap_strings( str.Split(sch.ToArray(), StringSplitOptions.None)); } return true; default: return Object[typeof (string)].TryDynamicCall( sctx, subject, args, call, id, out result); } return result != null; }
/// <summary> /// Tries to handle prexonite object member calls. /// </summary> /// <param name = "sctx">The stack context of the call.</param> /// <param name = "args">The arguments for the call.</param> /// <param name = "call">Indicates the mode of call.</param> /// <param name = "id">The id of the member to call (empty for the default member).</param> /// <param name = "result">The result returned by the call.</param> /// <returns>True, if the call succeeded, false otherwise.</returns> /// <remarks> /// <paramref name = "result" /> is not defined if the function returns false. /// </remarks> /// <exception cref = "ArgumentNullException"><paramref name = "sctx" /> is null.</exception> public bool TryDynamicCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { if (sctx == null) throw new ArgumentNullException("sctx"); if (args == null) args = new PValue[] {}; if (id == null) id = ""; result = null; PValue arg0; switch (id.ToLowerInvariant()) { case "": if (args.Length == 1) { if (args[0].TryConvertTo(sctx, PType.Int, out arg0)) { var i = (int) arg0.Value; if (i == 0) result = _key; else result = i == 1 ? _value : PType.Null.CreatePValue(); } } break; case "key": result = _key; break; case "value": result = _value; break; case "equals": if (args.Length == 1) { if (args[0].TryConvertTo(sctx, _objectType, out arg0)) { var pair = (PValueKeyValuePair) arg0.Value; result = _key.Equals(pair._key) && _value.Equals(pair._value); } } break; case "tostring": result = String.Concat(_key.CallToString(sctx), ": ", _value.CallToString(sctx)); break; } return result != null; }
public override bool TryDynamicCall( StackContext sctx, PValue subject, PValue[] args, PCall call, string id, out PValue result) { if (sctx == null) throw new ArgumentNullException("sctx"); if (subject == null) throw new ArgumentNullException("subject"); if (args == null) throw new ArgumentNullException("args"); if (id == null) id = ""; var c = (char) subject.Value; CultureInfo ci; switch (id.ToLowerInvariant()) { case "getnumericvalue": result = System.Char.GetNumericValue(c); break; case "getunicodecategory": result = sctx.CreateNativePValue(System.Char.GetUnicodeCategory(c)); break; case "iscontrol": result = System.Char.IsControl(c); break; case "isdigit": result = System.Char.IsDigit(c); break; case "ishighsurrogate": result = System.Char.IsHighSurrogate(c); break; case "isletter": result = System.Char.IsLetter(c); break; case "isletterordigit": result = System.Char.IsLetterOrDigit(c); break; case "islower": result = System.Char.IsLower(c); break; case "islowsurrogate": result = System.Char.IsLowSurrogate(c); break; case "isnumber": result = System.Char.IsNumber(c); break; case "ispunctuation": result = System.Char.IsPunctuation(c); break; case "issurrogate": result = System.Char.IsSurrogate(c); break; case "issymbol": result = System.Char.IsSymbol(c); break; case "isupper": result = System.Char.IsUpper(c); break; case "iswhitespace": result = System.Char.IsWhiteSpace(c); break; case "tolower": if (args.Length > 0 && args[0].TryConvertTo(sctx, false, out ci)) result = System.Char.ToLower(c, ci); else result = System.Char.ToLower(c); break; case "toupper": if (args.Length > 0 && args[0].TryConvertTo(sctx, false, out ci)) result = System.Char.ToUpper(c, ci); else result = System.Char.ToUpper(c); break; case "tolowerinvariant": result = System.Char.ToLowerInvariant(c); break; case "toupperinvariant": result = System.Char.ToUpperInvariant(c); break; case "length": result = 1; break; default: //Try CLR dynamic call var clrint = Object[subject.ClrType]; if (!clrint.TryDynamicCall(sctx, subject, args, call, id, out result)) result = null; break; } return result != null; }
public override bool TryDynamicCall( StackContext sctx, PValue subject, PValue[] args, PCall call, string id, out PValue result) { if (sctx == null) throw new ArgumentNullException("sctx"); result = null; switch (id.ToUpperInvariant()) { case "TO": if (args.Length < 1) break; var upperLimitPV = args[0].ConvertTo(sctx, Int, true); var stepPV = args.Length > 1 ? args[1].ConvertTo(sctx, Int, true) : 1; var lowerLimit = (int) subject.Value; var upperLimit = (int) upperLimitPV.Value; var step = (int) stepPV.Value; result = sctx.CreateNativePValue (new Coroutine(new CoroutineContext(sctx, _generateIntegerRange(lowerLimit, step, upperLimit)))); break; } if (result != null) return true; //Try CLR dynamic call var clrint = Object[subject.ClrType]; return clrint.TryDynamicCall(sctx, subject, args, call, id, out result); }
public override bool TryStaticCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { result = null; return false; }
public override bool TryDynamicCall( StackContext sctx, PValue subject, PValue[] args, PCall call, string id, out PValue result) { if (sctx == null) throw new ArgumentNullException("sctx"); if (subject == null) throw new ArgumentNullException("subject"); if (args == null) args = new PValue[] {}; if (id == null) id = ""; var pvht = subject.Value as PValueHashtable; if (pvht == null) throw new ArgumentException("Subject must be a Hash."); result = null; for (var i = 0; i < args.Length; i++) { if (args[i] == null) args[i] = Null.CreatePValue(); } var argc = args.Length; switch (id.ToLowerInvariant()) { case "": if (call == PCall.Get && argc > 0) { var key = args[0]; if (pvht.ContainsKey(key)) result = pvht[key]; else result = Null.CreatePValue(); } else if (call == PCall.Set) { if (argc > 1) { pvht.AddOverride(args[0], args[1]); result = Null.CreatePValue(); } else if (argc == 1) { goto case "add"; } } break; case "add": if (argc == 1) { PValueKeyValuePair pair; result = Null.CreatePValue(); if (args[0].IsNull) { } //Ignore this one else if (_tryConvertToPair(sctx, args[0], out pair)) pvht.AddOverride(pair); } else if (argc > 1) { pvht.AddOverride(args[0], args[1]); result = Null.CreatePValue(); } break; case "clear": pvht.Clear(); result = Null.CreatePValue(); break; case "containskey": if (argc == 1) { result = pvht.ContainsKey(args[0]); } else if (argc > 1) { var found = true; foreach (var arg in args) { if (!pvht.ContainsKey(arg)) { found = false; break; } } result = found; } break; case "containsvalue": if (argc == 1) { result = pvht.ContainsValue(args[0]); } else if (argc > 1) { var found = true; foreach (var arg in args) { if (!pvht.ContainsValue(arg)) { found = false; break; } } result = found; } break; case "count": case "length": result = pvht.Count; break; case "getenumerator": result = Object.CreatePValue(new PValueEnumeratorWrapper(pvht.GetPValueEnumerator())); break; case "gethashcode": result = pvht.GetHashCode(); break; case "gettype": result = Object.CreatePValue(typeof (PValueHashtable)); break; case "keys": result = List.CreatePValue(new List<PValue>(pvht.Keys)); break; case "remove": if (argc == 1) { result = pvht.Remove(args[0]); } else if (argc > 1) { var removed = new List<PValue>(pvht.Count); foreach (var arg in args) removed.Add(pvht.Remove(arg)); result = List.CreatePValue(removed); } break; case "tostring": var sb = new StringBuilder("{ "); foreach (var pair in pvht) { sb.Append(pair.Key.CallToString(sctx)); sb.Append(": "); sb.Append(pair.Value.CallToString(sctx)); sb.Append(", "); } if (pvht.Count > 0) sb.Length -= 2; sb.Append(" }"); result = sb.ToString(); break; case "trygetvalue": if (argc >= 2) { PValue value; if (pvht.TryGetValue(args[0], out value)) { args[1].IndirectCall(sctx, new[] {value}); result = true; } else { result = false; } } break; case "values": result = List.CreatePValue(new List<PValue>(pvht.Values)); break; default: return PValueHashtable.ObjectType.TryDynamicCall( sctx, subject, args, call, id, out result); } return result != null; }
public bool TryDynamicCall(StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { result = null; switch (id.ToUpperInvariant()) { case "FORCE": result = Force(sctx); break; case "EVALUATED": case "ISEVALUATED": result = IsEvaluated; break; } return result != null; }
protected AstGetSetImplBase(ISourcePosition position, PCall call) : base(position) { _call = call; _proxy = new ArgumentsProxy(new List<AstExpr>()); }
public virtual bool TryDynamicCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { result = null; switch (id.ToUpperInvariant()) { case "GETOPTIMIZEDNODE": CompilerTarget target; if (args.Length < 1 || (target = args[0].Value as CompilerTarget) == null) throw new PrexoniteException( "_GetOptimizedNode(CompilerTarget target) requires target."); var expr = this as AstExpr; if (expr == null) throw new PrexoniteException("The node is not an AstExpr."); result = target.Loader.CreateNativePValue(_GetOptimizedNode(target, expr)); break; case "EMITEFFECTCODE": if (args.Length < 1 || (target = args[0].Value as CompilerTarget) == null) throw new PrexoniteException( "EmitEffectCode(CompilerTarget target) requires target."); EmitEffectCode(target); result = PType.Null; break; } return result != null; }
internal AstGetSetStatic(Parser p, PCall call, AstTypeExpr typeExpr, string memberId) : this(p.scanner.File, p.t.line, p.t.col, call, typeExpr, memberId) { }
public AstNamespaceUsage(ISourcePosition position, PCall call, [NotNull] Namespace @namespace) : base(position, call) { if (@namespace == null) throw new ArgumentNullException("namespace"); _namespace = @namespace; }
public bool TryDynamicCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { if (sctx == null) throw new ArgumentNullException("sctx"); if (args == null) args = new PValue[0]; if (id == null) id = ""; result = null; switch (id.ToLower(CultureInfo.InvariantCulture)) { case "include": foreach (var arg in args) Include(arg.ConvertTo<PFunction>(sctx)); result = PType.Null.CreatePValue(); break; case "includerange": foreach (var arg in args) { if (arg.Type.ToBuiltIn() != PType.BuiltIn.List) continue; foreach (var func in (List<PValue>) arg.Value) Include(func.ConvertTo<PFunction>(sctx)); } result = PType.Null.CreatePValue(); break; case "includeall": Application tapp; if (!(args.Length > 0 && args[0].TryConvertTo(sctx, out tapp))) tapp = sctx.ParentApplication; IncludeAll(tapp); result = PType.Null.CreatePValue(); break; case "measureall": bool verbose; if (!(args.Length > 0 && args[0].TryConvertTo(sctx, out verbose))) verbose = true; var plst = MeasureAll(verbose).Select(sctx.CreateNativePValue).ToList(); result = (PValue) plst; break; case "warmup": WarmUp(); result = PType.Null.CreatePValue(); break; } return result != null; }
internal AstGetSetImplBase(Parser p, PCall call) : this(p.scanner.File, p.t.line, p.t.col, call) { }
internal AstGetSetMemberAccess(Parser p, PCall call, AstExpr subject, string id) : this(p.scanner.File, p.t.line, p.t.col, call, subject, id) { }
public override bool TryStaticCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { if (sctx == null) throw new ArgumentNullException("sctx"); if (args == null) args = new PValue[] {}; if (id == null) id = ""; result = null; for (var i = 0; i < args.Length; i++) { if (args[i] == null) args[i] = Null.CreatePValue(); } PValueHashtable pvht; switch (id.ToLowerInvariant()) { case "create": //Create(params KeyValuePair[] pairs) pvht = new PValueHashtable(args.Length); foreach (var arg in args) { PValueKeyValuePair pairArg; if (_tryConvertToPair(sctx, arg, out pairArg)) pvht.AddOverride(pairArg); } result = new PValue(pvht, this); break; case "createFromArgs": if (args.Length%2 != 0) break; pvht = new PValueHashtable(args.Length/2); for (var i = 0; i < args.Length; i += 2) pvht.AddOverride(args[i], args[i + 1]); result = new PValue(pvht, this); break; default: return PValueHashtable.ObjectType.TryStaticCall(sctx, args, call, id, out result); } return result != null; }
public AstGetSetMemberAccess(string file, int line, int column, PCall call, string id) : this(file, line, column, call, null, id) { }
internal AstGetSetMemberAccess(Parser p, PCall call, string id) : this(p, call, null, id) { }
public AstExpand(ISourcePosition position, EntityRef entity, PCall call) : base(position, call) { _entity = entity; }
public override bool TryStaticCall( StackContext sctx, PValue[] args, PCall call, string id, out PValue result) { //Try CLR static call var clrint = Object[typeof (int)]; return clrint.TryStaticCall(sctx, args, call, id, out result); }
public override bool TryDynamicCall( StackContext sctx, PValue subject, PValue[] args, PCall call, string id, out PValue result) { result = null; if (Engine.StringsAreEqual(id, "tostring")) result = String.CreatePValue(""); else if (Engine.StringsAreEqual(id, @"\boxed")) result = sctx.CreateNativePValue(CreatePValue()); return result != null; }