| 1 |
#!/usr/local/bin/perl -w |
| 2 |
|
| 3 |
# Highly experimental tcl -> perl converter, aimed at eventually |
| 4 |
# completeing Tix merge |
| 5 |
|
| 6 |
use Tk::Pretty; |
| 7 |
use Carp; |
| 8 |
|
| 9 |
@operators = ( |
| 10 |
[qw(return shift next last)], |
| 11 |
[qw(= += -=)], |
| 12 |
[qw(?:)], |
| 13 |
[qw([]),'()'], |
| 14 |
[qw(|| |)], |
| 15 |
[qw(&& &)], |
| 16 |
[qw(< <= > >= == != =~)], |
| 17 |
[qw(+ - .)], |
| 18 |
[qw(* /)], |
| 19 |
[qw(.)], |
| 20 |
['->','&()','eval','bindsub','->{}','++','--','glob'], |
| 21 |
[qw(lindex)], |
| 22 |
[qw(!)] |
| 23 |
); |
| 24 |
|
| 25 |
my $ClassInit; |
| 26 |
|
| 27 |
my $pri = 0; |
| 28 |
my $group; |
| 29 |
foreach $group (@operators) |
| 30 |
{ |
| 31 |
$pri++; |
| 32 |
my $op; |
| 33 |
foreach $op (@$group) |
| 34 |
{ |
| 35 |
$rightpri{$op} = $pri; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
%leftpri = %rightpri; |
| 40 |
%perlpri = %rightpri; |
| 41 |
|
| 42 |
$leftpri{'.'} = 0; |
| 43 |
|
| 44 |
|
| 45 |
$global = 1; |
| 46 |
$InBind = 0; |
| 47 |
|
| 48 |
%widget = (); |
| 49 |
|
| 50 |
foreach (qw(entry menu menubutton frame text canvas scale scrollbar |
| 51 |
button label radiobutton checkbutton)) |
| 52 |
{ |
| 53 |
$widget{$_} = \&tcl_widget; |
| 54 |
} |
| 55 |
|
| 56 |
sub tokenize |
| 57 |
{ |
| 58 |
my $term = shift; |
| 59 |
croak unless defined $term; |
| 60 |
local ($_) = shift if (@_); |
| 61 |
my @tokens = (); |
| 62 |
if (/^(\s*(#.*)\n)/) |
| 63 |
{ |
| 64 |
push(@tokens,$2); |
| 65 |
substr($_,0,length($1)) = ""; |
| 66 |
$.++; |
| 67 |
return @tokens; |
| 68 |
} |
| 69 |
while (length($_)) |
| 70 |
{ |
| 71 |
if (/^(\s*[$term]?)/s) |
| 72 |
{ |
| 73 |
my $spc = $1; |
| 74 |
$. += $spc =~ tr/\n/\n/; |
| 75 |
substr($_,0,length($spc)) = ""; |
| 76 |
last if ($spc =~ /[$term]/s); |
| 77 |
} |
| 78 |
if (/^(\{|!?\[)/) |
| 79 |
{ |
| 80 |
substr($_,0,length($1)) = ""; |
| 81 |
my $count = 1; |
| 82 |
my $open = $1; |
| 83 |
my $close; |
| 84 |
my $var = ""; |
| 85 |
if ($open eq '{') |
| 86 |
{ |
| 87 |
$close = '}'; |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
$close = ']'; |
| 92 |
$var = $open; |
| 93 |
$open = '['; |
| 94 |
} |
| 95 |
while (length($_)) |
| 96 |
{ |
| 97 |
if (/^"([^\\"]|\\.)*"/) |
| 98 |
{ |
| 99 |
$var .= $&; |
| 100 |
$_ = $'; |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
my $ch = substr($_,0,1); |
| 105 |
substr($_,0,1) = ""; |
| 106 |
$count++ if ($ch eq $open); |
| 107 |
$count-- if ($ch eq $close); |
| 108 |
last unless ($count); |
| 109 |
$var .= $ch; |
| 110 |
} |
| 111 |
} |
| 112 |
$var .= ']' if ($close eq ']'); |
| 113 |
push(@tokens,$var); |
| 114 |
} |
| 115 |
elsif (/^("([^\\"]|\\.)*"|([^\s$term]|\\\s)+)/) |
| 116 |
{ |
| 117 |
my $token = $1; |
| 118 |
die "$_" if ($token =~ /^"/ && $token !~ /"$/); |
| 119 |
substr($_,0,length($token)) = ""; |
| 120 |
push(@tokens,$token); |
| 121 |
} |
| 122 |
} |
| 123 |
return @tokens; |
| 124 |
} |
| 125 |
|
| 126 |
sub callback |
| 127 |
{ |
| 128 |
local $_ = shift; |
| 129 |
$_ = $1 if (/^"(.*)"$/); |
| 130 |
my @stat = block($_); |
| 131 |
if (@stat == 1) |
| 132 |
{ |
| 133 |
my @items = @{$stat[0]}; |
| 134 |
my $op = shift(@items); |
| 135 |
if ($op eq '->' || $op eq '&()') |
| 136 |
{ |
| 137 |
return ['[]',@items] ; |
| 138 |
} |
| 139 |
warn "Weird callback:".Pretty($op,@items); |
| 140 |
} |
| 141 |
return ['bindsub',\@stat]; |
| 142 |
} |
| 143 |
|
| 144 |
sub tcl_args |
| 145 |
{ |
| 146 |
my $i = 0; |
| 147 |
my @args = @_; |
| 148 |
while ($i < @args) |
| 149 |
{ |
| 150 |
local $_ = $args[$i]; |
| 151 |
if (/^-options$/) |
| 152 |
{ |
| 153 |
$i++; |
| 154 |
if ($i < @args) |
| 155 |
{ |
| 156 |
my @items = tokenize("\0",$args[$i]); |
| 157 |
$args[$i] = ['[]',@items]; |
| 158 |
} |
| 159 |
} |
| 160 |
elsif (/^-\w*command$/) |
| 161 |
{ |
| 162 |
$i++; |
| 163 |
$args[$i] = callback($args[$i]) if ($i < @args); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
$args[$i] = expr($_) unless (/^"/); |
| 168 |
} |
| 169 |
$i++; |
| 170 |
} |
| 171 |
return @args; |
| 172 |
} |
| 173 |
|
| 174 |
sub tcl_call |
| 175 |
{ |
| 176 |
my @items = (); |
| 177 |
my $key = shift; |
| 178 |
if (@_ && $_[0] =~ /\.\w+$/) |
| 179 |
{ |
| 180 |
my ($base) = ($_[0] =~ /\$?(.*)/); |
| 181 |
unless (exists $variable{$base}) |
| 182 |
{ |
| 183 |
return tcl_widget("\u$key",@_); |
| 184 |
} |
| 185 |
} |
| 186 |
if (substr($key,0,1) =~ /[\$%[]/) |
| 187 |
{ |
| 188 |
unless (@_) |
| 189 |
{ |
| 190 |
return ['->',expr($key),'Call']; |
| 191 |
} |
| 192 |
if (@_ && $_[0] =~ /^\w[\w:-]*$/) |
| 193 |
{ |
| 194 |
return ['->',expr($key),subname(shift),&tcl_args]; |
| 195 |
} |
| 196 |
} |
| 197 |
if (@_ && ($_[0] eq '%W' || $_[0] eq '$w')) |
| 198 |
{ |
| 199 |
my $obj = shift; |
| 200 |
return ['->',expr($obj),subname($key),&tcl_args]; |
| 201 |
} |
| 202 |
return [subname($key),&tcl_args]; |
| 203 |
} |
| 204 |
|
| 205 |
sub find_widget |
| 206 |
{ |
| 207 |
if (defined(@vars) && @vars) |
| 208 |
{ |
| 209 |
return $vars[0]->[1]; |
| 210 |
} |
| 211 |
return '$Widget'; |
| 212 |
} |
| 213 |
|
| 214 |
sub primary |
| 215 |
{ |
| 216 |
croak "No string!" unless defined $_; |
| 217 |
my $start = $_; |
| 218 |
my $val = undef; |
| 219 |
s/^\s+//; |
| 220 |
if (/^\(/) |
| 221 |
{ |
| 222 |
$_ = $'; |
| 223 |
$val = subexpr(0); |
| 224 |
if (/^\s*\)/) |
| 225 |
{ |
| 226 |
$_ = $'; |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
die ") missing in '$_' from '$start'"; |
| 231 |
} |
| 232 |
} |
| 233 |
elsif (/^\[/) |
| 234 |
{ |
| 235 |
substr($_,0,1) = ""; |
| 236 |
my $count = 1; |
| 237 |
my $str = ""; |
| 238 |
while (length($_)) |
| 239 |
{ |
| 240 |
my $ch = substr($_,0,1); |
| 241 |
substr($_,0,1) = ""; |
| 242 |
$count++ if ($ch eq '['); |
| 243 |
$count-- if ($ch eq ']'); |
| 244 |
last unless $count; |
| 245 |
$str .= $ch; |
| 246 |
} |
| 247 |
$val = translate(tokenize("\n;",$str)); |
| 248 |
} |
| 249 |
elsif (/^\{/) |
| 250 |
{ |
| 251 |
substr($_,0,1) = ""; |
| 252 |
my $count = 1; |
| 253 |
$val = ""; |
| 254 |
while (length($_)) |
| 255 |
{ |
| 256 |
if (/^"([^\\"]|\\.)*"/) |
| 257 |
{ |
| 258 |
$val .= $&; |
| 259 |
$_ = $'; |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
my $ch = substr($_,0,1); |
| 264 |
substr($_,0,1) = ""; |
| 265 |
$count++ if ($ch eq '{'); |
| 266 |
$count-- if ($ch eq '}'); |
| 267 |
last unless $count; |
| 268 |
$val .= $ch; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
elsif (/^(\w+)\(/) |
| 273 |
{ |
| 274 |
my $key = $1; |
| 275 |
$_ = $'; |
| 276 |
my $str = ""; |
| 277 |
my $count = 1; |
| 278 |
$val = ""; |
| 279 |
while (length($_)) |
| 280 |
{ |
| 281 |
my $ch = substr($_,0,1); |
| 282 |
substr($_,0,1) = ""; |
| 283 |
$count++ if ($ch eq '('); |
| 284 |
$count-- if ($ch eq ')'); |
| 285 |
last unless $count; |
| 286 |
$val .= $ch; |
| 287 |
} |
| 288 |
my @args = tokenize("\0",$str); |
| 289 |
return [$key,map(expr($_),@args)]; |
| 290 |
} |
| 291 |
elsif (/^\$(\$*[\w:]+)/) |
| 292 |
{ |
| 293 |
my $base = $1; |
| 294 |
my $vname = $&; |
| 295 |
my $index = ""; |
| 296 |
$_ = $'; |
| 297 |
if (/^\(/) |
| 298 |
{ |
| 299 |
my $count = 0; |
| 300 |
while (length($_)) |
| 301 |
{ |
| 302 |
my $ch = substr($_,0,1); |
| 303 |
substr($_,0,1) = ""; |
| 304 |
$index .= $ch; |
| 305 |
$count++ if ($ch eq '('); |
| 306 |
$count-- if ($ch eq ')'); |
| 307 |
last unless $count; |
| 308 |
} |
| 309 |
} |
| 310 |
if ($base =~ /^(\$\w+)(.+)$/) |
| 311 |
{ |
| 312 |
$val = ['->{}',$1,$2]; |
| 313 |
$val = ['->{}',$val,$index] if (defined $index); |
| 314 |
} |
| 315 |
elsif ($global) |
| 316 |
{ |
| 317 |
unless (exists $global{$base} || exists $global{$vname}) |
| 318 |
{ |
| 319 |
$global{$base} = '$'.$base; |
| 320 |
} |
| 321 |
$val = $global{$base}.$index; |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
unless (exists $variable{$base}) |
| 326 |
{ |
| 327 |
push(@vars,['my',($variable{$base} = '$'.$base)]); |
| 328 |
} |
| 329 |
$val = $variable{$base}; |
| 330 |
$val .= $index if (defined $index); |
| 331 |
} |
| 332 |
} |
| 333 |
elsif (/^((#|0x)[\da-fA-F]+)/ || /^(\d+(\.\d+|[cm])?)/) |
| 334 |
{ |
| 335 |
$val = $1; |
| 336 |
$_ = $'; |
| 337 |
} |
| 338 |
elsif ($_ eq '.' || /^\.\W/) |
| 339 |
{ |
| 340 |
substr($_,0,1) = ""; |
| 341 |
$val = ['->',find_widget(),'MainWindow']; |
| 342 |
} |
| 343 |
elsif (/^(-((\w*|\*)-)+\*)/) |
| 344 |
{ |
| 345 |
$val = $1; |
| 346 |
$_ = $'; |
| 347 |
} |
| 348 |
elsif (/^([-!\/+\@])/) |
| 349 |
{ |
| 350 |
my $op = $1; |
| 351 |
$_ = $'; |
| 352 |
return $op unless (length($_)); |
| 353 |
my $rhs = primary(); |
| 354 |
return $op.$rhs if ($op eq '-' && $rhs =~ /^\w+$/); |
| 355 |
return ['.',$op,$rhs] if ($op eq '/' || $op eq '@'); |
| 356 |
return $rhs if ($op eq '+' && !ref($rhs) && $rhs =~ /\d+/); |
| 357 |
$val = [$op,$rhs]; |
| 358 |
} |
| 359 |
elsif (/^([\w:\$]+|"([^\\"]|\\.)*"|%\w|<[\w-]+>)/) |
| 360 |
{ |
| 361 |
$val = $1; |
| 362 |
$_ = $'; |
| 363 |
$val = $1 if ($val =~ /^"(\w+)"$/); |
| 364 |
} |
| 365 |
unless (defined $val) |
| 366 |
{ |
| 367 |
die "operand expected '$start'"; |
| 368 |
} |
| 369 |
return $val; |
| 370 |
} |
| 371 |
|
| 372 |
sub subexpr |
| 373 |
{ |
| 374 |
my $pri = shift; |
| 375 |
my $lhs = primary(); |
| 376 |
while (/^\s*(\.|<=?|>=?|==|&&?|\|\|?|!=|[-+*\/])/ && $pri <= $leftpri{$1}) |
| 377 |
{ |
| 378 |
my $op = $1; |
| 379 |
$_ = $'; |
| 380 |
my $rhs = subexpr($rightpri{$op}); |
| 381 |
if (!ref($rhs) && !length($rhs) && ($op eq '==' || $op eq '!=')) |
| 382 |
{ |
| 383 |
$lhs = ['defined',$lhs]; |
| 384 |
$lhs = ['!',$lhs] if ($op eq '=='); |
| 385 |
} |
| 386 |
elsif (!ref($rhs) && $rhs eq '0' && ($op eq '==' || $op eq '!=')) |
| 387 |
{ |
| 388 |
$lhs = ['!',$lhs] if ($op eq '=='); |
| 389 |
} |
| 390 |
elsif ($op eq '.') |
| 391 |
{ |
| 392 |
$lhs = ['->{}',$lhs,'.'.$rhs]; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
$lhs = [$op,$lhs,$rhs]; |
| 397 |
} |
| 398 |
} |
| 399 |
return $lhs; |
| 400 |
} |
| 401 |
|
| 402 |
sub expr |
| 403 |
{ |
| 404 |
local ($_) = @_; |
| 405 |
croak "No arg" unless (defined $_); |
| 406 |
return "" if (/^\s*$/); |
| 407 |
my $val; |
| 408 |
eval { $val = subexpr(0) }; |
| 409 |
croak "$@ in $_[0]" if ($@); |
| 410 |
croak "Trailing expression:$_ in $_[0]" if (/\S/); |
| 411 |
return $val; |
| 412 |
} |
| 413 |
|
| 414 |
sub translate; |
| 415 |
sub block; |
| 416 |
|
| 417 |
sub tcl_option |
| 418 |
{ |
| 419 |
my $key = $_[0]; |
| 420 |
if ($key eq 'add') |
| 421 |
{ |
| 422 |
shift; |
| 423 |
return class_init(['->','$w','optionAdd',@_]); |
| 424 |
} |
| 425 |
else |
| 426 |
{ |
| 427 |
&tcl_call; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
sub tcl_return |
| 432 |
{ |
| 433 |
@_ = tokenize("\0",shift) if (@_ == 1); |
| 434 |
return ['return',&tcl_args]; |
| 435 |
} |
| 436 |
|
| 437 |
sub class_init |
| 438 |
{ |
| 439 |
unless (defined $ClassInit) |
| 440 |
{ |
| 441 |
if (defined $subname) |
| 442 |
{ |
| 443 |
$subname = 'ClassInit'; |
| 444 |
unshift(@vars,['my','$mw',['shift']]); |
| 445 |
unshift(@vars,['my','$class',['shift']]); |
| 446 |
$ClassInit = \@subbody; |
| 447 |
return @_; |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
$ClassInit = [@_]; |
| 452 |
unshift(@$ClassInit,['my','$mw',['shift']]); |
| 453 |
unshift(@$ClassInit,['my','$class',['shift']]); |
| 454 |
return ['sub','ClassInit',$ClassInit]; |
| 455 |
} |
| 456 |
} |
| 457 |
push(@$ClassInit,@_); |
| 458 |
return (); |
| 459 |
} |
| 460 |
|
| 461 |
sub tcl_bind |
| 462 |
{ |
| 463 |
my $obj = shift; |
| 464 |
my @args = (); |
| 465 |
my @body = (); |
| 466 |
my $isclass = 0; |
| 467 |
if (substr($obj,0,1) eq '$') |
| 468 |
{ |
| 469 |
$obj = expr($obj); |
| 470 |
} |
| 471 |
else |
| 472 |
{ |
| 473 |
$isclass = 1; |
| 474 |
unless (defined $class) |
| 475 |
{ |
| 476 |
$class = $obj; |
| 477 |
$prefix = "\l$class" unless (defined $prefix); |
| 478 |
} |
| 479 |
push(@args,'$class'); |
| 480 |
$obj = '$mw'; |
| 481 |
} |
| 482 |
push(@args,shift); |
| 483 |
if (@_) |
| 484 |
{ |
| 485 |
local $global = 1; |
| 486 |
local @vars = (); |
| 487 |
map(s/^"(.*)"$/$1/,@_); |
| 488 |
my @stat = block(join(' ',@_)); |
| 489 |
@stat = (['->','%W','NoOp']) unless (@stat); |
| 490 |
if (@stat == 1 && $stat[0]->[1] eq '%W') |
| 491 |
{ |
| 492 |
my ($op,$junk,$meth,@items) = @{shift(@stat)}; |
| 493 |
foreach (@items) |
| 494 |
{ |
| 495 |
$_ = ['Ev',$1] if (!ref($_) && /^%(\w)$/); |
| 496 |
} |
| 497 |
if (@items) |
| 498 |
{ |
| 499 |
push(@body,['[]',$meth,@items]); |
| 500 |
} |
| 501 |
else |
| 502 |
{ |
| 503 |
push(@body,$meth); |
| 504 |
} |
| 505 |
} |
| 506 |
else |
| 507 |
{ |
| 508 |
if (@stat) |
| 509 |
{ |
| 510 |
unshift(@stat,['my','$Ev',['->','$w','XEvent']]); |
| 511 |
unshift(@stat,['my','$w',['shift']]); |
| 512 |
} |
| 513 |
push(@body,['bindsub',\@stat]); |
| 514 |
} |
| 515 |
} |
| 516 |
my $stat = ['->',$obj,'bind',@args,@body]; |
| 517 |
return class_init($stat) if ($isclass); |
| 518 |
return $stat; |
| 519 |
} |
| 520 |
|
| 521 |
sub tcl_proc |
| 522 |
{ |
| 523 |
local $global = 0; |
| 524 |
my @items = (); |
| 525 |
local (%variable); |
| 526 |
local $subname = shift(@_); |
| 527 |
local ($_) = shift(@_); |
| 528 |
my @args = tokenize("\0"); |
| 529 |
local @subbody = (); |
| 530 |
local @vars = (); |
| 531 |
if (@args) |
| 532 |
{ |
| 533 |
my $arg; |
| 534 |
foreach $arg (@args) |
| 535 |
{ |
| 536 |
my @v = tokenize("\0",$arg); |
| 537 |
my $v = shift(@v); |
| 538 |
$variable{$v} = '$'.$v; |
| 539 |
my $i = (!@v) ? ['shift'] : ['?:','@_',['shift'],expr(shift(@v))]; |
| 540 |
push(@vars,['my',$variable{$v},$i]); |
| 541 |
} |
| 542 |
} |
| 543 |
push(@subbody,block(shift(@_))); |
| 544 |
unshift(@subbody,@vars); |
| 545 |
push(@items,'sub' => $subname); |
| 546 |
push(@items,\@subbody); |
| 547 |
return \@items; |
| 548 |
} |
| 549 |
|
| 550 |
sub variable |
| 551 |
{ |
| 552 |
return expr('$'.shift); |
| 553 |
} |
| 554 |
|
| 555 |
sub tcl_after |
| 556 |
{ |
| 557 |
my @args = ('->',find_widget(),'after',expr(shift)); |
| 558 |
push(@args,callback(join(' ',@_))) if (@_); |
| 559 |
return \@args; |
| 560 |
} |
| 561 |
|
| 562 |
sub tix_callmethod |
| 563 |
{ |
| 564 |
my @args = ('->',find_widget(),'after',expr(shift)); |
| 565 |
push(@args,callback(join(' ',@_))) if (@_); |
| 566 |
return \@args; |
| 567 |
} |
| 568 |
|
| 569 |
sub tix_idle |
| 570 |
{ |
| 571 |
my @args = ('->',find_widget(),'afterIdle'); |
| 572 |
push(@args,callback(join(' ',@_))); |
| 573 |
return \@args; |
| 574 |
} |
| 575 |
|
| 576 |
sub tcl_incr |
| 577 |
{ |
| 578 |
my @items = (); |
| 579 |
my $var = variable(shift); |
| 580 |
if (@_) |
| 581 |
{ |
| 582 |
return ['+=',$var,expr(shift)]; |
| 583 |
} |
| 584 |
else |
| 585 |
{ |
| 586 |
return ['++',$var]; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
sub tcl_set |
| 591 |
{ |
| 592 |
my $vname = shift(@_); |
| 593 |
if (@_) |
| 594 |
{ |
| 595 |
return ['=',variable($vname),expr(shift)]; |
| 596 |
} |
| 597 |
else |
| 598 |
{ |
| 599 |
return variable($vname); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
sub tcl_widget |
| 604 |
{ |
| 605 |
my $kind = shift; |
| 606 |
my $path = shift; |
| 607 |
my ($parent,$name) = ($path =~ /^(.*)\.([^.]+)$/); |
| 608 |
if (defined($parent) && defined($name)) |
| 609 |
{ |
| 610 |
unshift(@_,Name => $2); |
| 611 |
return ['=',expr($path),['->',expr($parent),$kind,&tcl_args]]; |
| 612 |
} |
| 613 |
else |
| 614 |
{ |
| 615 |
return ['=',expr($path),['->',find_widget(),$kind,&tcl_args]]; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
sub tcl_case |
| 621 |
{ |
| 622 |
my @items = (); |
| 623 |
while($_[0] =~ /^-/) { shift } # XXX -glob and -regexp not used |
| 624 |
my $what = expr(shift(@_)); |
| 625 |
my $case = shift(@_); |
| 626 |
$case = shift(@_) if ($case eq 'in'); |
| 627 |
push(@items,'if'); |
| 628 |
my @case = tokenize("\0",$case); |
| 629 |
while (@case >= 2) |
| 630 |
{ |
| 631 |
my $lab = shift(@case); |
| 632 |
my $stat= shift(@case); |
| 633 |
my (@lab) = tokenize("\0",$lab); |
| 634 |
my $exp = ['==',$what,expr(shift(@lab))]; |
| 635 |
while (@lab) |
| 636 |
{ |
| 637 |
$exp = ['||',$exp,['==',$what,expr(shift(@lab))]]; |
| 638 |
} |
| 639 |
$stat = [block($stat)]; |
| 640 |
push(@items,$exp,$stat); |
| 641 |
} |
| 642 |
die Pretty(@case) if (@case); |
| 643 |
return \@items; |
| 644 |
} |
| 645 |
|
| 646 |
sub tcl_catch |
| 647 |
{ |
| 648 |
my @items = (); |
| 649 |
push(@items,'eval'); |
| 650 |
push(@items,[block(shift(@_))]); |
| 651 |
return \@items; |
| 652 |
} |
| 653 |
|
| 654 |
sub tcl_string |
| 655 |
{ |
| 656 |
return [@_]; |
| 657 |
} |
| 658 |
|
| 659 |
sub tcl_glob |
| 660 |
{ |
| 661 |
while (@_ && $_[0] =~ /^-\w+/) |
| 662 |
{ |
| 663 |
shift; |
| 664 |
} |
| 665 |
return ['glob',@_]; |
| 666 |
} |
| 667 |
|
| 668 |
sub tcl_global |
| 669 |
{ |
| 670 |
my $vname; |
| 671 |
foreach $vname (@_) |
| 672 |
{ |
| 673 |
$variable{$vname} = '$Tix::'.$vname; |
| 674 |
} |
| 675 |
return (); |
| 676 |
} |
| 677 |
|
| 678 |
sub tcl_uplevel |
| 679 |
{ |
| 680 |
if ($_[0] eq '#0') |
| 681 |
{ |
| 682 |
local $global = 1; |
| 683 |
shift; |
| 684 |
&translate; |
| 685 |
} |
| 686 |
else |
| 687 |
{ |
| 688 |
&tcl_call; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
sub tcl_upvar |
| 693 |
{ |
| 694 |
my ($where,$what,$alias) = @_; |
| 695 |
if ($where eq '#0' && $what =~ /^\$/) |
| 696 |
{ |
| 697 |
$variable{$alias} = $what.'->'; |
| 698 |
return (); |
| 699 |
} |
| 700 |
else |
| 701 |
{ |
| 702 |
&tcl_call; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
sub tcl_unset |
| 707 |
{ |
| 708 |
my @items = (); |
| 709 |
my $var = variable(shift); |
| 710 |
return ['undef',$var]; |
| 711 |
return \@items; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
sub tcl_foreach |
| 717 |
{ |
| 718 |
my @items = (); |
| 719 |
push(@items,'foreach'); |
| 720 |
push(@items,variable(shift)); |
| 721 |
push(@items,expr(shift)); |
| 722 |
push(@items,[block(shift)]); |
| 723 |
return \@items; |
| 724 |
} |
| 725 |
|
| 726 |
sub tix_foreach |
| 727 |
{ |
| 728 |
my @items = (); |
| 729 |
push(@items,'tixforeach'); |
| 730 |
my @vars = tokenize("\0",shift); |
| 731 |
foreach (@vars) |
| 732 |
{ |
| 733 |
$_ = variable($_); |
| 734 |
} |
| 735 |
push(@items,['[]',@vars]); |
| 736 |
push(@items,expr(shift)); |
| 737 |
push(@items,[block(shift)]); |
| 738 |
return \@items; |
| 739 |
} |
| 740 |
|
| 741 |
sub tcl_for |
| 742 |
{ |
| 743 |
my @items = (); |
| 744 |
push(@items,'for'); |
| 745 |
push(@items,expr('['. shift(@_) .']')); |
| 746 |
push(@items,expr(shift)); |
| 747 |
push(@items,expr('['. shift(@_) .']')); |
| 748 |
push(@items,[block(shift)]); |
| 749 |
return \@items; |
| 750 |
} |
| 751 |
|
| 752 |
sub tcl_while |
| 753 |
{ |
| 754 |
my @items = (); |
| 755 |
push(@items,'while'); |
| 756 |
push(@items,expr(shift(@_))); |
| 757 |
push(@items,[block(shift(@_))]); |
| 758 |
return \@items; |
| 759 |
} |
| 760 |
|
| 761 |
sub tcl_regexp |
| 762 |
{ |
| 763 |
my $regexp = shift; |
| 764 |
my $val = shift; |
| 765 |
return ['=~',$val,"/$regexp/"]; |
| 766 |
} |
| 767 |
|
| 768 |
sub tcl_if |
| 769 |
{ |
| 770 |
my @items = (); |
| 771 |
my $key = 'if'; |
| 772 |
push(@items,'if'); |
| 773 |
while ($key eq 'if' || $key eq 'elseif') |
| 774 |
{ |
| 775 |
push(@items,expr(shift(@_))); |
| 776 |
push(@items,[block(shift(@_))]); |
| 777 |
last unless (@_); |
| 778 |
$key = shift(@_); |
| 779 |
} |
| 780 |
if ($key eq 'else') |
| 781 |
{ |
| 782 |
push(@items,[block(shift(@_))]); |
| 783 |
} |
| 784 |
return \@items; |
| 785 |
} |
| 786 |
|
| 787 |
sub tcl_eval |
| 788 |
{ |
| 789 |
my @items = (); |
| 790 |
push(@items,'->'); |
| 791 |
push(@items,expr(shift)); |
| 792 |
push(@items,'Call',&tcl_args); |
| 793 |
return \@items; |
| 794 |
} |
| 795 |
|
| 796 |
sub tcl_info |
| 797 |
{ |
| 798 |
if (@_ == 2 && $_[0] eq 'exists') |
| 799 |
{ |
| 800 |
return ['exists',variable($_[1])]; |
| 801 |
} |
| 802 |
return tcl_call('info',@_);; |
| 803 |
} |
| 804 |
|
| 805 |
sub tcl_wm |
| 806 |
{ |
| 807 |
my @items = (); |
| 808 |
push(@items,'->'); |
| 809 |
my $method = shift; |
| 810 |
croak "No arg" unless (defined $method); |
| 811 |
my $widget = expr(shift); |
| 812 |
push(@items,$widget,$method,&tcl_args); |
| 813 |
return \@items; |
| 814 |
} |
| 815 |
|
| 816 |
sub tcl_pack |
| 817 |
{ |
| 818 |
my @items = (); |
| 819 |
push(@items,'->'); |
| 820 |
my $key = 'pack'; |
| 821 |
if ($_[0] =~ /^(forget|info|propagate)$/) |
| 822 |
{ |
| 823 |
my $sub = shift; |
| 824 |
$key .= "\u$sub"; |
| 825 |
} |
| 826 |
my $widget = expr(shift); |
| 827 |
push(@items,$widget,$key,&tcl_args); |
| 828 |
return \@items; |
| 829 |
} |
| 830 |
|
| 831 |
sub tcl_expr |
| 832 |
{ |
| 833 |
return expr(join(' ',@_)); |
| 834 |
} |
| 835 |
|
| 836 |
sub tcl_format |
| 837 |
{ |
| 838 |
my $fmt = shift; |
| 839 |
if ($fmt =~ /^%s\(([-\w:]+)\)$/ && @_ == 1) |
| 840 |
{ |
| 841 |
return ['->{}',expr(shift),$1]; |
| 842 |
} |
| 843 |
return ['sprintf',$fmt,&tcl_args]; |
| 844 |
} |
| 845 |
|
| 846 |
sub tix_class |
| 847 |
{ |
| 848 |
$prefix = shift; |
| 849 |
%info = tokenize("\0",shift); |
| 850 |
if (exists $info{-classname}) |
| 851 |
{ |
| 852 |
$class = $info{-classname}; |
| 853 |
$class =~ s/^[Tt]ix//; |
| 854 |
} |
| 855 |
return (); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
%tcl = ( 'proc' => \&tcl_proc, |
| 860 |
'bind' => \&tcl_bind, |
| 861 |
'case' => \&tcl_case, |
| 862 |
'switch' => \&tcl_case, |
| 863 |
'eval' => \&tcl_eval, |
| 864 |
'info' => \&tcl_info, |
| 865 |
'expr' => \&tcl_expr, |
| 866 |
'format' => \&tcl_format, |
| 867 |
'incr' => \&tcl_incr, |
| 868 |
'after' => \&tcl_after, |
| 869 |
'tixCallMethod' => \&tcl_call, |
| 870 |
'tixDoWhenIdle' => \&tix_idle, |
| 871 |
'tixWidgetDoWhenIdle' => \&tix_idle, # What is different? |
| 872 |
'set' => \&tcl_set, |
| 873 |
'if' => \&tcl_if, |
| 874 |
'regexp' => \&tcl_regexp, |
| 875 |
'while' => \&tcl_while, |
| 876 |
'pack' => \&tcl_pack, |
| 877 |
'wm' => \&tcl_wm, |
| 878 |
'winfo' => \&tcl_wm, |
| 879 |
'catch' => \&tcl_catch, |
| 880 |
'foreach' => \&tcl_foreach, |
| 881 |
'tixForEach' => \&tix_foreach, |
| 882 |
'for' => \&tcl_for, |
| 883 |
'global' => \&tcl_global, |
| 884 |
'string' => \&tcl_string, |
| 885 |
'glob' => \&tcl_glob, |
| 886 |
'return' => \&tcl_return, |
| 887 |
'upvar' => \&tcl_upvar, |
| 888 |
'uplevel' => \&tcl_uplevel, |
| 889 |
'unset' => \&tcl_unset, |
| 890 |
'break' => sub { ['last'] }, |
| 891 |
'continue' => sub { ['next'] }, |
| 892 |
'tixWidgetClass' => \&tix_class, |
| 893 |
'tixClass' => \&tix_class, |
| 894 |
'option' => \&tcl_option, |
| 895 |
); |
| 896 |
|
| 897 |
sub translate |
| 898 |
{ |
| 899 |
# &Tk::Pretty::PrintArgs; |
| 900 |
if (@_) |
| 901 |
{ |
| 902 |
my $key = shift; |
| 903 |
if (substr($key,0,1) eq '#') |
| 904 |
{ |
| 905 |
return $key; |
| 906 |
} |
| 907 |
elsif (exists $tcl{$key}) |
| 908 |
{ |
| 909 |
return &{$tcl{$key}}; |
| 910 |
} |
| 911 |
elsif (exists $widget{$key}) |
| 912 |
{ |
| 913 |
return &tcl_widget("\u$key",@_); |
| 914 |
} |
| 915 |
else |
| 916 |
{ |
| 917 |
eval { $what = tcl_call($key,@_) }; |
| 918 |
croak "$@ in ".join(' ',$key,@_) if ($@); |
| 919 |
return $what; |
| 920 |
} |
| 921 |
} |
| 922 |
return (); |
| 923 |
} |
| 924 |
|
| 925 |
sub block |
| 926 |
{ |
| 927 |
local ($_) = @_; |
| 928 |
croak unless defined $_; |
| 929 |
my @stat = (); |
| 930 |
while (length($_)) |
| 931 |
{ |
| 932 |
push(@stat,translate(tokenize("\n;"))); |
| 933 |
} |
| 934 |
return @stat; |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
sub indent |
| 939 |
{ |
| 940 |
my $depth = shift; |
| 941 |
return '' if ($depth <= 0); |
| 942 |
return ' ' x $depth; |
| 943 |
} |
| 944 |
|
| 945 |
sub statement; |
| 946 |
sub expression; |
| 947 |
|
| 948 |
sub output_block |
| 949 |
{ |
| 950 |
my $depth = shift; |
| 951 |
my $body = shift; |
| 952 |
print indent($depth),"{\n"; |
| 953 |
statements($depth+1,@$body); |
| 954 |
print indent($depth),"}"; |
| 955 |
print shift if (@_); |
| 956 |
print "\n"; |
| 957 |
} |
| 958 |
|
| 959 |
sub subname |
| 960 |
{ |
| 961 |
local ($_) = shift; |
| 962 |
croak $_ if (/^&/); |
| 963 |
carp "Weird name ".Pretty($_) if (ref $_); |
| 964 |
s/^${class}:+// if (defined $class); |
| 965 |
s/^${prefix}:+// if (defined $prefix); |
| 966 |
s/^config-//; |
| 967 |
s/[:-]/_/g; |
| 968 |
print STDERR "Bad '$_'\n" if (/[^\w:]/); |
| 969 |
s/[^\w:]/_/g; |
| 970 |
return $_; |
| 971 |
} |
| 972 |
|
| 973 |
sub output_sub |
| 974 |
{ |
| 975 |
my ($depth,$key,$name,$body) = @_; |
| 976 |
print indent($depth),"\nsub ",subname($name),"\n"; |
| 977 |
output_block($depth,$body); |
| 978 |
} |
| 979 |
|
| 980 |
sub output_foreach |
| 981 |
{ |
| 982 |
my ($depth,$key,$var,$list,$body) = @_; |
| 983 |
print indent($depth),$key," "; |
| 984 |
expression(0,$var); |
| 985 |
print " ("; |
| 986 |
expression(0,$list); |
| 987 |
print ")\n"; |
| 988 |
output_block($depth+1,$body); |
| 989 |
} |
| 990 |
|
| 991 |
sub output_for |
| 992 |
{ |
| 993 |
my ($depth,$key,$start,$cond,$end,$body) = @_; |
| 994 |
print indent($depth),$key," ("; |
| 995 |
expression(0,$start); |
| 996 |
print "; "; |
| 997 |
expression(0,$cond); |
| 998 |
print "; "; |
| 999 |
expression(0,$end); |
| 1000 |
print ")\n"; |
| 1001 |
output_block($depth+1,$body); |
| 1002 |
} |
| 1003 |
|
| 1004 |
sub output_if |
| 1005 |
{ |
| 1006 |
my $depth = shift; |
| 1007 |
my $name = shift; |
| 1008 |
if (@_ <= 3) |
| 1009 |
{ |
| 1010 |
my $cond = $_[0]; |
| 1011 |
croak Pretty($name,@_) unless defined $cond; |
| 1012 |
if (ref($cond) && @$cond == 2 && $cond->[0] eq '!') |
| 1013 |
{ |
| 1014 |
$name = 'unless' if ($name eq 'if'); |
| 1015 |
$name = 'until' if ($name eq 'while'); |
| 1016 |
$_[0] = $cond = $cond->[1]; |
| 1017 |
} |
| 1018 |
if (@_ == 2 && @{$_[1]} == 1 && ref($_[1]->[0])) |
| 1019 |
{ |
| 1020 |
my $kind = $_[1]->[0]->[0]; |
| 1021 |
unless (exists $statement{$kind}) |
| 1022 |
{ |
| 1023 |
print indent($depth); |
| 1024 |
expression(0,$_[1]->[0]); |
| 1025 |
print " $name ("; |
| 1026 |
expression(0,$cond); |
| 1027 |
print ");\n"; |
| 1028 |
return; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
while (@_ >= 2) |
| 1033 |
{ |
| 1034 |
print indent($depth),$name," ("; |
| 1035 |
expression(0,shift); |
| 1036 |
print ")\n"; |
| 1037 |
output_block($depth+1,shift); |
| 1038 |
$name = 'elsif'; |
| 1039 |
} |
| 1040 |
if (@_) |
| 1041 |
{ |
| 1042 |
print indent($depth),"else\n"; |
| 1043 |
output_block($depth+1,shift); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
sub output_cond |
| 1048 |
{ |
| 1049 |
my ($pri,$name,$cond,$true,$false) = @_; |
| 1050 |
print '('; |
| 1051 |
expression(0,$cond); |
| 1052 |
print ') ? '; |
| 1053 |
expression(0,$true); |
| 1054 |
print " : "; |
| 1055 |
expression(0,$false); |
| 1056 |
} |
| 1057 |
|
| 1058 |
sub output_diadic |
| 1059 |
{ |
| 1060 |
my $pri = shift; |
| 1061 |
my $name = shift; |
| 1062 |
if (@_ == 2) |
| 1063 |
{ |
| 1064 |
expression($pri,shift); |
| 1065 |
print " $name "; |
| 1066 |
expression($pri,shift); |
| 1067 |
} |
| 1068 |
else |
| 1069 |
{ |
| 1070 |
print $name; |
| 1071 |
expression($pri,shift); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
sub isString |
| 1076 |
{ |
| 1077 |
my $op = shift; |
| 1078 |
return !ref($op) && ($op !~ /^[$%]/); |
| 1079 |
} |
| 1080 |
|
| 1081 |
%strCmp = ( '<' => 'lt', '>' => 'gt', |
| 1082 |
'<=' => 'le', '>=' => 'ge', |
| 1083 |
'==' => 'eq', '!=' => 'ne'); |
| 1084 |
|
| 1085 |
sub output_compare |
| 1086 |
{ |
| 1087 |
my ($pri,$name,$lhs,$rhs) = @_; |
| 1088 |
$name = $strCmp{$name} if (isString($lhs) || isString($rhs)); |
| 1089 |
&Tk::Pretty::PrintArgs unless (defined $name); |
| 1090 |
expression($pri,$lhs); |
| 1091 |
print " $name "; |
| 1092 |
expression($pri,$rhs); |
| 1093 |
} |
| 1094 |
|
| 1095 |
sub output_member |
| 1096 |
{ |
| 1097 |
my ($pri,$name,$lhs,$rhs) = @_; |
| 1098 |
expression($pri,$lhs); |
| 1099 |
print "->{"; |
| 1100 |
expression($pri,$rhs); |
| 1101 |
print "}"; |
| 1102 |
} |
| 1103 |
|
| 1104 |
sub output_prefix |
| 1105 |
{ |
| 1106 |
my ($pri,$name,$right) = @_; |
| 1107 |
print "$name "; |
| 1108 |
expression($pri,$right); |
| 1109 |
} |
| 1110 |
|
| 1111 |
sub output_my |
| 1112 |
{ |
| 1113 |
my ($depth,$name,$left,$right) = @_; |
| 1114 |
print indent($depth),"my "; |
| 1115 |
expression(0,$left); |
| 1116 |
if (defined $right) |
| 1117 |
{ |
| 1118 |
print " = "; |
| 1119 |
expression(0,$right); |
| 1120 |
} |
| 1121 |
print ";\n"; |
| 1122 |
} |
| 1123 |
|
| 1124 |
sub output_eval |
| 1125 |
{ |
| 1126 |
my ($pri,$key,$block) = @_; |
| 1127 |
if (@$block == 1 && ref($block->[0]) && $block->[0]->[0] eq 'undef') |
| 1128 |
{ |
| 1129 |
expression($pri,$block->[0]); |
| 1130 |
} |
| 1131 |
else |
| 1132 |
{ |
| 1133 |
print "$key\n"; |
| 1134 |
output_block($depth+1,$block); |
| 1135 |
print indent($depth); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
sub output_list |
| 1140 |
{ |
| 1141 |
print "("; |
| 1142 |
while (@_) |
| 1143 |
{ |
| 1144 |
expression(0,shift); |
| 1145 |
print "," if (@_); |
| 1146 |
} |
| 1147 |
print ")"; |
| 1148 |
} |
| 1149 |
|
| 1150 |
sub output_call |
| 1151 |
{ |
| 1152 |
my $pri = shift; |
| 1153 |
print subname(shift); |
| 1154 |
&output_list; |
| 1155 |
} |
| 1156 |
|
| 1157 |
sub output_glob |
| 1158 |
{ |
| 1159 |
my $pri = shift; |
| 1160 |
my $key = shift; |
| 1161 |
print "("; |
| 1162 |
while (@_) |
| 1163 |
{ |
| 1164 |
print "<",shift,">"; |
| 1165 |
print "," if (@_); |
| 1166 |
} |
| 1167 |
print ")"; |
| 1168 |
} |
| 1169 |
|
| 1170 |
sub output_return |
| 1171 |
{ |
| 1172 |
my $pri = shift; |
| 1173 |
my $key = shift; |
| 1174 |
print "$key"; |
| 1175 |
if (@_) |
| 1176 |
{ |
| 1177 |
print " "; |
| 1178 |
if (@_ > 1) |
| 1179 |
{ |
| 1180 |
&output_list; |
| 1181 |
} |
| 1182 |
else |
| 1183 |
{ |
| 1184 |
expression(0,shift); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
sub output_method |
| 1190 |
{ |
| 1191 |
my $pri = shift; |
| 1192 |
my $op = shift; |
| 1193 |
my $obj = shift; |
| 1194 |
expression($pri,$obj); |
| 1195 |
print $op; |
| 1196 |
if (@_ > 1) |
| 1197 |
{ |
| 1198 |
output_call($pri,@_) if (@_); |
| 1199 |
} |
| 1200 |
else |
| 1201 |
{ |
| 1202 |
print subname(shift); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
sub output_bind |
| 1207 |
{ |
| 1208 |
my ($pri,$key,$body) = @_; |
| 1209 |
local $InBind = 1; |
| 1210 |
print "\n"; |
| 1211 |
print indent($depth+1),"sub\n"; |
| 1212 |
output_block($depth+2,$body); |
| 1213 |
print indent($depth); |
| 1214 |
} |
| 1215 |
|
| 1216 |
sub output_lindex |
| 1217 |
{ |
| 1218 |
my ($pri,$key,$lhs,$rhs) = @_; |
| 1219 |
expression($pri,$lhs); |
| 1220 |
print '['; |
| 1221 |
expression($pri,$rhs); |
| 1222 |
print ']'; |
| 1223 |
} |
| 1224 |
|
| 1225 |
sub output_group |
| 1226 |
{ |
| 1227 |
my $pri = shift; |
| 1228 |
my $key = shift; |
| 1229 |
print substr($key,0,1); |
| 1230 |
while (@_) |
| 1231 |
{ |
| 1232 |
expression($pri,shift); |
| 1233 |
print ',' if (@_); |
| 1234 |
} |
| 1235 |
print substr($key,1,1); |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
%expression = ( |
| 1240 |
'==' => \&output_compare, |
| 1241 |
'!=' => \&output_compare, |
| 1242 |
'<=' => \&output_compare, |
| 1243 |
'<' => \&output_compare, |
| 1244 |
'>=' => \&output_compare, |
| 1245 |
'>' => \&output_compare, |
| 1246 |
'=' => \&output_diadic, |
| 1247 |
'.' => \&output_diadic, |
| 1248 |
'+=' => \&output_diadic, |
| 1249 |
'+' => \&output_diadic, |
| 1250 |
'||' => \&output_diadic, |
| 1251 |
'&' => \&output_diadic, |
| 1252 |
'&&' => \&output_diadic, |
| 1253 |
'=~' => \&output_diadic, |
| 1254 |
'[]' => \&output_group, |
| 1255 |
'()' => \&output_group, |
| 1256 |
'-' => \&output_diadic, |
| 1257 |
'*' => \&output_diadic, |
| 1258 |
'/' => \&output_diadic, |
| 1259 |
'->' => \&output_method, |
| 1260 |
'?:' => \&output_cond, |
| 1261 |
'!' => \&output_prefix, |
| 1262 |
'++' => \&output_prefix, |
| 1263 |
'bindsub' => \&output_bind, |
| 1264 |
'eval' => \&output_eval, |
| 1265 |
'lindex' => \&output_lindex, |
| 1266 |
'return' => \&output_return, |
| 1267 |
'last' => \&output_return, |
| 1268 |
'next' => \&output_return, |
| 1269 |
'shift' => \&output_return, |
| 1270 |
'glob' => \&output_glob, |
| 1271 |
'->{}' => \&output_member, |
| 1272 |
); |
| 1273 |
|
| 1274 |
|
| 1275 |
sub expression |
| 1276 |
{ |
| 1277 |
my ($pri,$item) = @_; |
| 1278 |
croak "No item" unless defined($item); |
| 1279 |
if (ref($item)) |
| 1280 |
{ |
| 1281 |
if (ref($item) eq 'ARRAY') |
| 1282 |
{ |
| 1283 |
my $kind = $item->[0]; |
| 1284 |
if (exists $expression{$kind}) |
| 1285 |
{ |
| 1286 |
unless (exists $perlpri{$kind}) |
| 1287 |
{ |
| 1288 |
warn "Don't know priority of $kind"; |
| 1289 |
$perlpri{$kind} = $perlpri{'&()'}; |
| 1290 |
} |
| 1291 |
my $opri = $perlpri{$kind}; |
| 1292 |
print "(" if ($opri < $pri); |
| 1293 |
&{$expression{$kind}}($opri,@$item); |
| 1294 |
print ")" if ($opri < $pri); |
| 1295 |
} |
| 1296 |
else |
| 1297 |
{ |
| 1298 |
output_call($pri,@$item); |
| 1299 |
} |
| 1300 |
} |
| 1301 |
else |
| 1302 |
{ |
| 1303 |
die "Not an array reference $item"; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
else |
| 1307 |
{ |
| 1308 |
if ($item =~ /^(\$\w[^(]*)\((.*)\)$/) |
| 1309 |
{ |
| 1310 |
expression($pri,"$1"); |
| 1311 |
my $index; |
| 1312 |
foreach $index (split(/,/,$2)) |
| 1313 |
{ |
| 1314 |
print "{"; |
| 1315 |
expression(0,$index); |
| 1316 |
print "}"; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
elsif ($item =~ /^["\$]/ || $item =~ /^-?\d+(\.\d+)?$/) |
| 1320 |
{ |
| 1321 |
print $item; |
| 1322 |
} |
| 1323 |
elsif ($item =~ /^%(\w)$/) |
| 1324 |
{ |
| 1325 |
if ($1 eq 'W') |
| 1326 |
{ |
| 1327 |
print '$w'; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
{ |
| 1331 |
print "\$Ev->$1"; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
else |
| 1335 |
{ |
| 1336 |
warn "$item" if ($item =~ /\(/); |
| 1337 |
if ($item =~ /\$/) |
| 1338 |
{ |
| 1339 |
print "\"$item\""; |
| 1340 |
} |
| 1341 |
else |
| 1342 |
{ |
| 1343 |
print "'$item'"; |
| 1344 |
} |
| 1345 |
} |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
%statement = ( 'sub' => \&output_sub, |
| 1351 |
'my' => \&output_my, |
| 1352 |
'if' => \&output_if, |
| 1353 |
'while' => \&output_if, |
| 1354 |
'foreach' => \&output_foreach, |
| 1355 |
'tixforeach' => \&output_foreach, |
| 1356 |
'for' => \&output_for, |
| 1357 |
); |
| 1358 |
|
| 1359 |
sub statement |
| 1360 |
{ |
| 1361 |
local $depth = shift; |
| 1362 |
my $item = shift; |
| 1363 |
croak "No item!" unless defined $item; |
| 1364 |
if (ref($item)) |
| 1365 |
{ |
| 1366 |
if (ref($item) eq 'ARRAY') |
| 1367 |
{ |
| 1368 |
if (@$item) |
| 1369 |
{ |
| 1370 |
my $kind = $item->[0]; |
| 1371 |
if (exists $statement{$kind}) |
| 1372 |
{ |
| 1373 |
&{$statement{$kind}}($depth,@$item); |
| 1374 |
} |
| 1375 |
else |
| 1376 |
{ |
| 1377 |
print indent($depth); |
| 1378 |
expression(0,$item); |
| 1379 |
print ";\n"; |
| 1380 |
} |
| 1381 |
} |
| 1382 |
else |
| 1383 |
{ |
| 1384 |
print "\n"; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
else |
| 1388 |
{ |
| 1389 |
die "Not an array reference $item"; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
else |
| 1393 |
{ |
| 1394 |
print indent($depth),$item,"\n"; |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
sub statements |
| 1399 |
{ |
| 1400 |
my $depth = shift; |
| 1401 |
while (@_) |
| 1402 |
{ |
| 1403 |
statement($depth,shift); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
$SIG{INT} = sub { croak "Interrupt" }; |
| 1408 |
|
| 1409 |
undef $/; |
| 1410 |
foreach $file (@ARGV) |
| 1411 |
{ |
| 1412 |
if ($file =~ /\.tcl$/) |
| 1413 |
{ |
| 1414 |
my $perl = $file; |
| 1415 |
$perl =~ s/\.tcl/.pm/; |
| 1416 |
open(TCL,"<$file") || die "Cannot open $file:$!"; |
| 1417 |
print STDERR "$file => $perl\n"; |
| 1418 |
my $prog = <TCL>; |
| 1419 |
close(TCL); |
| 1420 |
$prog =~ s/\\\n/ /sg; |
| 1421 |
my @body = block($prog); |
| 1422 |
push(@$ClassInit,['return','$class']) if (defined $ClassInit); |
| 1423 |
open(PERL,">$perl") || die "Cannot open $perl:$!"; |
| 1424 |
my $old = select(PERL); |
| 1425 |
if (defined $class) |
| 1426 |
{ |
| 1427 |
print "package Tk::",$class,";\n"; |
| 1428 |
if (exists $info{-superclass}) |
| 1429 |
{ |
| 1430 |
my $superclass = $info{-superclass}; |
| 1431 |
$superclass =~ s/^[Tt]ix//; |
| 1432 |
print '@Tk::',$class,'::ISA = qw(Tk::',$superclass,");\n"; |
| 1433 |
} |
| 1434 |
} |
| 1435 |
statements(0,@body); |
| 1436 |
select($old); |
| 1437 |
close(PERL); |
| 1438 |
if (system("perl","-wc",$perl) != 0) |
| 1439 |
{ |
| 1440 |
rename($perl,"$perl.oops"); |
| 1441 |
exit(4) |
| 1442 |
} |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|