예제 #1
0
        public static object Apply(object command, Water.List args)
        {
            if (command is Water.List)
            {
                Water.List list     = (Water.List)command;
                object     command2 = Apply(list.First(), list.NotFirst());

                //TODO I don't like this.
                if (command2 == null && args.Count == 0)
                {
                    return(null);
                }

                return(Apply(command2, args));
            }
            else if (command is Water.Identifier)
            {
                Water.Identifier identifier = (Water.Identifier)command;
                object           command2   = Evaluate(identifier);
                if (command2 == null)
                {
                    throw new Water.Error(identifier.Value + " is not defined.");
                }
                return(Apply(command2, args));
            }
            else
            {
                Water.Operator op = (Water.Operator)command;
                return(op.Evaluate(args));
            }
        }
예제 #2
0
 /// <summary>
 /// Evaluates any type of object.
 /// </summary>
 /// <param name="o"></param>
 /// <returns></returns>
 public static object Evaluate(object o)
 {
     if (o is Water.List)
     {
         Water.List list = (Water.List)o;
         return(Apply(list.First(), list.NotFirst()));
     }
     else if (o is Water.Identifier)
     {
         return(EvaluateIdentifier((Water.Identifier)o));
     }
     else if (o is string)
     {
         return(EvaluateString((string)o));
     }
     else if (o is Water.Quote)
     {
         Water.Quote quote = (Water.Quote)o;
         return(quote.Evaluate(new Water.List()));
     }
     else
     {
         return(o);
     }
 }
예제 #3
0
        public override object Evaluate(Water.List expressions)
        {
            //Push a stack frame
            Water.Environment.Push();


            // Evaluate local variables.
            Water.List expressions_to_eval = expressions.Copy();
            Water.List values  = new Water.List();
            bool       hasRest = false;

            foreach (Water.Identifier parameter in this.Parameters)
            {
                if (hasRest)
                {
                    throw new Water.Error("Cannot have parameters after a rest parameter in function " + this._name);
                }

                if (IsRest(parameter))
                {
                    //Add the rest.
                    Water.List rest = new Water.List();
                    foreach (object expression in expressions_to_eval)
                    {
                        rest.Add(Water.Evaluator.Evaluate(expression));
                    }
                    expressions_to_eval.Clear();
                    values.Add(rest);
                    hasRest = true;
                }
                else if (IsOptional(parameter))
                {
                    //If it doesn't exist add null.
                    if (expressions_to_eval.Count == 0)
                    {
                        values.Add(null);
                    }
                    else
                    {
                        object value = Water.Evaluator.Evaluate(expressions_to_eval.First());
                        values.Add(value);

                        expressions_to_eval = expressions_to_eval.NotFirst();
                    }
                }
                else if (IsUnevaluated(parameter))
                {
                    values.Add(expressions_to_eval.First());

                    expressions_to_eval = expressions_to_eval.NotFirst();
                }
                else
                {
                    //Throw exception if null.
                    if (expressions_to_eval.Count == 0)
                    {
                        throw new Water.Error("Parameter is required: " + parameter.Value);
                    }
                    else
                    {
                        object value = Water.Evaluator.Evaluate(expressions_to_eval.First());
                        if (value == null)
                        {
                            throw new Water.Error("Parameter is required: " + parameter.Value);
                        }
                        values.Add(value);

                        expressions_to_eval = expressions_to_eval.NotFirst();
                    }
                }
            }
            if (expressions_to_eval.Count > 0)
            {
                throw new Water.Error("Too many expressions passed to function: " + this._name);
            }


            // Push local variables.
            int i = 0;

            foreach (Water.Identifier parameter in this.Parameters)
            {
                if (Water.Environment.IsConstant(parameter.Value))
                {
                    throw new Water.Error("Constant \"" + parameter.Value + "\" is already defined.");
                }
                if (IsOptional(parameter))
                {
                    string name = GetOptional(parameter);
                    Water.Environment.DefineVariable(name, values[i]);
                }
                else if (IsRest(parameter))
                {
                    string name = GetRest(parameter);
                    Water.Environment.DefineVariable(name, values[i]);
                }
                else if (IsUnevaluated(parameter))
                {
                    string name = GetUnevaluated(parameter);
                    Water.Environment.DefineVariable(name, values[i]);
                }
                else
                {
                    Water.Environment.DefineVariable(parameter.Value, values[i]);
                }
                i++;
            }


            //Interpret the function.
            Water.Interpreter.Interpret(new StatementIterator(this.Statements, false));


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


            object returnValue = Water.Environment.ReturnValue;

            Water.Environment.Return      = false;
            Water.Environment.ReturnValue = null;
            return(returnValue);
        }
예제 #4
0
        public override void Evaluate(Water.List expressions, Water.List statements)
        {
            if (expressions.Count != 0)
            {
//TODO				throw new Water.Error("Invalid arguments.\n\t" + expressions.ToString());
            }

            string exception_name = null;

            Water.Dictionary block = new Water.Dictionary();             // TODO block = GroupBlock(tags);

            Water.List try_block = new Water.List();
            block["try"] = try_block;
            Water.List current_block = try_block;

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

                    if (statement.Count == 2 && tag.Equals("catch"))                    // && depth == 0
                    {
                        //TODO check the number of expressions.

                        Water.List catch_block = new Water.List();
                        block["catch"] = catch_block;
                        current_block  = catch_block;

                        exception_name = ((Water.Identifier)statement[1]).Value;
                    }
                    else
                    {
                        current_block.Add(block_statement);
                    }
                }
                else
                {
                    current_block.Add(block_statement);
                }
            }

            if (exception_name == null)
            {
                //TODO throw exception.
            }

            try
            {
                //TODO use generics
                Water.Interpreter.Interpret(new StatementIterator((Water.List)block["try"], false));
            }
            catch (System.Reflection.TargetInvocationException exception)
            {
                Water.Environment.Push();
                Water.Environment.DefineVariable(exception_name, exception.InnerException);
                //TODO use generics
                Water.Interpreter.Interpret(new StatementIterator((Water.List)block["catch"], false));
                Water.Environment.Pop();
            }
            catch (System.Exception exception)
            {
                Water.Environment.Push();
                Water.Environment.DefineVariable(exception_name, exception);
                //TODO use generics
                Water.Interpreter.Interpret(new StatementIterator((Water.List)block["catch"], false));
                Water.Environment.Pop();
            }
        }
예제 #5
0
파일: IfBlock.cs 프로젝트: mstanford/water
        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;
            }
        }
예제 #6
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;
            }
        }