forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsBinaryExpression.cs
More file actions
60 lines (53 loc) · 2.23 KB
/
JsBinaryExpression.cs
File metadata and controls
60 lines (53 loc) · 2.23 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
using System.Collections.Generic;
namespace ServiceStack.Script
{
public class JsBinaryExpression : JsExpression
{
public JsBinaryExpression(JsToken left, JsBinaryOperator @operator, JsToken right)
{
Left = left ?? throw new SyntaxErrorException($"Left Expression missing in Binary Expression");
Operator = @operator ?? throw new SyntaxErrorException($"Operator missing in Binary Expression");
Right = right ?? throw new SyntaxErrorException($"Right Expression missing in Binary Expression");
}
public JsBinaryOperator Operator { get; set; }
public JsToken Left { get; set; }
public JsToken Right { get; set; }
public override string ToRawString() => "(" + JsonValue(Left) + Operator.Token + JsonValue(Right) + ")";
protected bool Equals(JsBinaryExpression other) =>
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((JsBinaryExpression) 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 object Evaluate(ScriptScopeContext scope)
{
var lhs = Left.Evaluate(scope);
var rhs = Right.Evaluate(scope);
return Operator.Evaluate(lhs, rhs);
}
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;
}
}
}