-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix2pir.pl
executable file
·61 lines (48 loc) · 1.01 KB
/
infix2pir.pl
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
#!/usr/bin/perl -w
use strict;
use Parse::Eyapp;
use Infix;
use I2PIR;
sub TERMINAL::info { $_[0]{attr} }
sub build_dec {
our %s;
my $dec = "";
if (%s) {
my @vars = sort keys %s;
my $last = pop @vars;
$dec .= "$_, " for @vars;
$dec .= $last;
}
return $dec;
}
sub peephole_optimization {
$_[0] =~ s{(\$N\d+)\s*=\s*(.*\n)\s* ([a-zA-Z_]\w*)\s*=\s*\1}{$3 = $2}gx;
}
sub output_code {
my ($trans, $dec) = @_;
# Indent
$$trans =~ s/^/\t/gm;
# Output the code
print << "TRANSLATION";
.sub 'main' :main
\t.local num $$dec
$$trans
.end
TRANSLATION
}
################# main ######################
my $filename = shift;
my $parser = Infix->new();
$parser->slurp_file($filename);
my $t = $parser->YYParse() || exit(1);
# Machine independent optimizations
$t->s(our @algebra);
# Address Assignment
our $reg_assign;
$reg_assign->s($t);
# Translate to PARROT
$t->bud(our @translation);
# variable declarations
my $dec = build_dec();
peephole_optimization($t->{tr});
output_code(\$t->{tr}, \$dec);