| 1 | package Sidef::Deparse::Sidef; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | use Scalar::Util qw(refaddr reftype); | ||
| 6 | |||
| 7 | # Tracks ref addresses to avoid infinite recursion on circular structures. | ||
| 8 | # Reset on each new() call. | ||
| 9 | my %addr; | ||
| 10 | |||
| 11 | # ========================================================================= | ||
| 12 | # Sidef internal class name → Sidef source type name | ||
| 13 | # ========================================================================= | ||
| 14 | |||
| 15 | my %DATA_TYPES = qw( | ||
| 16 | Sidef::DataTypes::Bool::Bool Bool | ||
| 17 | Sidef::DataTypes::Array::Array Array | ||
| 18 | Sidef::DataTypes::Array::Pair Pair | ||
| 19 | Sidef::DataTypes::Array::Vector Vector | ||
| 20 | Sidef::DataTypes::Array::Matrix Matrix | ||
| 21 | Sidef::DataTypes::Hash::Hash Hash | ||
| 22 | Sidef::DataTypes::Set::Set Set | ||
| 23 | Sidef::DataTypes::Set::Bag Bag | ||
| 24 | Sidef::DataTypes::Regex::Regex Regex | ||
| 25 | Sidef::DataTypes::String::String String | ||
| 26 | Sidef::DataTypes::Number::Number Number | ||
| 27 | Sidef::DataTypes::Number::Mod Mod | ||
| 28 | Sidef::DataTypes::Number::Gauss Gauss | ||
| 29 | Sidef::DataTypes::Number::Quadratic Quadratic | ||
| 30 | Sidef::DataTypes::Number::Quaternion Quaternion | ||
| 31 | Sidef::DataTypes::Number::Complex Complex | ||
| 32 | Sidef::DataTypes::Number::Polynomial Polynomial | ||
| 33 | Sidef::DataTypes::Number::PolynomialMod PolynomialMod | ||
| 34 | Sidef::DataTypes::Number::Fraction Fraction | ||
| 35 | Sidef::DataTypes::Range::Range Range | ||
| 36 | Sidef::DataTypes::Range::RangeNumber RangeNum | ||
| 37 | Sidef::DataTypes::Range::RangeString RangeStr | ||
| 38 | Sidef::DataTypes::Block::Block Block | ||
| 39 | Sidef::DataTypes::Glob::Socket Socket | ||
| 40 | Sidef::DataTypes::Glob::Pipe Pipe | ||
| 41 | Sidef::DataTypes::Glob::Backtick Backtick | ||
| 42 | Sidef::DataTypes::Glob::DirHandle DirHandle | ||
| 43 | Sidef::DataTypes::Glob::FileHandle FileHandle | ||
| 44 | Sidef::DataTypes::Glob::SocketHandle SocketHandle | ||
| 45 | Sidef::DataTypes::Glob::Dir Dir | ||
| 46 | Sidef::DataTypes::Glob::File File | ||
| 47 | Sidef::DataTypes::Perl::Perl Perl | ||
| 48 | Sidef::DataTypes::Object::Object Object | ||
| 49 | Sidef::DataTypes::Sidef::Sidef Sidef | ||
| 50 | Sidef::DataTypes::Object::Lazy Lazy | ||
| 51 | Sidef::DataTypes::Object::LazyMethod LazyMethod | ||
| 52 | Sidef::DataTypes::Object::Enumerator Enumerator | ||
| 53 | Sidef::DataTypes::Variable::NamedParam NamedParam | ||
| 54 | |||
| 55 | Sidef::Math::Math Math | ||
| 56 | Sidef::Meta::Glob::ARGF ARGF | ||
| 57 | Sidef::Meta::Glob::STDIN STDIN | ||
| 58 | Sidef::Meta::Glob::STDOUT STDOUT | ||
| 59 | Sidef::Meta::Glob::STDERR STDERR | ||
| 60 | Sidef::Parser Parser | ||
| 61 | |||
| 62 | Sidef::Types::Nil::Nil nil | ||
| 63 | Sidef::Types::Null::Null null | ||
| 64 | Sidef::Types::Block::Next next | ||
| 65 | Sidef::Types::Block::Break break | ||
| 66 | Sidef::Types::Block::Continue continue | ||
| 67 | |||
| 68 | Sidef::Time::Time Time | ||
| 69 | Sidef::Time::Date Date | ||
| 70 | Sidef::Sys::Sig Sig | ||
| 71 | Sidef::Sys::Sys Sys | ||
| 72 | Sidef::Meta::Unimplemented ... | ||
| 73 | ); | ||
| 74 | |||
| 75 | # ========================================================================= | ||
| 76 | # Constructor | ||
| 77 | # ========================================================================= | ||
| 78 | |||
| 79 | sub new { | ||
| 80 | my (undef, %args) = @_; | ||
| 81 | |||
| 82 | my %opts = ( | ||
| 83 | before => '', | ||
| 84 | between => ";\n", | ||
| 85 | after => ";\n", | ||
| 86 | class => 'main', | ||
| 87 | extra_parens => 0, | ||
| 88 | opt => {}, | ||
| 89 | data_types => {%DATA_TYPES}, | ||
| 90 | %args, | ||
| 91 | ); | ||
| 92 | |||
| 93 | undef %addr; # reset address-tracking hash for this deparse run | ||
| 94 | bless \%opts, __PACKAGE__; | ||
| 95 | } | ||
| 96 | |||
| 97 | # ========================================================================= | ||
| 98 | # Low-level dump helpers | ||
| 99 | # ========================================================================= | ||
| 100 | |||
| 101 | sub _dump_string { | ||
| 102 | my ($self, $str) = @_; | ||
| 103 | Sidef::Types::String::String->new($str)->dump->get_value; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _dump_number { | ||
| 107 | my ($self, $num) = @_; | ||
| 108 | |||
| 109 | my ($type, $str) = $num->_dump; | ||
| 110 | |||
| 111 | state $table = { | ||
| 112 | '@inf@' => 'Inf', | ||
| 113 | '-@inf@' => 'Inf->neg()', | ||
| 114 | '@nan@' => 'NaN', | ||
| 115 | 'inf' => 'Inf', | ||
| 116 | '-inf' => 'Inf->neg()', | ||
| 117 | 'nan' => 'NaN', | ||
| 118 | }; | ||
| 119 | |||
| 120 | state $special_values = { | ||
| 121 | '@inf@' => Sidef::Types::Number::Number->inf, | ||
| 122 | '-@inf@' => Sidef::Types::Number::Number->ninf, | ||
| 123 | '@nan@' => Sidef::Types::Number::Number->nan, | ||
| 124 | }; | ||
| 125 | |||
| 126 | if ($type eq 'complex') { | ||
| 127 | my ($real, $imag) = | ||
| 128 | map { $self->_dump_number($special_values->{lc($_)} // Sidef::Types::Number::Number->new($_)) } split(' ', substr($str, 1, -1)); | ||
| 129 | return "Complex($real, $imag)"; | ||
| 130 | } | ||
| 131 | |||
| 132 | $table->{lc($str)} // do { | ||
| 133 | if ($type eq 'float') { $str . 'f' } | ||
| 134 | elsif (index($str, '/') != -1) { qq{Number("$str")} } | ||
| 135 | else { $str } | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _dump_array { | ||
| 140 | my ($self, $array) = @_; | ||
| 141 | '[' . join(', ', map { $self->deparse_expr(ref($_) eq 'HASH' ? $_ : {self => $_}) } @{$array}) . ']'; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub _dump_reftype { | ||
| 145 | my ($self, $obj) = @_; | ||
| 146 | |||
| 147 | my $ref = ref($obj); | ||
| 148 | |||
| 149 | return $self->{data_types}{$ref} | ||
| 150 | if exists $self->{data_types}{$ref}; | ||
| 151 | |||
| 152 | return $obj->{class} . '::' . $obj->{name} | ||
| 153 | if $ref eq 'Sidef::Variable::ClassInit' | ||
| 154 | || $ref eq 'Sidef::Variable::Struct' | ||
| 155 | || $ref eq 'Sidef::Variable::Subset'; | ||
| 156 | |||
| 157 | return 'Block' | ||
| 158 | if $ref eq 'Sidef::Types::Block::BlockInit'; | ||
| 159 | |||
| 160 | return substr($ref, rindex($ref, '::') + 2); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _dump_vars { | ||
| 164 | my ($self, @vars) = @_; | ||
| 165 | |||
| 166 | join( | ||
| 167 | ', ', | ||
| 168 | map { | ||
| 169 | my $v = $_; | ||
| 170 | |||
| 171 | my $prefix = | ||
| 172 | exists($v->{array}) ? '*' | ||
| 173 | : exists($v->{hash}) ? ':' | ||
| 174 | : ''; | ||
| 175 | |||
| 176 | my $class_prefix = | ||
| 177 | (exists($v->{class}) && $v->{class} ne $self->{class}) | ||
| 178 | ? $v->{class} . '::' | ||
| 179 | : ''; | ||
| 180 | |||
| 181 | my $type_prefix = | ||
| 182 | (exists($v->{ref_type}) && ($v->{type} eq 'var' || $v->{type} eq 'has')) | ||
| 183 | ? $self->_dump_reftype($v->{ref_type}) . ' ' | ||
| 184 | : ''; | ||
| 185 | |||
| 186 | my $subset = | ||
| 187 | exists($v->{subset}) | ||
| 188 | ? ' < ' . $self->_dump_reftype($v->{subset}) | ||
| 189 | : ''; | ||
| 190 | |||
| 191 | my $where_block = | ||
| 192 | exists($v->{where_block}) | ||
| 193 | ? $self->deparse_expr({self => $v->{where_block}}) | ||
| 194 | : ''; | ||
| 195 | |||
| 196 | my $where_expr = | ||
| 197 | exists($v->{where_expr}) | ||
| 198 | ? '(' . $self->deparse_expr({self => $v->{where_expr}}) . ')' | ||
| 199 | : ''; | ||
| 200 | |||
| 201 | my $default = | ||
| 202 | exists($v->{value}) | ||
| 203 | ? '=(' . $self->deparse_expr({self => $v->{value}}) . ')' | ||
| 204 | : ''; | ||
| 205 | |||
| 206 | $prefix . $class_prefix . $type_prefix . $v->{name} . $subset . $where_block . $where_expr . $default; | ||
| 207 | } @vars | ||
| 208 | ); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub _dump_init_vars { | ||
| 212 | my ($self, $init_obj) = @_; | ||
| 213 | |||
| 214 | my $type = $init_obj->{vars}[0]{type} // 'var'; | ||
| 215 | my $code = $type . '(' . $self->_dump_vars(@{$init_obj->{vars}}) . ')'; | ||
| 216 | |||
| 217 | $code .= '=' . $self->deparse_args($init_obj->{args}) | ||
| 218 | if exists $init_obj->{args}; | ||
| 219 | |||
| 220 | $code; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub _dump_class_name { | ||
| 224 | my ($self, $obj) = @_; | ||
| 225 | |||
| 226 | return $self->_dump_reftype($obj->{name}) if ref($obj->{name}); | ||
| 227 | return '' if $obj->{name} eq ''; | ||
| 228 | return $obj->{class} . '::' . $obj->{name}; | ||
| 229 | } | ||
| 230 | |||
| 231 | # ========================================================================= | ||
| 232 | # Generic deparsing helpers | ||
| 233 | # ========================================================================= | ||
| 234 | |||
| 235 | sub deparse_generic { | ||
| 236 | my ($self, $before, $sep, $after, @args) = @_; | ||
| 237 | |||
| 238 | $before | ||
| 239 | . join($sep, | ||
| 240 | grep { $_ ne '' } | ||
| 241 | map { ref($_) eq 'HASH' ? $self->deparse_script($_) : ref($_) ? $self->deparse_expr({self => $_}) : $self->_dump_string($_) } @args) | ||
| 242 | . $after; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub deparse_args { | ||
| 246 | my ($self, @args) = @_; | ||
| 247 | $self->deparse_generic('(', ', ', ')', @args); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub deparse_bare_block { | ||
| 251 | my ($self, @args) = @_; | ||
| 252 | |||
| 253 | $Sidef::SPACES += $Sidef::SPACES_INCR; | ||
| 254 | |||
| 255 | my $indent = ' ' x $Sidef::SPACES; | ||
| 256 | my $outer_indent = ' ' x ($Sidef::SPACES - $Sidef::SPACES_INCR); | ||
| 257 | |||
| 258 | my $code = $self->deparse_generic("{(\n$indent", ");(\n$indent", "\n${outer_indent})}", @args); | ||
| 259 | |||
| 260 | $Sidef::SPACES -= $Sidef::SPACES_INCR; | ||
| 261 | $code; | ||
| 262 | } | ||
| 263 | |||
| 264 | # ========================================================================= | ||
| 265 | # deparse_expr node handlers (one per complex node type) | ||
| 266 | # ========================================================================= | ||
| 267 | |||
| 268 | sub _deparse_variable { | ||
| 269 | my ($self, $expr, $obj, $ref, $refaddr) = @_; | ||
| 270 | |||
| 271 | my $type = $obj->{type}; | ||
| 272 | |||
| 273 | if ( $type eq 'var' | ||
| 274 | || $type eq 'static' | ||
| 275 | || $type eq 'const' | ||
| 276 | || $type eq 'has' | ||
| 277 | || $type eq 'global') { | ||
| 278 | return $obj->{name} =~ /^[0-9]+\z/ | ||
| 279 | ? '$' . $obj->{name} | ||
| 280 | : ($obj->{class} ne $self->{class} ? $obj->{class} . '::' : '') . $obj->{name}; | ||
| 281 | } | ||
| 282 | |||
| 283 | if ($type eq 'func' || $type eq 'method') { | ||
| 284 | if ($addr{$refaddr}++) { | ||
| 285 | return $obj->{name} eq '' | ||
| 286 | ? '__FUNC__' | ||
| 287 | : ($obj->{class} ne $self->{class} ? $obj->{class} . '::' : '') . $obj->{name}; | ||
| 288 | } | ||
| 289 | |||
| 290 | my $block = $obj->{value}; | ||
| 291 | my $code = $type . ' ' . $obj->{name}; | ||
| 292 | |||
| 293 | local $self->{class} = $obj->{class}; | ||
| 294 | my $var_obj = delete $block->{init_vars}; | ||
| 295 | my $skip = ($type eq 'method') ? 1 : 0; | ||
| 296 | my @sig_vars = @{$var_obj->{vars}}[$skip .. $#{$var_obj->{vars}}]; | ||
| 297 | |||
| 298 | $code .= '(' . $self->_dump_vars(@sig_vars) . ') '; | ||
| 299 | $code .= 'is cached ' if exists $obj->{cached}; | ||
| 300 | |||
| 301 | if (exists $obj->{returns}) { | ||
| 302 | my $ret = join(',', map { $self->deparse_expr({self => $_}) } @{$obj->{returns}}); | ||
| 303 | $code .= "-> ($ret) "; | ||
| 304 | } | ||
| 305 | |||
| 306 | $code .= $self->deparse_expr({self => $block}); | ||
| 307 | $block->{init_vars} = $var_obj; | ||
| 308 | return $code; | ||
| 309 | } | ||
| 310 | |||
| 311 | return ''; | ||
| 312 | } | ||
| 313 | |||
| 314 | sub _deparse_struct { | ||
| 315 | my ($self, $expr, $obj, $ref, $refaddr) = @_; | ||
| 316 | |||
| 317 | my $name = $self->_dump_class_name($obj); | ||
| 318 | return $name if $addr{$refaddr}++; | ||
| 319 | return "struct $name" . ' {' . $self->_dump_vars(@{$obj->{vars}}) . '}'; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub _deparse_subset { | ||
| 323 | my ($self, $expr, $obj, $ref, $refaddr) = @_; | ||
| 324 | |||
| 325 | my $name = $self->_dump_class_name($obj); | ||
| 326 | return $name if $addr{$refaddr}++; | ||
| 327 | |||
| 328 | my $inherits = | ||
| 329 | exists($obj->{inherits}) | ||
| 330 | ? ' < ' . join(',', map { $self->deparse_expr({self => $_}) } @{$obj->{inherits}}) | ||
| 331 | : ''; | ||
| 332 | |||
| 333 | my $block = | ||
| 334 | exists($obj->{block}) | ||
| 335 | ? ' ' . $self->deparse_expr({self => $obj->{block}}) | ||
| 336 | : ''; | ||
| 337 | |||
| 338 | return "subset $name$inherits$block"; | ||
| 339 | } | ||
| 340 | |||
| 341 | sub _deparse_class_init { | ||
| 342 | my ($self, $expr, $obj, $ref, $refaddr) = @_; | ||
| 343 | |||
| 344 | return $self->_dump_class_name($obj) if $addr{$refaddr}++; | ||
| 345 | |||
| 346 | local $self->{class} = $obj->{class}; | ||
| 347 | my $block = $obj->{block}; | ||
| 348 | my $name = $self->_dump_class_name($obj); | ||
| 349 | my $class_vars = $self->_dump_vars(@{$obj->{vars}}); | ||
| 350 | my $code = 'class ' . $name; | ||
| 351 | |||
| 352 | $code .= "($class_vars)" if length($class_vars); | ||
| 353 | |||
| 354 | if (exists $obj->{inherit}) { | ||
| 355 | my $inherited = join(', ', grep { $_ ne $name } | ||
| 356 | map { $self->_dump_class_name($_) } @{$obj->{inherit}}); | ||
| 357 | $code .= " < $inherited " if $inherited ne ''; | ||
| 358 | } | ||
| 359 | |||
| 360 | $code .= $self->deparse_expr({self => $block}); | ||
| 361 | return $code; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub _deparse_block_init { | ||
| 365 | my ($self, $expr, $obj, $ref, $refaddr) = @_; | ||
| 366 | |||
| 367 | if ($addr{$refaddr}++) { | ||
| 368 | return keys(%{$obj}) ? '__BLOCK__' : 'Block'; | ||
| 369 | } | ||
| 370 | |||
| 371 | return 'Block' unless keys %{$obj}; | ||
| 372 | |||
| 373 | my $code = '{'; | ||
| 374 | |||
| 375 | if (exists $obj->{init_vars}) { | ||
| 376 | $code .= '|' . $self->_dump_vars(@{$obj->{init_vars}{vars}}) . '|'; | ||
| 377 | } | ||
| 378 | |||
| 379 | $Sidef::SPACES += $Sidef::SPACES_INCR; | ||
| 380 | my @statements = $self->deparse_script($obj->{code}); | ||
| 381 | |||
| 382 | $statements[-1] = '(' . $statements[-1] . ')' if @statements; | ||
| 383 | |||
| 384 | if (@statements == 1 && length($statements[0]) + $Sidef::SPACES <= 80) { | ||
| 385 | $code .= " $statements[0] }"; | ||
| 386 | } | ||
| 387 | elsif (@statements) { | ||
| 388 | my $indent = ' ' x $Sidef::SPACES; | ||
| 389 | my $outer_indent = ' ' x ($Sidef::SPACES - $Sidef::SPACES_INCR); | ||
| 390 | $code .= "\n$indent" . join(";\n$indent", @statements) . "\n${outer_indent}}"; | ||
| 391 | } | ||
| 392 | else { | ||
| 393 | $code .= '}'; | ||
| 394 | } | ||
| 395 | |||
| 396 | $Sidef::SPACES -= $Sidef::SPACES_INCR; | ||
| 397 | return $code; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub _deparse_if { | ||
| 401 | my ($self, $expr, $obj) = @_; | ||
| 402 | |||
| 403 | my $code = ''; | ||
| 404 | for my $i (0 .. $#{$obj->{if}}) { | ||
| 405 | my $info = $obj->{if}[$i]; | ||
| 406 | $code .= ($i == 0 ? 'if' : 'elsif') . $self->deparse_args($info->{expr}) . $self->deparse_expr({self => $info->{block}}); | ||
| 407 | } | ||
| 408 | |||
| 409 | $code .= 'else' . $self->deparse_bare_block($obj->{else}{block}{code}) | ||
| 410 | if exists $obj->{else}; | ||
| 411 | |||
| 412 | return $code; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub _deparse_with { | ||
| 416 | my ($self, $expr, $obj) = @_; | ||
| 417 | |||
| 418 | my $code = ''; | ||
| 419 | for my $i (0 .. $#{$obj->{with}}) { | ||
| 420 | my $info = $obj->{with}[$i]; | ||
| 421 | $code .= ($i == 0 ? 'with' : 'orwith') . $self->deparse_args($info->{expr}) . $self->deparse_expr({self => $info->{block}}); | ||
| 422 | } | ||
| 423 | |||
| 424 | $code .= 'else' . $self->deparse_bare_block($obj->{else}{block}{code}) | ||
| 425 | if exists $obj->{else}; | ||
| 426 | |||
| 427 | return $code; | ||
| 428 | } | ||
| 429 | |||
| 430 | sub _deparse_for_in { | ||
| 431 | my ($self, $expr, $obj) = @_; | ||
| 432 | |||
| 433 | my $loops = join( | ||
| 434 | ', ', | ||
| 435 | map { | ||
| 436 | my $loop = $_; | ||
| 437 | join(',', map { ($_->{slurpy} ? ($_->{array} ? '*' : ':') : '') . $self->deparse_expr({self => $_}) } @{$loop->{vars}}) . ' in (' | ||
| 438 | . $self->deparse_expr({self => $loop->{expr}}) . ')' | ||
| 439 | } @{$obj->{loops}} | ||
| 440 | ); | ||
| 441 | |||
| 442 | return 'for ' . $loops . ' ' . $self->deparse_bare_block($obj->{block}{code}); | ||
| 443 | } | ||
| 444 | |||
| 445 | sub _deparse_included { | ||
| 446 | my ($self, $expr, $obj) = @_; | ||
| 447 | |||
| 448 | my @statements; | ||
| 449 | for my $info (@{$obj->{included}}) { | ||
| 450 | if ($info->{name} ne '') { # create a new namespace | ||
| 451 | local $self->{class} = $info->{name}; | ||
| 452 | push @statements, "module $info->{name} " . $self->deparse_bare_block($info->{ast}); | ||
| 453 | } | ||
| 454 | else { # included in the current namespace | ||
| 455 | push @statements, join(";\n", $self->deparse_script($info->{ast})); | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | return join(";\n", @statements); | ||
| 460 | } | ||
| 461 | |||
| 462 | # ========================================================================= | ||
| 463 | # deparse_expr: index and call application | ||
| 464 | # ========================================================================= | ||
| 465 | |||
| 466 | sub _apply_indices { | ||
| 467 | my ($self, $code, $expr) = @_; | ||
| 468 | |||
| 469 | return $code unless exists $expr->{ind}; | ||
| 470 | |||
| 471 | $code = "($code)"; | ||
| 472 | for my $ind (@{$expr->{ind}}) { | ||
| 473 | if (exists $ind->{array}) { | ||
| 474 | $code .= $self->_dump_array($ind->{array}); | ||
| 475 | } | ||
| 476 | else { | ||
| 477 | $code .= '{' . join(',', map { ref($_) eq 'HASH' ? $self->deparse_expr($_) : $self->deparse_generic('', '', '', $_) } @{$ind->{hash}}) . '}'; | ||
| 478 | } | ||
| 479 | } | ||
| 480 | |||
| 481 | return $code; | ||
| 482 | } | ||
| 483 | |||
| 484 | # Attempt constant-folding for unary +/- applied to a literal number. | ||
| 485 | # Returns the deparsed folded string, or undef if folding is not applicable. | ||
| 486 | sub _try_fold_unary { | ||
| 487 | my ($self, $ref, $code, $method, $call) = @_; | ||
| 488 | |||
| 489 | return unless $ref eq 'Sidef::Operator::Unary' && $code eq ''; | ||
| 490 | |||
| 491 | state $unary_methods = {'-' => 'neg', '+' => undef}; | ||
| 492 | return unless exists $unary_methods->{$method}; | ||
| 493 | |||
| 494 | # Drill down through the argument structure to find a bare Number literal. | ||
| 495 | my $data = $call->{arg}; | ||
| 496 | return unless @$data == 1; | ||
| 497 | |||
| 498 | $data = $data->[0]; | ||
| 499 | return unless ref($data) eq 'HASH' && keys(%$data) == 1; | ||
| 500 | |||
| 501 | my ($class) = keys(%$data); | ||
| 502 | $data = $data->{$class}; | ||
| 503 | return unless ref($data) eq 'ARRAY' && @$data == 1; | ||
| 504 | |||
| 505 | $data = $data->[0]; | ||
| 506 | $data = $data->{self} | ||
| 507 | if ref($data) eq 'HASH' && keys(%$data) == 1 && exists $data->{self}; | ||
| 508 | |||
| 509 | return unless ref($data) eq 'Sidef::Types::Number::Number'; | ||
| 510 | |||
| 511 | my $method_name = $unary_methods->{$method}; | ||
| 512 | return | ||
| 513 | $self->deparse_expr( | ||
| 514 | { | ||
| 515 | self => defined($method_name) ? $data->$method_name : $data | ||
| 516 | } | ||
| 517 | ); | ||
| 518 | } | ||
| 519 | |||
| 520 | sub _apply_calls { | ||
| 521 | my ($self, $code, $expr, $ref) = @_; | ||
| 522 | |||
| 523 | return $code unless exists $expr->{call}; | ||
| 524 | |||
| 525 | for my $i (0 .. $#{$expr->{call}}) { | ||
| 526 | my $call = $expr->{call}[$i]; | ||
| 527 | my $method = $call->{method}; | ||
| 528 | |||
| 529 | # A method literally named 'call' with arguments: treat as bare invocation. | ||
| 530 | if (defined $method && $method eq 'call' && exists $call->{arg}) { | ||
| 531 | $code = "($code)" | ||
| 532 | if substr($code, 0, 1) eq '(' && substr($code, -1) eq ')'; | ||
| 533 | undef $method; | ||
| 534 | } | ||
| 535 | |||
| 536 | if (defined $method) { | ||
| 537 | |||
| 538 | # Prefix-colon shorthand (first call only) | ||
| 539 | if ($i == 0 && $ref eq 'Sidef::Meta::PrefixColon' && $method eq ':') { | ||
| 540 | $code = ':' . $self->deparse_args(@{$call->{arg}}); | ||
| 541 | next; | ||
| 542 | } | ||
| 543 | |||
| 544 | if (ref($method) ne '') { | ||
| 545 | |||
| 546 | # Dynamic/computed method expression | ||
| 547 | $code .= '->(' . $self->deparse_expr(ref($method) eq 'HASH' ? $method : {self => $method}) . ')'; | ||
| 548 | } | ||
| 549 | elsif ($method =~ /^[^\W\d]/) { | ||
| 550 | |||
| 551 | # Named (word) method | ||
| 552 | if ($ref eq 'Sidef::Types::Block::BlockInit' && $method eq 'loop') { | ||
| 553 | $code = "loop $code"; | ||
| 554 | } | ||
| 555 | else { | ||
| 556 | $code .= '->' if $code ne ''; | ||
| 557 | $code .= $method; | ||
| 558 | } | ||
| 559 | $code .= '()' unless exists $call->{arg}; | ||
| 560 | } | ||
| 561 | else { | ||
| 562 | # Operator method — try constant-folding first | ||
| 563 | if (defined(my $folded = $self->_try_fold_unary($ref, $code, $method, $call))) { | ||
| 564 | $code = $folded; | ||
| 565 | next; | ||
| 566 | } | ||
| 567 | |||
| 568 | if ($ref eq 'Sidef::Variable::Ref' || $ref eq 'Sidef::Operator::Unary') { | ||
| 569 | $code .= $method; | ||
| 570 | } | ||
| 571 | else { | ||
| 572 | $code = "($code)->$method"; | ||
| 573 | } | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | # Optional keyword prefix before argument list | ||
| 578 | if (exists $call->{keyword}) { | ||
| 579 | if ($code ne '') { | ||
| 580 | if (substr($code, 0, 1) eq '(' && substr($code, -1) eq ')') { | ||
| 581 | $code = "($code)"; | ||
| 582 | } | ||
| 583 | else { | ||
| 584 | $code .= ' '; | ||
| 585 | } | ||
| 586 | } | ||
| 587 | $code .= $call->{keyword}; | ||
| 588 | } | ||
| 589 | |||
| 590 | $code .= $self->deparse_args(@{$call->{arg}}) if exists $call->{arg}; | ||
| 591 | |||
| 592 | if (exists $call->{block}) { | ||
| 593 | $code .= $self->deparse_bare_block(@{$call->{block}}); | ||
| 594 | next; | ||
| 595 | } | ||
| 596 | } | ||
| 597 | |||
| 598 | return $code; | ||
| 599 | } | ||
| 600 | |||
| 601 | # ========================================================================= | ||
| 602 | # Dispatch table: ref type → handler (populated after all handlers defined) | ||
| 603 | # ========================================================================= | ||
| 604 | |||
| 605 | my %EXPR_DISPATCH; | ||
| 606 | |||
| 607 | # ========================================================================= | ||
| 608 | # Main expression deparsing entry point | ||
| 609 | # ========================================================================= | ||
| 610 | |||
| 611 | sub deparse_expr { | ||
| 612 | my ($self, $expr) = @_; | ||
| 613 | |||
| 614 | my $obj = $expr->{self}; | ||
| 615 | my $ref = ref($obj); | ||
| 616 | my $refaddr = refaddr($obj); | ||
| 617 | my $code = ''; | ||
| 618 | |||
| 619 | if ($ref eq 'HASH') { | ||
| 620 | $code = join( | ||
| 621 | ', ', exists($obj->{self}) | ||
| 622 | ? $self->deparse_expr($obj) | ||
| 623 | : $self->deparse_script($obj) | ||
| 624 | ); | ||
| 625 | $code = "($code)" if $self->{extra_parens}; | ||
| 626 | } | ||
| 627 | elsif (my $handler = $EXPR_DISPATCH{$ref}) { | ||
| 628 | $code = $self->$handler($expr, $obj, $ref, $refaddr); | ||
| 629 | } | ||
| 630 | elsif (exists $self->{data_types}{$ref}) { | ||
| 631 | $code = $self->{data_types}{$ref}; | ||
| 632 | } | ||
| 633 | elsif ($ref =~ /^Sidef::/ && UNIVERSAL::can($obj, 'dump')) { | ||
| 634 | $code = $obj->dump->get_value; | ||
| 635 | } | ||
| 636 | |||
| 637 | $code = $self->_apply_indices($code, $expr); | ||
| 638 | $code = $self->_apply_calls($code, $expr, $ref); | ||
| 639 | |||
| 640 | return $code; | ||
| 641 | } | ||
| 642 | |||
| 643 | # ========================================================================= | ||
| 644 | # Top-level deparse methods | ||
| 645 | # ========================================================================= | ||
| 646 | |||
| 647 | sub deparse_script { | ||
| 648 | my ($self, $struct) = @_; | ||
| 649 | |||
| 650 | my @results; | ||
| 651 | for my $class (keys %$struct) { | ||
| 652 | for my $i (0 .. $#{$struct->{$class}}) { | ||
| 653 | my $expr = $struct->{$class}[$i]; | ||
| 654 | push @results, ref($expr) eq 'HASH' | ||
| 655 | ? $self->deparse_expr($expr) | ||
| 656 | : $self->deparse_expr({self => $expr}); | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | wantarray ? @results : $results[-1]; | ||
| 661 | } | ||
| 662 | |||
| 663 | sub deparse { | ||
| 664 | my ($self, $struct) = @_; | ||
| 665 | my @statements = $self->deparse_script($struct); | ||
| 666 | $self->{before} . join($self->{between}, grep { $_ ne '' } @statements) . $self->{after}; | ||
| 667 | } | ||
| 668 | |||
| 669 | # ========================================================================= | ||
| 670 | # Populate dispatch table (all handlers must be defined above this point) | ||
| 671 | # ========================================================================= | ||
| 672 | |||
| 673 | { | ||
| 674 | # Shared handler: classes that deparse as "ClassName::memberName" | ||
| 675 | my $class_or_name = sub { | ||
| 676 | my ($self, $expr, $obj) = @_; | ||
| 677 | ($obj->{class} ne $self->{class} ? $obj->{class} . '::' : '') . $obj->{name}; | ||
| 678 | }; | ||
| 679 | |||
| 680 | # Shared handler: Array and HCArray both deparse via _dump_array | ||
| 681 | my $dump_array = sub { | ||
| 682 | my ($self, $expr, $obj) = @_; | ||
| 683 | $self->_dump_array($obj); | ||
| 684 | }; | ||
| 685 | |||
| 686 | %EXPR_DISPATCH = ( | ||
| 687 | |||
| 688 | # --- Variables --------------------------------------------------- | ||
| 689 | |||
| 690 | 'Sidef::Variable::Variable' => \&_deparse_variable, | ||
| 691 | |||
| 692 | 'Sidef::Variable::ClassAttr' => sub { $_[0]->_dump_init_vars($_[2]) }, | ||
| 693 | |||
| 694 | 'Sidef::Variable::Init' => sub { $_[0]->_dump_init_vars($_[2]) }, | ||
| 695 | |||
| 696 | 'Sidef::Variable::ConstInit' => sub { | ||
| 697 | my ($self, $expr, $obj) = @_; | ||
| 698 | my $sep = | ||
| 699 | $obj->{type} eq 'global' | ||
| 700 | ? ', ' | ||
| 701 | : ";\n" . ' ' x $Sidef::SPACES; | ||
| 702 | join($sep, $self->_dump_init_vars($obj)); | ||
| 703 | }, | ||
| 704 | |||
| 705 | 'Sidef::Variable::Const' => $class_or_name, | ||
| 706 | 'Sidef::Variable::Static' => $class_or_name, | ||
| 707 | 'Sidef::Variable::Define' => $class_or_name, | ||
| 708 | |||
| 709 | 'Sidef::Variable::ClassVar' => sub { | ||
| 710 | my ($self, $expr, $obj) = @_; | ||
| 711 | $self->_dump_reftype($obj->{class}) . '!' . $obj->{name}; | ||
| 712 | }, | ||
| 713 | |||
| 714 | 'Sidef::Variable::Local' => sub { | ||
| 715 | my ($self, $expr, $obj) = @_; | ||
| 716 | 'local (' . $self->deparse_script($obj->{expr}) . ')'; | ||
| 717 | }, | ||
| 718 | |||
| 719 | 'Sidef::Variable::NamedParam' => sub { | ||
| 720 | my ($self, $expr, $obj) = @_; | ||
| 721 | $obj->{name} . ':' . $self->deparse_args(@{$obj->{value}}); | ||
| 722 | }, | ||
| 723 | |||
| 724 | 'Sidef::Variable::Label' => sub { | ||
| 725 | my ($self, $expr, $obj) = @_; | ||
| 726 | '@:' . $obj->{name}; | ||
| 727 | }, | ||
| 728 | |||
| 729 | 'Sidef::Variable::Ref' => sub { | ||
| 730 | my ($self, $expr) = @_; | ||
| 731 | exists($expr->{call}) ? '' : 'Ref'; | ||
| 732 | }, | ||
| 733 | |||
| 734 | 'Sidef::Variable::Magic' => sub { | ||
| 735 | my ($self, $expr, $obj) = @_; | ||
| 736 | $obj->{dump} // $obj->{name}; | ||
| 737 | }, | ||
| 738 | |||
| 739 | # --- Classes and structures -------------------------------------- | ||
| 740 | |||
| 741 | 'Sidef::Variable::ClassInit' => \&_deparse_class_init, | ||
| 742 | 'Sidef::Variable::Struct' => \&_deparse_struct, | ||
| 743 | 'Sidef::Variable::Subset' => \&_deparse_subset, | ||
| 744 | |||
| 745 | # --- Blocks ------------------------------------------------------ | ||
| 746 | |||
| 747 | 'Sidef::Types::Block::BlockInit' => \&_deparse_block_init, | ||
| 748 | |||
| 749 | 'Sidef::Types::Block::Return' => sub { | ||
| 750 | my ($self, $expr) = @_; | ||
| 751 | exists($expr->{call}) ? '' : 'return'; | ||
| 752 | }, | ||
| 753 | |||
| 754 | 'Sidef::Types::Block::Do' => sub { | ||
| 755 | my ($self, $expr, $obj) = @_; | ||
| 756 | 'do ' . $self->deparse_expr({self => $obj->{block}}); | ||
| 757 | }, | ||
| 758 | |||
| 759 | 'Sidef::Types::Block::Loop' => sub { | ||
| 760 | my ($self, $expr, $obj) = @_; | ||
| 761 | 'loop ' . $self->deparse_expr({self => $obj->{block}}); | ||
| 762 | }, | ||
| 763 | |||
| 764 | 'Sidef::Types::Block::Try' => sub { | ||
| 765 | my ($self, $expr, $obj) = @_; | ||
| 766 | my $code = 'try ' . $self->deparse_expr({self => $obj->{try}}); | ||
| 767 | $code .= ' catch ' . $self->deparse_expr({self => $obj->{catch}}) | ||
| 768 | if defined $obj->{catch}; | ||
| 769 | $code; | ||
| 770 | }, | ||
| 771 | |||
| 772 | 'Sidef::Types::Block::If' => \&_deparse_if, | ||
| 773 | |||
| 774 | 'Sidef::Types::Block::While' => sub { | ||
| 775 | my ($self, $expr, $obj) = @_; | ||
| 776 | 'while' . $self->deparse_args($obj->{expr}) . $self->deparse_expr({self => $obj->{block}}); | ||
| 777 | }, | ||
| 778 | |||
| 779 | 'Sidef::Types::Block::ForEach' => sub { | ||
| 780 | my ($self, $expr, $obj) = @_; | ||
| 781 | 'foreach' . $self->deparse_args($obj->{expr}) . $self->deparse_expr({self => $obj->{block}}); | ||
| 782 | }, | ||
| 783 | |||
| 784 | 'Sidef::Types::Block::CFor' => sub { | ||
| 785 | my ($self, $expr, $obj) = @_; | ||
| 786 | 'for(' . join(';', map { $self->deparse_args($_) } @{$obj->{expr}}) . ')' . $self->deparse_bare_block($obj->{block}{code}); | ||
| 787 | }, | ||
| 788 | |||
| 789 | 'Sidef::Types::Block::ForIn' => \&_deparse_for_in, | ||
| 790 | |||
| 791 | 'Sidef::Types::Block::Given' => sub { | ||
| 792 | my ($self, $expr, $obj) = @_; | ||
| 793 | 'given ' . $self->deparse_args($obj->{expr}) . $self->deparse_expr({self => $obj->{block}}); | ||
| 794 | }, | ||
| 795 | |||
| 796 | 'Sidef::Types::Block::When' => sub { | ||
| 797 | my ($self, $expr, $obj) = @_; | ||
| 798 | 'when(' . $self->deparse_args($obj->{expr}) . ')' . $self->deparse_expr({self => $obj->{block}}); | ||
| 799 | }, | ||
| 800 | |||
| 801 | 'Sidef::Types::Block::Case' => sub { | ||
| 802 | my ($self, $expr, $obj) = @_; | ||
| 803 | 'case(' . $self->deparse_args($obj->{expr}) . ')' . $self->deparse_expr({self => $obj->{block}}); | ||
| 804 | }, | ||
| 805 | |||
| 806 | 'Sidef::Types::Block::Default' => sub { | ||
| 807 | my ($self, $expr, $obj) = @_; | ||
| 808 | 'default' . $self->deparse_bare_block($obj->{block}{code}); | ||
| 809 | }, | ||
| 810 | |||
| 811 | 'Sidef::Types::Block::With' => \&_deparse_with, | ||
| 812 | |||
| 813 | 'Sidef::Types::Block::Gather' => sub { | ||
| 814 | my ($self, $expr, $obj) = @_; | ||
| 815 | 'gather ' . $self->deparse_expr({self => $obj->{block}}); | ||
| 816 | }, | ||
| 817 | |||
| 818 | 'Sidef::Types::Block::Take' => sub { | ||
| 819 | my ($self, $expr, $obj) = @_; | ||
| 820 | 'take' . $self->deparse_args($obj->{expr}); | ||
| 821 | }, | ||
| 822 | |||
| 823 | # --- Control flow / ternary -------------------------------------- | ||
| 824 | |||
| 825 | 'Sidef::Types::Bool::Ternary' => sub { | ||
| 826 | my ($self, $expr, $obj) = @_; | ||
| 827 | '(' . $self->deparse_script($obj->{cond}) . ' ? ' . $self->deparse_args($obj->{true}) . ' : ' . $self->deparse_args($obj->{false}) . ')'; | ||
| 828 | }, | ||
| 829 | |||
| 830 | # --- Meta / special syntax --------------------------------------- | ||
| 831 | |||
| 832 | 'Sidef::Meta::PrefixMethod' => sub { | ||
| 833 | my ($self, $expr, $obj) = @_; | ||
| 834 | "::$obj->{name}" . $self->deparse_args($obj->{expr}); | ||
| 835 | }, | ||
| 836 | |||
| 837 | 'Sidef::Meta::Assert' => sub { | ||
| 838 | my ($self, $expr, $obj) = @_; | ||
| 839 | $obj->{act} . $self->deparse_args($obj->{arg}); | ||
| 840 | }, | ||
| 841 | |||
| 842 | 'Sidef::Meta::Error' => sub { | ||
| 843 | my ($self, $expr, $obj) = @_; | ||
| 844 | 'die' . $self->deparse_args($obj->{arg}); | ||
| 845 | }, | ||
| 846 | |||
| 847 | 'Sidef::Meta::Warning' => sub { | ||
| 848 | my ($self, $expr, $obj) = @_; | ||
| 849 | 'warn' . $self->deparse_args($obj->{arg}); | ||
| 850 | }, | ||
| 851 | |||
| 852 | 'Sidef::Meta::Module' => sub { | ||
| 853 | my ($self, $expr, $obj) = @_; | ||
| 854 | local $self->{class} = $obj->{name}; | ||
| 855 | "module $obj->{name} " . $self->deparse_bare_block($obj->{block}{code}); | ||
| 856 | }, | ||
| 857 | |||
| 858 | 'Sidef::Meta::Included' => \&_deparse_included, | ||
| 859 | |||
| 860 | 'Sidef::Meta::Glob::DATA' => sub { | ||
| 861 | my ($self, $expr, $obj) = @_; | ||
| 862 | if (!exists $addr{$obj->{data}}) { | ||
| 863 | $self->{after} .= "\n__DATA__\n" . ${$obj->{data}}; | ||
| 864 | $addr{$obj->{data}} = 1; | ||
| 865 | } | ||
| 866 | 'DATA'; | ||
| 867 | }, | ||
| 868 | |||
| 869 | # --- Eval -------------------------------------------------------- | ||
| 870 | |||
| 871 | 'Sidef::Eval::Eval' => sub { | ||
| 872 | my ($self, $expr, $obj) = @_; | ||
| 873 | 'eval' . $self->deparse_args($obj->{expr}); | ||
| 874 | }, | ||
| 875 | |||
| 876 | # --- Module imports ---------------------------------------------- | ||
| 877 | |||
| 878 | 'Sidef::Module::OO' => sub { | ||
| 879 | my ($self, $expr, $obj) = @_; | ||
| 880 | '%O' . $self->_dump_string($obj->{module}); | ||
| 881 | }, | ||
| 882 | |||
| 883 | 'Sidef::Module::Func' => sub { | ||
| 884 | my ($self, $expr, $obj) = @_; | ||
| 885 | '%S' . $self->_dump_string($obj->{module}); | ||
| 886 | }, | ||
| 887 | |||
| 888 | # --- Literal data types ------------------------------------------ | ||
| 889 | |||
| 890 | 'Sidef::Types::Number::Number' => sub { | ||
| 891 | my ($self, $expr, $obj) = @_; | ||
| 892 | $self->_dump_number($obj); | ||
| 893 | }, | ||
| 894 | |||
| 895 | 'Sidef::Types::Array::Array' => $dump_array, | ||
| 896 | 'Sidef::Types::Array::HCArray' => $dump_array, | ||
| 897 | |||
| 898 | 'Sidef::Types::Array::List' => sub { | ||
| 899 | my ($self, $expr, $obj) = @_; | ||
| 900 | join(', ', map { $self->deparse_expr({self => $_}) } @{$obj}); | ||
| 901 | }, | ||
| 902 | |||
| 903 | 'Sidef::Types::Hash::Hash' => sub { | ||
| 904 | my ($self, $expr, $obj) = @_; | ||
| 905 | $obj->dump->get_value; | ||
| 906 | }, | ||
| 907 | ); | ||
| 908 | } | ||
| 909 | |||
| 910 | 1 |