This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
stdlibs.pl
147 lines (116 loc) · 3 KB
/
stdlibs.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
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
#! perl
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
use strict;
use warnings;
use Getopt::Long;
# Grab parameters.
my ( $cfg_file, $output_file );
GetOptions( 'output=s' => \$output_file, ) or usage();
$cfg_file = shift @ARGV;
usage() if !$cfg_file || @ARGV;
# Parse rules file.
my $cfg = parse_cfg($cfg_file);
# Generate code.
my $pir = generate_pir($cfg);
# Finally, write generated PIR to output file.
open my $fh, '>', $output_file
or die "Unable to open $output_file: $!\n";
print $fh $pir;
close $fh;
##############################################################################
# Subroutines.
##############################################################################
# Parse configuration file and build a data structure.
# ####################################################
sub parse_cfg {
# Get filename and open the file.
my ($filename) = @_;
open my $fh, '<', $filename
or die "Unable to open $filename: $!\n";
my $lib = {};
my $func;
while (<$fh>) {
if (/^#.*$/) {
# warn "Comment $_";
}
elsif (/^\s*$/) {
# warn "Empty\n";
}
elsif (/^\@([A-Z_a-z][0-9A-Z_a-z]*)\s+([0-9]+)\s*$/) {
# warn "Lib $1 $2\n";
$func = {};
$lib->{$2} = [ $1, $func ];
}
elsif (/^([A-Z_a-z][0-9A-Z_a-z]*)\s+([0-9]+)\s+([0-9]+)\s*$/) {
# warn "Fct $1 $2 $3\n";
$func->{$2} = [ $1, $3 ];
}
else {
warn "cfg? $_";
}
}
close $fh;
return $lib;
}
# Generate the code.
# ##################
sub generate_pir {
my ($cfg) = @_;
# Emit the initial PIR.
my $pir = <<"PIRCODE";
# ex: set ro:
# DO NOT EDIT THIS FILE
# Generated by $0 from src/wmlslibs.cfg
.sub '__onload' :anon :load
.local pmc stdlibs
.local pmc lib
new stdlibs, 'Hash'
PIRCODE
while ( my ( $num_lib, $lib ) = each %{$cfg} ) {
my ( $name, $h_lib ) = @{$lib};
$pir .= "\n # $name\n";
$pir .= " new lib, 'Hash'\n";
while ( my ( $num_func, $func ) = each %{$h_lib} ) {
my ( $name, $nb ) = @{$func};
$pir .= " lib[$num_func] = $nb\t# $name\n";
}
$pir .= " stdlibs[$num_lib] = lib\n";
}
# Emit the final PIR.
$pir .= <<'PIRCODE';
set_hll_global 'stdlibs', stdlibs
.end
.sub 'get_nb_arg_lib'
.param int lib
.param int func
.local pmc stdlib
stdlib = get_hll_global 'stdlibs'
$P0 = stdlib[lib]
$I0 = $P0[func]
.return ($I0)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
PIRCODE
# Return generated code.
return $pir;
}
# Usage message.
# ##############
sub usage {
print <<'USAGE';
Usage:
perl build/stdlibs.pl --output src/stdlibs.pir src/wmlslibs.cfg
USAGE
exit(1);
}
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: