/usr/share/perl5/JE/toperl.pl is in libje-perl 0.060-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 | package JE::Code;
use strict;
use warnings; no warnings qw 'utf8 parenthesis';
use constant 1.03 our $_const = do {
my $x = 0;
+{ map +($_ => $x++),
'cx_stm', # statement
'cx_void', # statement; retval ignored
# expression contexts
'cx_void_expr',
'cx_any', # includes lvalue
'cx_rv', # rvalue
'cx_lv', # lvalue
'cx_bool', # plain boolean, not necessarily a JE::Boolean
'cx_str', # Perl scalar in UTF-16 (with surrogates)
'cx_num', # Perl scalar
'ret_void', # no retval
'ret_maybe', # might have a retval (which could be an lval)
'ret_break', # break or continue
'ret_any', # includes lvalue
'ret_lv',
'ret_rv',
'ret_str',
'ret_num',
'ret_bool',
}
};
BEGIN { no strict; delete @{__PACKAGE__."::"}{_const => keys %$_const} }
=begin notes
The cx_stm constant for statement-level context applies to a context in
which any return type (including an lvalue) is expected, but an lvalue has
to have ->get called on it before it is returned.
The to_perl methods take 1 argument, a cx_ constant, and return 2 or 3:
0. ret_ constant indicating possible return types
1. string of Perl code
( 2. alternate string of Perl code that can be used only in some
contexts, such as "..." for JE::String->new(...) )
The reason we have all those return values is most easily demonstrated by
example:
For the + operator, we have two completely different behaviours, depending
on the types of the operands. If an operand can only be a string, we get:
ret_str, 'JE::String->new($global, "...")', '"..."'
If we see one of those, we can stringify the other operand, returning code
like this:
"..." . [other operand]->to_string->value16
We need to_perl’s second retval, for cases like - :
[str operand]->to_number->value - [num operand]
=end notes
=cut
sub _esc_str($) {
my $str = shift;
$str =~ s/(['\\])/\\$1/g;
"'$str'";
}
our %labels;
our %loop_labels;
our $cache_indx;
our $code;
no warnings 'redefine';
sub optimise {
my $self = shift;
local %labels; local %loop_labels; local $cache_indx;
local $code = $self;
my $global = $self->{global};
$self->set_global(undef);
$self->{psrc} = 'no warnings"utf8","exiting";'
. $self->{tree}->to_perl(cx_stm);
$self->{tree}=[$self->{tree}[0]];
$self->set_global($global);
}
use Scalar::Util 'refaddr';
sub JE::Code::Statement::to_perl {
my $stm = shift;
my $cx = shift;
my $type = $$stm[1];
$type eq 'empty' and return ret_void, '';
if($type eq 'function' ) {
(my $new_code_obj = bless {
map+($_=>$code->{$_}),qw/global source file line/
}, 'JE::Code')
->{tree} = $$_[4];
$new_code_obj->{vars} = $$_[5];
$new_code_obj->optimise;
$$stm[4] = $new_code_obj;
return ret_void, '';
}
my $pos = $$stm[0][0];
if ($type eq 'labelled') {
my @labels = @$stm[2..$#$stm-1];
my $is_loop =
$$stm[-1][1] =~ /^(?:do|while|for|switch)\z/;
# We have to alias the labels to a single label, because
# Perl does not support multiple labels on a sin-
# gle statement. Nor can we stack them up with
# blocks, ( foo:{bar:{...}} ) because that won’t work
# for loop-control constructs (next foo). We have
# to rename them, since JS idents can have $ in them.
my $alias_to = refaddr $stm->[-1];
local @labels{@labels} = ($alias_to)x@labels;
# ‘continue’ statements only look in this hash:
local @loop_labels{@labels} = ($alias_to)x@labels
if $is_loop;
my($rettype,$code) = $$stm[-1]->to_perl($cx);
return
$rettype,
# loops add their own label, since some have to put
# something before it
$is_loop ? $code :
"JE_Code_$alias_to:" . (
$$stm[-1][1] =~ /^{/
? $code
: length $code ? "{$code}" : '{;}'
); # Without the ; it’s a hash.
}
if ($type eq 'statements') {
# At run time, the statements are executed one by one, and
# the return value of the last statement that actually
# returned one is returned. To avoid passing cx_stm context
# when cx_void will do, we work backwards.
# ~~~ This logic is currently flawed. It needs to account
# for a ‘break’ or similar in between two ret_any stms.
# And also ‘return’ satetments.
my @code; my $last_rettype;
for (@$stm[reverse 2..$#$stm]) {
my($rettype,$code) = $_->to_perl($cx);
$last_rettype = $rettype;
push @code, $code;
if($rettype != ret_void && $rettype != ret_maybe) {
$cx = cx_void
}
}
my $code = join '', reverse @code;
# $rettype will be undefined if this is an empty block
return defined $last_rettype?$last_rettype:ret_void, $code;
}
if ($type eq 'var') {
# The Perl code looks like this:
# $scope->find_var("foo")->set( ...expr... );
my $stm_code = '';
for (@$stm[2..$#$stm]) { if (@$_ == 2) {
my($rettype,$code) = _term_to_perl($$_[1],cx_rv);
$stm_code .=
'$scope->find_var(' . _esc_str($$_[0]) .')->set('
. $code . ");";
}}
return ret_void, $stm_code;
}
if ($type eq 'if') {
# 2 3 4
# we have: expr statement statement?
my($rettype,$code) = $$stm[2]->to_perl(cx_bool);
my $stm_code = "if($code){";
($rettype,$code) = $$stm[3]->to_perl($cx);
$stm_code .= "$code}";
my $rettype2;
if(exists $$stm[4]) {
($rettype2,$code) = $$stm[4]->to_perl($cx);
$stm_code .= "else{$code}"
}
return $cx == cx_void ? ret_void
: (defined $rettype2 ? $rettype2 == ret_void : 1)
&& $rettype == ret_void
? ret_void
: ret_maybe,
$stm_code
}
if ($type =~ /^(?:do|while|for|switch)\z/) {
# We have one of the following:
#
# 1 2 3 4 5
# 'do' statement expression
# 'while' expression statement
# 'for' expression 'in' expression statement
# 'for' var_decl 'in' expression statement
# 'for' expression expression expression statement
# 'for' var_decl expression expression statement
#
# In those last two cases, expression may be 'empty'.
# (See further down for 'switch').
my $label = refaddr $stm;
my $stm_code = "JE_Code_$label:";
# Alias for simple break/continue statements with-
# out a label:
local $labels{''} = local $loop_labels{''} = $label;
if ($type eq 'do') {
# For a do statement we need two loop labels:
# outer: {
# do { inner: {
#
# }} while foo
# }
# continue statements use the inner label.
# In case there are aliases to the existing label:
my @js_labels = grep $labels{$_} == $label =>=>
keys %loop_labels;
local @loop_labels{'',@js_labels} =
($label.'c')x(@js_labels+1);
my($rettype,$code) = $$stm[2]->to_perl($cx);
my(undef,$boolcode)
= $$stm[3]->to_perl(cx_bool);
return $rettype,
"$stm_code\{do{JE_Code_${label}c:{" .
(length $code ? $code : ';') # foo:{} is a hash
."}}while "
. "$boolcode}";
}
elsif ($type eq 'while') {
my(undef,$boolcode)
= $$stm[2]->to_perl(cx_bool);
my($rettype,$code) = $$stm[3]->to_perl($cx);
return $rettype,
"${stm_code}while($boolcode){$code}";
}
elsif ($type eq 'for' and $$stm[3] eq 'in') {
# for(var i = 3 in ...) {
# break;
# }
# translates into
# $scope->find_var('i')->set(3);
# JE_Code_loop: for((my $o = ...)->keys) {
# next if not defined $o->prop($_);
# # in which case it's been deleted
# $scope->find_var('i')->set($_);
# last JE_Code_loop;
# }
# (except that the arguments to ->set() have to be
# expressions returning JE::(Number|String)). For
# statements without the ‘var’, we don’t evaluate
# the lhs before the loop, but only in the loop. It
# takes the place of $scope->find_var.
my $left_side = $$stm[2];
if ($left_side->[1] eq 'var') {
substr $stm_code,0,0 =>=
($left_side->to_perl(cx_void))[1];
$left_side = _esc_str($left_side->[2][0]);
# now contains the identifier within a
# Perl string
}
(undef,my $obj_code) = $$stm[4]->to_perl(cx_rv);
$stm_code .=
"for((my\$o=$obj_code)->keys){" .
'next if!defined prop$o $_;' .
( ref $left_side
? ( $left_side->to_perl(cx_lv) )[1]
: "\$scope->find_var($left_side)" ) .
'->set(_new JE\'String $global,$_);';
my($rettype, $code) = $$stm[5]->to_perl($cx);
return $rettype, "$stm_code$code}";
}
elsif ($type eq 'for') { # for(;;)
$stm_code .= 'for(';
if(ref $$stm[2]) {
my $code = ($$stm[2]->to_perl(cx_void))[1];
length $code and $stm_code .=
"do{$code}" # We need the do
} # because to_perl
$stm_code .= ';'; # returns whole
if(ref $$stm[3]) { # statements
my(undef,$code) =
$$stm[3]->to_perl(cx_bool);
$stm_code .= $code;
}
$stm_code .= ';';
ref $$stm[4] and $stm_code .=
( $$stm[4]->to_perl(cx_void_expr) )[1];
my($rettype, $code) = $$stm[5]->to_perl($cx);
return $rettype, "$stm_code){$code}";
}
else { # switch
# $stm->[2] is the parenthesized
# expression.
# Each pair of elements thereafter
# represents one case clause, an expr
# followed by statements, except for
# the default clause, which has the
# string 'default' for its first elem
# We need to turn something like
# switch(3) {
# case 5: a();
# default: f();
# case 3636:
# case 838: break;
# case 3: h();
# }
# into
# JE_Code_loop: for(3) {
# $_->id eq 5->id and goto JE_Code_case1;
# $_->id eq 3636->id and goto JE_Code_case2;
# $_->id eq 838->id and goto JE_Code_case3;
# $_->id eq 3->id and goto JE_Code_case4;
# goto JE_Code_default;
# JE_Code_case1: $scope->find_var('a')->call;
# JE_Code_default: $scope->find_var('f')->call;
# JE_Code_case2: ;
# JE_Code_case3: last JE_Code_loop;
# JE_Code_case4: $scope->find_var('f')->call;
# }
# (except that the expressions are more complex).
# header
$stm_code .= 'for(' .
( $$stm[2]->to_perl(cx_rv) )[1] . '){';
# Go through the case clauses and add goto state-
# ments to $stm_code while also creating the state-
# ments that go further down.
my $more_statements = '';
my($n, $there_is_a_default) = 1;
while (($n+=2) < @$stm) {
if($$stm[$n] eq 'default') {
$more_statements .=
'JE_Code_default:';
++ $there_is_a_default
}
else {
$stm_code .= 'id$_ eq ' .
($$stm[$n]->to_perl(cx_rv))[1] .
"->id&&goto JE_Code_case$n;";
$more_statements.="JE_Code_case$n:"
}
my $stm = ( $$stm[$n+1]->to_perl($cx) )[1];
$more_statements .= length $stm?$stm:';' ;
} ;
return $cx==ret_void ? ret_void : ret_maybe, # ~~~ need to do some detecting
# for the rettype
$stm_code . (
$there_is_a_default
? 'goto JE_Code_default' : 'last'
) . ";$more_statements}"
} # switch
}
if ($type eq 'continue') {
my $label = exists $$stm[2] ? $$stm[2] : '';
return ret_break, exists $loop_labels{$label}
? "next JE_Code_$loop_labels{$label};"
: "die q[continue $label: label '$label' not found];"
}
if ($type eq 'break') {
my $label = exists $$stm[2] ? $$stm[2] : '';
return ret_break, exists $labels{$label}
? "last JE_Code_$labels{$label};"
: "die q[break $label: label '$label' not found];"
}
if ($type eq 'return') {
return ret_any, '$return=' . ( exists $$stm[2]
? ($$stm[2]->to_perl(cx_rv))[1]
: 'undef'
) . ';last RETURN;' ;
}
if ($type eq 'with') {
my($rettype, $code) = $$stm[3]->to_perl($cx);
return $rettype,
'{local$scope=bless[@$scope,'
. ($$stm[2]->to_perl(cx_rv))[1]
. "->to_object],'JE::Scope';$code}";
}
if ($type eq 'throw') {
return ret_void,'die '.($$stm[2]->to_perl(cx_rv))[1]. ';' ;
}
if ($type eq 'try') {
# We have one of the following:
# 1 2 3 4 5
# 'try' block ident block (catch)
# 'try' block block (finally)
# 'try' block ident block block (catch & finally)
# For a try-catch, we can simply use eval{...;1}||do{...}.
# With finally, it’s a lot more complicated. We can’t use
# Scope::Guard, because a destructor can’t affect execution
# flow. So we have to set up a complicated net to catch
# every return/break/continue in addition to exceptions.
# For every unique value in %labels, we need a separate
# block. What we end up with is something like this:
# ~~~(The inner do{} is actually redundant. Maybe I should
# refactor it a bit.)
# { # lexical scope for all these variables:
# my $l; #label
# my $e; #exception
# my $c; #continue
# eval {
# JE_Code_labels: {
# RETURN: {
# JE_Code_1: for(0,1){ $_ and ++$c, last;
# JE_Code_2: for(0,1){ $_ and ++$c, last;
# JE_Code_3: for(0,1){ $_ and ++$c, last;
# my $r = $return;
# eval { ... try ...; last JE_Code_labels };
# ... catch ...;
# last JE_Code_labels;
# } $l = 3, last JE_Code_labels
# } $l = 2, last JE_Code_labels
# } $l = 1, last JE_Code_labels
# } $l = 'r'
# }
# 1
# } or $e=$@;
# my $r = $return;
# ... finally ...
# defined $e && die $e;
# $return = $r;
# $l and $l == 'r' ? last RETURN :
# $c ? next "JE_Code_$l" : last "JE_Code_$l"
# }
# A catch’s do-block (this is not in a do when there is a
# finally) is as follows:
# do {
# $return = $r;
# ref $@ or $@ = _objectify_error($@);
# (my $o = new JE::Object $global)
# ->prop({
# name => 'e',
# value => $@,
# dontdel => 1,
# });
# local $scope = bless [
# @$scope, $o
# ], 'JE::Scope';
# ...code...
# };
# Got that?
my $finally = $#$stm == 3 || $#$stm == 5;
# try and catch
my $inner_code = '';
# We don’t want try{3; throw...} to return 3, so we save
# the previous value of $return before entering the eval.
my $we_can_catch = !ref $$stm[3];
$inner_code .= 'my$r=$return;eval{' if $we_can_catch;
my($rettype,$code) = $$stm[2]->to_perl($cx); # try
$inner_code .= $code;
$inner_code .= 'last JE_Code_labels;' if $finally;
if($we_can_catch) {
$inner_code .= ($finally ? '};' : '1}||do{')
. '$return=$r;' # prevent { 3; throw ... }
# from returning 3
# Turn miscellaneous errors into
# Error objects
.q"ref$@or$@=_objectify_error($@);"
.q"(my$o=new JE'Object $global)->prop({"
. 'name=>' . _esc_str($$stm[3]) . ','
. 'value=>$@,'
. 'dontdel=>1'
.'});'
.'local$scope='
. 'bless[@$scope,$o],"JE::Scope";';
my($rettype, $code) = $$stm[4]->to_perl($cx);
$inner_code .= $code
. ($finally ? ';last JE_Code_labels' : '}');
}
if ($finally) {
# get a list of labels
my @labels; my %seen;
for(values %labels, values %loop_labels) {
push @labels, $_ unless $seen{$_}++
}
my $stm_code =
'{'
. 'my($l,$e,$c);'
. 'eval{'
. 'JE_Code_labels:{'
. 'RETURN:{';
$stm_code .=
"JE_Code_$_:for(0,1){\$_ and++\$c,last;",
for @labels;
$stm_code .= $inner_code;
$stm_code .=
'}$l='._esc_str($_).",last JE_Code_labels"
for reverse @labels;
$stm_code .=
'}$l="r"'
. '}'
. '1'
. '}or$e=$@;'
. 'my$r=$return;'
. ( $$stm[-1]->to_perl(cx_void) )[1]
. 'defined$e&&die$e;'
. '$return=$r;'
. '$l and$l eq"r"?last RETURN:'
. '$c?eval"next JE_Code_$l"'
. ':eval"last JE_Code_$l"'
.'}';
return $cx==ret_void ? ret_void : ret_maybe,
$stm_code;
}
else {
return $cx==ret_void ? ret_void : ret_maybe,
$we_can_catch ? "{$inner_code}" :
$inner_code;
}
# ~~~ need to do some detecting for the rettype
}
}
=begin for-me
Types of expressions:
'new' term args?
'member/call' term ( subscript | args) *
'postfix' term op
'hash' term*
'array' term? (comma term?)*
'prefix' op+ term
'lassoc' term (op term)*
'assign' term (op term)* (term term)?
(the last two terms are the 2nd and 3rd terms of ? :
'expr' term*
(commas are omitted from the array)
'function' ident? params statements
=end for-me
=cut
# Note: each expression object is an array ref. The elems are:
# [0] - an array ref containing
# [0] - the starting position in the source code and
# [1] - the ending position
# [1] - the type of expression
# [2..$#] - the various terms/tokens that make up the expr
sub JE::Code::Expression::to_perl {
#ret_rv, "hooha()" . ';' x ($_[1] == cx_stm || $_[1] == cx_void)
# ~~~ ++ $ops>$counting and last JE_Code_OP if $counting;
my $expr = shift;
my $cx = shift;
my $type = $$expr[1];
# ~~~ $pos = $$expr[0][0];
if ($type eq 'expr') {
my $sc = '';
$cx == cx_stm
? ($sc = ';', $cx = cx_any) :
$cx == cx_void && ($sc = ';', $cx = cx_void_expr);
if(@$expr == 3) { # no comma
my($rettype,$code) =
_term_to_perl($$expr[-1], $cx);
if($rettype == ret_lv && $sc) {
$code =
"{my\$v=$code;\$v->get;\$return=\$v}";
}elsif($rettype==ret_any && $sc){
$code = "{my\$v=$code;"
. 'ref$v eq"JE::LValue"&&$v->get'
. ';$return=$v'
.'}';
}
elsif($sc) {
$code = "\$return=$code;"
}
return $rettype, $code;
}
else { # comma op
my $result = join ',', map+
(_term_to_perl($_, cx_void_expr))[1],
@$expr[2..$#$expr-1];
$result .= ',';
my($rettype,$code) =
_term_to_perl($$expr[-1], ret_rv);
$result .= $code;
if($sc) {
$result = "\$return=($result);";
}
else {
$result = "scalar($result)";
}
return $cx == cx_void_expr ? ret_void : $rettype,
$result;
}
}
if ($type eq 'assign') {
my @copy = @$expr[2..$#$expr];
# Evaluation is done left-first in JS, unlike in
# Perl, so a = b = c is evaluated in this order:
# - evaluate a
# - evaluate b
# - evaluate c
# - assign c to b
# - assign b to a
# Check first to see whether we have the terms
# of a ? : at the end:
my @qc_terms = @copy >= 3 && (
ref $copy[-2] # avoid stringification
|| $copy[-2] =~ /^(?:[tfu]\z|[si0-9])/
)
? (pop @copy, pop @copy) : ();
# @qc_terms is now in reverse order
# Rough sketch of what we want to accomplish:
# a += b += c += d
#
# do {
# my $l = a;
# $l->set($l->get + do{
# my $l = b;
# $l->set($l->get + do {
# my $l = c;
# $l->set($l->get + d)
# })
# })
# }
# a = b
#
# a->set(b->get)
# a += b = c
#
# do {
# my $l = a;
# $l->set($l->get + b->set(my $v = c))
# }
# Get the first rhs ready:
my $perl_expr = pop @copy;
# Now apply ? : if it's there
if(@qc_terms) {
$perl_expr = (_term_to_perl($perl_expr,cx_bool))[1]
. '?' . (_term_to_perl($qc_terms[1],cx_rv))[1]
. ':' . (_term_to_perl($qc_terms[0],cx_rv))[1]
}
else {
$perl_expr = (_term_to_perl($perl_expr,cx_rv))[1];
}
# short-circuit if we only have ? : and no assignment
# ~~~ check return types from both legs
return ret_rv, $perl_expr unless @copy;
# Iterate through the ops, wrapping each previous expr with
# the current one.
while(@copy) {
my ($op, $term) = (pop @copy, pop @copy);
if(length $op > 1) {
$perl_expr = 'do{my$l='
. (_term_to_perl($term,cx_lv))[1] . ';'
. '$l->set('
. "'JE::Code::Expression::in".substr $op,0,-1,
. "'->(\$l->get,$perl_expr))}"
}
else {
$perl_expr =
(_term_to_perl($term,cx_lv))[1]
. "->set($perl_expr)"
}
}
# ~~~ T and tainted $taint and $val->can('taint')
# and $val = taint $val $taint;
return ret_rv, $perl_expr;
}
if($type eq 'lassoc') { # left-associative
my @copy = @$expr[2..$#$expr];
my $result = (_term_to_perl(shift @copy, cx_rv))[1];
while(@copy) {
# We have to deal with || && specially for the sake
# of short-circuiting
my $op = $copy[0];
if ($op =~ m&^(?:\&\&|\|\|)\z&) {
$result =
"$result$op "
.(_term_to_perl($copy[1],cx_rv))[1]
}
else {
$result = "'JE::Code::Expression::in$op'->"
. "($result,"
.(_term_to_perl($copy[1],cx_rv))[1]
.')'
}
splice @copy, 0, 2; # double shift
}
return ret_rv,$result;
}
if ($type eq 'prefix') {
# ~~~ taintedness
# $$expr[1] -- 'prefix'
# @$expr[2..-2] -- prefix ops
# $$expr[-1] -- operand
my $term = (
_term_to_perl( $$expr[-1],
$$expr[-2] =~ m-^(?:\+\+|\-\-)\z- ? cx_lv :
$$expr[-2] =~ m e^(?:typ\eof|d\el\et\e)\ze ? cx_any :
cx_rv
)
)[1];
$term = "'JE::Code::Expression::pre$_'->($term)"
for reverse @$expr[2..@$expr-2];
return ret_rv, $term;
}
if ($type eq 'postfix') {
# ~~~ taintedness
# ~~~ These are supposed to use the same rules
# as the + and - infix ops for the actual
# addition part. Verify that they do this.
# This will need to be made more efficient:
# do{
# my $l = ...; $l->set(
# "JE::Code::Expression::in+"->(
# $l->get,
# JE::Number->new($global, 1)
# )
# );
# $v
# }
return ret_rv, 'do{'
. 'my$l=' . (_term_to_perl($$expr[2], cx_lv))[1] . ';'
. '$l->set('
. '"JE::Code::Expression::in+"->('
. 'my$v=$l->get->to_number,'
. _cached('JE\'Number->new($global,'
. (-1,1)[$$expr[3] eq '++']. ')')
. ')'
. ')'
. ';$v' x ($cx != cx_void_expr)
.'}'
}
if ($type eq 'new') {
# ~~~ ? T && tainted $taint
#~~~ ? map $_->can('taint') ?taint $_ $taint:$_,
return ret_rv, (_term_to_perl($$expr[2],cx_rv))[1]
.'->construct'
. ( @$expr == 4
? '(' . $$expr[-1]->to_perl . ')'
: '' )
}
if($type eq 'member/call') {
my($type, $obj) = _term_to_perl( $$expr[2],
exists $$expr[3]
? ref $$expr[3]eq 'JE::Code::Subscript'
? cx_rv
: cx_any
: $cx
);
# ~~~ We can probably optimise this not to create LValue objects for calls,
# but use the ‘apply’ method directly.
for (3..$#$expr) {
my $cx = exists $$expr[$_+1]
? ref $$expr[$_+1]eq 'JE::Code::Subscript'
? cx_rv
: cx_any
: $cx;
if(ref $$expr[$_] eq 'JE::Code::Subscript') {
if($cx != cx_lv && $cx != cx_any) {
$obj .= '->prop('
. $$expr[$_]->to_perl
.')';
if($cx == cx_void_expr) {
$type = ret_void
} else {
$obj = "do{my\$v=$obj;"
. 'defined$v?$v:$global->undefined}';
$type = ret_rv;
}
}
else {
$obj = "JE'LValue->new($obj,"
. $$expr[$_]->to_perl . ')';
$type = ret_lv
}
}
else {
# ~~~ taintedness for calls
if($cx != cx_lv && $cx != cx_any) {
$obj = 'do{'
. "my\$v=$obj->call("
. $$expr[$_]->to_perl
. ');'
. 'ref$v eq"JE::LValue"' . (
$cx==cx_void_expr
?'&&$v->get'
:'?$v->get:$v'
)
.'}';
$type = ret_rv;
}
else {
$obj = "$obj->call("
. $$expr[$_]->to_perl
.')';
$type = ret_any
}
# If $obj is an lvalue,
# JE::LValue::call will make
# the lvalue's base object the 'this'
# value. Otherwise,
# JE::Object::Function::call
# will make the
# global object the 'this' value.
}
# ~~~ need some error-checking
}
return $type, $obj; # which may be an lvalue
}
if($type eq 'array') {
if($#$expr < 2) {
return ret_rv, "JE'Object'Array->new(\$global)"
}
my @ary;
for (2..$#$expr) {
if(ref $$expr[$_] eq 'comma') {
ref $$expr[$_-1] eq 'comma' || $_ == 2
and push @ary, 'undef';
}
else {
push @ary,
(_term_to_perl( $$expr[$_], cx_rv ))[1];
}
}
return ret_rv, 'do{my$a=new JE\'Object\'Array $global;'
. '$$$a{array}=[' . join(',',@ary) . '];$a}'
# sticking it in like that
# makes 'undef' elements non-
# existent, rather
# than undefined
}
if($type eq 'hash') {
local @_ = @$expr[2..$#$expr];
if(!@_) { return ret_rv, "JE'Object->new(\$global)"; }
my $obj = 'do{my$o=new JE\'Object $global;';
my ($key, $value);
while(@_) { # I have to loop through them to keep
# the order.
$key = _esc_str(shift);
$value = (_term_to_perl( shift, cx_rv ))[1];
$obj .= "\$o->prop($key,$value);";
}
return ret_rv, $obj . '$o}';
}
if ($type eq 'func') {
# format: [[...], function=> 'name',
# [ params ], $statements_obj, \@vars]
# or: [[...], function =>
# [ params ], $statements_obj, \@vars]
# The code we need to produce will be like this:
# e.g.: function(foo){ bar }
# JE'Object'Function->new({
# scope=>$scope,
# argnames=>['foo'],
# function=>$$cache[0],
# })
# If there is a name (function x (){}), it’s a little
# more complex:
# do{
# my $f = JE'Object'Function->new({
# name => 'x',
# scope =>[@$scope, my $o = new JE::Object $global],
# ... argnames and function ...
# });
# $o->prop({ name => 'x', value => $f,
# readonly => 1, dontdel => 1 });
# $f
# }
# The code object has to be created beforehand and placed
# in the cache.
# ~~~ When we merge this with JE::Parser, the entire code
# for the code object can be placed in a
# $$cache[0]||=....
my($name,$params,$statements) = ref $$expr[2] ?
(undef, @$expr[2,3]) : @$expr[2..4];
my $ret; my $scope;
if($name) {
$name = _esc_str($name);
$ret = 'do{my$f=';
# ~~~ I should be able to remove this ‘bless’. See
# the comment in jE::Object::Function::New
$scope = 'bless('
. '[@$scope,my$o=new JE\'Object $global]'
. ',"JE::Scope")'
}
my $c = $code->{cache}->[my $indx = $cache_indx++] = bless{
map+($_,$code->{$_}),qw/global source file line/
}, "JE::Code";
$c->{tree} = $statements;
$c->{vars} = $$expr[-1];
$c->optimise;
$ret .=
"JE'Object'Function->new({"
. 'scope=>' . ($scope||'$scope') . ','
. (defined $name ? ("name=>$name,") : '')
. 'argnames=>[' . join(',',map _esc_str($_),@$params).'],'
. "function=>\$\$cache[$indx]"
.'})';
if($name) {
$ret .=
';$o->prop({'
. "name=>$name,"
. 'value=>$f,'
. 'readonly=>1,'
. 'dontdel=>1'
.'})}'
}
return ret_rv,$ret;
}
}
sub _cached($) {
my $code_str = shift;
my $indx = $cache_indx++;
return "(\$\$cache[$indx]||=$code_str)";
}
use constant nan => sin 9**9**9;
use constant inf => 9**9**9;
sub _term_to_perl {
my $term = $_[0];
my $cx = $_[1];
# For booleans, we just use the expression itself, since the over-
# loaded booleanness is much faster than ->to_boolean, which has to
# create an object.
# For strings and numbers, ->to_string->value16 is only slightly
# faster than "" for objects, but the latter is 4x the speed for
# strings and about 5% faster for strings (fewer method calls).
# The converse holds true (exact speed differences aside) for
# ->to_number->value vs 0+ (actually, in 5.8.8 the 0+ overload-
# ing for JE::Number is slower than two method calls, but it’s
# faster in 5.10). If we might have an object, we must
# string-/numbify it immediately so that toString and
# valueOf are called at the right time.
# ~~~ Having said all that, we can’t use "..." because it will Uni-
# codify the string (un-UTF-16-ify it). We probably need some
# new method added to JE::Types and all the classes.
if(ref $term eq 'JE::Code::Expression') {
my($rettype,$code,$alt) = $term->to_perl($cx);
return $rettype,
$cx == cx_str ?
defined $alt ? $alt : "$code->to_string->value16" :
$cx == cx_num ?
defined $alt ? $alt : "(0+$code)" :
$cx == cx_bool ?
defined $alt ? $alt : $code :
$code
}
elsif(ref $term) { # ’better be’n array
my $code = _cached("scalar(require JE'Object'RegExp,"
. "JE'Object'RegExp->new(\$global,"
. _esc_str($$term[0])
. (defined $$term[1]?',' . _esc_str($$term[1]):'')
. '))');
return ret_rv,
$cx == cx_str ?
"$code->to_string->value16" :
$cx == cx_num ?
"(0+$code)" :
$code
}
if($term =~ /^i/) {
my $find = "\$scope->find_var("
. _esc_str(substr $term,1)
. ")";
return
$cx == cx_lv || $cx == cx_any ? ( ret_lv, $find ) :
$cx==cx_str ?( ret_str, "$find->get->to_string->value16") :
$cx==cx_bool?( ret_bool,"$find->get" ) :
$cx==cx_num ?( ret_num, "(0+$find->get)" ) :
( ret_rv, "$find->get" )
}
return (ret_void,'0') if $cx == cx_void;
return ret_rv,'die(new JE\'Object\'Error\'ReferenceError $global,'
.'add_line_number"Cannot assign to a non-lvalue")'
if $cx == cx_lv;
$term eq'this'?
$cx == cx_str ? (ret_str, "\$this->to_string->value16") :
$cx == cx_bool ? (ret_bool, '$this') :
$cx == cx_num ? (ret_num, '(0+$this)') :
( ret_rv, '$this', 0)
:
$term =~ /^s/ ? do {
my $esc = _esc_str(my $str = substr $term,1);
$cx == cx_str ? (ret_str, $esc) :
$cx == cx_bool ? (ret_bool, 0+(length $term > 1)) :
$cx == cx_num ? (ret_num,
# ~~~ JE::Number probably needs a function for
# this, as it is a repeat of code found else- # where (in JE::String)
$str =~ /^[\p{Zs}\s\ck]*
(
[+-]?
(?:
(?=[0-9]|\.[0-9]) [0-9]* (?:\.[0-9]*)?
(?:[Ee][+-]?[0-9]+)?
|
Infinity
)
[\p{Zs}\s\ck]*
)?
\z
/ox ? defined $1 ? $1 eq 'Infinity' ? 9**9**9 : $1 : 0 :
$str =~ /^ [\p{Zs}\s\ck]* 0[Xx] ([A-Fa-f0-9]+)
[\p{Zs}\s\ck]*\z/ox
? hex $1 : nan
) : (
ret_str, _cached "_new JE'String \$global,$esc",
$esc
)
} :
$term eq 't' ?
$cx == cx_str ? (ret_str, '"true"') :
$cx == cx_bool ? (ret_bool, 1) :
$cx == cx_num ? (ret_num, 1) :
( ret_bool, '$global->true', 1)
:
$term eq 'f' ?
$cx == cx_str ? (ret_str, '"false"') :
$cx == cx_bool ? (ret_bool, 0) :
$cx == cx_num ? (ret_num, 0) :
( ret_bool, '$global->false', 0)
:
$term eq 'n' ?
$cx == cx_str ? (ret_str, '"null"') :
$cx == cx_bool ? (ret_bool, 0) :
$cx == cx_num ? (ret_num, 0) :
( ret_rv, '$global->null')
:
$cx == cx_str ? (ret_str,
$term == inf ? '"Infinity"' :
$term ==-+inf ? '"-Infinity"':
$term == $term? $term :
'"NaN"'
) :
$cx == cx_bool ? (ret_bool, 0+($term && $term == $term)) :
$cx == cx_num ? (ret_num, $term ) :
( ret_rv, _cached "JE'Number->new(\$global," . (
$term == inf ? '"inf"' : $term == -+inf ? '"-inf"':
$term == $term ? $term : '"nan"'
) . ')', $term )
}
sub JE::Code::Subscript::to_perl {
my $val = (my $self = shift)->[1];
ref $val ? $val->to_perl(cx_str) : _esc_str($val);
}
sub JE::Code::Arguments::to_perl {
my $self = shift;
join ',', map +(_term_to_perl($_,cx_rv))[1], @$self[1..$#$self];
}
1
|