Пример #1
0
 public void SetProperty(string str, ExpressionValue value)
 {
     this.members.Remove(str);
     this.members.Add(str, value);
 }
Пример #2
0
 public void Substitute(ExpressionValue value)
 {
     this.bValue = value.Bool;
     this.nValue = value.Number;
     this.sValue = value.String;
 }
Пример #3
0
        private ExpressionValue ExecuteStatement(Statement statement, Environment env)
        {
            switch (statement.kind)
            {
            case StatementType.ASSIGN:
            {
                AssignmentStatement asg = statement as AssignmentStatement;
                this.Assign(asg.assignment);
                return(null);
            }

            case StatementType.IF:
            {
                IfStatement     st    = statement as IfStatement;
                ExpressionValue value = this.evaluator.Evaluate(st.condition.Expression, this.env);
                if (value.Bool == true)
                {
                    return(this.ExecuteStatements(st.body));
                }
                return(null);
            }

            case StatementType.IF_ELSE:
            {
                IfElseStatement st    = statement as IfElseStatement;
                ExpressionValue value = this.evaluator.Evaluate(st.condition.Expression, this.env);
                if (value.Bool == true)
                {
                    return(this.ExecuteStatements(st.ifBody));
                }
                else
                {
                    return(this.ExecuteStatements(st.elseBody));
                }
            }

            case StatementType.WHILE:
            {
                WhileStatement  st    = statement as WhileStatement;
                ExpressionValue value = this.evaluator.Evaluate(st.condition.Expression, this.env);
                if (value.Bool == true)
                {
                    Statements ss = new Statements();
                    ss.AddStatement(st.body);
                    ss.AddStatement(st);
                    return(this.ExecuteStatements(ss));
                }

                return(null);
            }

            case StatementType.FUNC_DECL:
            {
                FunctionStatement st = statement as FunctionStatement;
                this.DeclareFunction(st.function);
                return(null);
            }

            case StatementType.FUNCTION:
            {
                FunctionStatement st = statement as FunctionStatement;
                return(this.ExecuteFunction(st.val));
            }

            case StatementType.RETURN:
            {
                FunctionStatement st = statement as FunctionStatement;
                return(this.evaluator.Evaluate(st.val, this.Environment));
            }
            }

            return(null);
        }
Пример #4
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);
            }
        }
Пример #5
0
        private ExpressionValue ExecuteSystemFunction(Expression _fun)
        {
            FunctionExpression fun = _fun as FunctionExpression;

            if (fun.name == "print")
            {
                string          str = "";
                ExpressionValue val = this.evaluator.Evaluate(fun.parameters[0], this.env);
                if (val.IsNumber)
                {
                    if (float.IsInfinity(val.Number))
                    {
                        str = "Infinity";
                    }
                    else if (float.IsNaN(val.Number))
                    {
                        str = "NaN";
                    }
                    else
                    {
                        str = val.Number.ToString();
                    }
                }
                else if (val.IsBool)
                {
                    str = val.Bool.ToString();
                }
                else if (val.IsString)
                {
                    str = val.String;
                }
                else if (val.IsFunction)
                {
                    str = ("Function " + val.Function.Identifier);
                }
                else if (val.IsObject)
                {
                    str = "Object";
                }
                else
                {
                    str = "Undefined";
                }



                if (fun.parameters.Count < 5 || fun.parameters.Count > 1)
                {
                    List <string> vals = new List <string>();
                    for (int i = 1; i < fun.parameters.Count; i++)
                    {
                        ExpressionValue exp = this.evaluator.Evaluate(fun.parameters[i], this.env);

                        switch (i)
                        {
                        case 1:     // font size
                            if (exp.IsNumber)
                            {
                                vals.Add(exp.Number.ToString());
                            }
                            break;

                        case 2:     // color
                            if (exp.IsString)
                            {
                                vals.Add(exp.String);
                            }
                            break;

                        case 3:     // top
                            if (exp.IsNumber)
                            {
                                vals.Add(exp.Number.ToString());
                            }
                            break;

                        case 4:     // left
                            if (exp.IsNumber)
                            {
                                vals.Add(exp.Number.ToString());
                            }
                            break;

                        case 5:     // font
                            if (exp.IsString)
                            {
                                vals.Add(exp.String);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    OutputFormatter op = new OutputFormatter(vals.ToArray(), str);
                    Console.WriteLine(op.ToString());
                }
                else
                {
                    Console.WriteLine(OutputFormatter.GetOnlyTag(str));
                }

                return(null);
            }
            else if (fun.name == "import")
            {
                if (fun.parameters.Count != 1)
                {
                    return(null);
                }

                ExpressionValue fileName = this.evaluator.Evaluate(fun.parameters[0], this.env);

                if (!fileName.IsString)
                {
                    return(null);
                }

                Interpreter i = Interpreter.FromFile(fileName.String);
                i.Init();
                ExpressionValue v = i.RunAsModule();
                return(v);
            }
            else
            {
                return(null);
            }
        }