This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
wmlsstdlibs.pir
208 lines (173 loc) · 3.83 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
207
208
# Copyright (C) 2006-2008, The Perl Foundation.
# $Id$
=head1 NAME
src/wmlsstdlibs.pir - WMLScript standard libraries
=head1 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
.loadlib 'wmls_ops'
.HLL 'WMLScript', 'wmls_group'
.sub '__onload' :load :anon
# print "__onload (wmlsstdlibs.pir)\n"
load_bytecode 'languages/WMLScript/runtime/wmlslang.pbc'
load_bytecode 'languages/WMLScript/runtime/wmlsfloat.pbc'
load_bytecode 'languages/WMLScript/runtime/wmlsstring.pbc'
load_bytecode 'languages/WMLScript/runtime/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
=head2 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]
.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_1
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
# push_eh _handler_2
.local pmc entry
$S0 = url
$S0 .= ':'
$S0 .= function
entry = get_hll_global $S0
if_null entry, _handler_2
.return (entry)
_handler_1:
.local pmc e
.local string msg
.get_results (e)
msg = e
print msg
print "\n"
$S0 = "verification failed (can't translate '"
$S0 .= url
$S0 .= "')"
die $S0
_handler_2:
$S0 = "external function '"
$S0 .= function
$S0 .= "' not found in '"
$S0 .= url
$S0 .= "'"
die $S0
L1:
die "unable to load compilation unit"
.end
.sub 'load_script'
.param string filename
.local pmc pio
.local string content
pio = new 'ParrotIO'
push_eh _handler
content = pio.'slurp'(filename)
if content goto L1
$S0 = err
print "Can't slurp '"
print filename
print "' ("
print $S0
print ")\n"
L1:
_handler:
.return (content)
.end
.sub 'save_pbc'
.param string pbc_out
.param string filename
.local string output
.local pmc fh
output = concat filename, '.pbc'
fh = open output, '>'
if fh goto L1
$S0 = err
print "Can't open '"
print output
print "' ("
print $S0
print ")\n"
goto L2
L1:
print fh, pbc_out
close fh
L2:
.return (output)
.end
.sub 'save_pir'
.param string gen_pir
.param string filename
.local string output
.local pmc fh
output = concat filename, '.pir'
fh = open output, '>'
if fh goto L1
$S0 = err
print "Can't open '"
print output
print "' ("
print $S0
print ")\n"
goto L2
L1:
print fh, gen_pir
close fh
L2:
.end
=back
=head1 AUTHORS
Francois Perrad.
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: