This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
PostOrPrefixOperator.cs
162 lines (144 loc) · 4.69 KB
/
PostOrPrefixOperator.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// PostOrPrefixOperator.cs:
//
// Author:
// Cesar Lopez Nataren (cesar@ciencias.unam.mx)
//
// (C) 2003, Cesar Lopez Nataren
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Diagnostics;
using System.Reflection.Emit;
namespace Microsoft.JScript {
public class PostOrPrefixOperator : UnaryOp {
bool prefix;
public PostOrPrefixOperator (int operatorTok)
: base (null, null)
{
oper = (JSToken) operatorTok;
}
internal PostOrPrefixOperator (AST parent, AST operand, JSToken oper, bool prefix, Location location)
: base (parent, location)
{
this.operand = operand;
this.oper = oper;
this.prefix = prefix;
}
[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public object EvaluatePostOrPrefix (ref object v)
{
double value = Convert.ToNumber (v);
v = value;
int oper = (int) this.oper;
if (oper % 2 == 1) /* prefix? */
return value + 1;
else
return value - 1;
}
internal override bool Resolve (Environment env)
{
return operand.Resolve (env);
}
internal override bool Resolve (Environment env, bool no_effect)
{
if (operand is Exp)
return ((Exp) operand).Resolve (env, no_effect);
else
return operand.Resolve (env);
}
internal override void Emit (EmitContext ec)
{
if (oper == JSToken.None)
operand.Emit (ec);
else {
ILGenerator ig = ec.ig;
Type post_prefix = typeof (PostOrPrefixOperator);
LocalBuilder post_prefix_local = ig.DeclareLocal (post_prefix);
LocalBuilder tmp_obj = ig.DeclareLocal (typeof (object));
switch (this.oper) {
case JSToken.Increment:
if (prefix)
ig.Emit (OpCodes.Ldc_I4_3);
else
ig.Emit (OpCodes.Ldc_I4_1);
break;
case JSToken.Decrement:
if (prefix)
ig.Emit (OpCodes.Ldc_I4_2);
else
ig.Emit (OpCodes.Ldc_I4_0);
break;
}
ig.Emit (OpCodes.Newobj, post_prefix.GetConstructor (new Type [] { typeof (int) }));
ig.Emit (OpCodes.Stloc, post_prefix_local);
Binary assign = null;
if (operand is Identifier)
((Identifier) operand).EmitLoad (ec);
else if (operand is Binary) {
Binary binary = operand as Binary;
binary.no_effect = false;
binary.assign = false;
assign = new Binary (binary.parent, binary.left, binary.right, binary.op, binary.location);
assign.assign = true;
assign.late_bind = true;
assign.no_effect = false;
if (binary.op == JSToken.LeftBracket || binary.op == JSToken.AccessField) {
binary.Emit (ec);
ig.Emit (OpCodes.Box, typeof (object));
} else
throw new NotImplementedException (String.Format ("Unhandled binary op {0}", operand));
} else {
Console.WriteLine ("PostOrPrefixOperator: prefix = {0}, oper = {1}, operand = {2}",
prefix, oper, operand.GetType ());
throw new NotImplementedException ();
}
ig.Emit (OpCodes.Stloc, tmp_obj);
ig.Emit (OpCodes.Ldloc, post_prefix_local);
ig.Emit (OpCodes.Ldloca_S, tmp_obj);
ig.Emit (OpCodes.Call, post_prefix.GetMethod ("EvaluatePostOrPrefix"));
//
// if does not appear as a global expression
//
if (prefix && !(parent is ScriptBlock)) {
ig.Emit (OpCodes.Dup);
ig.Emit (OpCodes.Stloc, tmp_obj);
}
if (operand is Identifier)
((Identifier) operand).EmitStore (ec);
else if (operand is Binary)
assign.Emit (ec);
else
throw new NotImplementedException ();
//
// If value will be used, load the
// temp var that holded the value
// before inc/dec was evaluated
//
if (!(parent is ScriptBlock || parent is FunctionDeclaration ||
parent is FunctionExpression || parent is Block))
ig.Emit (OpCodes.Ldloc, tmp_obj);
}
}
}
}