This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
luac2pir.pir
111 lines (96 loc) · 1.99 KB
/
luac2pir.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
# Copyright (C) 2008-2009, Parrot Foundation.
# $Id$
=head1 Lua 5.1 VM bytecode to Parrot PIR Translator
=head2 Synopsis
parrot luac2pir.pir file.luac
=head2 Description
B<luac2pir> translates a Lua 5.1 VM bytecode file to Parrot PIR.
=head2 See Also
luad
=cut
.loadlib 'lua_group'
.sub 'main' :main
.param pmc argv
.local int argc
.local string progname
.local string filename
.local string content
argc = elements argv
if argc != 2 goto USAGE
progname = shift argv
filename = shift argv
push_eh _handler
content = load_file(filename)
.local pmc script
new $P0, 'LuaBytecode'
script = $P0.'undump'(content)
.local string gen_pir
gen_pir = script.'translate'()
save_pir(gen_pir, filename)
pop_eh
end
USAGE:
.local pmc stderr
stderr = getstderr
print stderr, "Usage: parrot luac2pir.pir filename\n"
exit -1
_handler:
.local pmc e
.local string msg
.get_results (e)
msg = e
say msg
end
.end
.sub 'load_file' :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 'save_pir'
.param string gen_pir
.param string filename
.local string output
.local pmc fh
fh = new 'FileHandle'
output = concat filename, '.pir'
push_eh _handler
fh.'open'(output, 'w')
pop_eh
fh.'print'(gen_pir)
fh.'close'()
.return ()
_handler:
.local pmc e
.get_results (e)
$S0 = "Can't open '"
$S0 .= output
$S0 .= "' ("
$S1 = err
$S0 .= $S1
$S0 .= ")\n"
e = $S0
rethrow e
.end
.include 'lua/lib/luabytecode.pir'
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: