This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
luaperl.pir
165 lines (134 loc) · 2.75 KB
/
luaperl.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
157
158
159
160
161
162
163
164
165
# Copyright (C) 2007-2009, Parrot Foundation.
# $Id$
=head1 Lua Perl Compiler
=head2 Description
Encapsulation of the compiler written in Perl as a Parrot registered compiler.
This encapsulation is used to implement some Lua functions :
=over 4
=item dofile
=item loadfile
=item loadstring
=back
It's a temporary work. Waiting for the real PIR compiler/interpreter.
=head3 Methods
=over 4
=cut
.namespace []
.sub '__onload' :anon :load
$P0 = loadlib 'os'
$P0 = newclass [ 'Lua'; 'PerlCompiler' ]
new $P1, $P0
compreg 'lua', $P1
.end
.namespace [ 'Lua'; 'PerlCompiler' ]
.sub 'unlink' :anon
.param string filename
new $P0, 'OS'
push_eh _handler
$P0.'rm'(filename)
pop_eh
_handler:
.return ()
.end
.sub 'save_lua' :anon
.param string code
.param string filename
.local pmc fh
fh = new 'FileHandle'
push_eh _handler
fh.'open'(filename, 'w')
pop_eh
fh.'print'(code)
fh.'close'()
.return ()
_handler:
.local pmc e
.get_results (e)
$S0 = "Can't open '"
$S0 .= filename
$S0 .= "' ("
$S1 = err
$S0 .= $S1
$S0 .= ")\n"
e = $S0
rethrow e
.end
.sub 'load_script' :anon
.param string filename
.local pmc fh
.local string content
fh = new 'FileHandle'
push_eh _handler
content = fh.'readall'(filename)
pop_eh
.return (content)
_handler:
.local pmc e
.get_results (e)
$S0 = "Can't slurp '"
$S0 .= filename
$S0 .= "' ("
$S1 = err
$S0 .= $S1
$S0 .= ")\n"
e = $S0
rethrow e
.end
.sub 'compile_file' :anon
.param string filename
.const string out = 'luac.out'
# $I0 = spawnw 'cd'
$I0 = index filename, '.'
$S1 = substr filename, 0, $I0
$S1 .= '.pir'
unlink($S1)
unlink(out)
$S0 = 'perl luac.pl '
$S0 .= filename
$S0 .= ' > '
$S0 .= out
$I0 = spawnw $S0
.local string pir
push_eh _handler
pir = load_script($S1)
pop_eh
unlink(out) # cleaning up the temporary file
.local pmc pir_comp
pir_comp = compreg 'PIR'
$P0 = pir_comp(pir)
.return ($P0)
_handler:
.local pmc e
.get_results (e)
$P0 = new 'FileHandle'
$S0 = $P0.'readall'(out)
printerr $S0
unlink(out) # cleaning up the temporary file
rethrow e
.end
=item compile(pmc source)
Compile C<source>.
=cut
.sub 'compile' :method
.param string source
$S0 = 'tmp'
$P0 = getprop 'num', self
unless null $P0 goto L1
new $P0, 'Integer'
set $P0, 0
L1:
inc $P0
$S1 = $P0
$S0 .= $S1
setprop self, 'num', $P0
$S1 = $S0 . '.lua'
save_lua(source, $S1)
.tailcall compile_file($S1)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: