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();
                        }
                    }
                }
            }
        }
Example #5
0
 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)));
     }
 }
Example #7
0
 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();
        }
Example #10
0
        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);
                }
            }
        }
Example #11
0
 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);
     }
 }
Example #12
0
        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);
        }
Example #13
0
        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>()
                }));
        }
Example #14
0
 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;
        }
Example #16
0
        public static void Extend(AphidInterpreter interpreter, AphidObject type, AphidObject extensions)
        {
            var typeStr = (string)type.Value;

            TypeExtender.Extend(interpreter, (string)type.Value, extensions);
        }
Example #17
0
 public static AphidObject Serialize(AphidObject obj)
 {
     return new AphidObject(_serializer.Serialize(obj));
 }
Example #18
0
 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);
 }
Example #19
0
 public static AphidObject Deserialize(AphidObject obj)
 {
     return _serializer.Deserialize((string)obj.Value);
 }
Example #20
0
 public AphidPropertyValuePair(PropertyInfo property, AphidObject value)
 {
     Property = property;
     Value    = value;
 }
Example #21
0
 private static List<AphidObject> ListOrderByDescending(AphidInterpreter interpreter, AphidObject items, AphidObject keySelector)
 {
     return ListOrder(interpreter, items, keySelector, Enumerable.OrderByDescending);
 }
Example #22
0
        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)));
 }
 public AphidPropertyValuePair(PropertyInfo property, AphidObject value)
 {
     Property = property;
     Value = value;
 }
Example #26
0
 private void SetReturnValue(AphidObject obj)
 {
     _currentScope.Add(_return, obj);
 }
Example #27
0
 public static AphidObject NotEqual(AphidObject x, AphidObject y)
 {
     return(new AphidObject(!EqualsCore(x, y)));
 }
Example #28
0
 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));
 }
Example #29
0
 public void EnterChildScope()
 {
     _currentScope = new AphidObject(null, _currentScope);
 }
Example #30
0
 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);
 }
Example #38
0
 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)));
 }
Example #42
0
        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));
 }