public AphidObject Multiply(AphidObject x, AphidObject y) { object val; if (x.Value is Decimal) { val = ((decimal)x.Value) * (decimal)y.Value; } else if (x.Value is string) { var s = x.Value as string; var sb = new StringBuilder(); for (int i = 0; i < (decimal)y.Value; i++) { sb.Append(s); } return _callInitFunction(new AphidObject(sb.ToString())); //val = sb.ToString(); } else { throw new AphidRuntimeException("Could not multiply type"); } return new AphidObject(val); }
public string Serialize(AphidObject o) { var sb = new StringBuilder(); ObjToString(o, sb, 0); return sb.ToString(); }
public static void Lock(AphidInterpreter interpreter, AphidObject obj, AphidFunction body) { lock (obj) { interpreter.CallFunction(body); } }
public static string HttpRequest(AphidObject request) { var req = request.ConvertTo<AphidRequest>(); try { var req2 = req.ToHttpRequest(); using (var client = HttpClient.Connect(req.Host, req.Port)) { client.Write(req2); var resp = client.Read().GetBodyString(); return resp; } } finally { if (req.Files != null) { foreach (var f in req.Files) { if (f.Stream != null) { f.Stream.Dispose(); } } } } }
public static AphidObject BinaryShiftLeft(AphidObject x, AphidObject y) { if (x == null || y == null) { throw new AphidOperationException("binary shift left"); } else { return new AphidObject((decimal)((int)(decimal)x.Value << (int)(decimal)y.Value)); } }
public AphidObject Add(AphidObject x, AphidObject y) { if (x.Value is decimal && y.Value is decimal) { return new AphidObject((decimal)x.Value + (decimal)y.Value); } else { return _callInitFunction(new AphidObject(Convert.ToString(x.Value) + Convert.ToString(y.Value))); } }
public static AphidObject BinaryOr(AphidObject x, AphidObject y) { if (x == null || y == null) { throw new AphidOperationException("binary or"); } else { return new AphidObject((decimal)((long)(decimal)x.Value | (long)(decimal)y.Value)); } }
public static string[] LoadTestCases(AphidObject obj) { string[] testCases; if (_testCaseCache.TryGetValue(obj, out testCases)) { return testCases; } testCases = AphidObjectConverter.ToStringArray(obj); Cli.WriteLine("~Green~{0}~R~ test cases loaded", testCases.Length); return testCases; }
public void OnBound(AphidObject source) { var p = source["projects"] .GetStringList() .Select(x => new { Name = x, Project = ReleaseProject.Load(PathHelper.GetExecutingPath(Root, x), "debug") }) .ToArray(); p.Single(x => x.Name == MainProject).Project.IsMainProject = true; Projects = p.Select(x => x.Project).ToArray(); }
public static void Extend(AphidInterpreter interpreter, string type, AphidObject extensions) { foreach (var extension in extensions) { var nameStr = extension.Key; var key = GetName(type, nameStr); if (interpreter.CurrentScope.ContainsKey(key)) { interpreter.CurrentScope[key] = extension.Value; } else { interpreter.CurrentScope.Add(key, extension.Value); } } }
public static void Kill(AphidObject process) { if (process.Value is int) { Process.GetProcessById((int)process.Value).Kill(); } else if (process.Value is string) { foreach (var p in Process.GetProcessesByName((string)process.Value)) { p.Kill(); } } else { throw new AphidRuntimeException("Invalid process id: {0}", process.Value); } }
public AphidObject CallFunction(AphidObject function, params object[] args) { var value = function.Value; var func = value as AphidFunction; if (func != null) { return CallFunction(func, args); } var func2 = value as AphidInteropFunction; if (func2 != null) { return CallInteropFunction(func2, (AphidObject[])args); } throw new AphidRuntimeException("Object is not function: {0}", function); }
public AphidInterpreter() { Init(); _currentScope = new AphidObject(); _currentScope.Add( "__initList", ValueHelper.Wrap(new AphidFunction() { Args = new[] { "x" }, Body = new List<Expression>() })); _currentScope.Add( "__initString", ValueHelper.Wrap(new AphidFunction() { Args = new[] { "x" }, Body = new List<Expression>() })); }
public static AphidObject Add(AphidObject x, AphidObject y) { if (x == null || y == null) { throw new AphidOperationException("addition"); } else if (x.Value is decimal && y.Value is decimal) { return new AphidObject((decimal)x.Value + (decimal)y.Value); } else if ((x.Value is sbyte || x.Value is short || x.Value is int || x.Value is long || x.Value is byte || x.Value is ushort || x.Value is uint || x.Value is ulong || x.Value is float || x.Value is double || x.Value is decimal) && (y.Value is sbyte || y.Value is short || y.Value is int || y.Value is long || y.Value is byte || y.Value is ushort || y.Value is uint || y.Value is ulong || y.Value is float || y.Value is double || y.Value is decimal)) { return new AphidObject(Convert.ToDecimal(x.Value) + Convert.ToDecimal(y.Value)); } else { return new AphidObject(Convert.ToString(x.Value) + Convert.ToString(y.Value)); } }
public static byte[] ToBytes(AphidObject obj) { var v = obj.Value; byte[] bytes; List<AphidObject> objects; string str; if ((objects = v as List<AphidObject>) != null) { bytes = objects.Select(x => (byte)(decimal)x.Value).ToArray(); } else if ((str = v as string) != null) { bytes = StandardLibrary.Encoding.GetBytes(str); } else { throw new AphidRuntimeException("Invalid object passed as buffer: {0}", v); } return bytes; }
public static void Extend(AphidInterpreter interpreter, AphidObject type, AphidObject extensions) { var typeStr = (string)type.Value; TypeExtender.Extend(interpreter, (string)type.Value, extensions); }
public static AphidObject Serialize(AphidObject obj) { return new AphidObject(_serializer.Serialize(obj)); }
private static string StringSubstring(AphidObject str, AphidObject index, AphidObject length) { var str2 = (string)str.Value; return length == null || length.Value == null ? str2.Substring((int)(decimal)index.Value) : str2.Substring((int)(decimal)index.Value, (int)(decimal)length.Value); }
public static AphidObject Deserialize(AphidObject obj) { return _serializer.Deserialize((string)obj.Value); }
public AphidPropertyValuePair(PropertyInfo property, AphidObject value) { Property = property; Value = value; }
private static List<AphidObject> ListOrderByDescending(AphidInterpreter interpreter, AphidObject items, AphidObject keySelector) { return ListOrder(interpreter, items, keySelector, Enumerable.OrderByDescending); }
private object InterpetAssignmentExpression(BinaryOperatorExpression expression, bool returnRef = false) { var value = InterpretExpression(expression.RightOperand); var value2 = value as AphidObject; var idExp = expression.LeftOperand as IdentifierExpression; ArrayAccessExpression arrayAccessExp; if (idExp != null) { var id = idExp.Identifier; var destObj = InterpretIdentifierExpression(idExp); if (destObj == null) { destObj = new AphidObject(); _currentScope.Add(id, destObj); } else { destObj.Clear(); } if (value2 != null) { destObj.Value = value2.Value; destObj.Parent = value2.Parent; foreach (var x in value2) { destObj.Add(x.Key, x.Value); } } else { destObj.Value = value; } } else if ((arrayAccessExp = expression.LeftOperand as ArrayAccessExpression) != null) { var obj = InterpretArrayAccessExpression(arrayAccessExp); obj.Value = ValueHelper.Unwrap(value); } else { var objRef = InterpretBinaryOperatorExpression(expression.LeftOperand as BinaryOperatorExpression, true) as AphidRef; if (objRef.Object == null) { throw new AphidRuntimeException("Undefined variable {0}", expression.LeftOperand); } else if (objRef.Object.ContainsKey(objRef.Name)) { objRef.Object[objRef.Name].Value = ValueHelper.Unwrap(value); //ValueHelper.Wrap(value).CopyTo(objRef.Object[objRef.Name]); } else { objRef.Object.Add(objRef.Name, ValueHelper.Wrap(value)); } } return(value); }
public AphidObject BinaryShiftRight(AphidObject x, AphidObject y) { return new AphidObject((decimal)((int)(decimal)x.Value << (int)(decimal)y.Value)); }
public AphidObject Xor(AphidObject x, AphidObject y) { return(new AphidObject((decimal)((int)(decimal)x.Value ^ (int)(decimal)y.Value))); }
private void SetReturnValue(AphidObject obj) { _currentScope.Add(_return, obj); }
public static AphidObject NotEqual(AphidObject x, AphidObject y) { return(new AphidObject(!EqualsCore(x, y))); }
public static bool EqualsCore(AphidObject x, AphidObject y) { return(x.Value != null? x.Value.Equals(y.Value) : (null == y.Value && x.Count == 0 && y.Count == 0)); }
public void EnterChildScope() { _currentScope = new AphidObject(null, _currentScope); }
public AphidInterpreter(AphidObject currentScope) { Init(); _currentScope = currentScope; }
private void ObjToString(AphidObject obj, StringBuilder s, int indent) { if (obj == null) { s.Append("null"); } else if (obj.Value != null) { if (obj.Value is bool || obj.Value is decimal) { s.Append(obj.Value); } else if (obj.Value is string) { s.Append(string.Format("'{0}'", Escape((string)obj.Value))); } else if (obj.Value is List<AphidObject>) { var list = (List<AphidObject>)obj.Value; s.Append("[\r\n"); foreach (var x in list) { s.Append(new string(' ', (indent + 1) * 4)); ObjToString(x, s, indent + 1); s.Append(",\r\n"); } s.AppendFormat("{0}]", new string(' ', indent * 4)); } //else if (obj.Value is AphidFunction || // obj.Value is AphidInteropFunction) else { s.AppendFormat("'`{0}`'", obj.Value.GetType().Name); } //else //{ // throw new InvalidOperationException(); //} } else { s.Append("{\r\n"); foreach (var kvp in obj) { if (IgnoreFunctions && (kvp.Value.Value is AphidFunction || kvp.Value.Value is AphidInteropFunction)) { continue; } s.AppendFormat( "{0}{1}: ", new string(' ', (indent + 1) * 4), kvp.Key); ObjToString(kvp.Value, s, indent + 1); s.Append(",\r\n"); } s.AppendFormat("{0}}}", new string(' ', indent * 4)); } }
public AphidObject Mod(AphidObject x, AphidObject y) { return new AphidObject((decimal)x.Value % (decimal)y.Value); }
public AphidObject Xor(AphidObject x, AphidObject y) { return new AphidObject((decimal)((int)(decimal)x.Value ^ (int)(decimal)y.Value)); }
public AphidObject Subtract(AphidObject x, AphidObject y) { return new AphidObject(((decimal)x.Value) - (decimal)y.Value); }
public AphidObject BinaryOr(AphidObject x, AphidObject y) { return new AphidObject((decimal)((long)(decimal)x.Value | (long)(decimal)y.Value)); }
public AphidObject Divide(AphidObject x, AphidObject y) { return(new AphidObject(((decimal)x.Value) / (decimal)y.Value)); }
public AphidObject Divide(AphidObject x, AphidObject y) { return new AphidObject(((decimal)x.Value) / (decimal)y.Value); }
public void LoadLibrary <TLibrary>(AphidObject scope) { LoadLibrary(typeof(TLibrary), scope); }
public AphidObject BinaryShiftRight(AphidObject x, AphidObject y) { return(new AphidObject((decimal)((int)(decimal)x.Value << (int)(decimal)y.Value))); }
public AphidObject Mod(AphidObject x, AphidObject y) { return(new AphidObject((decimal)x.Value % (decimal)y.Value)); }
public AphidObject BinaryAnd(AphidObject x, AphidObject y) { return(new AphidObject((decimal)((long)(decimal)x.Value & (long)(decimal)y.Value))); }
private static List<AphidObject> ListOrder( AphidInterpreter interpreter, AphidObject items, AphidObject keySelector, Func<IEnumerable<AphidObject>, Func<AphidObject, object>, IOrderedEnumerable<AphidObject>> action) { var list = (List<AphidObject>)items.Value; var func = (AphidFunction)keySelector.Value; return action(list, x => interpreter.CallFunction(func, x).Value).ToList(); }
public static List<AphidObject> Members(AphidObject obj) { return obj.Select(x => new AphidObject(x.Key)).ToList(); }
public AphidObject Subtract(AphidObject x, AphidObject y) { return(new AphidObject(((decimal)x.Value) - (decimal)y.Value)); }