tagged release 0.6.4
[parrot.git] / src / ops / var.ops
blob1d6981f030c8092d6f45fa03c3795f17836a0f28
1 /*
2  * $Id$
3 ** var.ops
4 */
6 VERSION = PARROT_VERSION;
8 =head1 NAME
10 var.ops - Variable Ops
12 =head1 DESCRIPTION
14 These operations deal with both lexical and global variables,
15 as well as the symbol tables that contain them.
17 =cut
19 ###############################################################################
21 =head2 Lexical variable ops
23 Operations to create, modify and delete lexical variables.
25 =over 4
27 =cut
29 ########################################
31 =item B<store_lex>(in STR, invar PMC)
33 Store object $2 as lexical symbol $1. The opcode might succeed
34 or throw an exception on unknown lexical names depending on the
35 implementation of the LexPad PMC.
36 Parrot's LexPad throws an exception for unknown names.
39 =cut
41 op store_lex(in STR, invar PMC) {
42     parrot_context_t * const ctx      = CONTEXT(interp);
43     STRING           * const lex_name = $1;
44     PMC              * const lex_pad  = Parrot_find_pad(interp, lex_name, ctx);
46     if (PMC_IS_NULL(lex_pad)) {
47         real_exception(interp, NULL, LEX_NOT_FOUND,
48                 "Lexical '%Ss' not found", lex_name);
49     }
50     VTABLE_set_pmc_keyed_str(interp, lex_pad, lex_name, $2);
53 ########################################
55 =item B<find_lex>(out PMC, in STR)
57 Find the lexical variable named $2 and store it in $1. This
58 opcode either throws an exception or returns a Null PMC for the failure case,
59 depending on the implementation of the LexPad PMC. Parrot's
60 standard LexPad throws and exception for non-existing names.
62 =cut
64 op find_lex(out PMC, in STR) {
65     parrot_context_t * const ctx      = CONTEXT(interp);
66     STRING           * const lex_name = $2;
67     PMC              * const lex_pad  = Parrot_find_pad(interp, lex_name, ctx);
69     PMC * const result =
70         PMC_IS_NULL(lex_pad)
71             ? NULL
72             : VTABLE_get_pmc_keyed_str(interp, lex_pad, lex_name);
73     if (!result) {
74         real_exception(interp, NULL, LEX_NOT_FOUND,
75                 "Lexical '%Ss' not found", lex_name);
76     }
77     $1 = result;
80 =back
82 =cut
84 ###############################################################################
86 =head2 Namespace opcodes
88 =over 4
90 =item B<get_namespace>(out PMC)
92 Set $1 to the current namespace.
94 =item B<get_namespace>(out PMC, in PMC)
96 Set $1 to the namespace denoted by the key constant $2, relative to the
97 current namespace.  If the namespace doesn't exist, $1 is set to null.
99 =cut
101 op get_namespace(out PMC) {
102     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
103     $1 = cur_ns;
106 op get_namespace(out PMC, in PMC) {
107     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
108     PMC * const ns     = Parrot_get_namespace_keyed(interp, cur_ns, $2);
110     $1 = PMC_IS_NULL(ns) ? PMCNULL : ns;
113 =item B<get_hll_namespace>(out PMC)
115 Set $1 to the current HLL root namespace.
117 =item B<get_hll_namespace>(out PMC, in PMC)
119 Set $1 to the namespace denoted by the key constant $2, relative to the
120 current HLL root namespace.  If the namespace doesn't exist, $1 is set to
121 null.
123 =cut
125 op get_hll_namespace(out PMC) {
126     PMC * const hll_ns = Parrot_get_ctx_HLL_namespace(interp);
127     $1 = hll_ns;
130 op get_hll_namespace(out PMC, in PMC) {
131     PMC * const hll_ns = Parrot_get_ctx_HLL_namespace(interp);
132     if (PMC_IS_NULL(hll_ns))
133         $1 = PMCNULL;
134     else {
135         PMC * const ns = Parrot_get_namespace_keyed(interp, hll_ns, $2);
136         $1 = ns;
137     }
140 =item B<get_root_namespace>(out PMC)
142 Set $1 to the true root namespace.
144 =item B<get_root_namespace>(out PMC, in PMC)
146 Set $1 to the namespace denoted by the key constant $2, relative to the true
147 root namespace.  If the namespace doesn't exist, $1 is set to null.
149 =cut
151 op get_root_namespace(out PMC) {
152     PMC * const root_ns = interp->root_namespace;
153     $1 = root_ns;
156 op get_root_namespace(out PMC, in PMC) {
157     PMC * const root_ns = interp->root_namespace;
158     if (PMC_IS_NULL(root_ns))
159         $1 = PMCNULL;
160     else {
161         PMC * const ns = Parrot_get_namespace_keyed(interp, root_ns, $2);
162         $1 = ns;
163     }
166 =back
168 =cut
170 ###############################################################################
172 =head2 Global variable 'get' opcodes
174 =over 4
176 =item B<get_global>(out PMC, in STR)
178 Set $1 to the global named $2 in current namespace.  If the global doesn't
179 exist, $1 is set to null.
181 =item B<get_global>(out PMC, in PMC, in STR)
183 Set $1 to the global named $3 in the namespace denoted by the key constant
184 $2, relative to the current namespace.  If the namespace or the global
185 doesn't exist, $1 is set to null.
187 =cut
189 op get_global(out PMC, in STR) {
190     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
191     $1 = Parrot_find_global_op(interp, cur_ns, $2, expr NEXT());
194 op get_global(out PMC, in PMC, in STR) {
195     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
196     if (PMC_IS_NULL(cur_ns)) {
197         $1 = PMCNULL;
198     }
199     else {
200         PMC * const ns = Parrot_get_namespace_keyed(interp, cur_ns, $2);
201         if (PMC_IS_NULL(ns))
202             $1 = PMCNULL;
203         else
204             $1 = Parrot_find_global_op(interp, ns, $3, expr NEXT());
205     }
208 =item B<get_hll_global>(out PMC, in STR)
210 Set $1 to the global named $2 in the current HLL root namespace.  If the
211 global doesn't exist, $1 is set to null.
213 =item B<get_hll_global>(out PMC, in PMC, in STR)
215 Set $1 to the global named $3 in the namespace denoted by the key constant
216 $2, relative to the current HLL root namespace.  If the namespace or the
217 global doesn't exist, $1 is set to null.
219 =cut
221 op get_hll_global(out PMC, in STR) {
222     PMC * const hll_ns = Parrot_get_ctx_HLL_namespace(interp);
223     $1 = Parrot_find_global_op(interp, hll_ns, $2, expr NEXT());
226 op get_hll_global(out PMC, in PMC, in STR) {
227     PMC * const hll_ns = Parrot_get_ctx_HLL_namespace(interp);
228     if (PMC_IS_NULL(hll_ns)) {
229         $1 = hll_ns;
230     }
231     else {
232         PMC * const ns = Parrot_get_namespace_keyed(interp, hll_ns, $2);
233         if (PMC_IS_NULL(ns))
234             $1 = PMCNULL;
235         else
236             $1 = Parrot_find_global_op(interp, ns, $3, expr NEXT());
237     }
240 =item B<get_root_global>(out PMC, in STR)
242 Set $1 to the global named $2 in the true root namespace.  If the global
243 doesn't exist, $1 is set to null.
245 =item B<get_root_global>(out PMC, in PMC, in STR)
247 Set $1 to the global named $3 in the namespace denoted by the key constant
248 $2, relative to the true root namespace.  If the namespace or the global
249 doesn't exist, $1 is set to null.
251 =cut
253 op get_root_global(out PMC, in STR) {
254     PMC * const root_ns = interp->root_namespace;
255     $1 = Parrot_find_global_op(interp, root_ns, $2, expr NEXT());
258 op get_root_global(out PMC, in PMC, in STR) {
259     PMC * const root_ns = interp->root_namespace;
260     if (PMC_IS_NULL(root_ns))
261         $1 = PMCNULL;
262     else {
263         PMC * const ns = Parrot_get_namespace_keyed(interp, root_ns, $2);
264         if (PMC_IS_NULL(ns))
265             $1 = PMCNULL;
266         else
267             $1 = Parrot_find_global_op(interp, ns, $3, expr NEXT());
268     }
271 =back
273 =cut
275 ###############################################################################
277 =head2 Global variable 'set' opcodes
279 =over 4
281 =item B<set_global>(in STR, invar PMC)
283 Set the global named $1 in the current namespace to $2.
285 =item B<set_global>(in PMC, in STR, invar PMC)
287 Set the global named $2 in the namespace denoted by the key constant $1,
288 relative to the current namespace, to $3.  If the namespace does not exist,
289 it is created.
291 =cut
293 op set_global(in STR, invar PMC) {
294     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
295     Parrot_set_global(interp, cur_ns, $1, $2);
298 op set_global(in PMC, in STR, invar PMC) {
299     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
300     PMC * const ns     = Parrot_make_namespace_keyed(interp, cur_ns, $1);
302     Parrot_set_global(interp, ns, $2, $3);
305 =item B<set_hll_global>(in STR, invar PMC)
307 Set the global named $1 to $2 in the current HLL root namespace.
309 =item B<set_hll_global>(in PMC, in STR, invar PMC)
311 Set the global named $2 in the namespace denoted by the key constant
312 $1 (relative to the current HLL namespace) to $3. If the namespace
313 does not exist, it is created.
315 =cut
317 op set_hll_global(in STR, invar PMC) {
318     PMC * const hll_ns = Parrot_get_ctx_HLL_namespace(interp);
319     Parrot_set_global(interp, hll_ns, $1, $2);
322 op set_hll_global(in PMC, in STR, invar PMC) {
323     PMC * const hll_ns = Parrot_get_ctx_HLL_namespace(interp);
324     PMC * const ns     = Parrot_make_namespace_keyed(interp, hll_ns, $1);
326     Parrot_set_global(interp, ns, $2, $3);
329 =item B<set_root_global>(in STR, invar PMC)
331 Set the global named $1 in the true root namespace to $2.
333 =item B<set_root_global>(in PMC, in STR, invar PMC)
335 Set the global named $2 in the namespace denoted by the key constant
336 $1 (relative to the true root namespace) to $3.  If the namespace does
337 not exist, it is created.
339 =cut
341 op set_root_global(in STR, invar PMC) {
342     PMC * const root_ns = interp->root_namespace;
343     Parrot_set_global(interp, root_ns, $1, $2);
346 op set_root_global(in PMC, in STR, invar PMC) {
347     PMC * const root_ns = interp->root_namespace;
348     PMC * const ns      = Parrot_make_namespace_keyed(interp, root_ns, $1);
350     Parrot_set_global(interp, ns, $2, $3);
353 =back
355 =cut
357 ###############################################################################
359 =head2 Global variable ops
361 Operations to modify global variables
363 =over 4
365 =cut
367 ########################################
369 =item B<store_global>(in STR, invar PMC)
371 Store value $2 as global symbol $1 in the current namespace.
373 =item B<store_global>(in STR, in STR, invar PMC)
375 =item B<store_global>(in PMC, in STR, invar PMC)
377 Store global $3 as global symbol $2 in namespace designated by $1.  If
378 namespace does not exist, create it.  $1 may be a key, a string, or an array
379 of strings.
381 =cut
383 op store_global(in STR, invar PMC) :deprecated {
384     Parrot_store_global_cur(interp, $1, $2);
387 op store_global(in STR, in STR, invar PMC) :deprecated {
388     Parrot_store_global_s(interp, $1, $2, $3);
391 op store_global(in PMC, in STR, invar PMC) :deprecated {
392     Parrot_store_global_k(interp, $1, $2, $3);
395 ########################################
397 =item B<find_global>(out PMC, in STR)
399 Find the global named $2 in the current namespace and store it in $1.
401 If the global doesn't exist either throws an exception or sets $1 to the
402 Null PMC, depending on current errors settings. See B<errorson>.
404 =item B<find_global>(out PMC, in STR, in STR)
406 Find the global named $3 in the namespace named $2 and store it in $1.
408 If the global doesn't exist either throws an exception or sets $1 to the
409 Null PMC, depending on current errors settings. See B<errorson>.
411 =item B<find_global>(out PMC, in PMC, in STR)
413 Find the global named $3 in the namespace designated by $2 and store it in
414 $1.  $2 may be a key, a string, or an array of strings.
416 If the global doesn't exist either throws an exception or sets $1 to the
417 Null PMC, depending on current errors settings.  See B<errorson>.
419 =cut
421 op find_global(out PMC, in STR) :deprecated {
422     PMC * const cur_ns = CONTEXT(interp)->current_namespace;
423     $1 = Parrot_find_global_op(interp, cur_ns, $2, expr NEXT());
426 op find_global(out PMC, in STR, in STR) :deprecated {
427     PMC * const ns = Parrot_get_namespace_keyed_str(interp,
428                                                     Parrot_get_ctx_HLL_namespace(interp),
429                                                     $2);
430     $1 = Parrot_find_global_op(interp, ns, $3, expr NEXT());
433 op find_global(out PMC, in PMC, in STR) :deprecated {
434     PMC * const ns = Parrot_get_namespace_keyed(interp,
435                                                 Parrot_get_ctx_HLL_namespace(interp),
436                                                 $2);
437     $1 = Parrot_find_global_op(interp, ns, $3, expr NEXT());
440 =item B<find_name>(out PMC, in STR)
442 Find the name $2 in lexical, current, global, or builtin namespace and
443 store it in $1. If the name doesn't exist
444 either throws an exception or sets $1 to PMCNULL, depending on current
445 errors settings. See B<errorson>.
447 =cut
449 op find_name(out PMC, in STR) {
450     if (!$2)
451         real_exception(interp, NULL, 1, "Tried to find null name");
453     $1 = Parrot_find_name_op(interp, $2, expr NEXT());
456 =back
458 =head1 COPYRIGHT
460 Copyright (C) 2001-2008, The Perl Foundation.
462 =head1 LICENSE
464 This program is free software. It is subject to the same license
465 as the Parrot interpreter itself.
467 =cut
470  * Local variables:
471  *   c-file-style: "parrot"
472  * End:
473  * vim: expandtab shiftwidth=4:
474  */