This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
/
Compiler.pm
153 lines (128 loc) · 4.22 KB
/
Compiler.pm
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
INIT {
pir::load_bytecode('PCT/HLLCompiler.pbc');
}
class HLL::Compiler is PCT::HLLCompiler {
has $!language;
INIT {
HLL::Compiler.language('parrot');
}
my sub value_type($value) {
pir::isa($value, 'NameSpace')
?? 'namespace'
!! (pir::isa($value, 'Sub') ?? 'sub' !! 'var')
}
method get_exports($module, :$tagset, *@symbols) {
# convert a module name to something hash-like, if needed
if (!pir::does($module, 'hash')) {
$module := self.get_module($module);
}
$tagset := $tagset // (@symbols ?? 'ALL' !! 'DEFAULT');
my %exports;
my %source := $module{'EXPORT'}{~$tagset};
if !pir::defined(%source) {
%source := $tagset eq 'ALL' ?? $module !! {};
}
if @symbols {
for @symbols {
my $value := %source{~$_};
%exports{value_type($value)}{$_} := $value;
}
}
else {
for %source {
my $value := $_.value;
%exports{value_type($value)}{$_.key} := $value;
}
}
%exports;
}
method get_module($name) {
my @name := self.parse_name($name);
@name.unshift(pir::downcase($!language));
pir::get_root_namespace__PP(@name);
}
method language($name?) {
if $name {
$!language := $name;
pir::compreg__0sP($name, self);
}
$!language;
}
method load_module($name) {
my $base := pir::join('/', self.parse_name($name));
my $loaded := 0;
try { pir::load_bytecode("$base.pbc"); $loaded := 1 };
unless $loaded { pir::load_bytecode("$base.pir"); $loaded := 1 }
self.get_module($name);
}
method import($target, %exports) {
for %exports {
my $type := $_.key;
my %items := $_.value;
if pir::can(self, "import_$type") {
for %items { self."import_$type"($target, $_.key, $_.value); }
}
elsif pir::can($target, "add_$type") {
for %items { $target."add_$type"($_.key, $_.value); }
}
else {
for %items { $target{~$_.key} := $_.value; }
}
}
}
method autoprint($value) {
pir::say(~$value)
unless pir::tell__IP(pir::getstdout__P()) > $*AUTOPRINTPOS;
}
method interactive(*%adverbs) {
my $target := pir::downcase(%adverbs<target>);
pir::printerr__vS(self.commandline_banner);
my $stdin := pir::getstdin__P();
my $encoding := ~%adverbs<encoding>;
if $encoding && $encoding ne 'fixed_8' {
$stdin.encoding($encoding);
}
while 1 {
last unless $stdin;
my $prompt := self.commandline_prompt // '> ';
my $code := $stdin.readline_interactive(~$prompt);
last if pir::isnull($code);
# Set the current position of stdout for autoprinting control
my $*AUTOPRINTPOS := pir::tell__IP(pir::getstdout__P());
if $code {
$code := $code ~ "\n";
my $output;
{
$output := self.eval($code, |%adverbs);
CATCH {
pir::print($! ~ "\n");
next;
}
};
next if pir::isnull($output);
if !$target {
self.autoprint($output);
} elsif $target eq 'pir' {
pir::say($output);
} else {
self.dumper($output, $target, |%adverbs);
}
}
}
}
method eval($code, *@args, *%adverbs) {
my $output;
$output := self.compile($code, |%adverbs);
if !pir::isa($output, 'String')
&& %adverbs<target> eq '' {
my $outer_ctx := %adverbs<outer_ctx>;
if pir::defined($outer_ctx) {
$output[0].set_outer($outer_ctx<current_sub>);
}
pir::trace(%adverbs<trace>);
$output := $output(|@args);
pir::trace(0);
}
$output;
}
}