Debian Codegraph @

HList/Tix2perl

raw
1 #!/usr/local/bin/perl -w
2
3 use Tk::Pretty;
4
5 sub complete
6 {
7 local ($_) = @_;
8 my $open = tr/{/{/;
9 my $close = tr/}/}/;
10 return 0 if ($open > $close);
11 die "More } than { in \n$_" if ($close > $open);
12 my $open = tr/[/[/;
13 my $close = tr/]/]/;
14 return 0 if ($open > $close);
15 die "More ] than [ in \n$_" if ($close > $open);
16 return 1;
17 }
18
19 sub statement
20 {
21 # &Tk::Pretty::PrintArgs;
22 local ($_) = @_;
23 my ($sp,$rest) = (/^(\s*)(.*)$/s);
24 if (defined($rest) && length($rest))
25 {
26 my @words = split(/\s+/,$rest);
27 my $cmd = shift(@words);
28 my $i = 0;
29 while ($i < @words)
30 {
31 while (($i+1) < @words && !complete($words[$i]))
32 {
33 my $next = splice(@words,$i+1,1);
34 $words[$i] .= " $next";
35 }
36 $i++;
37 }
38 print PERL "$cmd(",join(',',@words),");\n";
39 }
40 else
41 {
42 print PERL "$_\n";
43 }
44 }
45
46 sub block
47 {
48 my @lines = split(/\n/,$_[0]);
49 local ($_);
50 while (@lines)
51 {
52 $_ = shift(@lines);
53 while (@lines && !complete($_))
54 {
55 my $more = shift(@lines);
56 $_ .= "\n$more";
57 }
58 statement($_);
59 }
60 }
61
62 sub convert
63 {
64 my ($tcl) = @_;
65 open(TCL,"expand $tcl|") || die "Cannot open expand $tcl:$!";
66 open(PERL,">$tcl.pm") || die "Cannot open $tcl.pm:$!";
67 LOOP:
68 while (<TCL>)
69 {
70 if (/^\s*(#.*)?$/)
71 {
72 print PERL $_;
73 }
74 else
75 {
76 if (/\\$/ || !complete($_))
77 {
78 my $more = <TCL>;
79 if (/\\$/)
80 {
81 chomp;
82 s/\\$//;
83 }
84 $_ .= $more;
85 redo LOOP;
86 }
87 # handling of args with defaults needs work for general case
88 if (/^proc\s+(\S+)\s*\{([^\}]*)}\s*\{(.*)\}\s*$/s)
89 {
90 my ($name,$args,$body) = ($1,$2,$3);
91 print STDERR "$name\n";
92 print PERL "sub $name\n{\n";
93 my @args = split(/\s/,$args);
94 if (@args)
95 {
96 print PERL " my (",join(',',map('$'.$_,@args)),") = \@_;\n";
97 }
98 block($body);
99 print PERL "}\n";
100 next LOOP;
101 }
102 else
103 {
104 chomp;
105 statement($_);
106 }
107 }
108 }
109 close(TCL);
110 close(PERL);
111 }
112
113 @ARGV = <*.tcl> unless (@ARGV);
114
115 foreach $file (@ARGV)
116 {
117 convert($file) if ($file =~ /\.tcl$/);
118 }