目录

Parrot - 例子

Parrot编程与汇编语言编程类似,您有机会在较低级别工作。 以下是编程示例列表,可让您了解Parrot编程的各个方面。

Classic Hello world!

创建一个名为hello.pir的文件,其中包含以下代码:

.sub _main
   print "Hello world!\n"
   end
.end

然后键入以下命令运行它:

parrot hello.pir

正如所料,这将显示文本'Hello world!' 在控制台上,后跟一个新行(由于\ n)。

在上面的例子中,'。sub_main'声明后面的指令组成了一个名为'_main'的子程序,直到遇到'.end'。 第二行包含打印指令。 在这种情况下,我们调用接受常量字符串的指令的变体。 汇编程序负责决定使用哪种指令变体。 第三行包含'end'指令,该指令使解释器终止。

使用寄存器 (Using Registers)

我们可以修改hello.pir,首先将字符串Hello world!\ n存储在寄存器中,然后将该寄存器与print指令一起使用。

.sub _main
   set S1, "Hello world!\n"
   print S1
   end
.end

这里我们准确说明了要使用的寄存器。 但是,通过用$ S1替换S1,我们可以将选择使用哪个寄存器委托给Parrot。 也可以使用=表示法而不是写入设置指令。

.sub _main
   $S0 = "Hello world!\n"
   print $S0
   end
.end

为了使PIR更具可读性,可以使用命名寄存器。 这些稍后映射到实数编号的寄存器。

.sub _main
   .local string hello
   hello = "Hello world!\n"
   print hello
   end
.end

'.local'指令表示只在当前编译单元内(即.sub和.end之间)需要命名寄存器。 以下'.local'是一种类型。 这可以是int(对于I寄存器),float(对于N寄存器),字符串(对于S寄存器),pmc(对于P寄存器)或PMC类型的名称。

平方和 (Summing squares)

此示例介绍了一些更多指令和PIR语法。 以#开头的行是注释。

.sub _main
   # State the number of squares to sum.
   .local int maxnum
   maxnum = 10
   # Some named registers we'll use. 
   # Note how we can declare many
   # registers of the same type on one line.
   .local int i, total, temp
   total = 0
   # Loop to do the sum.
   i = 1
loop:
   temp = i * i
   total += temp
   inc i
   if i <= maxnum goto loop
   # Output result.
   print "The sum of the first "
   print maxnum
   print " squares is "
   print total
   print ".\n"
   end
.end

PIR提供了一些语法糖,使它看起来比装配更高。 例如:

temp = i * i

只是另一种编写更多汇编的方式:

mul temp, i, i

和:

if i <= maxnum goto loop

是相同的:

le i, maxnum, loop

和:

total += temp

是相同的:

add total, temp

通常,每当Parrot指令修改寄存器的内容时​​,这将是以汇编形式写入指令时的第一个寄存器。

与汇编语言一样,循环和选择是根据条件分支语句和标签实现的,如上所示。 汇编编程是一个使用goto不是一个坏形式的地方!

斐波纳契数 (Fibonacci Numbers)

Fibonacci系列的定义如下:取两个数字,1和1.然后重复将系列中的最后两个数字加在一起,形成下一个数字:1,1,2,3,5,8,13等等。 斐波纳契数fib(n)是该系列中的第n个数。 这是一个简单的Parrot汇编程序,它找到前20个Fibonacci数:

# Some simple code to print some Fibonacci numbers
        print   "The first 20 fibonacci numbers are:\n"
        set     I1, 0
        set     I2, 20
        set     I3, 1
        set     I4, 1
REDO:   eq      I1, I2, DONE, NEXT
NEXT:   set     I5, I4
        add     I4, I3, I4
        set     I3, I5
        print   I3
        print   "\n"
        inc     I1
        branch  REDO
DONE:   end

这是Perl中的等效代码:

print "The first 20 fibonacci numbers are:\n";
my $i = 0;
my $target = 20;
my $a = 1;
my $b = 1;
until ($i == $target) {
   my $num = $b;
   $b += $a;
   $a = $num;
   print $a,"\n";
   $i++;
}

NOTE:作为一个很好的兴趣点,在Perl中打印Fibonacci系列的最短和最美的方法之一是perl -le'$ b = 1; 打印$ a + = $ b,同时打印$ b + = $ a'。

递归地计算阶乘 (Recursively computing factorial)

在这个例子中,我们定义了一个阶乘函数,并递归地调用它来计算阶乘。

.sub _fact
   # Get input parameter.
   .param int n
   # return (n > 1 ? n * _fact(n - 1) : 1)
   .local int result
   if n > 1 goto recurse
   result = 1
   goto return
recurse:
   $I0 = n - 1
   result = _fact($I0)
   result *= n
return:
   .return (result)
.end
.sub _main :main
   .local int f, i
   # We'll do factorial 0 to 10.
   i = 0
loop:
   f = _fact(i)
   print "Factorial of "
   print i
   print " is "
   print f
   print ".\n"
   inc i
   if i <= 10 goto loop
   # That's it.
   end
.end

我们先来看一下_fact sub。 之前掩盖的一点是为什么子程序的名称都以下划线开头! 这仅仅是为了表明标签是全局的而不是作用于特定子例程的方式。 这很重要,因为标签随后可见于其他子程序。

第一行.param int n指定此子例程采用一个整数参数,并且我们想要引用它传入的寄存器,名称为n,用于其余的子。

除了读行之外,前面的例子中已经看到了以下大部分内容:

result = _fact($I0)

这条单线PIR实际上代表了几行PASM。 首先,寄存器$ I0中的值被移入适当的寄存器,以便由_fact函数作为整数参数接收。 然后设置其他调用相关寄存器,然后调用_fact。 然后,一旦_fact返回,_fact返回的值将被放入给定名称结果的寄存器中。

在_fact sub的.end之前,.return指令用于确保寄存器中保存的值; 命名结果被放入正确的寄存器中,以便通过调用sub的代码将其视为返回值。

在main中对_fact的调用与在sub _fact本身内对_fact的递归调用的方式相同。 新语法的唯一剩余部分是:main,写在.sub _main之后。 默认情况下,PIR假定执行从文件中的第一个子开始。 可以通过将sub标记为以:main开头来更改此行为。

Compiling to PBC

要将PIR编译为字节码,请使用-o标志并指定扩展名为.pbc的输出文件。

parrot -o factorial.pbc factorial.pir

PIR vs. PASM

通过运行PIR可以将PIR转换为PASM:

parrot -o hello.pasm hello.pir

最后一个例子的PASM如下所示:

_main:
   set S30, "Hello world!\n"
   print S30
end

PASM不处理寄存器分配或提供对命名寄存器的支持。 它也没有.sub和.end指令,而是在指令开头用标签替换它们。

↑回到顶部↑
WIKI教程 @2018