This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
wmlsstdlibs.pir
206 lines (175 loc) · 3.73 KB
/
wmlsstdlibs.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
=head1 WMLScript standard libraries
=head2 Description
See "WMLScript Standard Libraries Specification".
=over 4
=item * Lang : F<languages/wmlscript/runtime/wmlslang.pir>
=item * Float : F<languages/wmlscript/runtime/wmlsfloat.pir>
=item * String : F<languages/wmlscript/runtime/wmlsstring.pir>
=item * Console : F<languages/wmlscript/runtime/wmlsconsole.pir>
=back
=cut
.sub 'load_stdlibs'
load_bytecode 'wmlslang.pbc'
load_bytecode 'wmlsfloat.pbc'
load_bytecode 'wmlsstring.pbc'
load_bytecode 'wmlsconsole.pbc'
new $P0, 'Hash'
$P1 = getLang()
$P0[0] = $P1
$P1 = getFloat()
$P0[1] = $P1
$P1 = getString()
$P0[2] = $P1
$P1 = getConsole()
$P0[99] = $P1
set_hll_global '@stdlibs', $P0
.end
.sub 'not_implemented'
die "not implemented"
.end
=head3 Helpers
=over 4
=item C<find_lib(int lindex, int findex)>
helper for CALL_LIB* opcodes.
=cut
.sub 'find_lib'
.param int lindex
.param int findex
$P0 = get_hll_global '@stdlibs'
push_eh _handler
$P1 = $P0[lindex]
$P2 = $P1[findex]
pop_eh
.return ($P2)
_handler:
.const 'Sub' not_implemented = 'not_implemented'
.return (not_implemented)
.end
=item C<find_lib_url(string url, string function)>
helper for CALL_URL* opcodes.
=cut
.sub 'find_lib_url'
.param string url
.param string function
.local string content
content = load_script(url)
unless content goto L1
.local pmc loader
.local pmc script
new loader, 'WmlsBytecode'
push_eh _handler
script = loader.'load'(content)
script['filename'] = url
.local string gen_pir
gen_pir = script.'translate'()
.local pmc pir_comp
.local pmc pbc_out
pir_comp = compreg 'PIR'
pbc_out = pir_comp(gen_pir)
$P0 = pbc_out[0]
$P0()
pop_eh
.local pmc entry
$S0 = url
$S0 .= ':'
$S0 .= function
entry = get_hll_global $S0
if_null entry, L2
.return (entry)
L2:
$S0 = "external function '"
$S0 .= function
$S0 .= "' not found in '"
$S0 .= url
$S0 .= "'"
die $S0
L1:
die "unable to load compilation unit"
_handler:
.local pmc e
.get_results (e)
$S0 = e
$S0 .= "\nverification failed (can't translate '"
$S0 .= url
$S0 .= "')"
e = $S0
rethrow e
.end
.sub 'load_script'
.param string filename
.local pmc fh
.local string content
fh = new 'FileHandle'
push_eh _handler
content = fh.'readall'(filename)
pop_eh
.return (content)
_handler:
$S0 = "Can't slurp '"
$S0 .= filename
$S0 .= "' ("
$S1 = err
$S0 .= $S1
$S0 .= ")\n"
printerr $S0
.return ('')
.end
.sub 'save_pbc'
.param string pbc_out
.param string filename
.local string output
.local pmc fh
fh = new 'FileHandle'
output = concat filename, '.pbc'
push_eh _handler
fh.'open'(output, 'w')
pop_eh
fh.'print'(pbc_out)
fh.'close'()
.return (output)
_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
.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
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: