Ejemplo n.º 1
0
        private AphidObject CallFunctionCore(AphidFunction function, IEnumerable <AphidObject> parms)
        {
            var functionScope = new AphidObject(null, function.ParentScope);
            var i             = 0;

            foreach (var arg in parms)
            {
                if (function.Args.Length == i)
                {
                    break;
                }

                functionScope.Add(function.Args[i++], arg);
            }

            var lastScope = _currentScope;

            _currentScope = functionScope;
            Interpret(function.Body);
            var retVal = GetReturnValue();

            _currentScope = lastScope;

            return(retVal);
        }
Ejemplo n.º 2
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>()
            }));
        }
Ejemplo n.º 3
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>()
                }));
        }
        private AphidObject InterpretObjectExpression(ObjectExpression expression)
        {
            var obj = new AphidObject();

            foreach (var kvp in expression.Pairs)
            {
                var id    = (kvp.LeftOperand as IdentifierExpression).Identifier;
                var value = ValueHelper.Wrap(InterpretExpression(kvp.RightOperand));
                obj.Add(id, value);
            }

            return(obj);
        }
Ejemplo n.º 5
0
 private void SetReturnValue(AphidObject obj)
 {
     _currentScope.Add(_return, obj);
 }
        private AphidObject InterpretObjectExpression(ObjectExpression expression)
        {
            var obj = new AphidObject();

            foreach (var kvp in expression.Pairs)
            {
                var id = (kvp.LeftOperand as IdentifierExpression).Identifier;
                var value = ValueHelper.Wrap(InterpretExpression(kvp.RightOperand));
                obj.Add(id, value);
            }

            return obj;
        }
Ejemplo n.º 7
0
        private static AphidObject Exec(AphidObject exeObj, AphidObject argsObj, AphidObject optionsObj)
        {
            string exe = (string)exeObj.Value, args = argsObj != null ? (string)argsObj.Value : null;

            var opt = optionsObj != null ?
                optionsObj.ConvertTo<ExecOptions>() :
                new ExecOptions();

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo(exe, args)
                {
                    RedirectStandardOutput = opt.RedirectOutput,
                    RedirectStandardError = opt.RedirectOutput,
                    UseShellExecute = !opt.RedirectOutput,
                }
            };

            if (!string.IsNullOrEmpty(opt.Working))
            {
                process.StartInfo.WorkingDirectory = opt.Working;
                process.StartInfo.UseShellExecute = false;
            }

            var sb = new StringBuilder();

            DataReceivedEventHandler handler = (o, e) =>
            {
                lock (sb)
                {
                    sb.AppendLine(e.Data);
                }
            };

            if (opt.RedirectOutput)
            {
                process.OutputDataReceived += handler;
                process.ErrorDataReceived += handler;
            }

            process.Start();

            if (opt.RedirectOutput)
            {
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
            }

            if (opt.RedirectOutput || opt.WaitForExit)
            {
                process.WaitForExit();

                var retVal = new AphidObject();
                retVal.Add("exitCode", new AphidObject((decimal)process.ExitCode));

                if (opt.RedirectOutput)
                {
                    retVal.Add("output", new AphidObject(sb.ToString()));
                }

                return retVal;
            }
            else
            {
                return null;
            }
        }
Ejemplo n.º 8
0
        private AphidObject CallFunctionCore(AphidFunction function, IEnumerable<AphidObject> parms)
        {
            var functionScope = new AphidObject(null, function.ParentScope);
            var i = 0;

            foreach (var arg in parms)
            {
                if (function.Args.Length == i)
                {
                    break;
                }

                functionScope.Add(function.Args[i++], arg);
            }

            var lastScope = _currentScope;
            _currentScope = functionScope;
            Interpret(function.Body);
            var retVal = GetReturnValue();
            _currentScope = lastScope;

            return retVal;
        }
Ejemplo n.º 9
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);
        }