protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Variable == null)
            {
                return(Completion.Exception(Properties.Language.VariableNameException, this));
            }
            if (!(Variable is Identifier))
            {
                return(Completion.Exception(Properties.Language.OnlyVariableAccept, Variable));
            }
            string name = (Variable as Identifier).Variable;

            if (Value == null)
            {
                enviroment.RegisterValue(name, null);
                return(Completion.Void);
            }
            else
            {
                var c = Value.Execute(enviroment);
                if (!c.IsValue)
                {
                    return(c);
                }
                enviroment.RegisterValue(name, c.ReturnValue);
                return(c);
            }
        }
Example #2
0
 public Completion Execute(ExecutionEnvironment enviroment)
 {
     if (Expression == null)
     {
         return(new Completion(null));
     }
     return(Expression.Execute(enviroment));
 }
 protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
 {
     if (Expression == null)
     {
         return(Completion.Void);
     }
     return(Expression.Execute(enviroment));
 }
        public Completion Execute(ExecutionEnvironment enviroment)
        {
            if (Min == null)
            {
                return(Completion.Exception("parameter of random can not be null", Min));
            }
            if (Max == null)
            {
                return(Completion.Exception("parameter of random can not be null", Max));
            }
            var left = Min.Execute(enviroment);

            if (left.Type != CompletionType.Value)
            {
                return(left);
            }
            var right = Max.Execute(enviroment);

            if (right.Type != CompletionType.Value)
            {
                return(right);
            }
            if (right.Type != CompletionType.Value)
            {
                return(right);
            }
            if (!TypeConverters.IsNumber(left.ReturnValue))
            {
                return(Completion.Exception("value is not a number", Min));
            }
            if (!TypeConverters.IsNumber(right.ReturnValue))
            {
                return(Completion.Exception("value is not a number", Max));
            }
            Type T = TypeConverters.GetNumberTypes(left.ReturnValue, right.ReturnValue);

            if (T == null)
            {
                return(Completion.Exception("Only nuber can accepted", this));
            }

            try
            {
                var l = TypeConverters.GetValue <int>(left.ReturnValue);
                var r = TypeConverters.GetValue <int>(right.ReturnValue);
                return(new Completion(new Random(1437).Next(l, r)));
            }
            catch (Exception e)
            {
                return(Completion.Exception(e.Message, this));
            }
        }
Example #5
0
        public Completion Execute(ExecutionEnvironment enviroment)
        {
            ExecutionEnvironment current = new ExecutionEnvironment();

            foreach (var f in enviroment.Module.Functions)
            {
                if (Function.Equals(f.Name))
                {
                    for (int i = 0; i < Args.Count; i++)
                    {
                        Expression e    = Args[i];
                        string     name = ArgTyps[i];

                        Completion cp = e.Execute(enviroment);
                        if (cp.Type != CompletionType.Value)
                        {
                            return(cp);
                        }
                        current.RegisterValue(f.Params[i].Name, cp.ReturnValue);
                    }
                    var c = f.Execute(current);
                    return(c);
                }
            }
            DelegateFunction func       = enviroment.GetFunction(Function);
            List <object>    parameters = new List <object>();

            if (func != null)
            {
                try
                {
                    for (int i = 0; i < Args.Count; i++)
                    {
                        Expression e    = Args[i];
                        string     name = ArgTyps[i];

                        Completion cp = e.Execute(enviroment);
                        if (cp.Type != CompletionType.Value)
                        {
                            return(cp);
                        }
                        parameters.Add(cp.ReturnValue);
                    }
                    return(new Completion(func.Invoke(parameters.ToArray())));
                }
                catch (Exception e)
                {
                    return(Completion.Exception(e.Message, this));
                }
            }
            return(Completion.Void);
        }
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Expression == null)
            {
                return(new Completion(null, CompletionType.Return));
            }
            var c = Expression.Execute(enviroment);

            if (c.Type == CompletionType.Value)
            {
                return(new Completion(c.ReturnValue, CompletionType.Return));
            }
            return(c);
        }
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Expression == null)
            {
                return(Completion.Exception(Properties.Language.NullException, this));
            }
            var c = Expression.Execute(enviroment);

            if (c.Type != CompletionType.Value)
            {
                return(c);
            }
            if (!TypeConverters.IsNumber(c.ReturnValue))
            {
                return(Completion.Exception(Properties.Language.NotNumber, Expression));
            }
            Type t = c.ReturnValue.GetType();

            if (t.Equals(typeof(int)))
            {
                int v   = TypeConverters.GetValue <int>(c.ReturnValue);
                int old = v;
                if (Operator == UpdateOperator.Add)
                {
                    v += 1;
                }
                else
                {
                    v = v - 1;
                }
                if (Expression is Identifier)
                {
                    enviroment.SetValue(((Identifier)Expression).Variable, v);
                }
                return(new Completion(IsPrefix ? v : old));
            }
            else if (t.Equals(typeof(float)))
            {
                float v   = TypeConverters.GetValue <float>(c.ReturnValue);
                float old = v;
                if (Operator == UpdateOperator.Add)
                {
                    v += 1;
                }
                else
                {
                    v = v - 1;
                }
                if (Expression is Identifier)
                {
                    enviroment.SetValue(((Identifier)Expression).Variable, v);
                }
                return(new Completion(IsPrefix ? v : old));
            }
            else
            {
                double v   = TypeConverters.GetValue <double>(c.ReturnValue);
                double old = v;
                if (Operator == UpdateOperator.Add)
                {
                    v += 1;
                }
                else
                {
                    v = v - 1;
                }
                if (Expression is Identifier)
                {
                    enviroment.SetValue(((Identifier)Expression).Variable, v);
                }
                return(new Completion(IsPrefix ? v : old));
            }
        }
Example #8
0
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            /*
             * if (DelegateFunction != null)
             * {
             *  List<object> ps = new List<object>();
             *  for (int i = 0; i < Args.Count; i++)
             *  {
             *      Expression e = Args[i];
             *      string name = ArgTyps[i];
             *
             *      Completion cp = e.Execute(enviroment);
             *      if (cp.Type != CompletionType.Value)
             *          return cp;
             *      ps.Add(cp.ReturnValue);
             *  }
             *  DelegateFunction.Invoke(ps.ToArray());
             *  return Completion.Void;
             * }*/
            foreach (var f in enviroment.Module.Functions)
            {
                if (Function.Equals(f.Name))
                {
                    ExecutionEnvironment current = new ExecutionEnvironment(enviroment.GetInstanceEnvironment());
                    for (int i = 0; i < Args.Count; i++)
                    {
                        Expression e    = Args[i];
                        string     name = ArgTyps[i];

                        Completion cp = e.Execute(enviroment);
                        if (cp.Type != CompletionType.Value)
                        {
                            return(cp);
                        }
                        current.RegisterValue(f.Params[i].Name, cp.ReturnValue);
                    }
                    var c = f.Execute(current);
                    return(c);
                }
            }
            DelegateFunction func       = enviroment.GetFunction(Function);
            List <object>    parameters = new List <object>();

            if (func != null)
            {
                try
                {
                    for (int i = 0; i < Args.Count; i++)
                    {
                        Expression e    = Args[i];
                        string     name = ArgTyps[i];
                        if (e == null)
                        {
                            return(Completion.Exception(Properties.Language.ParameterNullException, this));
                        }
                        Completion cp = e.Execute(enviroment);
                        if (cp.Type != CompletionType.Value)
                        {
                            return(cp);
                        }
                        parameters.Add(cp.ReturnValue);
                    }
                    return(new Completion(func.Invoke(parameters.ToArray())));
                }
                catch (Exception e)
                {
                    return(Completion.Exception(e.Message, this));
                }
            }
            return(Completion.Void);
        }
Example #9
0
        public Completion Execute(Class m)
        {
            IsAborting  = false;
            IsCompleted = false;
            _current    = m;
            if (_current == null)
            {
                return(null);
            }
            Completion           c           = Completion.Void;
            ExecutionEnvironment baseEnv     = this.GetBaseEnvironment();
            ExecutionEnvironment classEnv    = new ExecutionEnvironment(baseEnv);
            ExecutionEnvironment instanceEnv = new ExecutionEnvironment(classEnv);

            foreach (var v in m.Variables)
            {
                instanceEnv.RegisterValue(v.Name, v.Value);
            }
            foreach (var func in _current.Functions)
            {
                if ("main".Equals(func.Name, StringComparison.OrdinalIgnoreCase))
                {
                    var parameter = func.Params;
                    ExecutionEnvironment functionEnv = new ExecutionEnvironment(instanceEnv);
                    ExecutionStarted?.Invoke(this, null);
                    foreach (var p in parameter)
                    {
                        functionEnv.RegisterValue(p.Name, null);
                    }
                    foreach (var block in m.BlockStatements)
                    {
                        foreach (var s in block.Body)
                        {
                            if (s is ExpressionStatement)
                            {
                                Expression exp = (s as ExpressionStatement).Expression;
                                if (exp is VariableDeclarationExpression)
                                {
                                    exp.Execute(instanceEnv);
                                }
                            }
                        }
                    }
                    try
                    {
                        IsCompleted = false;
                        c           = func.Execute(functionEnv);
                        break;
                    }catch (Exception e)
                    {
                        IsCompleted = true;
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.StackTrace);
                        ExecutionAborted?.Invoke(this, new Completion(e.Message, CompletionType.Exception));
                        return(Completion.Void);
                    }
                }
            }
            IsCompleted = true;
            if (c.Type == CompletionType.Value)
            {
                ExecutionCompleted?.Invoke(this, c);
            }
            else if (c.Type == CompletionType.Exception)
            {
                ExecutionAborted?.Invoke(this, c);
            }
            else
            {
                ExecutionAborted?.Invoke(this, Completion.Exception(Properties.Language.UnknowException, null));
            }
            return(c);
        }