/
perl5.pir
157 lines (111 loc) · 3.27 KB
/
perl5.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
=head1 TITLE
perl5.pir - A Perl 5 compatibility interface for Parrot.
=head2 Description
This file sets up something that will work with HLLCompiler and
allow us to eval Perl 5 code.
=head2 Functions
=over 4
=item onload()
Creates the compiler using a C<PCT::HLLCompiler> object.
=cut
.loadlib 'blizkost_group'
.namespace [ 'Perl5' ; 'Compiler' ]
.sub 'onload' :anon :load :init
load_bytecode 'PCT.pbc'
$P0 = get_root_global ['parrot'], 'P6metaclass'
$P1 = $P0.'new_class'('Perl5::Compiler', 'parent'=>'PCT::HLLCompiler')
$P1.'language'('perl5')
$P0 = split ' ', 'make_interp'
setattribute $P1, '@stages', $P0
.end
=item make_interp()
=cut
.sub 'make_interp' :method
.param string source
.param pmc adverbs :slurpy :named
# We maintain a persistent P5Interpreter per Parrot interpreter. Should
# be a reasonable strategy, or at least we'll try it until somebody can
# say why it's wrong and give us a better one.
.local pmc parrot_interp, p5i
parrot_interp = getinterp
p5i = getprop '$!p5i', parrot_interp
unless null p5i goto have_interp
p5i = new 'P5Interpreter'
setprop parrot_interp, '$!p5i', p5i
have_interp:
# Set current "to eval" source code, and we're done.
p5i = source
.return (p5i)
.end
=item eval
=cut
.sub 'eval' :method
.param pmc code
.param pmc args :slurpy
.param pmc adverbs :slurpy :named
$P0 = self.'compile'(code, adverbs :flat :named)
($P1 :slurpy, $P2 :slurpy :named) = $P0()
.return ($P1 :flat, $P2 :flat :named)
.end
=item !return_value_helper
This is a little helper routine to save us having to mess around with the
Parrot calling conventions to be able to return a value from some eval'd
code.
=cut
.sub '!return_value_helper'
# Get the attached return value.
$P0 = getinterp
$P0 = $P0['sub']
$P0 = getprop '$!ret_val', $P0
.return ($P0)
.end
=item !return_value_helper_arr
Like !return_value_helper but takes an array of many return values and
flattens it.
=cut
.sub '!return_value_helper_arr'
$P0 = getinterp
$P0 = $P0['sub']
$P0 = getprop '$!ret_val', $P0
.return ($P0 :flat)
.end
=item load_library
Implements the HLLCompiler library loading interface.
=cut
.sub 'load_library' :method
.param pmc name
.param pmc extra :named :slurpy
# Construct a use into some dummy package.
# XXX Add a number to make it unique per use.
.local string package_name, name_str
package_name = 'BLIZKOST::TEMP::IMPORT'
$S0 = concat 'package ', package_name
$S0 = concat ";\nuse "
name_str = join '::', name
$S0 = concat name_str
self.'eval'($S0)
# Make namespace wrapper PMC.
.local pmc ns_wrapper, p5i
$P0 = getinterp
p5i = getprop '$!p5i', $P0
ns_wrapper = new ['P5Namespace'], p5i
ns_wrapper = name_str
# Set up imports. XXX No import symbols yet, todo.
.local pmc imports
imports = new ['Hash']
$P0 = new ['Hash']
imports['DEFAULT'] = $P0
# Construct library info hash.
.local pmc result
result = new ['Hash']
result['namespace'] = ns_wrapper
result['symbols'] = imports
.return (result)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: