Changes between Version 3 and Version 4 of PIR Tutorial

Show
Ignore:
Timestamp:
03/22/09 18:08:44 (5 years ago)
Author:
gaurav
Comment:

Tweaked the formatting and a few spellings, removed notice

Legend:

Unmodified
Added
Removed
Modified
  • PIR Tutorial

    v3 v4  
    11= PIR Tutorial = 
    2  
    3 (Currently being moved from [http://www.perlfoundation.org/parrot/index.cgi?pir_tutorial_and_faq the old Parrot wiki]; this article is probably incomplete!) 
    42 
    53== Introduction == 
     
    3331}}} 
    3432 
    35 That was not too hard, now was it? Before we continue to more complex examples, let's first analyze what happened. The first line in the file is `.sub main`. This indicates that we're defining a subroutine that goes by the name main. Note that it is not necessary to name your subroutine like this, even if it's the only subroutine. The name main does not indicate execution will start at that subroutine, like in C. In PIR, execution will start at the top-most defined subroutine in the file, not matter what its name is. (There are ways to change this, though, but we will forget that for now. More on that later). As you can see on the third line, the subroutine is closed with the .end directive. 
    36  
    37 In between these subroutine directives, you can define the subroutine body, which consists of PIR or Parrot assembly (PASM) instructions. In this simple program we just sticked to the print instruction. It takes 1 parameter that can be of any type, as long as it is something (i.e. it is not undefined or null). Please note that all instruction should be in between a .sub/.end pair. 
     33That was not too hard, now was it? Before we continue to more complex examples, let's first analyze what happened. The first line in the file is `.sub main`. This indicates that we're defining a subroutine that goes by the name main. Note that it is not necessary to name your subroutine like this, even if it's the only subroutine. The name main does not indicate execution will start at that subroutine, like in C. In PIR, execution will start at the top-most defined subroutine in the file, not matter what its name is. (There are ways to change this, though, but we will forget that for now. More on that later). As you can see on the third line, the subroutine is closed with the `.end` directive. 
     34 
     35In between these subroutine directives, you can define the subroutine body, which consists of PIR or Parrot assembly (PASM) instructions. In this simple program we just sticked to the print instruction. It takes one parameter that can be of any type, as long as it is something (i.e. it is not undefined or null). Please note that all instruction should be in between a `.sub`/`.end` pair. 
    3836 
    3937== More instructions == 
     
    4846  N10 = 3.14            # store 3.14 in numeric register 10 
    4947  S20 = "Hello world!"  # store this string in string register 20 
    50   P30 = new .String     # create a new String object in PMC register 30. See "Where to read further?" for links to more tutorials. 
     48  P30 = new .String     # create a new String object in PMC register 30. See "Where to read further?" for more information on Strings. 
    5149}}} 
    5250 
     
    6866This declares some temporary variables. Although this declares an integer and some numeric variables, you could use any of the following types: 
    6967 
    70  * int - declare an integer variable 
    71  * num - declare a floating-point number variable 
    72  * string - declare a string variable 
    73  * pmc - declare a Parrot Magic Cookie (PMC) variable 
     68 * `int` - declare an integer variable 
     69 * `num` - declare a floating-point number variable 
     70 * `string` - declare a string variable 
     71 * `pmc` - declare a Parrot Magic Cookie (PMC) variable 
    7472 
    7573You might wonder what the heck is a Parrot Magic Cookie. This is where Parrot's Magic comes in. In fact, it's so magical, there's a separate document written on that. Have a look at the section [#further-reading Where to read further?]. 
     
    140138 
    141139This section will discuss a little bit more on subroutines so you can do some useful stuff. Although there's much more to subroutines, we'll postpone that to a later section. 
    142 Passing parameters 
     140 
     141==== Passing parameters ==== 
    143142 
    144143In order to pass parameters to a sub, you'll need to define these parameters. This is easy: 
     
    152151 
    153152Sometimes you want a subroutine that might take a parameter, depending on the situation. In that case you'd want to use optional parameters. 
    154 Returning values 
     153 
     154==== Returning values ==== 
    155155 
    156156If you remember the example in which we implemented the ABC formula, you could see that we calculated them, but only printed them. Usually, you'd like to have some subroutine calculate something and then return the answers. Instead of printing them, you could return the answers, as shown in this code snippet: 
     
    163163}}} 
    164164 
    165 === Invoking subroutines === 
     165==== Invoking subroutines ==== 
    166166 
    167167Now you know how to define parameters and return values, it's time to explore how to invoke your subroutine. Some examples: 
     
    217217==== Goto statements ==== 
    218218 
    219 The most basic one is of course the goto instruction. It's very simple: 
     219The most basic one is of course the `goto` instruction. It's very simple: 
    220220{{{ 
    221221    ... 
     
    226226}}} 
    227227 
    228 Although the use of goto is not adviced in high-level languages (HLLs), in PIR you are hardly able to write useful programs without it. Besides, PIR is assembly language after all, so that's ok. 
     228Although the use of goto is not advised in high-level languages (HLLs), in PIR you are hardly able to write useful programs without it. Besides, PIR is assembly language after all, so that's okay. 
    229229 
    230230==== If statements ==== 
     
    269269Loop constructions 
    270270 
    271 PIR has no built-in while or for statement, but implementing a loop can be easily done using the if statement, some *goto*s and a couple of labels, like this: 
     271PIR has no built-in while or for statement, but implementing a loop can be easily done using the `if` statement, some `goto`s and a couple of labels, like this: 
    272272{{{ 
    273273    ... 
     
    289289 
    290290Sometimes it's easier to split up your program into multiple files. There are two ways to do this: 
    291  * use the .include directive 
    292  * compile the PIR files separately and load them using load_bytecode 
     291 * use the `.include` directive 
     292 * compile the PIR files separately and load them using `load_bytecode` 
    293293 
    294294The first way is the simplest. Use the .include directive at a point in your main file where you'd like to pull in the contents of another file. The contents of the specified file is read and replaces the .include directive, just as the #include directive does in the C preprocessor.