コード例 #1
0
ファイル: Statement.cs プロジェクト: mstanford/water
 public Statement(string file, int line, int column, Water.List expression)
 {
     this.File       = file;
     this.Line       = line;
     this.Column     = column;
     this.Expression = expression;
 }
コード例 #2
0
ファイル: Printer.cs プロジェクト: mstanford/water
        private static void PrintStatementStack(Water.List statements, System.IO.TextWriter output)
        {
            output.WriteLine();
            output.WriteLine("[STACK_TRACE]");

            if (statements.Count > 0)
            {
                for (int j = 0; j < statements.Count; j++)
                {
                    output.WriteLine();

                    Water.Statement statement = (Water.Statement)statements[j];

                    if (statement != null && statement.File != null)
                    {
                        output.Write("[" + new System.IO.FileInfo((string)statement.File).Name + " (" + statement.Line + "," + statement.Column + ")]");
                        output.Write("   ");
                    }

                    if (Water.StatementParser.GetBlock(statement.Expression) != null)
                    {
                        output.Write(Water.Generator.Generate(statement.Expression.NotLast()));
                    }
                    else
                    {
                        output.Write(Water.Generator.Generate(statement.Expression));
                    }

                    output.WriteLine();
                }
            }

            output.WriteLine();
        }
コード例 #3
0
ファイル: ConstantOperator.cs プロジェクト: mstanford/water
        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);
        }
コード例 #4
0
ファイル: NewOperator.cs プロジェクト: mstanford/water
        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);
            }
        }
コード例 #5
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));
            }
        }
コード例 #6
0
        public static Water.Blocks.Block GetBlock(Water.List list)
        {
            if (list == null || list.Count == 0)
            {
                return(null);
            }

            if (!(list[0] is Water.Identifier))
            {
                return(null);
            }

            Water.Identifier identifier = (Water.Identifier)list[0];

            object block = Water.Environment.Identify(identifier.Value);

            if (block == null)
            {
                return(null);
            }

            if (!(block is Water.Blocks.Block))
            {
                return(null);
            }

            return((Water.Blocks.Block)block);
        }
コード例 #7
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());
            }
        }
コード例 #8
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);
     }
 }
コード例 #9
0
ファイル: Parser.cs プロジェクト: mstanford/water
        //
        // Lists
        //

        public static Water.List ParseList(Water.TextReader reader)
        {
            #region ParseList

            Water.List list = new Water.List();

            // Read the '('
            reader.Read();

            TrimWhitespace(reader);

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Peek();

                if (ch == ')')
                {
                    reader.Read();
                    return(list);
                }
                else
                {
                    list.Add(Water.Parser.Parse(reader));
                }

                TrimWhitespace(reader);
            }

            throw new Water.Error("Parser error: Invalid list.");

            #endregion
        }
コード例 #10
0
        public Water.Statement Parse()
        {
            Water.Parser.TrimWhitespace(this._reader);

            if (this._reader.Peek() == -1)
            {
                return(null);
            }

            char ch = (char)this._reader.Peek();

            if (ch == ';')
            {
                Water.Parser.ParseComment(this._reader);

                // Try it again.
                return(Parse());
            }
            else if (ch == '(')
            {
                string     file        = this._reader.Filename;
                int        line        = this._reader.LineNumber;
                int        column      = this._reader.ColumnNumber;
                Water.List expressions = Water.Parser.ParseList(this._reader);

                return(new Water.Statement(file, line, column, expressions));
            }
            else
            {
                return(ParseStatement(this._reader));
            }
        }
コード例 #11
0
ファイル: ForeachBlock.cs プロジェクト: mstanford/water
        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;
            }
        }
コード例 #12
0
 public static object Apply(string command, params string[] args)
 {
     Water.List cmd = new Water.List();
     cmd.Add(new Water.Identifier(command));
     foreach (string arg in args)
     {
         cmd.Add(arg);
     }
     return(Water.Evaluator.Evaluate(cmd));
 }
コード例 #13
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);
        }
コード例 #14
0
 private bool HasRest(Water.List parameters)
 {
     foreach (Water.Identifier parameter in parameters)
     {
         if (Function.IsRest(parameter))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #15
0
 public static void Reset()
 {
     _output      = System.Console.Out;
     _break       = false;
     _return      = false;
     _returnValue = null;
     _constants   = new Water.Dictionary();
     _variables   = new Water.List();
     _stackDepth  = 0;
     _includes    = new Water.Dictionary();
 }
コード例 #16
0
        private object[] GetValues(Water.List expressions)
        {
            object[] values = new object[expressions.Count];
            int      i      = 0;

            foreach (object expression in expressions)
            {
                values[i] = Water.Evaluator.Evaluate(expression);
                i++;
            }
            return(values);
        }
コード例 #17
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());
            }
        }
コード例 #18
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);
        }
コード例 #19
0
        public Water.List NotLast()
        {
            if (this.Count == 0)
            {
                throw new Water.Error("List is empty.");
            }

            Water.List child = new Water.List();
            for (int i = 0; i < (this.Count - 1); i++)
            {
                child.Add(this[i]);
            }
            return(child);
        }
コード例 #20
0
ファイル: Printer.cs プロジェクト: mstanford/water
        public static void PrintError(System.Exception exception, System.IO.TextWriter output)
        {
            Water.List statements = new Water.List();
            for (int i = (Water.Environment.Variables.Count - 1); i >= 0; i--)
            {
                Water.Dictionary frame = (Water.Dictionary)Water.Environment.Variables[i];
                if (frame.IsDefined("_Statement"))
                {
                    statements.Add(frame["_Statement"]);
                }
            }

            PrintError(exception, statements, output);
        }
コード例 #21
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;
            }
        }
コード例 #22
0
ファイル: TypeofOperator.cs プロジェクト: mstanford/water
        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());
            }
        }
コード例 #23
0
ファイル: Quote.cs プロジェクト: mstanford/water
        private object CheckQuote(object o)
        {
            if (o is Water.List)
            {
                Water.List list = (Water.List)o;

                Water.List results = new Water.List();
                foreach (object item in list)
                {
                    results.Add(CheckQuote(item));
                }
                return(results);
            }
            else if (o is Water.Dictionary)
            {
                Water.Dictionary dictionary = (Water.Dictionary)o;

                Water.Dictionary results = new Water.Dictionary();
                foreach (string key in dictionary)
                {
                    results.Add(key, CheckQuote(dictionary[key]));
                }
                return(results);
            }
            else if (o is Water.Comma)
            {
                Water.Comma comma = (Water.Comma)o;

                return(comma.Evaluate(new Water.List()));
            }
            else if (o is System.String)
            {
                System.String s = (System.String)o;

                return(Water.Evaluator.EvaluateString(s));
            }
            else
            {
                return(o);
            }
        }
コード例 #24
0
        private void LoadSettings()
        {
            Water.Dictionary Settings = (Water.Dictionary)Water.Environment.Identify("Settings");

            if (Settings.IsDefined("WindowState"))
            {
                this.WindowState = (System.Windows.Forms.FormWindowState)System.Enum.Parse(typeof(System.Windows.Forms.FormWindowState), (string)Settings["WindowState"]);
            }
            if (this.WindowState == System.Windows.Forms.FormWindowState.Normal)
            {
                if (Settings.IsDefined("Left"))
                {
                    this.Left = (int)Settings["Left"];
                }
                if (Settings.IsDefined("Top"))
                {
                    this.Top = (int)Settings["Top"];
                }
                if (Settings.IsDefined("Width"))
                {
                    this.Width = (int)Settings["Width"];
                }
                if (Settings.IsDefined("Height"))
                {
                    this.Height = (int)Settings["Height"];
                }
            }


            if (Settings.IsDefined("Commands"))
            {
                Water.List commands = (Water.List)Settings["Commands"];
                foreach (Water.List command in commands)
                {
                    Water.Evaluator.Evaluate(command);
                }
            }
        }
コード例 #25
0
        private Water.List ParseBlockStatements(Water.TextReader reader, string name, string end, bool allowRecursion, Water.Statement start_statement)
        {
            Water.List block_statements = new Water.List();

            while (reader.Peek() != -1)
            {
                Water.Parser.TrimWhitespace(reader);

                Water.Statement statement = ParseStatement(reader);
                if (statement != null)
                {
                    if (statement.Expression.Count == 1 && statement.Expression[0].ToString().Equals(end))
                    {
                        return(block_statements);
                    }

                    block_statements.Add(statement);
                }
            }

//TODO DELETE			Water.Environment.Set("_Statement", start_statement);
            throw new Water.Error("\"" + end + "\" expected.");
        }
コード例 #26
0
        private void CheckRestLegal(string name, Water.List parameters)
        {
            int count = 0;

            foreach (Water.Identifier parameter in parameters)
            {
                if (Function.IsRest(parameter))
                {
                    if (count > 0)
                    {
                        throw new Water.Error("Only one rest parameter allowed in function " + name);
                    }
                    count++;
                }
                else
                {
                    if (count > 0)
                    {
                        throw new Water.Error("The rest parameter must appear after required parameters in function " + name);
                    }
                }
            }
        }
コード例 #27
0
ファイル: Printer.cs プロジェクト: mstanford/water
        private static void PrintError(System.Exception exception, Water.List statements, System.IO.TextWriter output)
        {
            if (exception is Water.Error)
            {
                PrintMessage(exception, output);
                PrintStatementStack(statements, output);
            }
            else if (exception is System.Reflection.TargetInvocationException)
            {
                PrintError(exception.InnerException, statements, output);
            }
            else
            {
                PrintMessage(exception, output);
                PrintStatementStack(statements, output);
                PrintStackTrace(exception, output);

                if (exception.InnerException != null)
                {
                    PrintMessage(exception.InnerException, output);
                    PrintStackTrace(exception.InnerException, output);
                }
            }
        }
コード例 #28
0
        private void SaveSettings()
        {
            if (Water.Environment.IsConstant("Window.CloseAllDocuments"))
            {
                Water.Evaluator.Apply("Window.CloseAllDocuments");
            }



            Water.Dictionary Settings = (Water.Dictionary)Water.Environment.Identify("Settings");

            Settings["WindowState"] = this.WindowState.ToString();
            if (this.WindowState == System.Windows.Forms.FormWindowState.Normal)
            {
                Settings["Left"]   = this.Left;
                Settings["Top"]    = this.Top;
                Settings["Width"]  = this.Width;
                Settings["Height"] = this.Height;
            }



            Water.List commands = new Water.List();
            Settings["Commands"] = commands;

            bool showStartPage = true;

            foreach (TD.SandDock.DockControl dockControl in this.documentContainer.Controls)
            {
                if (dockControl.Controls[0] is bamboo.Controls.Editor.EditorControl)
                {
                    bamboo.Controls.Editor.EditorControl editorControl = (bamboo.Controls.Editor.EditorControl)dockControl.Controls[0];

                    Water.List command = new Water.List();
                    command.Add(new Water.Identifier("File.Open"));
                    command.Add(editorControl.Filename);
                    commands.Add(command);
                    showStartPage = false;
                }
                else if (dockControl.Controls[0] is bamboo.Controls.AssemblyExplorer.AssemblyExplorerControl)
                {
                    bamboo.Controls.AssemblyExplorer.AssemblyExplorerControl assemblyExplorerControl = (bamboo.Controls.AssemblyExplorer.AssemblyExplorerControl)dockControl.Controls[0];

                    Water.List command = new Water.List();
                    command.Add(new Water.Identifier("File.Open"));
                    command.Add(assemblyExplorerControl.Filename);
                    commands.Add(command);
                    showStartPage = false;
                }
                else
                {
                    string tag = (string)dockControl.Controls[0].Tag;
                    if (tag != null)
                    {
                        Water.List command = new Water.List();
                        command.Add(new Water.Identifier(tag));
                        command.Add("Document");
                        commands.Add(command);
                        showStartPage = false;
                    }
                }
            }
            if (showStartPage)
            {
                Water.List command = new Water.List();
                command.Add(new Water.Identifier("View.StartPage"));
                commands.Add(command);
            }

            if (this.leftDockContainer.LayoutSystem.LayoutSystems.Count != 0)
            {
                TD.SandDock.ControlLayoutSystem controlLayoutSystem = (TD.SandDock.ControlLayoutSystem) this.leftDockContainer.LayoutSystem.LayoutSystems[0];
                foreach (TD.SandDock.DockControl dockControl in controlLayoutSystem.Controls)
                {
                    string tag = (string)dockControl.Controls[0].Tag;
                    if (tag != null)
                    {
                        Water.List command = new Water.List();
                        command.Add(new Water.Identifier(tag));
                        command.Add("Left");
                        commands.Add(command);
                    }
                }
            }

            if (this.bottomDockContainer.LayoutSystem.LayoutSystems.Count != 0)
            {
                TD.SandDock.ControlLayoutSystem controlLayoutSystem = (TD.SandDock.ControlLayoutSystem) this.bottomDockContainer.LayoutSystem.LayoutSystems[0];
                foreach (TD.SandDock.DockControl dockControl in controlLayoutSystem.Controls)
                {
                    string tag = (string)dockControl.Controls[0].Tag;
                    if (tag != null)
                    {
                        Water.List command = new Water.List();
                        command.Add(new Water.Identifier(tag));
                        command.Add("Bottom");
                        commands.Add(command);
                    }
                }
            }

            if (this.rightDockContainer.LayoutSystem.LayoutSystems.Count != 0)
            {
                TD.SandDock.ControlLayoutSystem controlLayoutSystem = (TD.SandDock.ControlLayoutSystem) this.rightDockContainer.LayoutSystem.LayoutSystems[0];
                foreach (TD.SandDock.DockControl dockControl in controlLayoutSystem.Controls)
                {
                    string tag = (string)dockControl.Controls[0].Tag;
                    if (tag != null)
                    {
                        Water.List command = new Water.List();
                        command.Add(new Water.Identifier(tag));
                        command.Add("Right");
                        commands.Add(command);
                    }
                }
            }
        }
コード例 #29
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);
        }
コード例 #30
0
 public Function(string name, Water.List parameters, Water.List statements)
 {
     this._name       = name;
     this._parameters = parameters;
     this._statements = statements;
 }