Changes between Version 3 and Version 4 of PIR Tutorial
- Timestamp:
- 03/22/09 18:08:44 (4 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
PIR Tutorial
v3 v4 1 1 = 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!)4 2 5 3 == Introduction == … … 33 31 }}} 34 32 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 .enddirective.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/.endpair.33 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. 34 35 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 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. 38 36 39 37 == More instructions == … … 48 46 N10 = 3.14 # store 3.14 in numeric register 10 49 47 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. 51 49 }}} 52 50 … … 68 66 This declares some temporary variables. Although this declares an integer and some numeric variables, you could use any of the following types: 69 67 70 * int- declare an integer variable71 * num- declare a floating-point number variable72 * string- declare a string variable73 * pmc- declare a Parrot Magic Cookie (PMC) variable68 * `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 74 72 75 73 You 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?]. … … 140 138 141 139 This 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 ==== 143 142 144 143 In order to pass parameters to a sub, you'll need to define these parameters. This is easy: … … 152 151 153 152 Sometimes 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 ==== 155 155 156 156 If 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: … … 163 163 }}} 164 164 165 === Invoking subroutines===165 ==== Invoking subroutines ==== 166 166 167 167 Now you know how to define parameters and return values, it's time to explore how to invoke your subroutine. Some examples: … … 217 217 ==== Goto statements ==== 218 218 219 The most basic one is of course the gotoinstruction. It's very simple:219 The most basic one is of course the `goto` instruction. It's very simple: 220 220 {{{ 221 221 ... … … 226 226 }}} 227 227 228 Although the use of goto is not advi ced 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.228 Although 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. 229 229 230 230 ==== If statements ==== … … 269 269 Loop constructions 270 270 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: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: 272 272 {{{ 273 273 ... … … 289 289 290 290 Sometimes it's easier to split up your program into multiple files. There are two ways to do this: 291 * use the .includedirective292 * compile the PIR files separately and load them using load_bytecode291 * use the `.include` directive 292 * compile the PIR files separately and load them using `load_bytecode` 293 293 294 294 The 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.
