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()); }
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); }
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))); }
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); } }
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()); }
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())); }
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)); }
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)); }
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(); }
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); } }
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)); }
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)); }
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 ()); }
public static TreeNode VariableSet(EvaluatorBase evaluator, TreeNode var, TreeNode value) { return VariableSet(evaluator, new TreeNode[] { var, value }, true); }
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 ()); }
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)); }
public static TreeNode VariableSet(EvaluatorBase evaluator, TreeNode var, TreeNode value) { return(VariableSet(evaluator, new TreeNode[] { var, value }, true)); }
private TreeNode SExprConstructElement (EvaluatorBase evaluator, TreeNode [] args) { return SExprConstructPipelinePart (evaluator, args, true); }
private TreeNode SExprConstructCaps (EvaluatorBase evaluator, TreeNode [] args) { return SExprConstructPipelinePart (evaluator, args, false); }