Introduction
We have learned a reasonable amount so far. We know how to write fairly trivial applications using Parrot Intermediate Representation. We could probably write a simple formula calculator that gets input from the user, ensures that the content is valid, and presents the results of applying user input to the formula.
It would be nice to write more ambitious programs, though. It would be painful - maybe even impossible - to create a modern program using only the tools and opcodes we have learned so far.
We can start examining PMCs by writing a version of our hypotenuse calculator from a few steps ago that has command line arguments.
Command Line Arguments
How do we tell Parrot that our program accepts command line arguments, though? We need some way to show that our :main
sub is ready to take parameters. Turns out that’s actually pretty easy.
.param
directive
The .param
directive is used at the start of a subroutine to indicate that the subroutine will accept a parameter and place it in the named variable.
argv
is a ResizableStringArray: an ordered collection of strings.
We can use the elements
opcode to find out how many arguments were passed to the file.
Try it out.
Three? Let’s look at the arguments individually and see if we can figure this out.
shift
The shift
opcode lets us pull the first item from an array. This shrinks the argv
array by one as it shifts the rest of its contents over to fill the empty space, but it’s not a concern for us right now.
We can use this code to look at our program arguments one at a time.
Oh, right. The program name is the first argument. That is not unusual, especially in some lower level languages. I should have remembered.
Calculating a Hypotenuse
Let’s take what we’ve learned about handling the command line and apply it to our hypotenuse calculator.
First we shift the first item off of argv
because we know for sure that it’s going to be the program name. Then we check to make sure that the user has provided us with two arguments that we can use for a
and b
. Not having two arguments is an error, so the program branches to displaying an error and quitting. When the argument count is right, the program shifts the arguments into a
and b
then uses them to calculate and display the hypotenuse.
Oh yeah - sooner or later you’re going to see this error message from Parrot:
What did line 24 look like?
It took me a while to realize that I had never declared .local num c_squared
. Unfortunately, Parrot’s error messages aren’t quite as descriptive as Perl’s. Perl has had a lot more time to figure out how to gently explain a user’s error to him, though.
That worked.
Parrot has many special opcodes for dealing with PMCs and soon we’ll be looking at more of them. I am especially interested in the opcodes that allow us to use arrays and other collection types.
Summary
Yes, this has been a very quick step. Maybe we didn’t learn a whole lot, but there’s a little bit of new stuff in there. We did learn how to add command line handling by using .param
to tell Parrot that our :main
method accepts parameters. We learned that for :main
, the parameter is a particular PMC - something called a ResizableStringArray. The shift
opcode removes the first item in a ResizableStringArray and lets us use it in a variable. We also saw that we can use the members
opcode to get the number of members in an array.
Added to vault 2024-01-15. Updated on 2024-01-26