tagged release 0.6.4
[parrot.git] / languages / lua / src / lib / luaperl.pir
blob61282762bfe7ea8d6f009f38aa969bf7ca0e4b97
1 # Copyright (C) 2007-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 lib/luaperl.pir
8 =head1 DESCRIPTION
10 Encapsulation of the compiler written in Perl as a Parrot registered compiler.
12 This encapsulation is used to implement some Lua functions :
14 =over 4
16 =item dofile
18 =item loadfile
20 =item loadstring
22 =back
24 It's a temporary work. Waiting for the real PIR compiler/interpreter.
26 =head2 Methods
28 =over 4
30 =cut
32 .namespace []
34 .sub '__onload' :anon :load :init
35     $P0 = newclass [ 'Lua::PerlCompiler' ]
36     new $P1, $P0
37     compreg 'Lua', $P1
38 .end
40 .namespace [ 'Lua::PerlCompiler' ]
42 .sub 'unlink' :anon
43     .param string filename
44     new $P0, 'OS'
45     push_eh _handler
46     $P0.'rm'(filename)
47   _handler:
48     .return ()
49 .end
51 .sub 'save_lua' :anon
52     .param string code
53     .param string filename
54     .local pmc fh
55     fh = open filename, '>'
56     if fh goto L1
57     $S0 = err
58     print "Can't open '"
59     print filename
60     print "' ("
61     print $S0
62     print ")\n"
63     goto L2
64   L1:
65     print fh, code
66     close fh
67   L2:
68 .end
71 .sub 'load_script' :anon
72     .param string filename
73     .local pmc pio
74     .local string content
75     pio = getclass 'ParrotIO'
76     push_eh _handler
77     content = pio.'slurp'(filename)
78     if content goto L1
79     $S0 = err
80     print "Can't slurp '"
81     print filename
82     print "' ("
83     print $S0
84     print ")\n"
85   L1:
86   _handler:
87     .return (content)
88 .end
91 .sub 'compile_file' :anon
92     .param string filename
93     .const string out = 'luac.out'
94 #    $I0 = spawnw 'cd'
95     $I0 = index filename, '.'
96     $S1 = substr filename, 0, $I0
97     $S1 .= '.pir'
98     unlink($S1)
99     unlink(out)
100     $S0 = 'perl -Ilanguages/lua languages/lua/luac.pl '
101     $S0 .= filename
102     $S0 .= ' > '
103     $S0 .= out
104     $I0 = spawnw $S0
105     .local string pir
106     pir = load_script($S1)
107     if pir goto L1
108     $P0 = getclass 'ParrotIO'
109     $S0 = $P0.'slurp'(out)
110     unlink(out)  # cleaning up the temporary file
111     die $S0
112   L1:
113     unlink(out)  # cleaning up the temporary file
114     .local pmc pir_comp
115     pir_comp = compreg 'PIR'
116     $P0 = pir_comp(pir)
117     .return ($P0)
118 .end
121 =item compile(pmc source)
123 Compile C<source>.
125 =cut
127 .sub 'compile' :method
128     .param string source
130     $S0 = 'tmp'
131     $P0 = getprop 'num', self
132     unless null $P0 goto L1
133     new $P0, 'Integer'
134     set $P0, 0
135   L1:
136     inc $P0
137     $S1 = $P0
138     $S0 .= $S1
139     setprop self, 'num', $P0
140     $S1 = $S0 . '.lua'
141     save_lua(source, $S1)
142     .return compile_file($S1)
143 .end
145 =back
147 =head1 AUTHOR
149 Francois Perrad
151 =cut
154 # Local Variables:
155 #   mode: pir
156 #   fill-column: 100
157 # End:
158 # vim: expandtab shiftwidth=4 ft=pir: