2
// CompareFunctionSet.cs
5
// Aaron Bockover <abockover@novell.com>
7
// Copyright (C) 2007-2008 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
namespace Hyena.SExpEngine
33
public class CompareFunctionSet : FunctionSet
35
[Function("compare-to")]
36
public virtual TreeNode OnCompareTo(TreeNode [] args)
38
if(args.Length != 2) {
39
throw new ArgumentException("must have two arguments");
42
return Compare(Evaluator, args[0], args[1]);
45
public static TreeNode Compare(EvaluatorBase evaluator, TreeNode a, TreeNode b)
47
TreeNode arg_a = evaluator.Evaluate(a);
48
TreeNode arg_b = evaluator.Evaluate(b);
50
if(arg_a.GetType() != arg_b.GetType()) {
51
throw new ArgumentException("arguments must be of the same type to compare");
56
if(arg_a is IntLiteral) {
57
result = (arg_a as IntLiteral).Value.CompareTo(
58
(arg_b as IntLiteral).Value);
59
} else if(arg_a is DoubleLiteral) {
60
result = (arg_a as DoubleLiteral).Value.CompareTo(
61
(arg_b as DoubleLiteral).Value);
62
} else if(arg_a is StringLiteral) {
63
result = (arg_a as StringLiteral).Value.CompareTo(
64
(arg_b as StringLiteral).Value);
65
} else if(arg_a is BooleanLiteral) {
66
result = (arg_a as BooleanLiteral).Value.CompareTo(
67
(arg_b as BooleanLiteral).Value);
69
throw new ArgumentException("invalid type for comparison");
72
return new IntLiteral(result);
75
[Function("less-than", "<")]
76
public virtual TreeNode OnCompareLessThan(TreeNode [] args)
78
IntLiteral result = (IntLiteral)OnCompareTo(args);
79
return new BooleanLiteral(result.Value < 0);
82
[Function("greater-than", ">")]
83
public virtual TreeNode OnCompareGreaterThan(TreeNode [] args)
85
IntLiteral result = (IntLiteral)OnCompareTo(args);
86
return new BooleanLiteral(result.Value > 0);
89
[Function("equal", "=")]
90
public virtual TreeNode OnCompareEqual(TreeNode [] args)
92
IntLiteral result = (IntLiteral)OnCompareTo(args);
93
return new BooleanLiteral(result.Value == 0);
96
[Function("not-equal", "!=")]
97
public virtual TreeNode OnCompareNotEqual(TreeNode [] args)
99
BooleanLiteral result = (BooleanLiteral)OnCompareEqual(args);
100
return new BooleanLiteral(!result.Value);
103
[Function("less-than-or-equal", "<=")]
104
public virtual TreeNode OnCompareLessThanOrEqual(TreeNode [] args)
106
BooleanLiteral a = (BooleanLiteral)OnCompareLessThan(args);
107
BooleanLiteral b = (BooleanLiteral)OnCompareEqual(args);
108
return new BooleanLiteral(a.Value || b.Value);
111
[Function("greater-than-or-equal", ">=")]
112
public virtual TreeNode OnCompareGreaterThanOrEqual(TreeNode [] args)
114
BooleanLiteral a = (BooleanLiteral)OnCompareGreaterThan(args);
115
BooleanLiteral b = (BooleanLiteral)OnCompareEqual(args);
116
return new BooleanLiteral(a.Value || b.Value);