-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-externs.pl
48 lines (43 loc) · 1.36 KB
/
convert-externs.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
$re_sym = qr/[a-zA-Z_][a-zA-Z0-9_]*/;
$re_type = qr/(?:(?:const|unsigned|short|long) )*$re_sym(?:\s*(?:\*|const\s*\*\s*)+\s*|\s+)/;
%type_map = (
'DdNode' => 'DdNode',
'DdManager' => 'DdManager',
'int' => 'c_int',
'unsignedint' => 'c_uint',
'long' => 'c_long',
'unsignedlong' => 'c_ulong',
'char' => 'c_char',
'constchar' => 'c_char',
'FILE' => 'FILE',
);
sub t {
my $t = shift(@_); my $tt = $t; my $ta = $t;
$tt =~ s/\s+//g; $tt =~ s/\*+//g; # no stars or spaces
$ta =~ s/(const\s*)?\*+//g; $ta =~ s/\s+$//; # no stars or trailing spaces
my $m = ($t =~ /^CUDD_/i) ? $t : ($type_map{$tt} || "c_$ta");
while ($t =~ /(const\s*)?\*/g) {
if($1 =~ /const/) {
$m = '*const ' . $m;
} else {
$m = '*mut ' . $m;
}
}
return $m;
}
while(<STDIN>) {
if($_ =~ /^extern ($re_type)($re_sym)\s*\(\s*((?:($re_type)($re_sym)\s*,?\s*)*)\)/) {
#(Cudd_\S+)\s*\((($re_type)($re_sym),?\s*)*\)/) {
my $type = t($1), $args = $3, @al = ();
print "fn $2(";
while( $args =~ /($re_type)($re_sym)/g ) {
push(@al, $2 . ': ' . t($1));
}
print join(", ", @al);
print ") -> $type;\n";
} elsif($_ =~ /^extern ($re_type)($re_sym)\s*\(($re_type)/) {
print "M1: $_\n";
} elsif (/^extern/) {
print "No match: $_\n";
}
}