Exemplo n.º 1
0
        private void Assign(Assignment assignment)
        {
            if (assignment.IsSimple)
            {
                if (assignment.IsGlobal)
                {
                    this.env.Modify(
                        assignment.Variable,
                        this.evaluator.Evaluate(assignment.Value, this.env)
                        );
                }
                else
                {
                    this.env.Declcare(
                        assignment.Variable,
                        this.evaluator.Evaluate(assignment.Value, this.env)
                        );
                }
            }
            else
            {
                List <string>   accessor   = assignment.AccesKey;
                ExpressionValue MainObject = this.env.Get(accessor[0]) as ExpressionValue;

                for (int i = 1; i < accessor.Count - 1; i++)
                {
                    MainObject = MainObject.GetProperty(accessor[i]);
                }

                MainObject.SetProperty(
                    accessor[accessor.Count - 1],
                    this.evaluator.Evaluate(assignment.Value, env)
                    );
            }
        }
Exemplo n.º 2
0
        public ExpressionValue ExecuteFunction(Expression _fun)
        {
            FunctionExpression fun = _fun as FunctionExpression;

            Function        f    = null;
            ExpressionValue obj  = null;
            ExpressionValue last = null;

            if (fun.AccessObj == null)
            {
                f = this.env.Get(fun.name) as Function;
            }
            else
            {
                List <string> accessor = fun.AccessObj;

                obj = last = this.env.Get(accessor[0]) as ExpressionValue;

                for (int i = 1; i < accessor.Count; i++)
                {
                    last = obj;
                    obj  = obj.GetProperty(accessor[i]);
                }

                f = obj.Function;
            }


            if (f == null)
            {
                if (this.IsSystemFunction(fun.name))
                {
                    return(this.ExecuteSystemFunction(fun));
                }
                else
                {
                    return(null);
                }
            }

            this.env.PushEnvironment();
            for (int i = 0; i < fun.parameters.Count; i++)
            {
                this.env.Declcare(
                    f.ParametersNames[i],
                    this.evaluator.Evaluate(fun.parameters[i], this.env)
                    );
            }


            if (obj != null)
            {
                this.env.Declcare("this", last);
            }

            ExpressionValue ev = this.ExecuteStatements(f.InnerStatements);

            this.env.PopEnvironment();
            return(ev);
        }
Exemplo n.º 3
0
        public ExpressionValue Evaluate(Expression exp, Environment env)
        {
            if (exp == null)
            {
                return(new ExpressionValue(ExpressionValueType.BOOLEAN, false));
            }

            switch (exp.kind)
            {
            case (ExpressionType.FUNCTION):
            {
                FunctionExpression ex = exp as FunctionExpression;
                return(this.vm.ExecuteFunction(ex));
            }

            case (ExpressionType.FUNCT_DECL):
            {
                FunctionExpression decl = exp as FunctionExpression;
                return(new ExpressionValue(ExpressionValueType.FUNCTION, decl.function));
            }

            case (ExpressionType.OBJECT):
            {
                return(new ExpressionValue(ExpressionValueType.OBJECT));
            }

            case (ExpressionType.GET_OBJ):
            {
                AccessKeyExpression get      = exp as AccessKeyExpression;
                List <string>       accessor = get.AccessObj;
                ExpressionValue     v        = env.Get(accessor [0]) as ExpressionValue;

                for (int i = 1; i < accessor.Count; i++)
                {
                    v = v.GetProperty(accessor [i]);
                }

                return(v);
            }

            case (ExpressionType.IDENTIFIER):
            {
                TokenExpression tok = exp as TokenExpression;
                string          id  = tok.token.Value;
                return(env.Get(id) as ExpressionValue);
            }

            case (ExpressionType.BOOL):
            {
                return(exp.value);
            }

            case (ExpressionType.STRING):
            {
                return(exp.value);
            }

            case (ExpressionType.INTEGER):
            {
                return(exp.value);
            }

            case (ExpressionType.ADD):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                if (v1.IsString)
                {
                    return(new ExpressionValue(ExpressionValueType.STRING, v2.String + v1.String));
                }
                else
                {
                    return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number + v2.Number));
                }
            }

            case (ExpressionType.SUBS):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number - v2.Number));
            }

            case (ExpressionType.MUL):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number * v2.Number));
            }

            case (ExpressionType.DIV):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number / v2.Number));
            }

            case (ExpressionType.AND):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.BOOLEAN, v1.Bool && v2.Bool));
            }

            case (ExpressionType.OR):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.BOOLEAN, v1.Bool || v2.Bool));
            }

            case (ExpressionType.EQUAL):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.BOOLEAN,
                                           (v1.Bool == v2.Bool) && (v1.Number == v2.Number) && (v1.String == v2.String)));
            }

            case (ExpressionType.DISEQUAL):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.BOOLEAN,
                                           (v1.Bool != v2.Bool) && (v1.Number != v2.Number) && (v1.String != v2.String)));
            }

            case (ExpressionType.LESS):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);


                return(new ExpressionValue(ExpressionValueType.BOOLEAN, v1.Number < v2.Number));
            }

            case (ExpressionType.GREATER):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number > v2.Number));
            }

            case (ExpressionType.LESS_OR_EQUAL):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);


                return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number <= v2.Number));
            }

            case (ExpressionType.GREATER_OR_EQUAL):
            {
                OperationExpression op = exp as OperationExpression;
                ExpressionValue     v1 = this.Evaluate(op.lhs, env);
                ExpressionValue     v2 = this.Evaluate(op.rhs, env);

                return(new ExpressionValue(ExpressionValueType.NUMBER, v1.Number >= v2.Number));
            }

            default:
                return(null);
            }
        }