2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / Literal.cs
1 //
2 // Literal.cs
3 //
4 // Author:
5 //      Cesar Lopez Nataren (cesar@ciencias.unam.mx)
6 //
7 // (C) 2003, 2004 Cesar Lopez Nataren 
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Reflection.Emit;
33 using System.Collections;
34
35 namespace Microsoft.JScript {
36
37         internal class This : AST {
38
39                 internal This ()
40                 {
41                 }
42
43                 internal override bool Resolve (IdentificationTable context)
44                 {
45                         throw new NotImplementedException ();
46                 }
47
48                 internal override void Emit (EmitContext ec)
49                 {
50                         throw new NotImplementedException ();
51                 }
52         }
53
54         internal class BooleanLiteral : Exp {
55
56                 internal bool val;
57
58                 internal BooleanLiteral (AST parent, bool val)
59                 {
60                         this.parent = parent;
61                         this.val = val;
62                 }
63
64                 public override string ToString ()
65                 {
66                         return val.ToString ();
67                 }
68
69                 internal override bool Resolve (IdentificationTable context)
70                 {
71                         return true;
72                 }
73
74                 internal override bool Resolve (IdentificationTable context, bool no_effect)
75                 {
76                         this.no_effect = no_effect;
77                         return true;
78                 }
79
80                 internal override void Emit (EmitContext ec)
81                 {
82                         ILGenerator ig = ec.ig;
83
84                         if (val)
85                                 ig.Emit (OpCodes.Ldc_I4_1);
86                         else
87                                 ig.Emit (OpCodes.Ldc_I4_0);
88
89                         ig.Emit (OpCodes.Box, typeof (System.Boolean));
90
91                         if (no_effect)
92                                 ig.Emit (OpCodes.Pop);
93                 }
94         }
95
96         public class NumericLiteral : Exp {
97
98                 double val;
99
100                 internal NumericLiteral (AST parent, double val)
101                 {
102                         this.parent = parent;
103                         this.val = val;
104                 }
105
106                 public override string ToString ()
107                 {
108                         return val.ToString ();
109                 }
110
111                 internal override bool Resolve (IdentificationTable context)
112                 {
113                         return true;
114                 }
115
116                 internal override bool Resolve (IdentificationTable context, bool no_effect)
117                 {                       
118                         this.no_effect = no_effect;
119                         return Resolve (context);
120                 }
121
122                 internal override void Emit (EmitContext ec)
123                 {
124                         ILGenerator ig = ec.ig;
125                         if (parent is Unary && (parent as Unary).oper == JSToken.Minus)
126                                 ig.Emit (OpCodes.Ldc_R8, (double) (val * -1));
127                         else
128                                 ig.Emit (OpCodes.Ldc_R8, (double) val);
129                         ig.Emit (OpCodes.Box, typeof (System.Double));
130                         if (no_effect)
131                                 ig.Emit (OpCodes.Pop);
132                 }
133         }
134
135         public class ObjectLiteral : Exp {
136                 
137                 internal ArrayList elems;
138                 
139                 internal ObjectLiteral (ArrayList elems)
140                 {
141                         this.elems = elems;
142                 }
143
144                 internal ObjectLiteral (AST parent)
145                 {
146                         this.parent = parent;
147                         elems = new ArrayList ();
148                 }
149
150                 internal override bool Resolve (IdentificationTable context, bool no_effect)
151                 {
152                         this.no_effect = no_effect;
153                         return Resolve (context);
154                 }
155
156                 internal override bool Resolve (IdentificationTable context)
157                 {
158                         bool r = true;
159                         foreach (AST ast in elems)
160                                 r &= ast.Resolve (context);
161                         return r;
162                 }
163
164                 internal override void Emit (EmitContext ec)
165                 {
166                         ILGenerator ig = ec.ig;
167
168                         ig.Emit (OpCodes.Ldarg_0);
169                         ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
170                         ig.Emit (OpCodes.Call, typeof (Microsoft.JScript.Vsa.VsaEngine).GetMethod ("GetOriginalObjectConstructor"));
171                         ig.Emit (OpCodes.Call, typeof (ObjectConstructor).GetMethod ("ConstructObject"));
172
173                         foreach (ObjectLiteralItem item in elems) {
174                                 ig.Emit (OpCodes.Dup);
175                                 item.Emit (ec);
176                                 ig.Emit (OpCodes.Call, typeof (JSObject).GetMethod ("SetMemberValue2"));
177                         }
178                         if (no_effect)
179                                 ig.Emit (OpCodes.Pop);
180                 }
181
182                 internal void Add (ObjectLiteralItem item)
183                 {
184                         elems.Add (item);
185                 }
186         }
187
188         internal class ObjectLiteralItem : AST {
189                 internal string property_name;
190                 internal AST exp;
191
192                 internal ObjectLiteralItem (object obj)
193                 {
194                         if (obj != null)
195                                 property_name = obj.ToString ();
196                 }
197
198                 internal override bool Resolve (IdentificationTable context)
199                 {
200                         return exp.Resolve (context);
201                 }
202
203                 internal override void Emit (EmitContext ec)
204                 {
205                         ec.ig.Emit (OpCodes.Ldstr, property_name);
206                         exp.Emit (ec);
207                 }
208         }
209
210         public class PropertyName {
211                 string name;
212                 internal string Name {
213                         get { return name; }
214                         set { name = value; }
215                 }
216         }
217
218         internal class RegExpLiteral : AST {
219                 internal string re;
220                 internal string flags;
221
222                 internal RegExpLiteral (string re, string flags)
223                 {
224                         this.re = re;
225                         this.flags = flags;
226                 }
227
228                 internal override bool Resolve (IdentificationTable context)
229                 {
230                         throw new NotImplementedException ();
231                 }
232
233                 internal override void Emit (EmitContext ec)
234                 {
235                         throw new NotImplementedException ();
236                 }
237         }               
238 }