Ejemplo n.º 1
0
        public override object Evaluate(Water.List expressions)
        {
            if (expressions.Count != 2)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }



            if (!(expressions[0] is Identifier))
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }
            string name = ((Identifier)expressions[0]).Value;

            object value = Water.Evaluator.Evaluate(expressions[1]);



            Water.Environment.DefineConstant(name, value);



            return(null);
        }
Ejemplo n.º 2
0
        public override object Evaluate(Water.List expressions)
        {
            if (expressions.Count == 4)
            {
                if (!(expressions[0] is Water.Identifier))
                {
                    throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
                }
                string name = ((Water.Identifier)expressions[0]).Value;


                string type_name     = ((Water.Identifier)expressions[1]).Value;
                string assembly_name = ((Water.Identifier)expressions[2]).Value;


                if (!(expressions[3] is Water.Identifier))
                {
                    throw new Water.Error("args[3] was not a valid method name.");
                }
                string method = ((Water.Identifier)expressions[3]).Value;


                LoadMethod(name, type_name, assembly_name, method);
                return(null);
            }
            else if (expressions.Count == 3)
            {
                if (!(expressions[0] is Water.Identifier))
                {
                    throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
                }
                string name = ((Water.Identifier)expressions[0]).Value;


                System.Type type = (System.Type)Water.Evaluator.Evaluate(expressions[1]);


                if (!(expressions[2] is Water.Identifier))
                {
                    throw new Water.Error("args[2] was not a valid method name.");
                }
                string method = ((Water.Identifier)expressions[2]).Value;


                LoadMethod(name, type, method);
                return(null);
            }
            else
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }
        }
Ejemplo n.º 3
0
        public override object Evaluate(Water.List expressions)
        {
            if (expressions.Count == 0)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }


            System.Type type = (System.Type)Water.Evaluator.Evaluate(expressions[0]);

            object[] parameters = new object[expressions.Count - 1];
            for (int i = 1; i < expressions.Count; i++)
            {
                parameters[i - 1] = Water.Evaluator.Evaluate(expressions[i]);
                if (parameters[i - 1] == null)
                {
                    //TODO DELETE throw new Water.Error("Expression was null: " + expressions[i].ToString());
                }
            }

            try
            {
                return(Activator.CreateInstance(type, parameters));
            }
            catch (System.MissingMethodException exception)
            {
                throw new System.Exception("Invalid arguments passed to constructor on '" + type.FullName + "'.", exception);
            }
        }
Ejemplo n.º 4
0
        public override object Evaluate(Water.List expressions)
        {
            if (expressions.Count == 3)
            {
                string name          = ((Water.Identifier)expressions[0]).Value;
                string type_name     = ((Water.Identifier)expressions[1]).Value;
                string assembly_name = ((Water.Identifier)expressions[2]).Value;

                this.LoadOperator(name, type_name, assembly_name);

                return(null);
            }
            else if (expressions.Count == 2)
            {
                string      name = ((Water.Identifier)expressions[0]).Value;
                System.Type type = (System.Type)Water.Evaluator.Evaluate(expressions[1]);

                this.LoadOperator(name, type);

                return(null);
            }
            else
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }
        }
Ejemplo n.º 5
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (expressions.Count != 3)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }

            string itemName = ((Water.Identifier)expressions[0]).Value;

            System.Collections.IEnumerable list = (System.Collections.IEnumerable)Water.Evaluator.Evaluate(expressions[2]);

            // Copy list to allow modification.
            System.Collections.ArrayList list2 = new System.Collections.ArrayList();
            foreach (object item in list)
            {
                list2.Add(item);
            }

            bool first = true;
            bool last  = false;
            int  i     = 1;

            foreach (object item in list2)
            {
                if (i == list2.Count)
                {
                    last = true;
                }

                //Push a stack frame
                Water.Environment.Push();

                Water.Environment.DefineVariable("first", first);
                Water.Environment.DefineVariable("last", last);
                Water.Environment.DefineVariable(itemName, item);

                if (first)
                {
                    first = false;
                }

                Water.Interpreter.Interpret(new StatementIterator(statements, true));

                //Pop a stack frame.
                Water.Environment.Pop();

                if (Water.Environment.Break)
                {
                    break;
                }

                i++;
            }

            if (Water.Environment.Break)
            {
                Water.Environment.Break = false;
            }
        }
Ejemplo n.º 6
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (!(expressions[0] is Water.Identifier))
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }
            string name = ((Water.Identifier)expressions[0]).Value;

            string namespace_ = (string)Water.Environment.Identify("_Namespace");

            if (namespace_ != null && namespace_.Length > 0)
            {
                name = namespace_ + "." + name;
            }

            Water.Functions.Function function = new Water.Functions.Function(name);

            for (int i = 1; i < expressions.Count; i++)
            {
                if (expressions[i] is Water.Identifier)
                {
                    Water.Identifier identifier = (Water.Identifier)expressions[i];
                    function.Parameters.Add(identifier);
                }
                else if ((expressions[i] is Water.Quote))
                {
                    Water.Quote      quote      = (Water.Quote)expressions[i];
                    Water.Identifier identifier = (Water.Identifier)quote.Expression;
                    function.Parameters.Add(new Water.Identifier("'" + identifier.Value));
                }
                else
                {
                    throw new Water.Error("'" + expressions[i] + "' is not a valid parameter name.");
                }
            }

            foreach (Water.Statement statement in statements)
            {
                function.Statements.Add(statement);
            }

            bool hasOptional = HasOptional(function.Parameters);
            bool hasRest     = HasRest(function.Parameters);

            if (hasOptional && hasRest)
            {
                throw new Water.Error("Cannot have both optional and rest parameters: " + name);
            }
            else if (hasRest)
            {
                CheckRestLegal(name, function.Parameters);
            }

            Water.Environment.DefineConstant(function.Name, function);
        }
Ejemplo n.º 7
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (!(expressions[0] is Water.Identifier))
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }

            string name = ((Water.Identifier)expressions[0]).Value;

            Water.Blocks.UserDefinedBlock userDefinedBlock = new Water.Blocks.UserDefinedBlock(name, expressions.NotFirst(), statements);

            Water.Environment.DefineConstant(name, userDefinedBlock);
        }
Ejemplo n.º 8
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (expressions.Count != 1)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }

            while (Water.Evaluator.EvaluateBoolean(expressions[0]) && !Water.Environment.Break)
            {
                Water.Interpreter.Interpret(new StatementIterator(statements, true));
            }

            if (Water.Environment.Break)
            {
                Water.Environment.Break = false;
            }
        }
Ejemplo n.º 9
0
        public override object Evaluate(Water.List expressions)
        {
            if (expressions.Count == 1)
            {
                string type_name = ((Water.Identifier)expressions[0]).Value;

                System.Type type = System.Type.GetType(type_name);
                if (type == null)
                {
                    throw new Water.Error("Cannot load type: " + type_name);
                }

                return(type);
            }
            else if (expressions.Count == 2)
            {
                string type_name     = ((Water.Identifier)expressions[0]).Value;
                string assembly_name = ((Water.Identifier)expressions[1]).Value;

                System.Reflection.Assembly assembly = Water.AssemblyCache.LoadAssembly(assembly_name);
                if (assembly == null)
                {
                    assembly = System.Reflection.Assembly.LoadWithPartialName(assembly_name);
                }
                if (assembly == null)
                {
                    throw new Water.Error("Cannot load assembly: " + assembly_name);
                }

                System.Type type = assembly.GetType(type_name);
                if (type == null)
                {
                    throw new Water.Error("Cannot load type: " + type_name);
                }

                return(type);
            }
            else
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }
        }
Ejemplo n.º 10
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (expressions.Count != 1)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }

            Water.Dictionary block = new Water.Dictionary();
            block["else_if"] = new Water.List();

            Water.List if_block = new Water.List();
            block["if"] = if_block;
            Water.List current_block = if_block;

            foreach (Water.Statement block_statement in statements)
            {
                Water.List statement = (Water.List)block_statement.Expression;
                if (statement.First() is Water.Identifier)
                {
                    string tag = ((Water.Identifier)statement.First()).Value;

                    if (tag.Equals("if"))
                    {
                        current_block.Add(block_statement);
                    }
                    else if (statement.Count == 2 && tag.Equals("else_if"))
                    {
                        Water.Dictionary else_if_block = new Water.Dictionary();
                        //TODO use generics
                        ((Water.List)block["else_if"]).Add(else_if_block);

                        else_if_block["Expressions"] = statement.NotFirst().First();

                        Water.List else_if_block_statements = new Water.List();
                        else_if_block["Statements"] = else_if_block_statements;
                        current_block = else_if_block_statements;
                    }
                    else if (statement.Count == 1 && tag.Equals("else"))                    // && depth == 0
                    {
                        Water.List else_block = new Water.List();
                        block["else"] = else_block;
                        current_block = else_block;
                    }
                    else if (statement.Count == 1 && tag.Equals("end_if"))
                    {
                        current_block.Add(block_statement);
                    }
                    else
                    {
                        current_block.Add(block_statement);
                    }
                }
                else
                {
                    current_block.Add(block_statement);
                }
            }

            if (Water.Evaluator.EvaluateBoolean(expressions[0]))
            {
                //TODO use generics
                Water.Interpreter.Interpret(new StatementIterator((Water.List)block["if"], false));
                return;
            }
            //TODO use generics
            foreach (Water.Dictionary elseIf in (Water.List)block["else_if"])
            {
                if (Water.Evaluator.EvaluateBoolean(elseIf["Expressions"]))
                {
                    //TODO use generics
                    Water.Interpreter.Interpret(new StatementIterator((Water.List)elseIf["Statements"], false));
                    return;
                }
            }
            if (block["else"] != null)
            {
                //TODO use generics
                Water.Interpreter.Interpret(new StatementIterator((Water.List)block["else"], false));
                return;
            }
        }
Ejemplo n.º 11
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (expressions.Count != 1)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }

            object switch_expression = Water.Evaluator.Evaluate(expressions[0]);

            Water.Dictionary block = new Water.Dictionary();
            block["case"] = new Water.List();

            Water.List current_block = null;

            int depth = 0;

            foreach (Water.Statement block_statement in statements)
            {
                //TODO use generics
                Water.List statement = (Water.List)block_statement.Expression;
                if (statement.Count == 2 && statement.First() is Water.Identifier && ((Water.Identifier)statement.First()).Value.Equals("case"))
                {
                    Water.Dictionary case_block = new Water.Dictionary();
                    //TODO use generics
                    ((Water.List)block["case"]).Add(case_block);

                    case_block["Expressions"] = statement.NotFirst().First();

                    Water.List case_block_statements = new Water.List();
                    case_block["Statements"] = case_block_statements;
                    current_block            = case_block_statements;
                }
                else if (statement.First() is Water.Identifier)
                {
                    string tag = ((Water.Identifier)statement.First()).Value;

                    if (tag.Equals("switch"))
                    {
                        current_block.Add(block_statement);
                        depth++;
                    }
                    //TODO No good.
                    else if (statement.Count == 1 && tag.Equals("end_switch"))
                    {
                        current_block.Add(block_statement);
                        depth--;
                    }
                    else if (statement.Count == 1 && tag.Equals("default") && depth == 0)
                    {
                        Water.List default_block = new Water.List();
                        block["default"] = default_block;
                        current_block    = default_block;
                    }
                    else
                    {
                        current_block.Add(block_statement);
                    }
                }
                else
                {
                    if (current_block == null)
                    {
                        //TODO file, line, column.
                        throw new Water.Error("Invalid statement in switch statement.");
                    }
                    current_block.Add(block_statement);
                }
            }

            //TODO use generics
            foreach (Water.Dictionary case_ in (Water.List)block["case"])
            {
                if (switch_expression.Equals(Water.Evaluator.Evaluate(case_["Expressions"])))
                {
                    //TODO use generics
                    Water.Interpreter.Interpret(new StatementIterator((Water.List)case_["Statements"], false));
                    return;
                }
            }
            if (block["default"] != null)
            {
                //TODO use generics
                Water.Interpreter.Interpret(new StatementIterator((Water.List)block["default"], false));
                return;
            }
        }
Ejemplo n.º 12
0
        public override object Evaluate(Water.List expressions)
        {
            if (expressions.Count != 2)
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }


            if (!(expressions[0] is Water.Identifier))
            {
                throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }
            string target = ((Water.Identifier)expressions[0]).Value;

            if (target.IndexOf(".") != -1)
            {
                object o             = Water.Evaluator.Evaluate(new Water.Identifier(target.Substring(0, target.LastIndexOf("."))));
                string property_name = target.Substring(target.LastIndexOf(".") + ".".Length);
                object value         = Water.Evaluator.Evaluate(expressions[1]);


                if (o is System.Collections.IDictionary)
                {
                    System.Collections.IDictionary dictionary = (System.Collections.IDictionary)o;
                    dictionary[property_name] = value;
                }
                else
                {
                    System.Reflection.FieldInfo fieldInfo = o.GetType().GetField(property_name);
                    if (fieldInfo != null)
                    {
                        fieldInfo.SetValue(o, value);
                        return(null);
                    }
                    else
                    {
                        foreach (System.ComponentModel.PropertyDescriptor propertyDescriptor in System.ComponentModel.TypeDescriptor.GetProperties(o))
                        {
                            if (propertyDescriptor.Name.ToLower().Equals(property_name.ToLower()))
                            {
                                propertyDescriptor.SetValue(o, value);
                                return(null);
                            }
                        }
                        foreach (System.Reflection.PropertyInfo propertyInfo in o.GetType().GetProperties())
                        {
                            if (propertyInfo.Name.ToLower().Equals(property_name.ToLower()))
                            {
                                propertyInfo.SetValue(o, value, new object[] { });
                                return(null);
                            }
                        }
                        throw new Water.Error("Property does not exist: " + property_name);
                    }
                }
            }
            else
            {
                if (expressions.Count != 2)
                {
                    throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
                }


                if (!(expressions[0] is Identifier))
                {
                    throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
                }
                string name = ((Identifier)expressions[0]).Value;

                Water.Environment.RedefineVariable(name, Water.Evaluator.Evaluate(expressions[1]));
            }


            return(null);
        }