コード例 #1
0
        public static TreeNode VariableSet(EvaluatorBase evaluator, TreeNode [] args, bool update)
        {
            if (args.Length != 2)
            {
                throw new ArgumentException("must have two arguments");
            }

            if (!(args[0] is FunctionNode))
            {
                throw new ArgumentException("first argument must be a variable");
            }

            FunctionNode variable_node = evaluator.ResolveFunction(args[0] as FunctionNode);

            if (variable_node != null)
            {
                variable_node.Body = evaluator.Evaluate(args[1]);
            }
            else
            {
                TreeNode parent = args[0].Parent;
                parent = parent.Parent ?? parent;

                parent.RegisterFunction((args[0] as FunctionNode).Function, evaluator.Evaluate(args[1]));
            }

            return(new VoidLiteral());
        }
コード例 #2
0
        public static TreeNode Compare(EvaluatorBase evaluator, TreeNode a, TreeNode b)
        {
            TreeNode arg_a = evaluator.Evaluate(a);
            TreeNode arg_b = evaluator.Evaluate(b);

            if(arg_a.GetType() != arg_b.GetType()) {
                throw new ArgumentException("arguments must be of the same type to compare");
            }

            int result = 0;

            if(arg_a is IntLiteral) {
                result = (arg_a as IntLiteral).Value.CompareTo(
                    (arg_b as IntLiteral).Value);
            } else if(arg_a is DoubleLiteral) {
                result = (arg_a as DoubleLiteral).Value.CompareTo(
                    (arg_b as DoubleLiteral).Value);
            } else if(arg_a is StringLiteral) {
                result = (arg_a as StringLiteral).Value.CompareTo(
                    (arg_b as StringLiteral).Value);
            } else if(arg_a is BooleanLiteral) {
                result = (arg_a as BooleanLiteral).Value.CompareTo(
                    (arg_b as BooleanLiteral).Value);
            } else {
                throw new ArgumentException("invalid type for comparison");
            }

            return new IntLiteral(result);
        }
コード例 #3
0
        public TreeNode Evaluate(EvaluatorBase evaluator, TreeNode [] args)
        {
            if (args != null && args.Length != FunctionCount && RequiresArguments)
            {
                throw new ArgumentException("Function " + function + " takes "
                                            + FunctionCount + " arguments, not " + args.Length);
            }

            if (args != null && RequiresArguments)
            {
                int       i     = 0;
                string [] names = new string[args.Length];

                foreach (KeyValuePair <string, FunctionNode> var in Functions)
                {
                    names[i++] = var.Key;
                }

                for (i = 0; i < args.Length; i++)
                {
                    (body as TreeNode).RegisterFunction(names[i], evaluator.Evaluate(args[i]));
                }
            }

            return(evaluator.Evaluate(ResolveBody(evaluator)));
        }
コード例 #4
0
        public void Load(EvaluatorBase evaluator)
        {
            this.evaluator = evaluator;

            foreach (MethodInfo method in GetType().GetMethods())
            {
                string [] names = null;
                bool      evaluate_variables = true;

                foreach (Attribute attr in method.GetCustomAttributes(false))
                {
                    if (attr is FunctionAttribute)
                    {
                        names = (attr as FunctionAttribute).Names;
                        evaluate_variables = (attr as FunctionAttribute).EvaluateVariables;
                        break;
                    }
                }

                if (names == null || names.Length == 0)
                {
                    continue;
                }

                evaluator.RegisterFunction(this, method, names, evaluate_variables);
            }
        }
コード例 #5
0
ファイル: StringFunctionSet.cs プロジェクト: Yetangitu/f-spot
        public static TreeNode ConcatenateStrings(EvaluatorBase evaluator, TreeNode [] args)
        {
            StringBuilder result = new StringBuilder();

            foreach(TreeNode arg in args) {
                TreeNode eval_arg = evaluator.Evaluate(arg);
                if(!(eval_arg is VoidLiteral)) {
                    result.Append(eval_arg);
                }
            }

            return new StringLiteral(result.ToString());
        }
コード例 #6
0
        public static TreeNode ConcatenateStrings(EvaluatorBase evaluator, TreeNode [] args)
        {
            var result = new StringBuilder();

            foreach (var arg in args)
            {
                var eval_arg = evaluator.Evaluate(arg);
                if (!(eval_arg is VoidLiteral))
                {
                    result.Append(eval_arg);
                }
            }

            return(new StringLiteral(result.ToString()));
        }
コード例 #7
0
        public static TreeNode Compare(EvaluatorBase evaluator, TreeNode a, TreeNode b)
        {
            TreeNode arg_a = evaluator.Evaluate(a);
            TreeNode arg_b = evaluator.Evaluate(b);

            if (arg_a.GetType() != arg_b.GetType())
            {
                throw new ArgumentException("arguments must be of the same type to compare");
            }

            int result = 0;

            if (arg_a is IntLiteral)
            {
                result = (arg_a as IntLiteral).Value.CompareTo(
                    (arg_b as IntLiteral).Value);
            }
            else if (arg_a is DoubleLiteral)
            {
                result = (arg_a as DoubleLiteral).Value.CompareTo(
                    (arg_b as DoubleLiteral).Value);
            }
            else if (arg_a is StringLiteral)
            {
                result = (arg_a as StringLiteral).Value.CompareTo(
                    (arg_b as StringLiteral).Value);
            }
            else if (arg_a is BooleanLiteral)
            {
                result = (arg_a as BooleanLiteral).Value.CompareTo(
                    (arg_b as BooleanLiteral).Value);
            }
            else
            {
                throw new ArgumentException("invalid type for comparison");
            }

            return(new IntLiteral(result));
        }
コード例 #8
0
ファイル: FunctionNode.cs プロジェクト: Yetangitu/f-spot
        public TreeNode Evaluate(EvaluatorBase evaluator, TreeNode [] args)
        {
            if(args != null && args.Length != FunctionCount && RequiresArguments) {
                throw new ArgumentException("Function " + function + " takes "
                    + FunctionCount + " arguments, not " + args.Length);
            }

            if(args != null && RequiresArguments) {
                int i = 0;
                string [] names = new string[args.Length];

                foreach(KeyValuePair<string, FunctionNode> var in Functions) {
                    names[i++] = var.Key;
                }

                for(i = 0; i < args.Length; i++) {
                    (body as TreeNode).RegisterFunction(names[i], evaluator.Evaluate(args[i]));
                }
            }

            return evaluator.Evaluate(ResolveBody(evaluator));
        }
コード例 #9
0
        public static TreeNode VariableSet(EvaluatorBase evaluator, TreeNode [] args, bool update)
        {
            if(args.Length != 2) {
                throw new ArgumentException("must have two arguments");
            }

            if(!(args[0] is FunctionNode)) {
                throw new ArgumentException("first argument must be a variable");
            }

            FunctionNode variable_node = evaluator.ResolveFunction(args[0] as FunctionNode);
            if(variable_node != null) {
                variable_node.Body = evaluator.Evaluate(args[1]);
            } else {
                TreeNode parent = args[0].Parent;
                parent = parent.Parent ?? parent;

                parent.RegisterFunction((args[0] as FunctionNode).Function, evaluator.Evaluate(args[1]));
            }

            return new VoidLiteral();
        }
コード例 #10
0
ファイル: FunctionSet.cs プロジェクト: Yetangitu/f-spot
        public void Load(EvaluatorBase evaluator)
        {
            this.evaluator = evaluator;

            foreach(MethodInfo method in GetType().GetMethods()) {
                string [] names = null;
                bool evaluate_variables = true;

                foreach(Attribute attr in method.GetCustomAttributes(false)) {
                    if(attr is FunctionAttribute) {
                        names = (attr as FunctionAttribute).Names;
                        evaluate_variables = (attr as FunctionAttribute).EvaluateVariables;
                        break;
                    }
                }

                if(names == null || names.Length == 0) {
                    continue;
                }

                evaluator.RegisterFunction(this, method, names, evaluate_variables);
            }
        }
コード例 #11
0
        private TreeNode ResolveBody(EvaluatorBase evaluator)
        {
            if (body == null)
            {
                throw new UnknownVariableException(Function);
            }

            if (body is string)
            {
                return(new StringLiteral((string)body));
            }
            else if (body is double)
            {
                return(new DoubleLiteral((double)body));
            }
            else if (body is int)
            {
                return(new IntLiteral((int)body));
            }
            else if (body is bool)
            {
                return(new BooleanLiteral((bool)body));
            }
            else if (body is SExpVariableResolutionHandler)
            {
                return(((SExpVariableResolutionHandler)body)(this));
            }
            else if (body is TreeNode)
            {
                return(evaluator.Evaluate((TreeNode)body));
            }

            throw new UnknownVariableException(String.Format(
                                                   "Unknown function type `{0}' for function `{1}'",
                                                   body.GetType(), Function));
        }
コード例 #12
0
ファイル: FunctionNode.cs プロジェクト: Yetangitu/f-spot
        private TreeNode ResolveBody(EvaluatorBase evaluator)
        {
            if(body == null) {
                throw new UnknownVariableException(Function);
            }

            if(body is string) {
                return new StringLiteral((string)body);
            } else if(body is double) {
                return new DoubleLiteral((double)body);
            } else if(body is int) {
                return new IntLiteral((int)body);
            } else if(body is bool) {
                return new BooleanLiteral((bool)body);
            } else if(body is SExpVariableResolutionHandler) {
                return ((SExpVariableResolutionHandler)body)(this);
            } else if(body is TreeNode) {
                return evaluator.Evaluate((TreeNode)body);
            }

            throw new UnknownVariableException(String.Format(
                "Unknown function type `{0}' for function `{1}'",
                body.GetType(), Function));
        }
コード例 #13
0
ファイル: Service.cs プロジェクト: petejohanson/banshee
        private TreeNode SExprConstructPipelinePart (EvaluatorBase evaluator, TreeNode [] args, bool element)
        {
            StringBuilder builder = new StringBuilder ();

            TreeNode list = new TreeNode ();
            foreach (TreeNode arg in args) {
                list.AddChild (evaluator.Evaluate (arg));
            }

            list = list.Flatten ();

            for (int i = 0; i < list.ChildCount; i++) {
                TreeNode node = list.Children[i];

                string value = node.ToString ().Trim ();

                builder.Append (value);

                if (i == 0) {
                    if (list.ChildCount > 1) {
                        builder.Append (element ? ' ' : ',');
                    }

                    continue;
                } else if (i % 2 == 1) {
                    builder.Append ('=');
                } else if (i < list.ChildCount - 1) {
                    builder.Append (element ? ' ' : ',');
                }
            }

            return new StringLiteral (builder.ToString ());
        }
コード例 #14
0
 public static TreeNode VariableSet(EvaluatorBase evaluator, TreeNode var, TreeNode value)
 {
     return VariableSet(evaluator, new TreeNode[] { var, value }, true);
 }
コード例 #15
0
ファイル: Service.cs プロジェクト: petejohanson/banshee
        private TreeNode SExprConstructPipeline (EvaluatorBase evaluator, TreeNode [] args)
        {
            StringBuilder builder = new StringBuilder ();
            List<string> elements = new List<string> ();

            for (int i = 0; i < args.Length; i++) {
                TreeNode node = evaluator.Evaluate (args[i]);
                if (!(node is LiteralNodeBase)) {
                    throw new ArgumentException ("node must evaluate to a literal");
                }

                string value = node.ToString ().Trim ();

                if (value.Length == 0) {
                    continue;
                }

                elements.Add (value);
            }

            for (int i = 0; i < elements.Count; i++) {
                builder.Append (elements[i]);
                if (i < elements.Count - 1) {
                    builder.Append (" ! ");
                }
            }

            return new StringLiteral (builder.ToString ());
        }
コード例 #16
0
ファイル: Service.cs プロジェクト: petejohanson/banshee
        private TreeNode SExprTestElement (EvaluatorBase evaluator, TreeNode [] args)
        {
            if (args.Length != 1) {
                throw new ArgumentException ("gst-test-element accepts one argument");
            }

            TreeNode arg = evaluator.Evaluate (args[0]);
            if (!(arg is StringLiteral)) {
                throw new ArgumentException ("gst-test-element requires a string argument");
            }

            StringLiteral element_node = (StringLiteral)arg;
            return new BooleanLiteral (TestPipeline (element_node.Value));
        }
コード例 #17
0
 public static TreeNode VariableSet(EvaluatorBase evaluator, TreeNode var, TreeNode value)
 {
     return(VariableSet(evaluator, new TreeNode[] { var, value }, true));
 }
コード例 #18
0
ファイル: Service.cs プロジェクト: petejohanson/banshee
 private TreeNode SExprConstructElement (EvaluatorBase evaluator, TreeNode [] args)
 {
     return SExprConstructPipelinePart (evaluator, args, true);
 }
コード例 #19
0
ファイル: Service.cs プロジェクト: petejohanson/banshee
 private TreeNode SExprConstructCaps (EvaluatorBase evaluator, TreeNode [] args)
 {
     return SExprConstructPipelinePart (evaluator, args, false);
 }