-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.pl
executable file
·88 lines (64 loc) · 1.16 KB
/
preprocess.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
#!/usr/bin/perl
use warnings;
use Getopt::Long;
my %defines;
my @search = (
'.',
'./stdlib',
);
my @files;
sub add_file { push @files, $_[0]; }
GetOptions (
'search=s' => \@search,
'defines=s' => \%defines,
'<>' => \&add_file,
);
sub cmd_include {
while(my($key, $value) = each(%defines)) { $deflist .= "-d $key='$value' " }
for my $module (@_) {
my $path;
my $found = 0;
for(@search) {
$path = "$_/$module.rl";
if (-e $path) {
system("perl $0 $deflist $path");
$found = 1;
last;
}
}
die "Could not find module '$module' in any search paths" unless $found;
}
}
sub cmd_if {
my ($condition, @rest) = @_;
if($defines{$condition}) {
my $rest = join ' ', @rest;
&line($rest);
}
}
sub cmd_echo {
print join(' ', @_), "\n";
}
sub cmd {
my ($name, $arg_str) = @_;
my $func = "cmd_$name";
die "Undefined macro: '$name'" unless(defined(&$func));
my @args = split /\s/, $arg_str;
&$func(@args);
}
sub line {
my ($line) = @_;
if($line =~ /^\%([a-zA-Z]*)\s+(\S[\s\S]*)$/g) {
&cmd($1, $2);
} else {
print "$line\n";
}
}
for(@files) {
open FH, '<', $_;
while(<FH>) {
chomp;
&line($_);
}
close FH;
}