/
01-sub.pir
118 lines (97 loc) · 2.82 KB
/
01-sub.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (C) 2006-2008, Parrot Foundation.
=for doc
The PAST that is set up in this example
roughly represents following Perl 6 code:
sub foo
{
my $a = 4;
my $b = $a + 1;
say($b);
}
=cut
.namespace []
.sub '__onload' :init
load_bytecode "dumper.pbc"
load_bytecode 'PGE.pbc'
load_bytecode 'PGE/Text.pbc'
load_bytecode 'PGE/Util.pbc'
load_bytecode 'PGE/Dumper.pbc'
load_bytecode 'PCT.pbc'
.end
.sub 'main' :main
.param pmc args
.local pmc block
block = new ['PAST';'Block']
block.'init'( 'blocktype' => 'declaration', 'name' => 'foo' )
block.'symbol'( '$a', 'scope' => 'lexical' )
block.'symbol'( '$b', 'scope' => 'lexical' )
.local pmc stmts
stmts = new ['PAST';'Stmts']
stmts.'init'()
stmts.'attr'( 'source', 'my $a = 4; my $b = $a + 1; say( $b );', 1 )
block.'push'(stmts)
# $a = 4
$P0 = new ['PAST';'Val']
$P0.'init'( 'value' => '4', 'returns' => 'Integer' )
$P0.'attr'( 'source', '4', 1 )
$P1 = new ['PAST';'Var']
$P1.'init'( 'name' => '$a', 'viviself' => 'Undef', 'isdecl' => 1 )
$P1.'attr'( 'source', '$a', 1 )
$P2 = new ['PAST';'Op']
$P2.'init'( $P1, $P0, 'pasttype' => 'copy', 'name' => 'infix:=', 'lvalue' => 1 )
$P2.'attr'( 'source', '=', 1 )
stmts.'push'($P2)
# $b = $a + 1
$P0 = new ['PAST';'Var']
$P0.'init'( 'name' => '$a', 'viviself' => 'Undef' )
$P1 = new ['PAST';'Val']
$P1.'init'( 'value' => '1', 'returns' => 'Integer')
$P2 = new ['PAST';'Op']
$P2.'init'( $P0, $P1, 'name' => 'infix:+', 'pirop' => 'add')
$P3 = new ['PAST';'Var']
$P3.'init'( 'name' => '$b', 'viviself' => 'Undef', 'isdecl' => 1 )
$P4 = new ['PAST';'Op']
$P4.'init'( $P3, $P2, 'name' => 'infix:=', 'pasttype' => 'copy')
$P4.'attr'( 'source', '=', 1 )
stmts.'push'($P4)
# say($b)
$P0 = new ['PAST';'Var']
$P0.'init'( 'name' => '$b' )
$P1 = new ['PAST';'Op']
$P1.'init'( $P0, 'name' => 'say', 'pasttype' => 'call' )
stmts.'push'($P1)
# set up compiler, preliminary stages are removed because we
# already have a PAST data structure
.local pmc astcompiler
astcompiler = new [ 'PCT';'HLLCompiler' ]
astcompiler.'removestage'('parse')
astcompiler.'removestage'('past')
=for development
# _dumper( block, 'block' )
# compile to PIR and display
$S99 = astcompiler.'compile'(block, 'target' => 'pir')
print $S99
=cut
#compile to bytecode and execute
$P99 = astcompiler.'compile'(block)
$P99()
.end
.sub 'say'
.param pmc args :slurpy
if null args goto end
.local pmc it
it = iter args
loop:
unless it goto end
$P0 = shift it
print $P0
goto loop
end:
print "\n"
.return ()
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: