예제 #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
        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
        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
        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
        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
        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
        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
        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
 private TreeNode SExprConstructElement (EvaluatorBase evaluator, TreeNode [] args)
 {
     return SExprConstructPipelinePart (evaluator, args, true);
 }
예제 #19
0
 private TreeNode SExprConstructCaps (EvaluatorBase evaluator, TreeNode [] args)
 {
     return SExprConstructPipelinePart (evaluator, args, false);
 }