Exemplo n.º 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));
            }
        }
Exemplo n.º 2
0
        private void LoadOperator(string name, System.Type type)
        {
            object instance = Activator.CreateInstance(type);

            if (!(instance is Water.Operator))
            {
                throw new Water.Error(type.FullName + " is not an ICommand.");
            }
            Water.Operator command = (Water.Operator)instance;

            Water.Environment.DefineConstant(name, command);
        }
Exemplo n.º 3
0
        private object Apply(string identifier, System.Collections.ArrayList expressions)
        {
            #region Apply

            switch (identifier)
            {
            case "variables":
            {
                Assert(expressions.Count == 0, "Invalid arguments.");
                return(this._variables.Domain());
            }

            case "definitions":
            {
                Assert(expressions.Count == 0, "Invalid arguments.");
                return(this._definitions);
            }

            case "dependencies":
            {
                Assert(expressions.Count == 0, "Invalid arguments.");
                return(this._dependencies);
            }

            case "equals":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                object a = expressions[0];
                object b = expressions[1];
                return(a.Equals(b));
            }

            case "union":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                if (expressions[0] == null || expressions[1] == null)
                {
                    return(null);
                }
                else if (expressions[0] is Surf.Set && expressions[1] is Surf.Set)
                {
                    Set a = (Set)expressions[0];
                    Set b = (Set)expressions[1];
                    return(a.Union(b));
                }
                else if (expressions[0] is Surf.Bitmap && expressions[1] is Surf.Bitmap)
                {
                    Surf.Bitmap a = (Surf.Bitmap)expressions[0];
                    Surf.Bitmap b = (Surf.Bitmap)expressions[1];
                    return(a.Or(b));
                }
                else
                {
                    throw new System.Exception("Invalid expression.");
                }
            }

            case "intersection":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                if (expressions[0] == null || expressions[1] == null)
                {
                    return(null);
                }
                else if (expressions[0] is Surf.Set && expressions[1] is Surf.Set)
                {
                    Set a = (Set)expressions[0];
                    Set b = (Set)expressions[1];
                    return(a.Intersection(b));
                }
                else if (expressions[0] is Surf.Bitmap && expressions[1] is Surf.Bitmap)
                {
                    Surf.Bitmap a = (Surf.Bitmap)expressions[0];
                    Surf.Bitmap b = (Surf.Bitmap)expressions[1];
                    return(a.And(b));
                }
                else
                {
                    throw new System.Exception("Invalid expression.");
                }
            }

            case "difference":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                if (expressions[0] == null || expressions[1] == null)
                {
                    return(null);
                }
                else if (expressions[0] is Surf.Set && expressions[1] is Surf.Set)
                {
                    Set a = (Set)expressions[0];
                    Set b = (Set)expressions[1];
                    return(a.Difference(b));
                }
                else if (expressions[0] is Surf.Bitmap && expressions[1] is Surf.Bitmap)
                {
                    Surf.Bitmap a = (Surf.Bitmap)expressions[0];
                    Surf.Bitmap b = (Surf.Bitmap)expressions[1];
                    return(a.And(b.Not()));
                }
                else
                {
                    throw new System.Exception("Invalid expression.");
                }
            }

            case "cartesian_product":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                Set a = (Set)expressions[0];
                Set b = (Set)expressions[1];
                if (a == null || b == null)
                {
                    return(null);
                }
                return(a.CartesianProduct(b));
            }

            case "domain":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Domain());
            }

            case "range":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Range());
            }

            case "transpose":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Transpose());
            }

            case "compose":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                Set a = (Set)expressions[0];
                Set b = (Set)expressions[1];
                if (a == null || b == null)
                {
                    return(null);
                }
                return(a.Compose(b));
            }

            case "nest":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Nest());
            }

            case "unnest":
            {
                Assert(expressions.Count == 1, "Invalid arguments.");

                Set a = (Set)expressions[0];
                if (a == null)
                {
                    return(null);
                }
                return(a.Unnest());
            }

            case "in":
            {
                Assert(expressions.Count == 2, "Invalid arguments.");

                Set    S   = (Set)expressions[0];
                object key = expressions[1];
                if (S == null || key == null)
                {
                    return(null);
                }
                return(S.IsDefined(key));
            }

            case "method":
            {
                Assert(expressions.Count == 4, "Invalid arguments.");

                string name          = (string)expressions[0];
                string type_name     = (string)expressions[1];
                string assembly_name = (string)expressions[2];
                string method_name   = (string)expressions[3];

                Water.Operators.LoadMethodOperator.LoadMethod(name, type_name, assembly_name, method_name);
                return(null);
            }

            default:
            {
                if (Water.Environment.IsConstant(identifier))
                {
                    Water.Operator op = (Water.Operator)Water.Environment.Identify(identifier);
                    return(op.Evaluate(new Water.List(expressions)));
                }
                else if (this._variables.IsDefined(identifier))
                {
                    Assert(expressions.Count > 0, "Invalid arguments.");
                    Set S = (Set)this._variables.Apply(identifier);
                    if (expressions.Count == 1)
                    {
                        return(S.Apply(expressions[0]));
                    }
                    else
                    {
                        return(S.Apply(new Surf.Tuple(expressions)));
                    }
                }
                else
                {
                    throw new System.Exception("Function does not exist.");
                }
            }
            }

            #endregion
        }
Exemplo n.º 4
0
        public static void Print(object value, System.IO.TextWriter output)
        {
            if (value is bool)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is bool[])
            {
                bool[] array = (bool[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is bool[, ])
            {
                bool[,] matrix = (bool[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is byte)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is byte[])
            {
                byte[] array = (byte[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is byte[, ])
            {
                byte[,] matrix = (byte[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is char)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is char[])
            {
                char[] array = (char[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is char[, ])
            {
                char[,] matrix = (char[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is short)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is short[])
            {
                short[] array = (short[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is short[, ])
            {
                short[,] matrix = (short[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is int)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is int[])
            {
                int[] array = (int[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is int[, ])
            {
                int[,] matrix = (int[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is long)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is long[])
            {
                long[] array = (long[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is long[, ])
            {
                long[,] matrix = (long[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is float)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is float[])
            {
                float[] array = (float[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is float[, ])
            {
                float[,] matrix = (float[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is double)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is double[])
            {
                double[] array = (double[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is double[, ])
            {
                double[,] matrix = (double[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is decimal)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is decimal[])
            {
                decimal[] array = (decimal[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is decimal[, ])
            {
                decimal[,] matrix = (decimal[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        output.Write(matrix[x, y].ToString());
                    }
                    output.WriteLine();
                }
            }
            else if (value is string)
            {
                output.WriteLine((string)value);
            }
            else if (value is string[])
            {
                string[] array = (string[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i]);
                }
                output.WriteLine();
            }
            else if (value is string[, ])
            {
                string[,] matrix = (string[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        if (matrix[x, y] == null)
                        {
                            output.Write("");
                        }
                        else
                        {
                            output.Write(matrix[x, y]);
                        }
                    }
                    output.WriteLine();
                }
            }
            else if (value is System.DateTime)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is System.Guid)
            {
                output.WriteLine(value.ToString());
            }
            else if (value is System.Type)
            {
                System.Type type = (System.Type)value;
                output.WriteLine(type.FullName);
            }
            else if (value is System.IO.Stream)
            {
                System.IO.Stream stream = (System.IO.Stream)value;

                long position = stream.Position;
                stream.Position = 0;

                System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                output.WriteLine(reader.ReadToEnd());

                stream.Position = position;
            }
            else if (value is Water.Operator)
            {
                Water.Operator op = (Water.Operator)value;

                output.WriteLine(op.ToString());
            }
            else if (value is object[])
            {
                object[] array = (object[])value;

                for (int i = 0; i < array.Length; i++)
                {
                    if (i > 0)
                    {
                        output.Write(" ");
                    }
                    output.Write(array[i].ToString());
                }
                output.WriteLine();
            }
            else if (value is object[, ])
            {
                object[,] matrix = (object[, ])value;

                for (int y = 0; y < matrix.GetLength(1); y++)
                {
                    for (int x = 0; x < matrix.GetLength(0); x++)
                    {
                        if (x > 0)
                        {
                            output.Write("\t");
                        }
                        if (matrix[x, y] == null)
                        {
                            output.Write("null");
                        }
                        else
                        {
                            output.Write(matrix[x, y].ToString());
                        }
                    }
                    output.WriteLine();
                }
            }
            else
            {
                output.WriteLine(Water.Generator.Generate(value, "\t", "\n"));
            }
        }