/
main.pir
125 lines (95 loc) · 2.44 KB
/
main.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
=head1 NAME
main.pir -- code for running Perl 6 from command line
=head1 DESCRIPTION
See Perl6.pir for a synopsis.
=head1 Functions
=over 4
=item C<main(PMC args)>
Handles program control when the Perl6.pbc file is executed
directly from the Parrot command line. It calls all of
the submodule :load routines, then processes commands from
the file specified on the command line or standard input.
The --target= command line option allows the compilation
to display a parse tree or abstract syntax tree in lieu of
executing program statements.
=cut
.namespace [ 'Perl6' ]
.sub 'main' :main
.param pmc args
$P0 = find_global 'Perl6', '__onload'
$P0()
$P0 = find_global 'Perl6::Grammar', '__onload_parse'
$P0()
load_bytecode 'dumper.pbc'
load_bytecode 'PGE/Dumper.pbc'
load_bytecode 'Getopt/Obj.pbc'
.local pmc getopts, opts
.local string arg0
arg0 = shift args
getopts = new 'Getopt::Obj'
getopts.'notOptStop'(1)
push getopts, 'target=s'
push getopts, 'dump-optable'
push getopts, 'help|h'
opts = getopts.'get_options'(args)
$S0 = opts['dump-optable']
if $S0 goto dump_optable
$S0 = opts['help']
if $S0 goto usage
.local string target
target = opts['target']
.local pmc perl6
perl6 = compreg 'Perl6'
$I0 = elements args
if $I0 > 0 goto file_arg
.local pmc stdin
stdin = getstdin
push stdin, 'utf8'
stmt_loop:
.local string stmt
stmt = readline stdin
unless stmt goto end
bsr perl6_eval
goto stmt_loop
file_arg:
.local string filename
filename = args[1]
$P0 = open filename, '<'
unless $P0 goto err_no_file
push $P0, 'utf8'
stmt = read $P0, 65535
close $P0
bsr perl6_eval
goto end
perl6_eval:
$P0 = perl6(stmt, 'target' => target)
if target == 'PIR' goto dump_pir
if target goto dump_object
goto dump_object
$P0()
ret
dump_pir:
print $P0
ret
dump_object:
'_dumper'($P0, target)
ret
err_no_file:
print 'Cannot open file '
print filename
print "\n"
end
dump_optable:
$P0 = find_global "Perl6::Grammar", "$optable"
"_dumper"($P0, "$Perl6::Grammar::optable")
goto end
usage:
print "usage: perl6.pbc [--dump-optable] [--target=OUT] [file]\n"
end:
.end
=back
=head1 LICENSE
Copyright (c) 2006 The Perl Foundation
This is free software; you may redistribute it and/or modify
it under the same terms as Parrot.
=cut