/
eval.pir
185 lines (153 loc) · 3.87 KB
/
eval.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
## $Id$
=head1 NAME
src/builtins/eval.pir - Cardinal evaluators
=head1 DESCRIPTION
This file implements methods and functions that evaluate code,
such as C<eval>, C<require>, and C<use>.
=head1 Methods
=over 4
=cut
.namespace []
.sub 'onload' :anon :init :load
$P0 = get_hll_namespace ['Any']
'!EXPORT'('evalfile', 'from'=>$P0)
.end
.namespace ['Any']
.sub 'evalfile' :method :multi(_)
.param pmc options :slurpy :named
.local string filename
filename = self
.local string lang
lang = options['lang']
if lang == 'Parrot' goto lang_parrot
if lang goto lang_compile
lang = 'cardinal'
lang_compile:
.local pmc compiler
compiler = compreg lang
.tailcall compiler.'evalfiles'(filename, 'encoding'=>'utf8', 'transcode'=>'ascii iso8859-1')
lang_parrot:
load_bytecode filename
.return (1)
.end
.namespace []
.sub 'require' :multi(_)
.param string name
.param pmc options :named :slurpy
.local int isfile
.local pmc file
isfile = 0
file = options['file']
if null file goto have_name
isfile = istrue file
have_name:
## see if we loaded this already
.local pmc inc_hash
inc_hash = get_hll_global '%INC'
$I0 = exists inc_hash[name]
unless $I0 goto require_name
$I0 = defined inc_hash[name]
.return ($I0)
require_name:
## loop through $:
.local pmc inc_it
$P0 = get_hll_global '$:'
inc_it = iter $P0
inc_loop:
unless inc_it goto inc_end
.local string basename, realfilename
$S0 = shift inc_it
basename = concat $S0, '/'
basename .= name
unless isfile goto try_module
realfilename = basename
$I0 = stat realfilename, 0
if $I0 goto eval_ruby
goto inc_loop
try_module:
realfilename = concat basename, '.pbc'
$I0 = stat realfilename, 0
if $I0 goto eval_parrot
realfilename = concat basename, '.pir'
$I0 = stat realfilename, 0
if $I0 goto eval_parrot
realfilename = concat basename, '.rb'
$I0 = stat realfilename, 0
if $I0 goto eval_ruby
goto inc_loop
inc_end:
$S0 = concat "Can't find ", basename
$S0 = concat $S0, ' in $:'
'die'($S0)
.return (0)
eval_parrot:
.local pmc result
result = 'evalfile'(realfilename, 'lang'=>'Parrot')
goto done
eval_ruby:
result = 'evalfile'(realfilename, 'lang'=>'cardinal')
done:
inc_hash[name] = realfilename
.return (result)
.end
.sub 'include'
.param pmc module
.local pmc it, item, callerns
$P0 = getinterp
callerns = $P0['namespace';1]
it = iter module
it_loop:
unless it goto it_loop_end
$S0 = shift it
item = module[$S0]
callerns[$S0] = item
goto it_loop
it_loop_end:
.end
.sub 'load'
.param string file
.param pmc args :slurpy
.param pmc options :slurpy :named
$P0 = 'require'(file, 'file'=>1)
.end
.sub 'foreign_load'
.param string lang
.param string module
.local pmc compiler, name, library, imports, callerns, foreignlibns
$P0 = getinterp
callerns = $P0['namespace';1]
'load-language'(lang)
compiler = compreg lang
name = split '/', module
library = compiler.'load_library'(name)
imports = library['symbols']
imports = imports['DEFAULT']
.local pmc it, item
it = iter imports
import_loop:
unless it goto import_loop_end
$S0 = shift it
$P0 = imports[$S0]
callerns[$S0] = $P0
goto import_loop
import_loop_end:
foreignlibns = library['namespace']
if null foreignlibns goto no_foreign_ns
$S0 = pop name
set_hll_global name, $S0, foreignlibns
no_foreign_ns:
.return (library)
.end
=back
=cut
.HLL 'parrot' # work around a stupid parrot bug... argh!!!!!
.sub 'load-language'
.param string lang
load_language lang
.end
.HLL 'cardinal'
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: