Ejemplo n.º 1
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));
 }
Ejemplo n.º 2
0
        //
        // 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
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Push a stack frame.
 /// </summary>
 public static void Push()
 {
     Water.Dictionary frame;
     if (_variables.Count == _stackDepth)
     {
         frame = new Water.Dictionary();
         _variables.Add(frame);
     }
     else
     {
         frame = (Water.Dictionary)_variables[_stackDepth];
     }
     _stackDepth++;
 }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
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.");
        }
Ejemplo n.º 8
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);
                    }
                }
            }
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        private Water.Statement ParseStatement(Water.TextReader reader)
        {
            Water.Parser.TrimWhitespace(reader);

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

            char ch = (char)reader.Peek();

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

                // Try it again.
                return(ParseStatement(reader));
            }

            string file   = reader.Filename;
            int    line   = reader.LineNumber;
            int    column = reader.ColumnNumber;

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

            while (reader.Peek() != -1)
            {
                if (Water.Parser.IsNewline(reader))
                {
//TODO DELETE					Water.Parser.TrimNewline(reader);
                    break;
                }

                if (Water.Parser.IsComment(reader))
                {
                    Water.Parser.ParseComment(reader);
                    break;
                }

                expressions.Add(Water.Parser.ParseExpression(reader));

                Water.Parser.TrimSpace(reader);
            }

            if (expressions.Count == 0)
            {
                // Try it again.
                return(ParseStatement(reader));
            }

            Water.Blocks.Block block = GetBlock(expressions);
            if (block != null)
            {
                Water.Statement statement = new Water.Statement(file, line, column, expressions);

                expressions.Add(
                    ParseBlockStatements(
                        reader,
                        block.Name,
                        "end_" + block.Name,
                        block.IsRecursive,
                        statement));

                return(statement);
            }

            return(new Water.Statement(file, line, column, expressions));
        }
Ejemplo n.º 11
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();
            }
        }
Ejemplo n.º 12
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.º 13
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;
            }
        }