forked from parrot/parrot
/
ops2pm.pl
120 lines (86 loc) · 1.94 KB
/
ops2pm.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
#! /usr/bin/perl -w
#
# ops2pm.pl
#
# Generate a Perl module from the operation definitions in an .ops file.
#
use strict;
use lib 'lib';
use Parrot::OpsFile;
use Data::Dumper;
$Data::Dumper::Useqq = 1;
#$Data::Dumper::Terse = 1;
#$Data::Dumper::Indent = 0;
sub Usage {
print STDERR <<_EOF_;
usage: $0 input.ops [input2.ops ...]
_EOF_
exit;
}
#
# Process command-line argument:
#
Usage() unless @ARGV;
my $file = 'core.ops';
my $base = $file;
$base =~ s/\.ops$//;
my $package = "${base}";
my $moddir = "lib/Parrot/OpLib";
my $module = "lib/Parrot/OpLib/${package}.pm";
#
# Read the first input file:
#
use Data::Dumper;
$file = shift @ARGV;
die "$0: Could not read ops file '$file'!\n" unless -e $file;
my $ops = new Parrot::OpsFile $file;
my %seen;
for $file (@ARGV) {
if ($seen{$file}) {
print STDERR "$0: Ops file '$file' mentioned more than once!\n";
next;
}
$seen{$file} = 1;
die "$0: Could not read ops file '$file'!\n" unless -e $file;
my $temp_ops = new Parrot::OpsFile $file;
for(@{$temp_ops->{OPS}}) {
push @{$ops->{OPS}},$_;
}
}
my $cur_code = 0;
for(@{$ops->{OPS}}) {
$_->{CODE}=$cur_code++;
}
my $num_ops = scalar $ops->ops;
my $num_entries = $num_ops + 1; # For trailing NULL
#
# Open the output file:
#
if (! -d $moddir) {
mkdir($moddir, 0755) or die "$0: Could not mkdir $moddir: $!!\n";
}
open MODULE, ">$module"
or die "$0: Could not open module file '$module' for writing: $!!\n";
#
# Print the preamble for the MODULE file:
#
my $version = $ops->version;
my $preamble = <<END_C;
#! perl -w
#
# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
#
# This file is generated automatically from '$file'.
# Any changes made here will be lost!
#
use strict;
package Parrot::OpLib::$package;
use vars qw(\$VERSION \$ops);
\$VERSION = "$version";
END_C
print MODULE $preamble;
print MODULE Data::Dumper->Dump([[ $ops->ops ]], [ qw($ops) ]);
print MODULE <<END_C;
1;
END_C
exit 0;