forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsLogicalExpression.cs
More file actions
62 lines (55 loc) · 2.25 KB
/
JsLogicalExpression.cs
File metadata and controls
62 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections.Generic;
namespace ServiceStack.Script
{
public class JsLogicalExpression : JsExpression
{
public JsLogicOperator Operator { get; set; }
public JsToken Left { get; set; }
public JsToken Right { get; set; }
public override string ToRawString() => "(" + JsonValue(Left) + Operator.Token + JsonValue(Right) + ")";
public JsLogicalExpression(JsToken left, JsLogicOperator @operator, JsToken right)
{
Left = left ?? throw new SyntaxErrorException($"Left Expression missing in Logical Expression");
Operator = @operator ?? throw new SyntaxErrorException($"Operator missing in Logical Expression");
Right = right ?? throw new SyntaxErrorException($"Right Expression missing in Logical Expression");
}
public override object Evaluate(ScriptScopeContext scope)
{
var lhs = Left.Evaluate(scope);
var rhs = Right.Evaluate(scope);
return Operator.Test(lhs, rhs);
}
protected bool Equals(JsLogicalExpression other)
{
return Equals(Operator, other.Operator) && Equals(Left, other.Left) && Equals(Right, other.Right);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((JsLogicalExpression) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Operator != null ? Operator.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Left != null ? Left.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Right != null ? Right.GetHashCode() : 0);
return hashCode;
}
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["operator"] = Operator.Token,
["left"] = Left.ToJsAst(),
["right"] = Right.ToJsAst(),
};
return to;
}
}
}