-
Notifications
You must be signed in to change notification settings - Fork 0
/
pjs.pir
178 lines (147 loc) · 3.57 KB
/
pjs.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
.HLL 'Pjs', 'pjs_group'
.loadlib 'pjs_group_ops'
.sub read_file
.param string path
.local pmc file
if path == '-' goto from_stdin
file = open path, '<'
if null file goto not_found
unless file goto not_found
goto next
from_stdin:
file = getstdin
next:
$S0 = file.'slurp'('')
close file
.return ($S0)
not_found:
$S0 = 'File not found: '
$S0 = $S0 . path
$P0 = new 'Exception'
$P0[0] = $S0
throw $P0
.end
.sub main :main :anon
.param pmc args
.local int argc
argc = args
if argc < 2 goto repl
$P0 = args[1]
$S0 = $P0
if $S0 == '--compile' goto compile_standalone
if $S0 == '-c' goto compile_standalone
if $S0 == '--compile-eval' goto compile_eval
if $S0 == '-C' goto compile_eval
if $S0 == '--help' goto help
if $S0 == '-h' goto help
run:
load_bytecode 'languages/pjs/lib/jscore.pbc'
# run each file
.local int i
.local string path
i = 1
loop:
unless i < argc goto end_loop
path = args[i]
inc i
if path == '-f' goto loop # skip -f (spidermonkey compatibility)
run_file(path)
goto loop
end_loop:
.return()
compile_error:
.get_results($P0, $S0)
say $S0
.return()
compile_eval:
if argc < 3 goto help
$P0 = args[2]
$S0 = $P0
'compile'($S0, 0)
.return ()
compile_standalone:
if argc < 3 goto help
$P0 = args[2]
$S0 = $P0
'compile'($S0, 1)
.return ()
help:
$P0 = args[0]
$S0 = $P0
print "\nUsage: parrot "
print $S0
print " <file.js> Execute file.js\n"
print " parrot "
print $S0
print " <option> <file.js> See further\n\n\n"
print "Options:\n\n"
print " --compile, -c Compile file.js as a standalone PIR\n"
print " (it is then executable, loadable)\n\n"
print " --compile-eval, -C Compile file.js for evaluation\n"
print " (only useful for debugging purposes)\n\n"
print " --help, -h Show this help message\n\n"
.return()
repl:
repl()
.end
.sub run_file
.param string path
$S0 = 'read_file'(path)
$S0 = pjs_compile $S0, 1
.local pmc os
.local string dir, curr_dir, file_name
os = new 'OS'
curr_dir = os.'cwd'()
(dir, file_name) = 'split_directory_file'(path)
os.'chdir'(dir)
.local pmc comp, execute
comp = compreg 'PIR'
execute = comp($S0)
#pop_eh
execute()
os.'chdir'(curr_dir)
.return()
.end
.sub compile :anon
.param string path
.param int is_standalone
.local string s
s = 'read_file'(path)
$S0 = pjs_compile s, is_standalone
say $S0
.end
.sub repl
load_bytecode 'languages/pjs/lib/jscore.pbc'
.local pmc global_env, global_obj
global_env = get_hll_global 'global_env'
global_obj = global_env[0]
.local pmc pio
pio = getstdin
$I0 = pio.'set_readline_interactive'(1)
if $I0 >= 0 goto loop
printerr "set_readline_interactive failed: "
goto ex
loop:
$P1 = pio.'readline'('pjs> ')
if null $P1 goto ex
$S0 = $P1
push_eh eval_error
$S0 = pjs_compile $S0, 0
$P0 = eval_js_pir($S0, global_env)
print_jsvalue($P0)
pop_eh
goto loop
eval_error:
.get_results($P0, $S0)
say $S0
goto loop
ex:
.end
.sub print_jsvalue
.param pmc p
if null p goto end
$I0 = isa p, 'PjsUndefined'
if $I0 goto end
say p
end:
.end