| 1 | package Sidef::Deparse::Perl; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use Scalar::Util qw(refaddr); | ||
| 7 | use Sidef::Types::Number::Number; | ||
| 8 | |||
| 9 | #---------------------------------------------------------------------- | ||
| 10 | # Configuration & Defaults | ||
| 11 | #---------------------------------------------------------------------- | ||
| 12 | |||
| 13 | my %addr; | ||
| 14 | my %top_add; | ||
| 15 | |||
| 16 | my %constant_number_cache; | ||
| 17 | my %constant_string_cache; | ||
| 18 | |||
| 19 | # Move static configuration out of 'new' | ||
| 20 | my %DEFAULT_OPTS = ( | ||
| 21 | before => '', | ||
| 22 | header => '', | ||
| 23 | top_program => '', | ||
| 24 | between => ';', | ||
| 25 | after => ';', | ||
| 26 | opt => {}, | ||
| 27 | environment_name => 'main', | ||
| 28 | ); | ||
| 29 | |||
| 30 | my %ASSIGNMENT_OPS = ('=' => '=',); | ||
| 31 | |||
| 32 | my %LAZY_OPS = ( | ||
| 33 | '?' => '?', | ||
| 34 | '||' => '||', | ||
| 35 | '&&' => '&&', | ||
| 36 | ':=' => '//=', | ||
| 37 | '||=' => '||=', | ||
| 38 | '&&=' => '&&=', | ||
| 39 | '\\\\' => '//', | ||
| 40 | '\\\\=' => '//=', | ||
| 41 | ); | ||
| 42 | |||
| 43 | my %OVERLOAD_METHODS = ( | ||
| 44 | '==' => 'eq', | ||
| 45 | '<=>' => 'cmp', | ||
| 46 | ); | ||
| 47 | |||
| 48 | my %REASSIGN_OPS = map { ("$_=", $_) } qw(+ - % * // / & | ^ ** && || << >> ÷); | ||
| 49 | |||
| 50 | my %INC_DEC_OPS = ( | ||
| 51 | '++' => 'inc', | ||
| 52 | '--' => 'dec', | ||
| 53 | ); | ||
| 54 | |||
| 55 | my %COMPOSITE_CONSTANTS = ( | ||
| 56 | 'Sidef::Types::Range::RangeNumber' => {name => 'RangeNum', fields => [qw(from to step)]}, | ||
| 57 | 'Sidef::Types::Range::RangeString' => {name => 'RangeStr', fields => [qw(from to step)]}, | ||
| 58 | 'Sidef::Types::Number::Gauss' => {name => 'Gauss', fields => [qw(a b)]}, | ||
| 59 | 'Sidef::Types::Number::Quadratic' => {name => 'Quadratic', fields => [qw(a b p q)]}, | ||
| 60 | 'Sidef::Types::Number::Quaternion' => {name => 'Quaternion', fields => [qw(a b c d)]}, | ||
| 61 | 'Sidef::Types::Number::Fraction' => {name => 'Fraction', fields => [qw(a b)]}, | ||
| 62 | 'Sidef::Types::Number::Mod' => {name => 'Mod', fields => [qw(n m)]}, | ||
| 63 | ); | ||
| 64 | |||
| 65 | my %DATA_TYPES = ( | ||
| 66 | 'Sidef::DataTypes::Bool::Bool' => 'Sidef::Types::Bool::Bool', | ||
| 67 | 'Sidef::DataTypes::Array::Array' => 'Sidef::Types::Array::Array', | ||
| 68 | 'Sidef::DataTypes::Array::Pair' => 'Sidef::Types::Array::Pair', | ||
| 69 | 'Sidef::DataTypes::Array::Vector' => 'Sidef::Types::Array::Vector', | ||
| 70 | 'Sidef::DataTypes::Array::Matrix' => 'Sidef::Types::Array::Matrix', | ||
| 71 | 'Sidef::DataTypes::Hash::Hash' => 'Sidef::Types::Hash::Hash', | ||
| 72 | 'Sidef::DataTypes::Set::Set' => 'Sidef::Types::Set::Set', | ||
| 73 | 'Sidef::DataTypes::Set::Bag' => 'Sidef::Types::Set::Bag', | ||
| 74 | 'Sidef::DataTypes::Regex::Regex' => 'Sidef::Types::Regex::Regex', | ||
| 75 | 'Sidef::DataTypes::String::String' => 'Sidef::Types::String::String', | ||
| 76 | 'Sidef::DataTypes::Number::Number' => 'Sidef::Types::Number::Number', | ||
| 77 | 'Sidef::DataTypes::Number::Mod' => 'Sidef::Types::Number::Mod', | ||
| 78 | 'Sidef::DataTypes::Number::Gauss' => 'Sidef::Types::Number::Gauss', | ||
| 79 | 'Sidef::DataTypes::Number::Quadratic' => 'Sidef::Types::Number::Quadratic', | ||
| 80 | 'Sidef::DataTypes::Number::Quaternion' => 'Sidef::Types::Number::Quaternion', | ||
| 81 | 'Sidef::DataTypes::Number::Complex' => 'Sidef::Types::Number::Complex', | ||
| 82 | 'Sidef::DataTypes::Number::Polynomial' => 'Sidef::Types::Number::Polynomial', | ||
| 83 | 'Sidef::DataTypes::Number::PolynomialMod' => 'Sidef::Types::Number::PolynomialMod', | ||
| 84 | 'Sidef::DataTypes::Number::Fraction' => 'Sidef::Types::Number::Fraction', | ||
| 85 | 'Sidef::DataTypes::Range::Range' => 'Sidef::Types::Range::Range', | ||
| 86 | 'Sidef::DataTypes::Range::RangeNumber' => 'Sidef::Types::Range::RangeNumber', | ||
| 87 | 'Sidef::DataTypes::Range::RangeString' => 'Sidef::Types::Range::RangeString', | ||
| 88 | 'Sidef::DataTypes::Block::Block' => 'Sidef::Types::Block::Block', | ||
| 89 | 'Sidef::DataTypes::Glob::Socket' => 'Sidef::Types::Glob::Socket', | ||
| 90 | 'Sidef::DataTypes::Glob::Pipe' => 'Sidef::Types::Glob::Pipe', | ||
| 91 | 'Sidef::DataTypes::Glob::Backtick' => 'Sidef::Types::Glob::Backtick', | ||
| 92 | 'Sidef::DataTypes::Glob::DirHandle' => 'Sidef::Types::Glob::DirHandle', | ||
| 93 | 'Sidef::DataTypes::Glob::FileHandle' => 'Sidef::Types::Glob::FileHandle', | ||
| 94 | 'Sidef::DataTypes::Glob::SocketHandle' => 'Sidef::Types::Glob::SocketHandle', | ||
| 95 | 'Sidef::DataTypes::Glob::Dir' => 'Sidef::Types::Glob::Dir', | ||
| 96 | 'Sidef::DataTypes::Glob::File' => 'Sidef::Types::Glob::File', | ||
| 97 | 'Sidef::DataTypes::Perl::Perl' => 'Sidef::Types::Perl::Perl', | ||
| 98 | 'Sidef::DataTypes::Object::Object' => 'Sidef::Object::Object', | ||
| 99 | 'Sidef::DataTypes::Sidef::Sidef' => 'Sidef', | ||
| 100 | 'Sidef::DataTypes::Object::Lazy' => 'Sidef::Object::Lazy', | ||
| 101 | 'Sidef::DataTypes::Object::LazyMethod' => 'Sidef::Object::LazyMethod', | ||
| 102 | 'Sidef::DataTypes::Object::Enumerator' => 'Sidef::Object::Enumerator', | ||
| 103 | 'Sidef::DataTypes::Variable::NamedParam' => 'Sidef::Variable::NamedParam', | ||
| 104 | 'Sidef::Meta::PrefixColon' => 'Sidef::Types::Hash::Hash', | ||
| 105 | 'Sidef::Sys::Sig' => 'Sidef::Sys::Sig', | ||
| 106 | 'Sidef::Sys::Sys' => 'Sidef::Sys::Sys', | ||
| 107 | 'Sidef::Math::Math' => 'Sidef::Math::Math', | ||
| 108 | 'Sidef::Time::Time' => 'Sidef::Time::Time', | ||
| 109 | 'Sidef::Time::Date' => 'Sidef::Time::Date', | ||
| 110 | ); | ||
| 111 | |||
| 112 | # Dispatch table mapping ref types to method names | ||
| 113 | my %handlers = ( | ||
| 114 | 'Sidef::Variable::Variable' => '_deparse_variable', | ||
| 115 | 'Sidef::Operator::Unary' => '_deparse_unary_ok', | ||
| 116 | 'Sidef::Variable::Local' => '_deparse_local', | ||
| 117 | 'Sidef::Variable::ClassVar' => '_deparse_class_var', | ||
| 118 | 'Sidef::Variable::Define' => '_deparse_define', | ||
| 119 | 'Sidef::Variable::Const' => '_deparse_const', | ||
| 120 | 'Sidef::Variable::Static' => '_deparse_static', | ||
| 121 | 'Sidef::Variable::ConstInit' => '_deparse_const_init', | ||
| 122 | 'Sidef::Variable::Init' => '_deparse_init', | ||
| 123 | 'Sidef::Variable::ClassInit' => '_deparse_class_init', | ||
| 124 | 'Sidef::Types::Block::BlockInit' => '_deparse_block_init', | ||
| 125 | 'Sidef::Variable::ClassAttr' => '_deparse_class_attr_ok', | ||
| 126 | 'Sidef::Variable::Struct' => '_deparse_struct', | ||
| 127 | 'Sidef::Variable::Subset' => '_deparse_subset', | ||
| 128 | 'Sidef::Types::Number::Number' => '_deparse_number', | ||
| 129 | 'Sidef::Types::String::String' => '_deparse_string', | ||
| 130 | 'Sidef::Types::Array::Array' => '_deparse_array', | ||
| 131 | 'Sidef::Types::Array::HCArray' => '_deparse_array', | ||
| 132 | 'Sidef::Types::Array::Vector' => '_deparse_vector', | ||
| 133 | 'Sidef::Types::Array::Matrix' => '_deparse_matrix', | ||
| 134 | 'Sidef::Types::Bool::Bool' => '_deparse_bool', | ||
| 135 | 'Sidef::Types::Regex::Regex' => '_deparse_regex', | ||
| 136 | 'Sidef::Types::Block::If' => '_deparse_if', | ||
| 137 | 'Sidef::Types::Block::While' => '_deparse_while', | ||
| 138 | 'Sidef::Types::Block::ForEach' => '_deparse_foreach', | ||
| 139 | 'Sidef::Types::Block::CFor' => '_deparse_cfor', | ||
| 140 | 'Sidef::Types::Block::ForIn' => '_deparse_forin', | ||
| 141 | 'Sidef::Types::Bool::Ternary' => '_deparse_ternary', | ||
| 142 | 'Sidef::Variable::NamedParam' => '_deparse_named_param', | ||
| 143 | 'Sidef::Types::Nil::Nil' => '_deparse_nil', | ||
| 144 | 'Sidef::Types::Hash::Hash' => '_deparse_hash', | ||
| 145 | 'Sidef::Meta::PrefixMethod' => '_deparse_prefix_method', | ||
| 146 | 'Sidef::Types::Block::Do' => '_deparse_do', | ||
| 147 | 'Sidef::Types::Block::Loop' => '_deparse_loop', | ||
| 148 | 'Sidef::Types::Block::Given' => '_deparse_given', | ||
| 149 | 'Sidef::Types::Block::When' => '_deparse_when', | ||
| 150 | 'Sidef::Types::Block::Case' => '_deparse_case', | ||
| 151 | 'Sidef::Types::Block::Default' => '_deparse_default', | ||
| 152 | 'Sidef::Types::Block::Continue' => '_deparse_continue', | ||
| 153 | 'Sidef::Types::Block::With' => '_deparse_with', | ||
| 154 | 'Sidef::Types::Block::Gather' => '_deparse_gather', | ||
| 155 | 'Sidef::Types::Block::Take' => '_deparse_take', | ||
| 156 | 'Sidef::Types::Block::Try' => '_deparse_try', | ||
| 157 | 'Sidef::Variable::Ref' => '_deparse_ref_ok', | ||
| 158 | 'Sidef::Types::Block::Break' => '_deparse_break', | ||
| 159 | 'Sidef::Types::Block::Next' => '_deparse_next', | ||
| 160 | 'Sidef::Types::Block::Return' => '_deparse_return', | ||
| 161 | 'Sidef::Meta::Glob::STDIN' => '_deparse_stdin', | ||
| 162 | 'Sidef::Meta::Glob::STDOUT' => '_deparse_stdout', | ||
| 163 | 'Sidef::Meta::Glob::STDERR' => '_deparse_stderr', | ||
| 164 | 'Sidef::Meta::Glob::ARGF' => '_deparse_argf', | ||
| 165 | 'Sidef::Meta::Glob::DATA' => '_deparse_data', | ||
| 166 | 'Sidef::Variable::Magic' => '_deparse_magic', | ||
| 167 | 'Sidef::Eval::Eval' => '_deparse_eval', | ||
| 168 | 'Sidef::Types::Number::Complex' => '_deparse_complex', | ||
| 169 | 'Sidef::Types::Array::Pair' => '_deparse_pair', | ||
| 170 | 'Sidef::Types::Null::Null' => '_deparse_null', | ||
| 171 | 'Sidef::Module::OO' => '_deparse_module_oo', | ||
| 172 | 'Sidef::Module::Func' => '_deparse_module_func', | ||
| 173 | 'Sidef::Types::Perl::Perl' => '_deparse_perl_code', | ||
| 174 | 'Sidef::Types::Glob::Backtick' => '_deparse_backtick', | ||
| 175 | 'Sidef::Types::Glob::File' => '_deparse_file', | ||
| 176 | 'Sidef::Types::Glob::Dir' => '_deparse_dir', | ||
| 177 | 'Sidef::Meta::Module' => '_deparse_meta_module', | ||
| 178 | 'Sidef::Meta::Included' => '_deparse_included', | ||
| 179 | 'Sidef::Meta::Assert' => '_deparse_assert', | ||
| 180 | 'Sidef::Meta::Error' => '_deparse_error', | ||
| 181 | 'Sidef::Meta::Warning' => '_deparse_warning', | ||
| 182 | 'Sidef::Types::Glob::Pipe' => '_deparse_pipe', | ||
| 183 | 'Sidef::Parser' => '_deparse_parser', | ||
| 184 | 'Sidef::Meta::Unimplemented' => '_deparse_unimplemented', | ||
| 185 | 'Sidef::Variable::Label' => '_deparse_label', | ||
| 186 | 'Sidef::Perl::Builtin' => '_deparse_builtin', | ||
| 187 | ); | ||
| 188 | |||
| 189 | sub new { | ||
| 190 | my (undef, %args) = @_; | ||
| 191 | |||
| 192 | # Merge defaults with args | ||
| 193 | my %opts = (%DEFAULT_OPTS, %args); | ||
| 194 | |||
| 195 | # Assign pointers to static config maps (for back-compat if $self uses them) | ||
| 196 | $opts{assignment_ops} = \%ASSIGNMENT_OPS; | ||
| 197 | $opts{lazy_ops} = \%LAZY_OPS; | ||
| 198 | $opts{overload_methods} = \%OVERLOAD_METHODS; | ||
| 199 | $opts{data_types} = \%DATA_TYPES; | ||
| 200 | $opts{reassign_ops} = \%REASSIGN_OPS; | ||
| 201 | $opts{inc_dec_ops} = \%INC_DEC_OPS; | ||
| 202 | |||
| 203 | $opts{header} .= <<"HEADER"; | ||
| 204 | |||
| 205 | use utf8; | ||
| 206 | use strict; | ||
| 207 | use feature qw(state unicode_strings unicode_eval evalbytes); | ||
| 208 | local \$| = 1; # autoflush | ||
| 209 | |||
| 210 | HEADER | ||
| 211 | |||
| 212 | if (defined $opts{opt}{w}) { | ||
| 213 | $opts{header} .= '$SIG{__WARN__} = sub { require Carp; Carp::cluck(@_) };'; | ||
| 214 | } | ||
| 215 | |||
| 216 | if (defined $opts{opt}{W}) { | ||
| 217 | $opts{header} .= '$SIG{__DIE__} = $SIG{__WARN__} = sub { require Carp; Carp::confess(@_) };'; | ||
| 218 | } | ||
| 219 | |||
| 220 | if (exists $opts{opt}{P}) { | ||
| 221 | my $precision = abs(int($opts{opt}{P})); | ||
| 222 | $opts{header} .= "BEGIN { \$Sidef::Types::Number::Number::PREC = 4*$precision };"; | ||
| 223 | } | ||
| 224 | |||
| 225 | if (exists $opts{opt}{M}) { | ||
| 226 | my $round = unpack('A*', lc($opts{opt}{M}) =~ s/^\s+//r); | ||
| 227 | my %modes = ( | ||
| 228 | 'zero' => 1, | ||
| 229 | '+inf' => 2, | ||
| 230 | '-inf' => 3, | ||
| 231 | 'inf' => 4, | ||
| 232 | 'faith' => 5, | ||
| 233 | ); | ||
| 234 | $round = $modes{$round} // 0; | ||
| 235 | $opts{header} .= "BEGIN { \$Sidef::Types::Number::Number::ROUND = $round };"; | ||
| 236 | } | ||
| 237 | |||
| 238 | undef %addr; | ||
| 239 | undef %top_add; | ||
| 240 | undef %constant_number_cache; | ||
| 241 | undef %constant_string_cache; | ||
| 242 | |||
| 243 | bless \%opts, __PACKAGE__; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub make_constant { | ||
| 247 | my ($self, $ref, $new_method, $name, %opt) = @_; | ||
| 248 | |||
| 249 | my $rel_name = $name . join('_', map { refaddr(\$_) } @{$opt{args}}) . CORE::int(CORE::rand(~0)); | ||
| 250 | my $full_name = $self->{environment_name} . '::' . $rel_name; | ||
| 251 | |||
| 252 | if (not $self->{_has_constant}) { | ||
| 253 | $self->{_has_constant} = 1; | ||
| 254 | $self->{before} .= "use constant {"; | ||
| 255 | } | ||
| 256 | |||
| 257 | if ($opt{new}) { | ||
| 258 | if ($self->{_has_constant}) { | ||
| 259 | $self->{before} .= '};'; | ||
| 260 | } | ||
| 261 | $self->{_has_constant} = 1; | ||
| 262 | $self->{before} .= "use constant {"; | ||
| 263 | } | ||
| 264 | |||
| 265 | $self->{before} .= "$rel_name => $ref" . ($opt{sub} ? '::' : '->') . "$new_method(" . join(',', @{$opt{args}}) . "),"; | ||
| 266 | "($full_name)"; | ||
| 267 | } | ||
| 268 | |||
| 269 | sub top_add { | ||
| 270 | my ($self, $line) = @_; | ||
| 271 | if (not exists $top_add{$line}) { | ||
| 272 | undef $top_add{$line}; | ||
| 273 | $self->{top_program} .= $line; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | sub load_mod { | ||
| 278 | my ($self, $mod) = @_; | ||
| 279 | $self->top_add( | ||
| 280 | $self->{opt}{c} | ||
| 281 | ? qq{main::__load_sidef_module__("$mod");} | ||
| 282 | : "use $mod;" | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | |||
| 286 | sub _get_reftype { | ||
| 287 | my ($self, $obj) = @_; | ||
| 288 | |||
| 289 | my $ref = ref($obj); | ||
| 290 | |||
| 291 | if (exists $DATA_TYPES{$ref}) { | ||
| 292 | my $target = $DATA_TYPES{$ref}; | ||
| 293 | |||
| 294 | $self->{_reftype_cache}{$ref} //= do { | ||
| 295 | $self->load_mod($target); | ||
| 296 | 1; | ||
| 297 | }; | ||
| 298 | |||
| 299 | return $target; | ||
| 300 | } | ||
| 301 | |||
| 302 | ($ref eq 'Sidef::Variable::Struct' || $ref eq 'Sidef::Variable::Subset') ? $self->_dump_class_name($obj) | ||
| 303 | : $ref eq 'Sidef::Variable::ClassInit' ? (ref($obj->{name}) ? $self->_dump_class_name($obj->{name}) : $self->_dump_class_name($obj)) | ||
| 304 | : $ref eq 'Sidef::Variable::Ref' ? 'REF' | ||
| 305 | : $ref eq 'Sidef::Types::Block::BlockInit' ? 'Sidef::Types::Block::Block' | ||
| 306 | : $ref; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub _dump_reftype { | ||
| 310 | my ($self, $obj) = @_; | ||
| 311 | $self->_dump_string($self->_get_reftype($obj)); | ||
| 312 | } | ||
| 313 | |||
| 314 | { | ||
| 315 | my %esc = ( | ||
| 316 | "\a" => "\\a", | ||
| 317 | "\b" => "\\b", | ||
| 318 | "\t" => "\\t", | ||
| 319 | "\n" => "\\n", | ||
| 320 | "\f" => "\\f", | ||
| 321 | "\r" => "\\r", | ||
| 322 | "\e" => "\\e", | ||
| 323 | ); | ||
| 324 | |||
| 325 | sub _dump_string { | ||
| 326 | my ($self, $str) = @_; | ||
| 327 | $str =~ s/([\\\"\@\$])/\\$1/g; | ||
| 328 | return qq("$str") if ($str !~ /[^\040-\176]/); # fast exit | ||
| 329 | |||
| 330 | $str =~ s/([\a\b\t\n\f\r\e])/$esc{$1}/g; | ||
| 331 | $str =~ s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg; | ||
| 332 | $str =~ s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg; | ||
| 333 | $str =~ s/([^\040-\176])/sprintf('\\x{%X}',ord($1))/eg; | ||
| 334 | |||
| 335 | return qq("$str"); | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | sub _dump_op_call { | ||
| 340 | my ($self, $method) = @_; | ||
| 341 | |||
| 342 | my $name = 'OP' . join('', unpack('C*', $method)) . refaddr($self); | ||
| 343 | my $line = "BEGIN { \$$self->{environment_name}\::$name = " . $self->_dump_string($method) . '};'; | ||
| 344 | $self->top_add($line); | ||
| 345 | '->$' . $self->{environment_name} . '::' . $name; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub _dump_var { | ||
| 349 | my ($self, $var, %opt) = @_; | ||
| 350 | |||
| 351 | $var->{name} eq '' and return 'undef'; | ||
| 352 | |||
| 353 | ( | ||
| 354 | $opt{init} | ||
| 355 | ? ( | ||
| 356 | exists($var->{array}) ? '@' | ||
| 357 | : exists($var->{hash}) ? '%' | ||
| 358 | : '$' | ||
| 359 | ) | ||
| 360 | : '$' | ||
| 361 | ) | ||
| 362 | . ($var->{type} eq 'global' ? ($var->{class} . '::') : '') | ||
| 363 | . $var->{name} | ||
| 364 | . ($var->{type} eq 'global' ? '' : ($opt{refaddr} // refaddr($var))); | ||
| 365 | } | ||
| 366 | |||
| 367 | sub _dump_init_vars { | ||
| 368 | my ($self, $init_obj) = @_; | ||
| 369 | |||
| 370 | my @vars = @{$init_obj->{vars}}; | ||
| 371 | |||
| 372 | if (!@vars or $vars[0]{type} eq 'del') { | ||
| 373 | return ''; | ||
| 374 | } | ||
| 375 | |||
| 376 | my @code; | ||
| 377 | |||
| 378 | push @code, | ||
| 379 | '(' . join(',', map { $self->_dump_var($_, init => 1) } @vars) . ')' . (exists($init_obj->{args}) ? '=' . $self->deparse_args($init_obj->{args}) : ''); | ||
| 380 | |||
| 381 | foreach my $var (@vars) { | ||
| 382 | |||
| 383 | ref($var) || next; | ||
| 384 | |||
| 385 | my $name = $var->{name} . refaddr($var); | ||
| 386 | my $decl = 'my '; | ||
| 387 | |||
| 388 | if ($var->{type} eq 'global') { | ||
| 389 | $name = $var->{class} . '::' . $var->{name}; | ||
| 390 | $decl = ''; | ||
| 391 | } | ||
| 392 | |||
| 393 | if (exists $var->{array}) { | ||
| 394 | push @{$self->{block_declarations}}, [$self->{current_block} // -1, $decl . '@' . $name . ';']; | ||
| 395 | if (exists $var->{value}) { | ||
| 396 | push @code, ('@' . $name . '=(' . $self->deparse_expr({self => $var->{value}}) . ") if not \@$name;"); | ||
| 397 | } | ||
| 398 | $self->load_mod('Sidef::Types::Array::Array'); | ||
| 399 | push @code, "\$$name = bless(\\\@$name, 'Sidef::Types::Array::Array');"; | ||
| 400 | } | ||
| 401 | elsif (exists $var->{hash}) { | ||
| 402 | push @{$self->{block_declarations}}, [$self->{current_block} // -1, $decl . '%' . $name . ';']; | ||
| 403 | if (exists $var->{value}) { | ||
| 404 | push @code, ('%' . $name . '=(' . $self->deparse_expr({self => $var->{value}}) . ") if not keys \%$name;"); | ||
| 405 | } | ||
| 406 | $self->load_mod('Sidef::Types::Hash::Hash'); | ||
| 407 | push @code, "\$$name = bless(\\\%$name, 'Sidef::Types::Hash::Hash');"; | ||
| 408 | } | ||
| 409 | elsif (exists $var->{value}) { | ||
| 410 | my $value = $self->deparse_expr({self => $var->{value}}); | ||
| 411 | if ($value ne '') { | ||
| 412 | push @code, "\$$name//=$value;"; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | if (my @non_globals = grep { $_->{type} ne 'global' } @vars) { | ||
| 418 | push @{$self->{block_declarations}}, [$self->{current_block} // -1, 'my(' . join(',', map { $self->_dump_var($_) } @non_globals) . ')' . ';']; | ||
| 419 | } | ||
| 420 | |||
| 421 | if (@code > 1 or exists($init_obj->{args})) { | ||
| 422 | push @code, '(' . join(',', map { $self->_dump_var($_) } @vars) . ')'; | ||
| 423 | return 'CORE::sub:lvalue{' . join(';', @code) . '}->()'; | ||
| 424 | } | ||
| 425 | |||
| 426 | '(' . join(',', @code) . ')'; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub _dump_class_attributes { | ||
| 430 | my ($self, @attrs) = @_; | ||
| 431 | |||
| 432 | my %seen; | ||
| 433 | my @code; | ||
| 434 | foreach my $attr (reverse @attrs) { | ||
| 435 | |||
| 436 | my @vars = @{$attr->{vars}}; | ||
| 437 | @vars || next; | ||
| 438 | |||
| 439 | my @dumped_vars = map { ref($_) ? $self->_dump_var($_, init => 1) : $_ } grep { !$seen{$_->{name}}++ } @vars; | ||
| 440 | @dumped_vars || next; | ||
| 441 | |||
| 442 | unshift @code, ('my(' . join(',', @dumped_vars) . ')' . (exists($attr->{args}) ? '=' . $self->deparse_args($attr->{args}) : '')); | ||
| 443 | |||
| 444 | foreach my $var (@vars) { | ||
| 445 | |||
| 446 | my $name = $var->{name} . refaddr($var); | ||
| 447 | |||
| 448 | if (exists $var->{array}) { | ||
| 449 | if (exists $var->{value}) { | ||
| 450 | push @code, ('@' . $name . '=(' . $self->deparse_expr({self => $var->{value}}) . ") if not \@$name;"); | ||
| 451 | } | ||
| 452 | $self->load_mod('Sidef::Types::Array::Array'); | ||
| 453 | push @code, "my \$$name = bless(\\\@$name, 'Sidef::Types::Array::Array');"; | ||
| 454 | } | ||
| 455 | elsif (exists $var->{hash}) { | ||
| 456 | if (exists $var->{value}) { | ||
| 457 | push @code, ('%' . $name . '=(' . $self->deparse_expr({self => $var->{value}}) . ") if not keys \%$name;"); | ||
| 458 | } | ||
| 459 | $self->load_mod('Sidef::Types::Hash::Hash'); | ||
| 460 | push @code, "my \$$name = bless(\\\%$name, 'Sidef::Types::Hash::Hash');"; | ||
| 461 | } | ||
| 462 | elsif (exists $var->{value}) { | ||
| 463 | my $value = $self->deparse_expr({self => $var->{value}}); | ||
| 464 | if ($value ne '') { | ||
| 465 | push @code, "\$$name//=$value;"; | ||
| 466 | } | ||
| 467 | } | ||
| 468 | } | ||
| 469 | } | ||
| 470 | @code ? (join(';', @code) . ';') : ''; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub _dump_sub_init_vars { | ||
| 474 | my ($self, @vars) = @_; | ||
| 475 | |||
| 476 | @vars || return ''; | ||
| 477 | |||
| 478 | my @dumped_vars = map { ref($_) ? $self->_dump_var($_, init => 1) : $_ } @vars; | ||
| 479 | my $code = "my(" . join(',', @dumped_vars) . ')=@_;'; | ||
| 480 | |||
| 481 | my $valid; | ||
| 482 | foreach my $var (@vars) { | ||
| 483 | |||
| 484 | $valid || (shift(@dumped_vars) eq 'undef' ? next : ($valid ||= 1)); | ||
| 485 | |||
| 486 | ref($var) || next; | ||
| 487 | if (exists $var->{array}) { | ||
| 488 | my $name = $var->{name} . refaddr($var); | ||
| 489 | if (exists $var->{value}) { | ||
| 490 | $code .= ('@' . $name . '=(' . $self->deparse_expr({self => $var->{value}}) . ") if not \@$name;"); | ||
| 491 | } | ||
| 492 | $self->load_mod('Sidef::Types::Array::Array'); | ||
| 493 | $code .= "my \$$name = bless(\\\@$name, 'Sidef::Types::Array::Array');"; | ||
| 494 | } | ||
| 495 | elsif (exists $var->{hash}) { | ||
| 496 | my $name = $var->{name} . refaddr($var); | ||
| 497 | if (exists $var->{value}) { | ||
| 498 | $code .= ('%' . $name . '=(' . $self->deparse_expr({self => $var->{value}}) . ") if not keys \%$name;"); | ||
| 499 | } | ||
| 500 | $self->load_mod('Sidef::Types::Hash::Hash'); | ||
| 501 | $code .= "my \$$name = bless(\\\%$name, 'Sidef::Types::Hash::Hash');"; | ||
| 502 | } | ||
| 503 | elsif (exists $var->{value}) { | ||
| 504 | my $value = $self->deparse_expr({self => $var->{value}}); | ||
| 505 | if ($value ne '') { | ||
| 506 | $code .= "\$$var->{name}" . refaddr($var) . "//=$value;"; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | $code .= '();'; | ||
| 512 | $valid ? $code : ''; | ||
| 513 | } | ||
| 514 | |||
| 515 | sub _dump_array { | ||
| 516 | my ($self, $ref, $array) = @_; | ||
| 517 | $self->load_mod($ref); | ||
| 518 | 'bless([' . join(',', grep { $_ ne '' } map { $self->deparse_expr(ref($_) eq 'HASH' ? $_ : {self => $_}) } @{$array}) . "], '${ref}')"; | ||
| 519 | } | ||
| 520 | |||
| 521 | sub _dump_indices { | ||
| 522 | my ($self, $array) = @_; | ||
| 523 | |||
| 524 | my @indices; | ||
| 525 | |||
| 526 | foreach my $entry (@{$array}) { | ||
| 527 | if (ref($entry) eq 'HASH' and exists($entry->{self}) and scalar(keys %$entry) == 1) { | ||
| 528 | my $obj = $entry->{self}; | ||
| 529 | if (ref($obj) eq 'HASH' and scalar(keys %$obj) == 1 and !exists($obj->{self})) { | ||
| 530 | foreach my $class (keys %$obj) { | ||
| 531 | my $statements = $obj->{$class}; | ||
| 532 | scalar(@$statements) == 1 or next; | ||
| 533 | $obj = $statements->[0]; | ||
| 534 | ref($obj) eq 'HASH' or next; | ||
| 535 | scalar(keys %$obj) == 1 or next; | ||
| 536 | exists($obj->{self}) or next; | ||
| 537 | $obj = $obj->{self}; | ||
| 538 | if (ref($obj) eq 'Sidef::Types::Number::Number') { | ||
| 539 | $entry = $obj; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | if (ref($entry) eq 'Sidef::Types::Number::Number') { | ||
| 546 | push @indices, (ref($$entry) ? CORE::int(Sidef::Types::Number::Number::__numify__($$entry)) : $$entry); | ||
| 547 | } | ||
| 548 | elsif (ref($entry)) { | ||
| 549 | my $str = $self->deparse_expr(ref($entry) eq 'HASH' ? $entry : {self => $entry}); | ||
| 550 | |||
| 551 | if ($str ne '') { | ||
| 552 | push @indices, | ||
| 553 | ( '(map { ref($_) eq "Sidef::Types::Number::Number" ? ' | ||
| 554 | . '(ref($$_) ? Sidef::Types::Number::Number::__numify__($$_) : $$_) ' | ||
| 555 | . ': do {my$sub=ref($_) && UNIVERSAL::can($_, "..."); ' | ||
| 556 | . '$sub ? $sub->($_) : CORE::int($_) } } ' | ||
| 557 | . $str | ||
| 558 | . ')'); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | else { | ||
| 562 | push @indices, $entry; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | join(',', @indices); | ||
| 567 | } | ||
| 568 | |||
| 569 | sub _dump_lookups { | ||
| 570 | my ($self, $array) = @_; | ||
| 571 | my @indices; | ||
| 572 | |||
| 573 | foreach my $entry (@{$array}) { | ||
| 574 | if (ref($entry) eq 'HASH' and exists($entry->{self}) and scalar(keys %$entry) == 1) { | ||
| 575 | my $obj = $entry->{self}; | ||
| 576 | if (ref($obj) eq 'HASH' and scalar(keys %$obj) == 1 and !exists($obj->{self})) { | ||
| 577 | foreach my $class (keys %$obj) { | ||
| 578 | my $statements = $obj->{$class}; | ||
| 579 | scalar(@$statements) == 1 or next; | ||
| 580 | $obj = $statements->[0]; | ||
| 581 | ref($obj) eq 'HASH' or next; | ||
| 582 | scalar(keys %$obj) == 1 or next; | ||
| 583 | exists($obj->{self}) or next; | ||
| 584 | $obj = $obj->{self}; | ||
| 585 | if (ref($obj) eq 'Sidef::Types::String::String' or ref($obj) eq 'Sidef::Types::Number::Number') { | ||
| 586 | $entry = $obj; | ||
| 587 | } | ||
| 588 | } | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | if (ref($entry) eq 'Sidef::Types::String::String') { | ||
| 593 | push @indices, $self->_dump_string($$entry); | ||
| 594 | } | ||
| 595 | elsif (ref($entry) eq 'Sidef::Types::Number::Number') { | ||
| 596 | push @indices, $self->_dump_string("$entry"); | ||
| 597 | } | ||
| 598 | elsif (ref($entry)) { | ||
| 599 | my $str = $self->deparse_expr(ref($entry) eq 'HASH' ? $entry : {self => $entry}); | ||
| 600 | |||
| 601 | if ($str ne '') { | ||
| 602 | push @indices, ('(map { ref($_) eq "Sidef::Types::String::String" ? $$_ : "$_" } ' . $str . ')'); | ||
| 603 | } | ||
| 604 | } | ||
| 605 | else { | ||
| 606 | push @indices, $self->_dump_string($entry); | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | join(',', @indices); | ||
| 611 | } | ||
| 612 | |||
| 613 | sub _dump_var_attr { | ||
| 614 | my ($self, @vars) = @_; | ||
| 615 | 'vars=>[' . join( | ||
| 616 | ',', | ||
| 617 | map { | ||
| 618 | '{name=>' | ||
| 619 | . $self->_dump_string($_->{name}) | ||
| 620 | . (exists($_->{slurpy}) ? (',slurpy=>' . $_->{slurpy}) : '') | ||
| 621 | . (exists($_->{array}) ? (',array=>' . $_->{array}) : '') | ||
| 622 | . (exists($_->{ref_type}) ? (',type=>' . $self->_dump_reftype($_->{ref_type})) : '') | ||
| 623 | . (exists($_->{subset}) ? (',subset=>' . $self->_dump_reftype($_->{subset})) : '') | ||
| 624 | . (exists($_->{has_value}) ? (',has_value=>1') : '') | ||
| 625 | . ( | ||
| 626 | exists($_->{where_block}) | ||
| 627 | ? (',where_block=>sub{' | ||
| 628 | . $self->_dump_sub_init_vars($_->{where_block}{init_vars}{vars}[0]) | ||
| 629 | . $self->deparse_generic('', ';', '', $_->{where_block}{code}) . '}') | ||
| 630 | : '' | ||
| 631 | ) | ||
| 632 | . ( | ||
| 633 | exists($_->{where_expr}) ? (',where_expr=>do{' . $self->deparse_expr({self => $_->{where_expr}}) . '}') | ||
| 634 | : '' | ||
| 635 | ) | ||
| 636 | . '}' | ||
| 637 | } @vars | ||
| 638 | ) | ||
| 639 | . ']' | ||
| 640 | . ',' | ||
| 641 | . 'table=>{' | ||
| 642 | . join(',', map { $self->_dump_string($vars[$_]{name}) . '=>' . $_ } 0 .. $#vars) . '}'; | ||
| 643 | } | ||
| 644 | |||
| 645 | sub _get_inherited_stuff { | ||
| 646 | my ($self, $classes, $callback) = @_; | ||
| 647 | |||
| 648 | my @vars; | ||
| 649 | foreach my $class (@{$classes}) { | ||
| 650 | push @vars, $callback->($class); | ||
| 651 | if (defined $class->{inherit}) { | ||
| 652 | unshift @vars, $self->_get_inherited_stuff($class->{inherit}, $callback); | ||
| 653 | } | ||
| 654 | } | ||
| 655 | @vars; | ||
| 656 | } | ||
| 657 | |||
| 658 | sub _dump_class_name { | ||
| 659 | my ($self, $class) = @_; | ||
| 660 | |||
| 661 | join('::', | ||
| 662 | $self->{environment_name}, | ||
| 663 | refaddr($class->{parent} || $class), | ||
| 664 | $class->{class} || 'main', | ||
| 665 | $class->{name} || (Sidef::normalize_type(ref($class)) . refaddr($class))); | ||
| 666 | } | ||
| 667 | |||
| 668 | sub _dump_static_var { | ||
| 669 | my ($self, $var, $refaddr) = @_; | ||
| 670 | |||
| 671 | if ($var->{array}) { | ||
| 672 | $self->load_mod("Sidef::Types::Array::Array"); | ||
| 673 | } | ||
| 674 | elsif ($var->{hash}) { | ||
| 675 | $self->load_mod("Sidef::Types::Hash::Hash"); | ||
| 676 | } | ||
| 677 | |||
| 678 | "state\$$var->{name}$refaddr=do{my \@data = (" | ||
| 679 | . (defined($var->{value}) ? $self->deparse_script($var->{value}) : '') . ');' | ||
| 680 | . ( | ||
| 681 | $var->{slurpy} | ||
| 682 | ? ( | ||
| 683 | $var->{array} | ||
| 684 | ? "bless(\\\@data, 'Sidef::Types::Array::Array')" | ||
| 685 | : "bless({\@data}, 'Sidef::Types::Hash::Hash')" | ||
| 686 | ) | ||
| 687 | : "\$data[0]" | ||
| 688 | ) . "}"; | ||
| 689 | } | ||
| 690 | |||
| 691 | sub deparse_generic { | ||
| 692 | my ($self, $before, $sep, $after, @args) = @_; | ||
| 693 | $before | ||
| 694 | . join($sep, map { ref($_) eq 'HASH' ? $self->deparse_script($_) : ref($_) ? $self->deparse_expr({self => $_}) : $self->_dump_string($_) } @args) | ||
| 695 | . $after; | ||
| 696 | } | ||
| 697 | |||
| 698 | sub deparse_args { | ||
| 699 | my ($self, @args) = @_; | ||
| 700 | $self->deparse_generic('(', ',', ')', @args); | ||
| 701 | } | ||
| 702 | |||
| 703 | sub deparse_bare_block { | ||
| 704 | my ($self, @args) = @_; | ||
| 705 | my $code = $self->deparse_generic('{', ';', '}', @args); | ||
| 706 | $code; | ||
| 707 | } | ||
| 708 | |||
| 709 | sub localize_declarations { | ||
| 710 | my ($self, $refaddr) = @_; | ||
| 711 | |||
| 712 | my $code = ''; | ||
| 713 | |||
| 714 | # Localize variable declarations | ||
| 715 | while ( exists($self->{block_declarations}) | ||
| 716 | and @{$self->{block_declarations}} | ||
| 717 | and $self->{block_declarations}[-1][0] == $refaddr) { | ||
| 718 | $code .= pop(@{$self->{block_declarations}})->[1]; | ||
| 719 | } | ||
| 720 | |||
| 721 | # Localize function declarations | ||
| 722 | while ( exists($self->{function_declarations}) | ||
| 723 | and @{$self->{function_declarations}} | ||
| 724 | and $self->{function_declarations}[-1][0] != $refaddr) { | ||
| 725 | $self->{depth} <= $self->{function_declarations}[-1][2] or last; | ||
| 726 | $code .= pop(@{$self->{function_declarations}})->[1]; | ||
| 727 | } | ||
| 728 | |||
| 729 | $code; | ||
| 730 | } | ||
| 731 | |||
| 732 | sub deparse_block_with_scope { | ||
| 733 | my ($self, $obj) = @_; | ||
| 734 | |||
| 735 | my $refaddr = refaddr($obj); | ||
| 736 | local $self->{current_block} = $refaddr; | ||
| 737 | local $self->{depth} = ($self->{depth} // 0) + 1; | ||
| 738 | |||
| 739 | my @statements = join(';', $self->deparse_script($obj->{code})); | ||
| 740 | |||
| 741 | my $code = '{'; | ||
| 742 | $code .= $self->localize_declarations($refaddr); | ||
| 743 | $code . join(';', @statements) . '}'; | ||
| 744 | } | ||
| 745 | |||
| 746 | #---------------------------------------------------------------------- | ||
| 747 | # Handler Methods (Extracted from old massive if/else) | ||
| 748 | #---------------------------------------------------------------------- | ||
| 749 | |||
| 750 | sub _deparse_variable { | ||
| 751 | my ($self, $obj, $refaddr) = @_; | ||
| 752 | my $code = ''; | ||
| 753 | |||
| 754 | if ($obj->{type} eq 'func' or $obj->{type} eq 'method') { | ||
| 755 | |||
| 756 | if ($obj->{name} eq '') { | ||
| 757 | $obj->{name} = "__FUNC__"; | ||
| 758 | } | ||
| 759 | |||
| 760 | my $name = $obj->{name}; | ||
| 761 | my $alphaname = $obj->{name}; | ||
| 762 | |||
| 763 | local $obj->{type} = 'func' if exists($obj->{parent}); | ||
| 764 | |||
| 765 | if (not $obj->{name} =~ /^[^\W\d]\w*+\z/) { | ||
| 766 | $alphaname = '__NONANN__'; | ||
| 767 | } | ||
| 768 | |||
| 769 | if ($addr{$refaddr}++) { | ||
| 770 | $code = "\$$alphaname$refaddr"; | ||
| 771 | } | ||
| 772 | else { | ||
| 773 | my $block = $obj->{value}; | ||
| 774 | $code .= "\$${alphaname}$refaddr"; | ||
| 775 | |||
| 776 | { | ||
| 777 | local $self->{function} = refaddr($block); | ||
| 778 | local $self->{parent_name} = [$obj->{type}, $name]; | ||
| 779 | local $self->{current_namespace} = $obj->{class}; | ||
| 780 | |||
| 781 | push @{$self->{function_declarations}}, [$self->{function}, "my \$${alphaname}$refaddr;", ($self->{opt}{i} ? 1 : ($self->{depth} // 0))]; | ||
| 782 | |||
| 783 | if ($self->{ref_class} and !exists($obj->{parent})) { | ||
| 784 | push @{$self->{function_declarations}}, | ||
| 785 | [$self->{function}, qq{state \$${alphaname}_code$refaddr = UNIVERSAL::can("\Q$self->{class_name}\E", "\Q$name\E");}, $self->{depth} // 0]; | ||
| 786 | } | ||
| 787 | if ((my $content = $self->deparse_expr({self => $block})) ne '') { | ||
| 788 | $code .= "=$content"; | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | if (exists $obj->{parent}) { | ||
| 793 | my $name = $self->deparse_expr({self => $obj->{parent}}); | ||
| 794 | $code = "do{CORE::push(\@{" . $name . "->{kids} //= []}, do{$code});$name}"; | ||
| 795 | } | ||
| 796 | elsif ($self->{ref_class}) { | ||
| 797 | chop $code; | ||
| 798 | $code .= qq{,(defined(\$${alphaname}_code$refaddr)?(fallback=>} . qq{Sidef::Types::Block::Block->new(code=>\$${alphaname}_code$refaddr)):()))}; | ||
| 799 | } | ||
| 800 | |||
| 801 | if (exists $obj->{returns}) { | ||
| 802 | my $types = '[' . join(',', map { $self->_dump_reftype($_) } @{$obj->{returns}}) . ']'; | ||
| 803 | $code = "do{$code;\$${alphaname}$refaddr\->{returns}=$types;\$${alphaname}$refaddr}"; | ||
| 804 | } | ||
| 805 | |||
| 806 | if ($obj->{cached}) { | ||
| 807 | $code = "do{$code;" . "\$${alphaname}$refaddr\->cache;\$${alphaname}$refaddr}"; | ||
| 808 | } | ||
| 809 | |||
| 810 | if ($obj->{type} eq 'func' and !$obj->{parent}) { | ||
| 811 | if (${alphaname} eq 'MAIN') { | ||
| 812 | $self->top_add('require Encode;'); | ||
| 813 | $self->{after} .= "Sidef::Variable::GetOpt->new([map{Encode::decode_utf8(\$_)}\@ARGV],\$${alphaname}$refaddr);"; | ||
| 814 | } | ||
| 815 | } | ||
| 816 | elsif ($obj->{type} eq 'method') { | ||
| 817 | if (${alphaname} eq 'AUTOLOAD') { | ||
| 818 | $code .= ';' | ||
| 819 | . "our\$AUTOLOAD;" | ||
| 820 | . "sub ${alphaname} {my\$self=shift;" | ||
| 821 | . "my(\$class,\$method)=(\$AUTOLOAD=~/^(.*[^:])::(.*)\$/);" | ||
| 822 | . "\$${alphaname}$refaddr->call(\$self,Sidef::Types::String::String->new(\$class),Sidef::Types::String::String->new(\$method),\@_)}"; | ||
| 823 | } | ||
| 824 | elsif (${alphaname} eq '__FUNC__') { | ||
| 825 | |||
| 826 | # Anonymous | ||
| 827 | } | ||
| 828 | else { | ||
| 829 | $code .= ";" | ||
| 830 | . "state\$_$refaddr=do{no strict 'refs';" | ||
| 831 | . "\$$self->{package_name}::__SIDEF_CLASS_METHODS__{'${name}'} = \$${alphaname}$refaddr;" . '*{' | ||
| 832 | . $self->_dump_string("$self->{package_name}::$name") | ||
| 833 | . "}=sub{\$${alphaname}$refaddr->call(\@_)}}"; | ||
| 834 | } | ||
| 835 | |||
| 836 | if (exists $OVERLOAD_METHODS{$name}) { | ||
| 837 | my $overload_name = $OVERLOAD_METHODS{$name}; | ||
| 838 | $code .= ";" | ||
| 839 | . qq(use overload q{$overload_name} =>) | ||
| 840 | . q(sub{my($first,$second,$swap)=@_;) | ||
| 841 | . q(if($swap){($first,$second)=($second,$first)}) | ||
| 842 | . q($first) | ||
| 843 | . $self->_dump_op_call($name) | ||
| 844 | . q(($second)};); | ||
| 845 | } | ||
| 846 | } | ||
| 847 | } | ||
| 848 | } | ||
| 849 | else { | ||
| 850 | my $name = $obj->{name} . $refaddr; | ||
| 851 | |||
| 852 | if ($obj->{name} eq 'ENV') { | ||
| 853 | $self->top_add("require Encode;"); | ||
| 854 | $self->top_add(qq{my \$$name = Sidef::Types::Hash::Hash->new} . qq{(map{Sidef::Types::String::String->new(Encode::decode_utf8(\$_))} \%ENV);}); | ||
| 855 | } | ||
| 856 | elsif ($obj->{name} eq 'ARGV') { | ||
| 857 | $self->top_add("require Encode;"); | ||
| 858 | $self->top_add( | ||
| 859 | qq{my \$$name = Sidef::Types::Array::Array->new} . qq{([map {Sidef::Types::String::String->new(Encode::decode_utf8(\$_))} \@ARGV]);}); | ||
| 860 | } | ||
| 861 | |||
| 862 | $code = $self->_dump_var($obj, refaddr => $refaddr); | ||
| 863 | } | ||
| 864 | return $code; | ||
| 865 | } | ||
| 866 | |||
| 867 | sub _deparse_local { | ||
| 868 | my ($self, $obj) = @_; | ||
| 869 | 'local ' . (defined($obj->{expr}) ? $self->deparse_args($obj->{expr}) : '()'); | ||
| 870 | } | ||
| 871 | |||
| 872 | sub _deparse_class_var { | ||
| 873 | my ($self, $obj) = @_; | ||
| 874 | '$' . $self->_get_reftype($obj->{class}) . '::' . $obj->{name}; | ||
| 875 | } | ||
| 876 | |||
| 877 | sub _deparse_define { | ||
| 878 | my ($self, $obj, $refaddr) = @_; | ||
| 879 | my $name = $obj->{name} . $refaddr; | ||
| 880 | if (not $addr{$refaddr}++) { | ||
| 881 | $self->top_add('use constant ' . $name . '=>do{' . $self->_dump_static_var($obj, $refaddr) . '};'); | ||
| 882 | } | ||
| 883 | '(' . $self->{environment_name} . '::' . $name . ')'; | ||
| 884 | } | ||
| 885 | |||
| 886 | sub _deparse_const { | ||
| 887 | my ($self, $obj, $refaddr) = @_; | ||
| 888 | my $name = $obj->{name} . $refaddr; | ||
| 889 | if ($addr{$refaddr}++) { | ||
| 890 | return "\$$name\->()"; | ||
| 891 | } | ||
| 892 | else { | ||
| 893 | my $code = $self->_dump_static_var($obj, $refaddr); | ||
| 894 | return "my \$$name = sub { $code }; \$$name\->()"; | ||
| 895 | } | ||
| 896 | } | ||
| 897 | |||
| 898 | sub _deparse_static { | ||
| 899 | my ($self, $obj, $refaddr) = @_; | ||
| 900 | my $name = $obj->{name} . $refaddr; | ||
| 901 | if ($addr{$refaddr}++) { | ||
| 902 | return "\$$name"; | ||
| 903 | } | ||
| 904 | else { | ||
| 905 | return '(' . $self->_dump_static_var($obj, $refaddr) . ')'; | ||
| 906 | } | ||
| 907 | } | ||
| 908 | |||
| 909 | sub _deparse_const_init { | ||
| 910 | my ($self, $obj) = @_; | ||
| 911 | join(($obj->{type} eq 'global' ? ',' : ';'), map { $self->deparse_expr({self => $_}) } @{$obj->{vars}}); | ||
| 912 | } | ||
| 913 | |||
| 914 | sub _deparse_init { | ||
| 915 | my ($self, $obj) = @_; | ||
| 916 | $self->_dump_init_vars($obj); | ||
| 917 | } | ||
| 918 | |||
| 919 | sub _deparse_class_init { | ||
| 920 | my ($self, $obj, $refaddr) = @_; | ||
| 921 | my $name = $self->_get_reftype($obj); | ||
| 922 | if ($addr{$refaddr}++) { | ||
| 923 | return q{'} . $name . q{'}; | ||
| 924 | } | ||
| 925 | else { | ||
| 926 | my $block = $obj->{block}; | ||
| 927 | my $code = 'do{package '; | ||
| 928 | my $package_name; | ||
| 929 | if (ref $obj->{name}) { | ||
| 930 | $code .= ($package_name = $self->_get_reftype($obj->{name})); | ||
| 931 | } | ||
| 932 | else { | ||
| 933 | $code .= ($package_name = $name); | ||
| 934 | } | ||
| 935 | |||
| 936 | local $self->{class} = refaddr($block); | ||
| 937 | local $self->{class_name} = $package_name; | ||
| 938 | local $self->{parent_name} = ['class', $package_name]; | ||
| 939 | local $self->{package_name} = $package_name; | ||
| 940 | local $self->{inherit} = $obj->{inherit}; | ||
| 941 | local $self->{class_vars} = $obj->{vars}; | ||
| 942 | local $self->{class_attributes} = $obj->{attributes}; | ||
| 943 | local $self->{ref_class} = ref($obj->{name}) ? 1 : 0; | ||
| 944 | |||
| 945 | $code .= $self->deparse_expr({self => $block}); | ||
| 946 | $code .= ";'${package_name}'}"; | ||
| 947 | return $code; | ||
| 948 | } | ||
| 949 | } | ||
| 950 | |||
| 951 | sub _deparse_block_init { | ||
| 952 | my ($self, $obj, $refaddr) = @_; | ||
| 953 | my $code = ''; | ||
| 954 | |||
| 955 | if ($addr{$refaddr}++) { | ||
| 956 | $code = "(\$block$refaddr)"; | ||
| 957 | } | ||
| 958 | else { | ||
| 959 | if (%{$obj}) { | ||
| 960 | my $is_function = exists($self->{function}) && $self->{function} == $refaddr; | ||
| 961 | my $is_class = exists($self->{class}) && $self->{class} == $refaddr; | ||
| 962 | |||
| 963 | local $self->{current_block} = $refaddr; | ||
| 964 | |||
| 965 | if ($is_class) { | ||
| 966 | $code = '{'; | ||
| 967 | if ($is_class) { | ||
| 968 | my $class_name = $self->{class_name}; | ||
| 969 | my $base_pkgs = ( | ||
| 970 | defined($self->{inherit}) | ||
| 971 | ? ( | ||
| 972 | join(' ', grep { $_ ne $class_name } | ||
| 973 | map { ref($_) ? $self->_get_reftype($_) : $_ } @{$self->{inherit}}) | ||
| 974 | ) | ||
| 975 | : '' | ||
| 976 | ) | ||
| 977 | . ($self->{ref_class} ? '' : ' Sidef::Object::Object'); | ||
| 978 | |||
| 979 | if ($base_pkgs ne '') { | ||
| 980 | $code .= "use parent qw(-norequire $base_pkgs);"; | ||
| 981 | } | ||
| 982 | } | ||
| 983 | |||
| 984 | if ($is_class and $self->{ref_class}) { | ||
| 985 | push @{$self->{function_declarations}}, [$refaddr, "", $self->{depth} // 0]; | ||
| 986 | } | ||
| 987 | |||
| 988 | if ($is_class and not $self->{ref_class}) { | ||
| 989 | my @self_class_vars = @{$self->{class_vars}}; | ||
| 990 | my @inherited_class_vars = ( | ||
| 991 | defined($self->{inherit}) | ||
| 992 | ? $self->_get_inherited_stuff($self->{inherit}, sub { exists($_[0]->{vars}) ? @{$_[0]->{vars}} : () }) | ||
| 993 | : (), | ||
| 994 | ); | ||
| 995 | |||
| 996 | my @self_class_attr = (defined($self->{class_attributes}) ? @{$self->{class_attributes}} : ()); | ||
| 997 | my @inherited_class_attr = ( | ||
| 998 | defined($self->{inherit}) | ||
| 999 | ? $self->_get_inherited_stuff( | ||
| 1000 | $self->{inherit}, | ||
| 1001 | sub { | ||
| 1002 | defined($_[0]->{attributes}) ? @{$_[0]->{attributes}} : (); | ||
| 1003 | } | ||
| 1004 | ) | ||
| 1005 | : () | ||
| 1006 | ); | ||
| 1007 | |||
| 1008 | my %in_self; | ||
| 1009 | foreach my $var (@self_class_vars) { | ||
| 1010 | $in_self{$var->{name}} = 1; | ||
| 1011 | } | ||
| 1012 | foreach my $attr (@self_class_attr) { | ||
| 1013 | foreach my $var (@{$attr->{vars}}) { | ||
| 1014 | $in_self{$var->{name}} = 1; | ||
| 1015 | } | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | my @class_vars = | ||
| 1019 | ((grep { !$in_self{$_->{name}} } @inherited_class_vars), @self_class_vars); | ||
| 1020 | my @class_attr = (@inherited_class_attr, @self_class_attr); | ||
| 1021 | |||
| 1022 | $code .= "\$new$refaddr=Sidef::Types::Block::Block->new(code=>sub{"; | ||
| 1023 | push @{$self->{function_declarations}}, [$refaddr, "my \$new$refaddr;", $self->{depth} // 0]; | ||
| 1024 | |||
| 1025 | $code .= $self->_dump_sub_init_vars(@class_vars) . $self->_dump_class_attributes(@class_attr); | ||
| 1026 | |||
| 1027 | my @class_var_attributes = do { | ||
| 1028 | my %seen; | ||
| 1029 | grep { !$seen{$_->{name}}++ } reverse | ||
| 1030 | |||
| 1031 | ((map { @{$_->{vars}} } @inherited_class_attr), (map { @{$_->{vars}} } @self_class_attr)); | ||
| 1032 | }; | ||
| 1033 | |||
| 1034 | $code .= 'my$self=bless{'; | ||
| 1035 | foreach my $var ( | ||
| 1036 | do { | ||
| 1037 | my %seen; | ||
| 1038 | grep { !$seen{$_->{name}}++ } (reverse(@class_vars), @class_var_attributes); | ||
| 1039 | } | ||
| 1040 | ) { | ||
| 1041 | $code .= qq{"\Q$var->{name}\E"=>} . $self->_dump_var($var) . ', '; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | $code .= '},__PACKAGE__;' . 'if(defined(my$sub=UNIVERSAL::can($self,"init"))){$sub->($self)}' . '$self;'; | ||
| 1045 | |||
| 1046 | $code .= | ||
| 1047 | '},' | ||
| 1048 | . $self->_dump_var_attr(@class_vars) | ||
| 1049 | . ',type=>' | ||
| 1050 | . $self->_dump_string('class') | ||
| 1051 | . ',name=>' | ||
| 1052 | . $self->_dump_string($self->{parent_name}[1]) . ');' | ||
| 1053 | . "state\$_$refaddr=do{no strict 'refs';*{" | ||
| 1054 | . $self->_dump_string("$self->{package_name}\::new") . '}=*{' | ||
| 1055 | . $self->_dump_string("$self->{package_name}\::call") | ||
| 1056 | . "}=sub{CORE::shift(\@_);\$new$refaddr->call(\@_)}};"; | ||
| 1057 | |||
| 1058 | foreach my $var (@class_vars, @class_var_attributes) { | ||
| 1059 | $code .= qq{sub $var->{name}:lvalue{\$_[0]->{"\Q$var->{name}\E"}}}; | ||
| 1060 | } | ||
| 1061 | } | ||
| 1062 | } | ||
| 1063 | else { | ||
| 1064 | push @{$self->{function_declarations}}, [$refaddr, "my \$block$refaddr;", $self->{depth} // 0]; | ||
| 1065 | $code = "\$block$refaddr=" . 'Sidef::Types::Block::Block->new('; | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | if (not $is_class) { | ||
| 1069 | $code .= 'code=>sub{'; | ||
| 1070 | if (exists($obj->{init_vars}) and @{$obj->{init_vars}{vars}}) { | ||
| 1071 | $code .= $self->_dump_sub_init_vars(@{$obj->{init_vars}{vars}}); | ||
| 1072 | } | ||
| 1073 | if ($is_function) { | ||
| 1074 | $code .= 'my @return;'; | ||
| 1075 | } | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | local $self->{depth} = ($self->{depth} // 0) + 1; | ||
| 1079 | |||
| 1080 | my @statements = $self->deparse_script($obj->{code}); | ||
| 1081 | |||
| 1082 | $code .= $self->localize_declarations($refaddr); | ||
| 1083 | |||
| 1084 | if ($is_function && @statements) { | ||
| 1085 | $statements[-1] = 'return do{' . $statements[-1] . '}'; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | $code .= join(';', @statements) . ($is_function ? (';' . "END$refaddr: \@return;") : '') . '}'; | ||
| 1089 | |||
| 1090 | if (not $is_class) { | ||
| 1091 | if ($is_function) { | ||
| 1092 | $code .= | ||
| 1093 | ',' . join(',', 'type=>' . $self->_dump_string($self->{parent_name}[0]), 'name=>' . $self->_dump_string($self->{parent_name}[1])); | ||
| 1094 | } | ||
| 1095 | else { | ||
| 1096 | $code .= ',' . join(',', 'type=>' . $self->_dump_string('block'), 'name=>' . $self->_dump_string('__BLOCK__'),); | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | if (exists $self->{class_name}) { | ||
| 1100 | $code .= ',class=>' . $self->_dump_string($self->{class_name}); | ||
| 1101 | } | ||
| 1102 | if (exists($self->{current_namespace}) and $self->{current_namespace} ne 'main') { | ||
| 1103 | $code .= ',namespace=>' . $self->_dump_string($self->{current_namespace}); | ||
| 1104 | } | ||
| 1105 | if (exists($obj->{init_vars}) and @{$obj->{init_vars}{vars}}) { | ||
| 1106 | $code .= ',' . $self->_dump_var_attr(@{$obj->{init_vars}{vars}}); | ||
| 1107 | } | ||
| 1108 | $code .= ')'; | ||
| 1109 | } | ||
| 1110 | } | ||
| 1111 | else { | ||
| 1112 | $code = q{'Sidef::Types::Block::Block'}; | ||
| 1113 | } | ||
| 1114 | } | ||
| 1115 | return $code; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | sub _deparse_struct { | ||
| 1119 | my ($self, $obj, $refaddr) = @_; | ||
| 1120 | my $name = $self->_get_reftype($obj); | ||
| 1121 | if ($addr{$refaddr}++) { | ||
| 1122 | return "'${name}'"; | ||
| 1123 | } | ||
| 1124 | else { | ||
| 1125 | my $code = | ||
| 1126 | "do{package $name {" | ||
| 1127 | . "\$new$refaddr=Sidef::Types::Block::Block->new(code=>sub{" | ||
| 1128 | . $self->_dump_sub_init_vars(@{$obj->{vars}}) | ||
| 1129 | . 'bless{' | ||
| 1130 | . join(',', map { $self->_dump_string($_->{name}) . '=>' . $self->_dump_var($_) } @{$obj->{vars}}) | ||
| 1131 | . "},__PACKAGE__" . '},' | ||
| 1132 | . 'name=>' | ||
| 1133 | . $self->_dump_string($name) . ',' | ||
| 1134 | . 'type=>' | ||
| 1135 | . $self->_dump_string('struct') . ',' | ||
| 1136 | . $self->_dump_var_attr(@{$obj->{vars}}) . ');' | ||
| 1137 | . "state \$_$refaddr = do{no strict 'refs';*{" | ||
| 1138 | . $self->_dump_string("$name\::new") . "}=*{" | ||
| 1139 | . $self->_dump_string("$name\::call") | ||
| 1140 | . "}=sub{CORE::shift(\@_);\$new$refaddr->call(\@_)}};" | ||
| 1141 | . join('', map { "sub $_->{name}:lvalue{\$_[0]->{$_->{name}}}" } @{$obj->{vars}}) | ||
| 1142 | . "};'${name}'}"; | ||
| 1143 | |||
| 1144 | push @{$self->{function_declarations}}, [$refaddr, "my\$new$refaddr;", $self->{depth} // 0]; | ||
| 1145 | return $code; | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | |||
| 1149 | sub _deparse_subset { | ||
| 1150 | my ($self, $obj, $refaddr) = @_; | ||
| 1151 | my $name = $self->_get_reftype($obj); | ||
| 1152 | |||
| 1153 | if ($addr{$refaddr}++) { | ||
| 1154 | return "'${name}'"; | ||
| 1155 | } | ||
| 1156 | else { | ||
| 1157 | my @parents; | ||
| 1158 | if (exists $obj->{inherits}) { | ||
| 1159 | @parents = map { $self->_get_reftype($_) } @{$obj->{inherits}}; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | my $parents_check = ''; | ||
| 1163 | if (@parents) { | ||
| 1164 | $parents_check .= "foreach my \$class (qw(@parents)) {"; | ||
| 1165 | $parents_check .= "my \$code = UNIVERSAL::can(\$class, '__subset_validation__');"; | ||
| 1166 | $parents_check .= "(\$code ? \$code->(\@_) : 1) || return;"; | ||
| 1167 | $parents_check .= "};"; | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | my $subset_block = ''; | ||
| 1171 | if (exists($obj->{block})) { | ||
| 1172 | my $block = $obj->{block}; | ||
| 1173 | $subset_block .= $self->_dump_sub_init_vars($block->{init_vars}{vars}[0]); | ||
| 1174 | $subset_block .= $parents_check; | ||
| 1175 | $subset_block .= $self->deparse_generic('', ';', '', $block->{code}); | ||
| 1176 | } | ||
| 1177 | else { | ||
| 1178 | $subset_block = "$parents_check; 1;"; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | return | ||
| 1182 | "do{package $name {use parent qw(-norequire @parents);" | ||
| 1183 | . "my \$sub = sub { $subset_block };" | ||
| 1184 | . "do{ no strict 'refs'; *{__PACKAGE__ . '::' . '__subset_validation__'} = \$sub };" | ||
| 1185 | . "};'${name}'}"; | ||
| 1186 | } | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | sub _deparse_number { | ||
| 1190 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1191 | my ($type, $content) = $obj->_dump; | ||
| 1192 | if ($type eq 'int') { | ||
| 1193 | return ($constant_number_cache{$content} //= $self->make_constant($ref, '_set_int', "Number", args => ["'${content}'"], sub => 1)); | ||
| 1194 | } | ||
| 1195 | else { | ||
| 1196 | return ($constant_number_cache{join(' ', $type, $content)} //= | ||
| 1197 | $self->make_constant($ref, '_set_str', "Number", args => ["'${type}'", "'${content}'"], sub => 1)); | ||
| 1198 | } | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | sub _deparse_string { | ||
| 1202 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1203 | return ($constant_string_cache{$$obj} //= $self->make_constant($ref, 'new', "String", args => [$self->_dump_string($$obj)])); | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | sub _deparse_array { | ||
| 1207 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1208 | return $self->_dump_array($ref eq 'Sidef::Types::Array::HCArray' ? 'Sidef::Types::Array::Array' : $ref, $obj); | ||
| 1209 | } | ||
| 1210 | sub _deparse_vector { $_[0]->_dump_array('Sidef::Types::Array::Vector', $_[1]) } | ||
| 1211 | sub _deparse_matrix { $_[0]->_dump_array('Sidef::Types::Array::Matrix', $_[1]) } | ||
| 1212 | |||
| 1213 | sub _deparse_bool { | ||
| 1214 | my ($self, $obj) = @_; | ||
| 1215 | return 'Sidef::Types::Bool::Bool::' . (${$obj} ? 'TRUE' : 'FALSE'); | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | sub _deparse_regex { | ||
| 1219 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1220 | return | ||
| 1221 | $self->make_constant($ref, 'new', "Regex", | ||
| 1222 | args => [$self->_dump_string("$obj->{raw}"), $self->_dump_string($obj->{flags} . ($obj->{global} ? 'g' : ''))]); | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | sub _deparse_if { | ||
| 1226 | my ($self, $obj) = @_; | ||
| 1227 | my $code = 'do{'; | ||
| 1228 | foreach my $i (0 .. $#{$obj->{if}}) { | ||
| 1229 | $code .= ($i == 0 ? 'if' : 'elsif'); | ||
| 1230 | my $info = $obj->{if}[$i]; | ||
| 1231 | my $vars = join(',', map { $self->_dump_var($_) } @{$info->{block}{init_vars}{vars}}); | ||
| 1232 | my $arg = $self->deparse_args($info->{expr}); | ||
| 1233 | if ($vars) { | ||
| 1234 | $arg = "(my ($vars) = $arg)[-1]"; | ||
| 1235 | } | ||
| 1236 | $code .= '(' . $arg . ')' . $self->deparse_block_with_scope($info->{block}); | ||
| 1237 | } | ||
| 1238 | if (exists $obj->{else}) { | ||
| 1239 | $code .= 'else' . $self->deparse_block_with_scope($obj->{else}{block}); | ||
| 1240 | } | ||
| 1241 | $code .= '}'; | ||
| 1242 | return $code; | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | sub _deparse_while { | ||
| 1246 | my ($self, $obj) = @_; | ||
| 1247 | my $vars = join(',', map { $self->_dump_var($_) } @{$obj->{block}{init_vars}{vars}}); | ||
| 1248 | my $arg = $self->deparse_args($obj->{expr}); | ||
| 1249 | if ($vars) { | ||
| 1250 | $arg = "(my ($vars) = $arg)[-1]"; | ||
| 1251 | } | ||
| 1252 | return 'while(' . $arg . ')' . $self->deparse_block_with_scope($obj->{block}); | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | sub _deparse_foreach { | ||
| 1256 | my ($self, $obj) = @_; | ||
| 1257 | return $self->deparse_args($obj->{expr}) . '->each' . '(' . $self->deparse_expr({self => $obj->{block}}) . ')'; | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | sub _deparse_cfor { | ||
| 1261 | my ($self, $obj) = @_; | ||
| 1262 | return 'for(' . join(';', map { $self->deparse_args($_) } @{$obj->{expr}}) . ')' . $self->deparse_block_with_scope($obj->{block}); | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | sub _deparse_forin { | ||
| 1266 | my ($self, $obj) = @_; | ||
| 1267 | $self->load_mod('Sidef::Types::Block::Block'); | ||
| 1268 | my @vars = map { $self->_dump_sub_init_vars(@{$_->{vars}}) } @{$obj->{loops}}; | ||
| 1269 | my $block = 'do' . $self->deparse_block_with_scope($obj->{block}); | ||
| 1270 | my @loops = @{$obj->{loops}}; | ||
| 1271 | my $code = $block; | ||
| 1272 | while (@loops) { | ||
| 1273 | my $loop = pop(@loops); | ||
| 1274 | my $vars = pop @vars; | ||
| 1275 | my $expr = $self->deparse_args($loop->{expr}); | ||
| 1276 | my $multi = 0; | ||
| 1277 | if (@{$loop->{vars}} > 1 or (@{$loop->{vars}} == 1 and exists($loop->{vars}[0]{slurpy}))) { | ||
| 1278 | $multi = 1; | ||
| 1279 | } | ||
| 1280 | $code = | ||
| 1281 | 'Sidef::Types::Block::Block::_iterate(sub { ' . ($multi ? 'local @_ = @{$_[0]->to_a};' : '') . "$vars; $code }, $expr)" . (@loops ? ' // last' : ''); | ||
| 1282 | } | ||
| 1283 | return $code; | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | sub _deparse_ternary { | ||
| 1287 | my ($self, $obj) = @_; | ||
| 1288 | return '(' . $self->deparse_script($obj->{cond}) . '?' . $self->deparse_args($obj->{true}) . ':' . $self->deparse_args($obj->{false}) . ')'; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | sub _deparse_named_param { | ||
| 1292 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1293 | return $ref . '->new(' . $self->_dump_string($obj->{name}) . ', ' . $self->deparse_args(@{$obj->{value}}) . ')'; | ||
| 1294 | } | ||
| 1295 | sub _deparse_nil { 'undef' } | ||
| 1296 | |||
| 1297 | sub _deparse_hash { | ||
| 1298 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1299 | $self->load_mod($ref); | ||
| 1300 | return | ||
| 1301 | 'bless({' | ||
| 1302 | . join(',', map { $self->_dump_string($_) . '=>' . (defined($obj->{$_}) ? $self->deparse_expr({self => $obj->{$_}}) : 'undef') } sort(keys(%{$obj}))) | ||
| 1303 | . "}, '${ref}')"; | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | sub _deparse_prefix_method { | ||
| 1307 | my ($self, $obj) = @_; | ||
| 1308 | return 'do{my($self,@args)=' . $self->deparse_args($obj->{expr}) . ';$self->' . $obj->{name} . '(@args)}'; | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | sub _deparse_do { | ||
| 1312 | my ($self, $obj) = @_; | ||
| 1313 | return 'do' . $self->deparse_block_with_scope($obj->{block}); | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | sub _deparse_loop { | ||
| 1317 | my ($self, $obj) = @_; | ||
| 1318 | return 'while(1)' . $self->deparse_block_with_scope($obj->{block}); | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | sub _deparse_given { | ||
| 1322 | my ($self, $obj) = @_; | ||
| 1323 | my $vars = join(',', map { $self->_dump_var($_) } @{$obj->{block}{init_vars}{vars}}); | ||
| 1324 | return | ||
| 1325 | "sub { my \@given_values; my \$continue = 1; my \$given_value = (my ($vars) = " | ||
| 1326 | . $self->deparse_args($obj->{expr}) | ||
| 1327 | . ')[-1];' . 'do' | ||
| 1328 | . $self->deparse_block_with_scope($obj->{block}) | ||
| 1329 | . '; wantarray ? @given_values : $given_values[-1] }->()'; | ||
| 1330 | } | ||
| 1331 | |||
| 1332 | sub _deparse_when { | ||
| 1333 | my ($self, $obj) = @_; | ||
| 1334 | my $vars = join(',', map { $self->_dump_var($_) } @{$obj->{block}{init_vars}{vars}}); | ||
| 1335 | my $arg = $self->deparse_args($obj->{expr}); | ||
| 1336 | if ($vars) { | ||
| 1337 | $arg = "(my ($vars) = $arg)[-1]"; | ||
| 1338 | } | ||
| 1339 | return | ||
| 1340 | "if (\$continue) {my \$t = $arg;" | ||
| 1341 | . "if (defined(\$given_value) ? defined(\$t) ? Sidef::Object::Object::smartmatch(\$given_value, \$t) : 0 : 1) {" | ||
| 1342 | . "\$continue = 0; \@given_values = do" | ||
| 1343 | . $self->deparse_block_with_scope($obj->{block}) . "}};"; | ||
| 1344 | } | ||
| 1345 | |||
| 1346 | sub _deparse_case { | ||
| 1347 | my ($self, $obj) = @_; | ||
| 1348 | my $vars = join(',', map { $self->_dump_var($_) } @{$obj->{block}{init_vars}{vars}}); | ||
| 1349 | my $arg = $self->deparse_args($obj->{expr}); | ||
| 1350 | if ($vars) { | ||
| 1351 | $arg = "(my ($vars) = $arg)[-1]"; | ||
| 1352 | } | ||
| 1353 | return "if (\$continue and $arg) { \$continue = 0;" . "\@given_values = do" . $self->deparse_block_with_scope($obj->{block}) . '}'; | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | sub _deparse_default { | ||
| 1357 | my ($self, $obj) = @_; | ||
| 1358 | return "if (\$continue) { \$continue = 0; \@given_values = do" . $self->deparse_block_with_scope($obj->{block}) . '}'; | ||
| 1359 | } | ||
| 1360 | sub _deparse_continue { '$continue = 1' } | ||
| 1361 | |||
| 1362 | sub _deparse_with { | ||
| 1363 | my ($self, $obj) = @_; | ||
| 1364 | my $code = 'do{'; | ||
| 1365 | foreach my $i (0 .. $#{$obj->{with}}) { | ||
| 1366 | $code .= ($i == 0 ? 'if' : 'elsif'); | ||
| 1367 | my $info = $obj->{with}[$i]; | ||
| 1368 | my $vars = join(',', map { $self->_dump_var($_) } @{$info->{block}{init_vars}{vars}}); | ||
| 1369 | $code .= "(defined((my ($vars) = do{" . $self->deparse_args($info->{expr}) . '})[-1]))' . $self->deparse_block_with_scope($info->{block}); | ||
| 1370 | } | ||
| 1371 | if (exists $obj->{else}) { | ||
| 1372 | $code .= 'else' . $self->deparse_block_with_scope($obj->{else}{block}); | ||
| 1373 | } | ||
| 1374 | $code .= '}'; | ||
| 1375 | return $code; | ||
| 1376 | } | ||
| 1377 | |||
| 1378 | sub _deparse_gather { | ||
| 1379 | my ($self, $obj, $refaddr) = @_; | ||
| 1380 | $self->load_mod("Sidef::Types::Array::Array"); | ||
| 1381 | return "do{my \@_$refaddr;" . 'do' . $self->deparse_block_with_scope($obj->{block}) . "; bless(\\\@_$refaddr, 'Sidef::Types::Array::Array')}"; | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | sub _deparse_take { | ||
| 1385 | my ($self, $obj) = @_; | ||
| 1386 | my $raddr = refaddr($obj->{gather}); | ||
| 1387 | return "do{ push \@_$raddr," . $self->deparse_args($obj->{expr}) . "; \$_$raddr\[-1] }"; | ||
| 1388 | } | ||
| 1389 | |||
| 1390 | sub _deparse_try { | ||
| 1391 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1392 | return | ||
| 1393 | $ref | ||
| 1394 | . '->new->try(' | ||
| 1395 | . $self->deparse_expr({self => $obj->{try}}) | ||
| 1396 | . ')->catch(' | ||
| 1397 | . (defined($obj->{catch}) ? $self->deparse_expr({self => $obj->{catch}}) : '') . ')'; | ||
| 1398 | } | ||
| 1399 | sub _deparse_ref_ok { '' } | ||
| 1400 | sub _deparse_class_attr_ok { '' } | ||
| 1401 | sub _deparse_unary_ok { '' } | ||
| 1402 | sub _deparse_break { 'last' } | ||
| 1403 | sub _deparse_next { 'next' } | ||
| 1404 | |||
| 1405 | sub _deparse_return { | ||
| 1406 | my ($self, $obj, $refaddr, $ref, $expr) = @_; | ||
| 1407 | if (not exists $expr->{call}) { | ||
| 1408 | if (exists $self->{function}) { | ||
| 1409 | return "goto END$self->{function}"; | ||
| 1410 | } | ||
| 1411 | else { | ||
| 1412 | return "return;"; | ||
| 1413 | } | ||
| 1414 | } | ||
| 1415 | return ''; | ||
| 1416 | } | ||
| 1417 | |||
| 1418 | sub _deparse_stdin { $_[0]->make_constant('Sidef::Types::Glob::FileHandle', 'new', "STDIN", args => ['\*STDIN']) } | ||
| 1419 | sub _deparse_stdout { $_[0]->make_constant('Sidef::Types::Glob::FileHandle', 'new', "STDOUT", args => ['\*STDOUT']) } | ||
| 1420 | sub _deparse_stderr { $_[0]->make_constant('Sidef::Types::Glob::FileHandle', 'new', "STDERR", args => ['\*STDERR']) } | ||
| 1421 | sub _deparse_argf { $_[0]->make_constant('Sidef::Types::Glob::FileHandle', 'new', "ARGF", args => ['\*ARGV']) } | ||
| 1422 | |||
| 1423 | sub _deparse_data { | ||
| 1424 | my ($self, $obj) = @_; | ||
| 1425 | require Encode; | ||
| 1426 | my $data = $self->_dump_string(Encode::encode_utf8(${$obj->{data}})); | ||
| 1427 | return $self->make_constant('Sidef::Types::Glob::FileHandle', 'new', "DATA", args => [qq{do{open my \$fh, '<:utf8', \\$data; \$fh}}]); | ||
| 1428 | } | ||
| 1429 | sub _deparse_magic { $_[1]->{name} } | ||
| 1430 | |||
| 1431 | sub _deparse_eval { | ||
| 1432 | my ($self, $obj, $refaddr) = @_; | ||
| 1433 | $Sidef::EVALS{$refaddr} = $obj; | ||
| 1434 | return qq~ | ||
| 1435 | eval do{ | ||
| 1436 | local \$Sidef::DEPARSER->{before} = ''; | ||
| 1437 | local \$Sidef::DEPARSER->{top_program} = ''; | ||
| 1438 | local \$Sidef::DEPARSER->{_has_constant}; | ||
| 1439 | local \$Sidef::DEPARSER->{function_declarations} = []; | ||
| 1440 | local \$Sidef::DEPARSER->{block_declarations} = []; | ||
| 1441 | \$Sidef::DEPARSER->deparse( | ||
| 1442 | do{ | ||
| 1443 | local \@{\$Sidef::PARSER}{keys \%{\$Sidef::EVALS{$refaddr}{parser}}} = values \%{\$Sidef::EVALS{$refaddr}{parser}}; | ||
| 1444 | local \$Sidef::PARSER->{line} = 1; | ||
| 1445 | local \$Sidef::PARSER->{eval_mode} = 1; | ||
| 1446 | local \$Sidef::PARSER->{file_name} = 'eval($refaddr)'; | ||
| 1447 | #local \$Sidef::PARSER->{vars} = \$Sidef::EVALS{$refaddr}{vars}; | ||
| 1448 | #local \$Sidef::PARSER->{ref_vars_refs} = \$Sidef::EVALS{$refaddr}{ref_vars_refs}; | ||
| 1449 | \$Sidef::PARSER->parse_script(code => do{my\$o=~ . $self->deparse_args($obj->{expr}) . qq~;\\"\$o"}); | ||
| 1450 | })}~; | ||
| 1451 | } | ||
| 1452 | |||
| 1453 | sub _deparse_complex { | ||
| 1454 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1455 | my ($real, $imag) = $obj->reals; | ||
| 1456 | my $name = "Complex$refaddr"; | ||
| 1457 | $self->top_add("use constant $name => $ref\->new(" . join(',', $self->deparse_expr({self => $real}), $self->deparse_expr({self => $imag})) . ');'); | ||
| 1458 | return "($self->{environment_name}\::$name)"; | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | sub _deparse_pair { | ||
| 1462 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1463 | return $ref . '->new(' . join(',', map { defined($_) ? $self->deparse_expr({self => $_}) : 'undef' } @{$obj}) . ')'; | ||
| 1464 | } | ||
| 1465 | |||
| 1466 | sub _deparse_null { | ||
| 1467 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1468 | return $self->make_constant($ref, 'new', "Null", args => []); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | sub _deparse_module_oo { | ||
| 1472 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1473 | return $self->make_constant($ref, '__NEW__', "ModuleOO", args => [$self->_dump_string($obj->{module})]); | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | sub _deparse_module_func { | ||
| 1477 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1478 | return $self->make_constant($ref, '__NEW__', "ModuleFunc", args => [$self->_dump_string($obj->{module})]); | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | sub _deparse_perl_code { | ||
| 1482 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1483 | return $self->make_constant($ref, 'new', "PerlCode", args => [$self->_dump_string(${$obj})]); | ||
| 1484 | } | ||
| 1485 | |||
| 1486 | sub _deparse_backtick { | ||
| 1487 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1488 | return $self->make_constant($ref, 'new', "Backtick", args => [$self->_dump_string(${$obj})]); | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | sub _deparse_file { | ||
| 1492 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1493 | return $self->make_constant($ref, 'new', "File", args => [$self->_dump_string(${$obj})]); | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | sub _deparse_dir { | ||
| 1497 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1498 | return $self->make_constant($ref, 'new', "Dir", args => [$self->_dump_string(${$obj})]); | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | sub _deparse_meta_module { | ||
| 1502 | my ($self, $obj) = @_; | ||
| 1503 | return substr($self->deparse_bare_block($obj->{block}{code}), 1, -1); | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | sub _deparse_included { | ||
| 1507 | my ($self, $obj) = @_; | ||
| 1508 | my $code = ''; | ||
| 1509 | foreach my $info (@{$obj->{included}}) { | ||
| 1510 | $code .= join(';', $self->deparse_script($info->{ast})) . ';'; | ||
| 1511 | } | ||
| 1512 | return $code; | ||
| 1513 | } | ||
| 1514 | |||
| 1515 | sub _deparse_assert { | ||
| 1516 | my ($self, $obj, $refaddr) = @_; | ||
| 1517 | my @args = $self->deparse_script($obj->{arg}); | ||
| 1518 | |||
| 1519 | if ($obj->{act} eq 'assert') { | ||
| 1520 | @args > 2 | ||
| 1521 | and die "[ERROR] Incorrect number of arguments for $obj->{act}\() at" . " $obj->{file} line $obj->{line} (expected 1 or 2 arguments)\n"; | ||
| 1522 | my $msg = $args[1] // 'undef'; | ||
| 1523 | return | ||
| 1524 | qq~do{my \$a$refaddr = do{$args[0]}; ~ | ||
| 1525 | . qq~\$a$refaddr or CORE::die((do{$msg} // "$obj->{act}(\$a$refaddr)") . " failed ~ | ||
| 1526 | . qq~at \Q$obj->{file}\E line $obj->{line}\\n")}~; | ||
| 1527 | } | ||
| 1528 | elsif ($obj->{act} eq 'assert_eq' or $obj->{act} eq 'assert_ne') { | ||
| 1529 | @args > 3 | ||
| 1530 | and die "[ERROR] Incorrect number of arguments for $obj->{act}\() at" . " $obj->{file} line $obj->{line} (expected 2 or 3 arguments)\n"; | ||
| 1531 | return | ||
| 1532 | "do{" | ||
| 1533 | . "my \$a$refaddr = do{$args[0]};" | ||
| 1534 | . "my \$b$refaddr = do{$args[1]};" | ||
| 1535 | . ($obj->{act} eq 'assert_ne' ? qq{!(\$a$refaddr eq \$b$refaddr)} : qq{\$a$refaddr eq \$b$refaddr}) | ||
| 1536 | . qq~ or CORE::die((~ | ||
| 1537 | . (defined($args[2]) ? "do{$args[2]}" : "undef") | ||
| 1538 | . qq~// ('$obj->{act}('.~ | ||
| 1539 | . qq~join(', ',map{(ref(\$_) && UNIVERSAL::can(\$_,'dump')) ? \$_->dump : \$_}(\$a$refaddr, \$b$refaddr)) . ')'))~ | ||
| 1540 | . qq~." failed at \Q$obj->{file}\E line $obj->{line}\\n")}~; | ||
| 1541 | } | ||
| 1542 | return ''; | ||
| 1543 | } | ||
| 1544 | |||
| 1545 | sub _deparse_error { | ||
| 1546 | my ($self, $obj) = @_; | ||
| 1547 | my @args = $self->deparse_args($obj->{arg}); | ||
| 1548 | return qq~do{CORE::die(@args, " at \Q$obj->{file}\E line $obj->{line}\\n")}~; | ||
| 1549 | } | ||
| 1550 | |||
| 1551 | sub _deparse_warning { | ||
| 1552 | my ($self, $obj) = @_; | ||
| 1553 | my @args = $self->deparse_args($obj->{arg}); | ||
| 1554 | return qq~((CORE::warn(@args, " at \Q$obj->{file}\E line $obj->{line}\\n")) ? ~ . qq~(Sidef::Types::Bool::Bool::FALSE) : (Sidef::Types::Bool::Bool::TRUE))~; | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | sub _deparse_pipe { | ||
| 1558 | my ($self, $obj, $refaddr, $ref) = @_; | ||
| 1559 | return $self->make_constant($ref, 'new', "Pipe", args => [map { $self->_dump_string($_) } @{$obj}]); | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | sub _deparse_parser { '$Sidef::PARSER' } | ||
| 1563 | |||
| 1564 | sub _deparse_unimplemented { | ||
| 1565 | my ($self, $obj) = @_; | ||
| 1566 | return qq{CORE::die "Unimplemented at " . } . $self->_dump_string($obj->{file}) . qq{. " line $obj->{line}\\n"}; | ||
| 1567 | } | ||
| 1568 | sub _deparse_label { $_[1]->{name} . ': ' } | ||
| 1569 | sub _deparse_builtin { '' } | ||
| 1570 | |||
| 1571 | #---------------------------------------------------------------------- | ||
| 1572 | # Main logic | ||
| 1573 | #---------------------------------------------------------------------- | ||
| 1574 | |||
| 1575 | sub deparse_expr { | ||
| 1576 | my ($self, $expr) = @_; | ||
| 1577 | |||
| 1578 | my $code = ''; | ||
| 1579 | my $obj = $expr->{self}; | ||
| 1580 | my $refaddr = refaddr($obj); | ||
| 1581 | |||
| 1582 | # Self obj | ||
| 1583 | my $ref = ref($obj) || return ''; | ||
| 1584 | |||
| 1585 | if (exists $handlers{$ref}) { | ||
| 1586 | my $method = $handlers{$ref}; | ||
| 1587 | $code = $self->$method($obj, $refaddr, $ref, $expr); | ||
| 1588 | } | ||
| 1589 | elsif ($ref eq 'HASH') { | ||
| 1590 | $code = join(',', exists($obj->{self}) ? $self->deparse_expr($obj) : $self->deparse_script($obj)); | ||
| 1591 | } | ||
| 1592 | elsif (exists($COMPOSITE_CONSTANTS{$ref})) { | ||
| 1593 | my $data = $COMPOSITE_CONSTANTS{$ref}; | ||
| 1594 | $code = $self->make_constant( | ||
| 1595 | $ref, 'new', | ||
| 1596 | $data->{name}, | ||
| 1597 | new => 1, | ||
| 1598 | args => [map { $self->deparse_expr({self => $obj->{$_}}) } @{$data->{fields}}] | ||
| 1599 | ); | ||
| 1600 | } | ||
| 1601 | elsif (exists $DATA_TYPES{$ref}) { | ||
| 1602 | my $mod = $DATA_TYPES{$ref}; | ||
| 1603 | $self->{_reftype_cache}{$mod} //= do { | ||
| 1604 | $self->load_mod($mod); | ||
| 1605 | 1; | ||
| 1606 | }; | ||
| 1607 | $code = "'" . $mod . "'"; | ||
| 1608 | } | ||
| 1609 | else { | ||
| 1610 | die "[PERL DEPARSER BUG] Unknown object of type <<$ref>>"; | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | # Array and hash indices | ||
| 1614 | if (exists $expr->{ind}) { | ||
| 1615 | my $limit = $#{$expr->{ind}}; | ||
| 1616 | foreach my $i (0 .. $limit) { | ||
| 1617 | |||
| 1618 | my $ind = $expr->{ind}[$i]; | ||
| 1619 | if (exists $ind->{array}) { | ||
| 1620 | my $indices = $self->_dump_indices($ind->{array}); | ||
| 1621 | |||
| 1622 | $code = '@{(' . $code . ')}'; | ||
| 1623 | |||
| 1624 | if ($indices ne '') { | ||
| 1625 | $code .= "[$indices]"; | ||
| 1626 | } | ||
| 1627 | } | ||
| 1628 | else { | ||
| 1629 | my $keys = $self->_dump_lookups($ind->{hash}); | ||
| 1630 | |||
| 1631 | if ($keys eq '') { | ||
| 1632 | $code = '%{(' . $code . ')}'; | ||
| 1633 | } | ||
| 1634 | else { | ||
| 1635 | $code = "\@{($code)}{$keys}"; | ||
| 1636 | } | ||
| 1637 | } | ||
| 1638 | |||
| 1639 | if ($i < $limit) { | ||
| 1640 | if ($expr->{ind}[$i + 1]{array}) { | ||
| 1641 | $self->load_mod('Sidef::Types::Array::Array'); | ||
| 1642 | $code = "($code//=bless([], 'Sidef::Types::Array::Array'))"; | ||
| 1643 | } | ||
| 1644 | else { | ||
| 1645 | $self->load_mod('Sidef::Types::Hash::Hash'); | ||
| 1646 | $code = "($code//=bless({}, 'Sidef::Types::Hash::Hash'))"; | ||
| 1647 | } | ||
| 1648 | } | ||
| 1649 | } | ||
| 1650 | } | ||
| 1651 | |||
| 1652 | # Method call on the self obj (+optional arguments) | ||
| 1653 | if (exists $expr->{call}) { | ||
| 1654 | |||
| 1655 | my $unary; | ||
| 1656 | my $end = $#{$expr->{call}}; | ||
| 1657 | |||
| 1658 | foreach my $i (0 .. $end) { | ||
| 1659 | |||
| 1660 | my $call = $expr->{call}[$i]; | ||
| 1661 | my $method = $call->{method}; | ||
| 1662 | |||
| 1663 | if ($code ne '') { | ||
| 1664 | $code = '(' . $code . ')'; | ||
| 1665 | } | ||
| 1666 | |||
| 1667 | # Optimization for hashes | ||
| 1668 | if ( | ||
| 1669 | ( | ||
| 1670 | $ref eq 'Sidef::DataTypes::Hash::Hash' and $i == 0 and ( $method eq 'call' | ||
| 1671 | or $method eq 'new') | ||
| 1672 | ) | ||
| 1673 | or ($ref eq 'Sidef::Meta::PrefixColon' and $method eq ':') | ||
| 1674 | ) { | ||
| 1675 | $code = 'bless({' . $self->deparse_args(@{$call->{arg}}) . "}, '$DATA_TYPES{$ref}')"; | ||
| 1676 | next; | ||
| 1677 | } | ||
| 1678 | |||
| 1679 | # Handle the return statement | ||
| 1680 | if ($ref eq 'Sidef::Types::Block::Return') { | ||
| 1681 | |||
| 1682 | if (exists $self->{function}) { | ||
| 1683 | $code .= 'do{'; | ||
| 1684 | if (@{$call->{arg}}) { | ||
| 1685 | $code .= '@return=' . $self->deparse_args(@{$call->{arg}}) . ';'; | ||
| 1686 | } | ||
| 1687 | $code .= 'goto ' . "END$self->{function}}"; | ||
| 1688 | } | ||
| 1689 | else { | ||
| 1690 | $code .= 'return' . $self->deparse_args(@{$call->{arg}}); | ||
| 1691 | } | ||
| 1692 | |||
| 1693 | next; | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | if (defined $method) { | ||
| 1697 | |||
| 1698 | if ($ref eq 'Sidef::Variable::Ref') { # variables | ||
| 1699 | |||
| 1700 | # Variable referencing | ||
| 1701 | if ($method eq '\\' or $method eq '&') { | ||
| 1702 | $code = '\\' . $self->deparse_args(@{$call->{arg}}); | ||
| 1703 | next; | ||
| 1704 | } | ||
| 1705 | |||
| 1706 | # Variable dereferencing | ||
| 1707 | elsif ($method eq '*') { | ||
| 1708 | $code = '${' . $self->deparse_args(@{$call->{arg}}) . '}'; | ||
| 1709 | next; | ||
| 1710 | } | ||
| 1711 | |||
| 1712 | # Prefix ++ and -- operators on variables | ||
| 1713 | elsif (exists $INC_DEC_OPS{$method}) { | ||
| 1714 | my $var = $self->deparse_args(@{$call->{arg}}); | ||
| 1715 | $code = "do{my\$r=\\$var;\$\$r=\$\$r\->$INC_DEC_OPS{$method}}"; | ||
| 1716 | next; | ||
| 1717 | } | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | # Postfix ++ and -- operators on variables | ||
| 1721 | if (exists($INC_DEC_OPS{$method})) { | ||
| 1722 | $code = "do{my\$r=\\$code;my\$v=\$\$r;\$\$r=\$v\->$INC_DEC_OPS{$method};\$v}"; | ||
| 1723 | next; | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | if (exists($LAZY_OPS{$method})) { | ||
| 1727 | $code .= $LAZY_OPS{$method} . $self->deparse_args(@{$call->{arg}}); | ||
| 1728 | next; | ||
| 1729 | } | ||
| 1730 | |||
| 1731 | # Variable assignment (=) | ||
| 1732 | if (exists($ASSIGNMENT_OPS{$method})) { | ||
| 1733 | $code = "($code$ASSIGNMENT_OPS{$method}" . $self->deparse_args(@{$call->{arg}}) . ")[-1]"; | ||
| 1734 | next; | ||
| 1735 | } | ||
| 1736 | |||
| 1737 | # Reassignment operators, such as: +=, -=, *=, /=, etc... | ||
| 1738 | if (exists $REASSIGN_OPS{$method}) { | ||
| 1739 | $code = | ||
| 1740 | "CORE::sub:lvalue{my\$r=\\$code;\$\$r=\$\$r" | ||
| 1741 | . $self->_dump_op_call($REASSIGN_OPS{$method}) | ||
| 1742 | . $self->deparse_args(@{$call->{arg}}) . '}->()'; | ||
| 1743 | next; | ||
| 1744 | } | ||
| 1745 | |||
| 1746 | # != and == methods | ||
| 1747 | if ($method eq '==' or $method eq '!=') { | ||
| 1748 | $code = | ||
| 1749 | 'do{my$bool=' | ||
| 1750 | . $code . 'eq' | ||
| 1751 | . $self->deparse_args(@{$call->{arg}}) | ||
| 1752 | . ';ref($bool) ? $bool : ($bool ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE)}' | ||
| 1753 | . ($method eq '!=' ? '->not' : ''); | ||
| 1754 | next; | ||
| 1755 | } | ||
| 1756 | |||
| 1757 | # !~ and ~~ methods | ||
| 1758 | if ($method eq '~~' or $method eq '!~') { | ||
| 1759 | $code = | ||
| 1760 | 'do{my$bool=Sidef::Object::Object::smartmatch(do{' | ||
| 1761 | . $code | ||
| 1762 | . '}, do{' | ||
| 1763 | . $self->deparse_args(@{$call->{arg}}) | ||
| 1764 | . '});ref($bool) ? $bool : ($bool ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE)}' | ||
| 1765 | . ($method eq '!~' ? '->not' : ''); | ||
| 1766 | next; | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | # <=> method | ||
| 1770 | if ($method eq '<=>') { | ||
| 1771 | $code = | ||
| 1772 | 'do{my$cmp=' | ||
| 1773 | . $code . 'cmp' | ||
| 1774 | . $self->deparse_args(@{$call->{arg}}) | ||
| 1775 | . ';ref($cmp) ? $cmp : defined($cmp) ? (($cmp<0) ? Sidef::Types::Number::Number::MONE : ' | ||
| 1776 | . '($cmp>0) ? Sidef::Types::Number::Number::ONE : Sidef::Types::Number::Number::ZERO) : undef}'; | ||
| 1777 | next; | ||
| 1778 | } | ||
| 1779 | |||
| 1780 | # Unary prefix operator | ||
| 1781 | if ($ref eq 'Sidef::Operator::Unary' and !$unary) { | ||
| 1782 | |||
| 1783 | $unary = 1; # once per call | ||
| 1784 | |||
| 1785 | if ($method eq '!') { | ||
| 1786 | $code = '(' . $self->deparse_args(@{$call->{arg}}) . '? Sidef::Types::Bool::Bool::FALSE : Sidef::Types::Bool::Bool::TRUE)'; | ||
| 1787 | next; | ||
| 1788 | } | ||
| 1789 | |||
| 1790 | if ($method eq '-') { | ||
| 1791 | |||
| 1792 | # Constant-folding: negate the literal number | ||
| 1793 | my $data = $call->{arg}; | ||
| 1794 | if (scalar(@$data) == 1 and ref($data->[0]) eq 'HASH' and scalar(keys %{$data->[0]}) == 1) { | ||
| 1795 | $data = $data->[0]; | ||
| 1796 | my ($class) = keys(%$data); | ||
| 1797 | $data = $data->{$class}; | ||
| 1798 | if (ref($data) eq 'ARRAY' and scalar(@$data) == 1) { | ||
| 1799 | $data = $data->[0]; | ||
| 1800 | if (ref($data) eq 'HASH' and scalar(keys %$data) == 1 and exists($data->{self})) { | ||
| 1801 | $data = $data->{self}; | ||
| 1802 | } | ||
| 1803 | if (ref($data) eq 'Sidef::Types::Number::Number') { | ||
| 1804 | $code = $self->deparse_expr({self => $data->neg}); | ||
| 1805 | next; | ||
| 1806 | } | ||
| 1807 | } | ||
| 1808 | } | ||
| 1809 | |||
| 1810 | $code = $self->deparse_args(@{$call->{arg}}) . '->neg'; | ||
| 1811 | next; | ||
| 1812 | } | ||
| 1813 | |||
| 1814 | if ($method eq '+') { | ||
| 1815 | $code = 'scalar' . $self->deparse_args(@{$call->{arg}}); | ||
| 1816 | next; | ||
| 1817 | } | ||
| 1818 | |||
| 1819 | if ($method eq '@') { | ||
| 1820 | $self->load_mod('Sidef::Types::Array::Array'); | ||
| 1821 | $code = | ||
| 1822 | '(do{my$obj=' | ||
| 1823 | . $self->deparse_args(@{$call->{arg}}) | ||
| 1824 | . ';my$sub=ref($obj) && UNIVERSAL::can($obj, "to_a"); ' | ||
| 1825 | . '$sub ? $sub->($obj) : bless([$obj], "Sidef::Types::Array::Array")})'; | ||
| 1826 | next; | ||
| 1827 | } | ||
| 1828 | |||
| 1829 | if ($method eq '@|') { | ||
| 1830 | $code = | ||
| 1831 | '(do{my$obj=' | ||
| 1832 | . $self->deparse_args(@{$call->{arg}}) | ||
| 1833 | . ';my$sub=ref($obj) && UNIVERSAL::can($obj, "..."); ' | ||
| 1834 | . '$sub ? $sub->($obj) : $obj })'; | ||
| 1835 | next; | ||
| 1836 | } | ||
| 1837 | |||
| 1838 | if ($method eq '~') { | ||
| 1839 | $code = $self->deparse_args(@{$call->{arg}}) . '->not'; | ||
| 1840 | next; | ||
| 1841 | } | ||
| 1842 | |||
| 1843 | if ($method eq '√') { | ||
| 1844 | $code = $self->deparse_args(@{$call->{arg}}) . '->sqrt'; | ||
| 1845 | next; | ||
| 1846 | } | ||
| 1847 | |||
| 1848 | if ($method eq '^') { | ||
| 1849 | $code = $self->deparse_args(@{$call->{arg}}) . '->range'; | ||
| 1850 | next; | ||
| 1851 | } | ||
| 1852 | |||
| 1853 | if ($method eq 'say' or $method eq '>') { | ||
| 1854 | $code = | ||
| 1855 | '((CORE::say' . $self->deparse_args(@{$call->{arg}}) . ') ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE)'; | ||
| 1856 | next; | ||
| 1857 | } | ||
| 1858 | |||
| 1859 | if ($method eq 'print' or $method eq '>>') { | ||
| 1860 | $code = | ||
| 1861 | '((CORE::print' . $self->deparse_args(@{$call->{arg}}) . ') ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE)'; | ||
| 1862 | next; | ||
| 1863 | } | ||
| 1864 | |||
| 1865 | if ($method eq 'defined') { | ||
| 1866 | $code = | ||
| 1867 | '((CORE::defined' . $self->deparse_args(@{$call->{arg}}) . ') ? Sidef::Types::Bool::Bool::TRUE : Sidef::Types::Bool::Bool::FALSE)'; | ||
| 1868 | next; | ||
| 1869 | } | ||
| 1870 | } | ||
| 1871 | |||
| 1872 | if (ref($method)) { | ||
| 1873 | $code .= | ||
| 1874 | '->${\\do{' . $self->deparse_expr(ref($method) eq 'HASH' ? $method : {self => $method}) . '}}'; | ||
| 1875 | } | ||
| 1876 | elsif ($method =~ /^[^\W\d]/) { | ||
| 1877 | |||
| 1878 | # Exclamation mark (!) at the end of a method | ||
| 1879 | if (substr($method, -1) eq '!') { | ||
| 1880 | $code = | ||
| 1881 | "CORE::sub:lvalue{my\$r=\\$code;\$\$r=\$\$r\->" | ||
| 1882 | . substr($method, 0, -1) | ||
| 1883 | . (exists($call->{arg}) ? $self->deparse_args(@{$call->{arg}}) : '') . '}->()'; | ||
| 1884 | next; | ||
| 1885 | } | ||
| 1886 | |||
| 1887 | # Special case for methods without '->' | ||
| 1888 | else { | ||
| 1889 | $code .= '->' if $code ne ''; | ||
| 1890 | $code .= $method; | ||
| 1891 | } | ||
| 1892 | } | ||
| 1893 | else { | ||
| 1894 | $code .= $self->_dump_op_call($method); | ||
| 1895 | } | ||
| 1896 | } | ||
| 1897 | |||
| 1898 | if (exists $call->{keyword}) { | ||
| 1899 | |||
| 1900 | if ($call->{keyword} eq 'if') { | ||
| 1901 | $code = $self->deparse_args(@{$call->{arg}}) . '&&' . $code; | ||
| 1902 | next; | ||
| 1903 | } | ||
| 1904 | |||
| 1905 | $code .= $call->{keyword}; | ||
| 1906 | } | ||
| 1907 | |||
| 1908 | if (exists $call->{arg}) { | ||
| 1909 | $code .= $self->deparse_args(@{$call->{arg}}); | ||
| 1910 | } | ||
| 1911 | } | ||
| 1912 | } | ||
| 1913 | |||
| 1914 | if ($code eq '') { | ||
| 1915 | $code = '()'; | ||
| 1916 | } | ||
| 1917 | |||
| 1918 | $code; | ||
| 1919 | } | ||
| 1920 | |||
| 1921 | sub deparse_script { | ||
| 1922 | my ($self, $struct) = @_; | ||
| 1923 | |||
| 1924 | my @results; | ||
| 1925 | |||
| 1926 | foreach my $class (keys %$struct) { | ||
| 1927 | |||
| 1928 | my $max = $#{$struct->{$class}}; | ||
| 1929 | foreach my $i (0 .. $max) { | ||
| 1930 | my $expr = $struct->{$class}[$i]; | ||
| 1931 | push @results, ref($expr) eq 'HASH' ? $self->deparse_expr($expr) : $self->deparse_expr({self => $expr}); | ||
| 1932 | } | ||
| 1933 | } | ||
| 1934 | |||
| 1935 | wantarray ? @results : $results[-1]; | ||
| 1936 | } | ||
| 1937 | |||
| 1938 | sub deparse { | ||
| 1939 | my ($self, $struct) = @_; | ||
| 1940 | my @statements = $self->deparse_script($struct); | ||
| 1941 | |||
| 1942 | ( | ||
| 1943 | $self->{before} | ||
| 1944 | . ($self->{_has_constant} ? '};' : '') | ||
| 1945 | . ( | ||
| 1946 | exists($self->{function_declarations}) && @{$self->{function_declarations}} ? join('', map { $_->[1] } @{$self->{function_declarations}}) | ||
| 1947 | : '' | ||
| 1948 | ) | ||
| 1949 | . ( | ||
| 1950 | exists($self->{block_declarations}) && @{$self->{block_declarations}} ? join('', map { $_->[1] } @{$self->{block_declarations}}) | ||
| 1951 | : '' | ||
| 1952 | ) | ||
| 1953 | . $self->{top_program} | ||
| 1954 | . join($self->{between}, @statements) | ||
| 1955 | . $self->{after} | ||
| 1956 | ) =~ s/^\s*/$self->{header}/r; | ||
| 1957 | } | ||
| 1958 | |||
| 1959 | 1 |