/
JsUnaryExpression.cs
52 lines (44 loc) · 1.64 KB
/
JsUnaryExpression.cs
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
using System.Collections.Generic;
namespace ServiceStack.Script;
public class JsUnaryExpression : JsExpression
{
public JsUnaryOperator Operator { get; }
public JsToken Argument { get; }
public override string ToRawString() => Operator.Token + JsonValue(Argument);
public JsUnaryExpression(JsUnaryOperator @operator, JsToken argument)
{
Operator = @operator ?? throw new SyntaxErrorException($"Operator missing in Unary Expression");
Argument = argument ?? throw new SyntaxErrorException($"Argument missing in Unary Expression");
}
public override object Evaluate(ScriptScopeContext scope)
{
var result = Argument.Evaluate(scope);
var afterUnary = Operator.Evaluate(result);
return afterUnary;
}
public override Dictionary<string, object> ToJsAst()
{
var to = new Dictionary<string, object>
{
["type"] = ToJsAstType(),
["operator"] = Operator.Token,
["argument"] = Argument.ToJsAst(),
};
return to;
}
protected bool Equals(JsUnaryExpression other) => Equals(Operator, other.Operator) && Equals(Argument, other.Argument);
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((JsUnaryExpression) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((Operator != null ? Operator.GetHashCode() : 0) * 397) ^ (Argument != null ? Argument.GetHashCode() : 0);
}
}
}