Author: leo
Date: Wed Aug  3 03:34:54 2005
New Revision: 8772

Modified:
   trunk/docs/compiler_faq.pod
Log:
Update compiler_faq.pod

* howto delete globals, lexicals
* howto access %ENV, %Config
* some more notes about tailcalls, source comments, classes, ...


Modified: trunk/docs/compiler_faq.pod
==============================================================================
--- trunk/docs/compiler_faq.pod (original)
+++ trunk/docs/compiler_faq.pod Wed Aug  3 03:34:54 2005
@@ -28,10 +28,13 @@ See C<languages/tcl/tcl.pir_template>'s 
 
 =head2 How do I embed source locations in my code for debugging?
 
-You can do this using the C<setfile> and C<setline> opcodes. Simply
-set the source file name or line number whenever it changes. This is
-sub-optimal for a number of reasons, so there should eventually be a
-better system for this.
+You can do this using either the C<setfile> and C<setline> opcodes or
+with C-like C<#line> comments:
+
+  #line 27 "my_source.file"
+
+Simply set the source file name or line number whenever it changes. But note
+that currently (Parrot 0.2.x) both are ignored in the lexer.
 
 =head1 Subroutines
 
@@ -63,6 +66,19 @@ The method name may also be a string var
   m = "bold"
   curses_obj.m()
 
+=head2 How do I generate a tailcall in PIR?
+
+  .sub foo
+      # do something
+      .return bar(42)           # tailcall sub bar
+  .end
+
+  .sub bar
+      # ...
+  .end
+
+The sub C<bar> will return to the caller of C<foo>.
+
 =head2 How do I generate a sub call with a variable-length parameter list in 
PIR?
 
 Use unprototyped calls and functions and pass as many arguments as you have.
@@ -125,6 +141,27 @@ or the C<find_global> op:
 
     find_global $P0, "name_of_the_global"
 
+=head2 How can I delete a global
+
+You can retrieve the namespace hash and use the C<delete> opcode.
+Nested namespace names have a NUL char prepended to their name.
+
+  .sub main @MAIN
+    $P0 = new Integer
+    store_global "foo", $P0
+    store_global "Bar", "baz", $P0
+    # ...
+    .include "interpinfo.pasm"
+    .local pmc ns, Bar_ns
+    ns = interpinfo .INTERPINFO_NAMESPACE_ROOT
+    delete ns["foo"]                # delete from top level
+    Bar_ns = ns["\0Bar"]            # get Bar namespace
+    delete Bar_ns["baz"]
+    $P0 = find_global "Bar", "baz"
+    print "never\n"
+  .end
+
+
 =head2 How do I use lexical pads to have both a function scope and a global 
scope?
 
 To create lexical variables, you'll need to keep track of how deeply
@@ -197,7 +234,7 @@ instead of:
 
 You will still need to do a C<store_lex> at some point (probably at the start
 of the block in which it is declared) to create the variable in the first
-place. Put a ParrotReference in it or something.
+place. Put a C<Undef> PMC in it or something.
 
 If, on the other hand, you never find said lexical variable (or if a
 block declares that variable to be global, or whatever other tricks
@@ -207,6 +244,21 @@ can access much the same way:
     find_global $P0, "bar"
     assign $P0, value_to_store
 
+=head2 How can I delete a lexical
+
+You can C<peek_pad> the current pad and use C<delete>.
+
+  .sub main @MAIN
+    new_pad 0
+    $P0 = new Integer
+    store_lex -1, "foo", $P0
+    .local pmc pad
+    pad = peek_pad
+    delete pad["foo"]                # delete from current pad
+    $P0 = find_lex "foo"
+    print "never\n"
+  .end
+
 =head2 How do I resolve a variable name?
 
 Use C<find_name>:
@@ -250,6 +302,13 @@ With the C<newclass> op:
 
     newclass $P0, "Animal"
 
+=head2 How do I add instance variables/attributes?
+
+Each class knows what attribute its object can have. You can add
+attributes to a class (not to individual objects) like so:
+
+    addattribute $P0, "legs"
+
 =head2 How do I add instance methods to a class?
 
 Methods are declared as functions in the class namespace with the
@@ -257,24 +316,23 @@ C<method> keyword appended to the functi
 
   .namespace [ "Animal" ]
 
-  .sub initialise method
-    setattribute self, ...
+  .sub __init method
+    $P0 = new Integer
+    setattribute self, "legs", $P0
     ...
   .end
   .sub run method
     ...
   .end
 
-=head2 How do I add instance variables/attributes?
-
-Each class knows what attribute its object can have. You can add
-attributes to a class (not to individual objects) like so:
+=head2 How do I access attributes?
 
-    addattribute $P0, "legs"
+Attributes can be access by a short name, fully qualified name or by index.
 
-=head2 How do I access attributes?
+  $P0 = getattribute self, "legs"
+  assign $P0, 4                   # set attribute's value
 
-Attributes can be access by a fully qualified name or by index.
+or
 
   $P0 = getattribute self, "Animal\0legs"
   assign $P0, 4                   # set attribute's value
@@ -326,7 +384,17 @@ add more parent classes to it with the C
 
 =head2 How can I specify the constructor of a class?
 
-You can specify the constructor method by setting the BUILD property of
+Just define a method named C<__init> in the namespace if the class.
+
+    newclass $P0, "Dog"         # create a class named Dog
+    # ...
+
+    .namespace ["Dog"]
+
+    .sub __init method
+      # ...
+
+Or you can specify the constructor method by setting the BUILD property of
 the class PMC:
 
     newclass $P0, "Dog"         # create a class named Dog
@@ -344,6 +412,10 @@ Then, you can create an instance of Dog 
 
     new $P0, $I0    # creates a Dog object and stores it in register $P0
 
+or for short:
+
+    new $P0, "Dog"
+
 During the C<new> opcode the constructor is called.
 
 =head2 How can I pass arguments to an constructor?
@@ -352,7 +424,7 @@ You can pass only a single argument to a
 By convention, a hash PMC is passed to the constructor that contains
 the arguments as key/value pairs:
 
-    new $P0, .PerlHash
+    new $P0, .Hash
     set $P0["greeting"], "hello"
     set $P0["size"], 1.23
 
@@ -386,11 +458,11 @@ Not too hard, is it?
 Use C<push_eh> to push an exception handler onto the stack.
 
     push_eh handler
-    $P0 = new Exception
-    throw $P0
+    $P0 = new Exception         # or any other code ...
+    throw $P0                   # ... that might throw
     clear_eh
     exit 0
-    
+
   handler:
     print "Exception caught!\n"
     exit 1
@@ -405,7 +477,7 @@ P5 is the register used for the Exceptio
     throw $P0
     clear_eh
     exit 0
-    
+
   handler:
     print "Exception: "
     $S0 = P5["_message"]
@@ -413,10 +485,31 @@ P5 is the register used for the Exceptio
     print "\n"
     exit 1
 
+=head1 Misc
+
+=head2 How can I access a programs environment.
+
+Create a new C<Env> PMC and access it like a hash.
+
+    .local pmc e
+    e = new .Env
+    $P0 = e['USER']      # lt
+
+=head2 How can I access parrot's configuration.
+
+    .include "iglobals.pasm"
+    .local pmc interp, cfg
+    interp = getinterp
+    cfg = interp[.IGLOBALS_CONFIG_HASH]
+    $S0 = cfg['VERSION']     "0.2.2"
+
+Seel F<config_lib.pasm> for all the keys in the config hash - or iterate
+over the config hash.
+
 =head1 VERSION
 
 =over 4
 
-=item Revision 0.4 - 28 June 2005
+=item Revision 0.5 - 2005.08.03
 
 =back

Reply via email to