| 1 | package Sidef { | ||
| 2 | |||
| 3 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use 5.016; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '26.09'; | ||
| 7 | |||
| 8 | our $SPACES = 0; # the current number of indentation spaces | ||
| 9 | our $SPACES_INCR = 4; # the number of indentation spaces | ||
| 10 | |||
| 11 | our %INCLUDED; # will keep track of included modules | ||
| 12 | our %EVALS; # will contain info required for eval() | ||
| 13 | |||
| 14 | 1 | ▶ | use constant { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | UPDATE_SEC => 5 * 60 * 60, # 5 hours | |
| 16 | DELETE_SEC => 2 * 24 * 60 * 60, # 2 days | ||
| 17 | SANITIZE_SEC => 3 * 24 * 60 * 60, # 3 days | ||
| 18 | 1 | }; | |
| 19 | |||
| 20 | 1 | ▶ | use List::Util qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use File::Spec qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | 1 | ▶ | use Sidef::Types::Bool::Bool; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 0 | ▶ | use Sidef::Types::Number::Number; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | |||
| 26 | sub new { | ||
| 27 | my ($class, %opt) = @_; | ||
| 28 | bless \%opt, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | *call = \&new; | ||
| 32 | |||
| 33 | sub version { | ||
| 34 | Sidef::Types::String::String->new('v' . $VERSION); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub numeric_version { | ||
| 38 | Sidef::Types::Number::Number->new($VERSION =~ s{_}{}rg); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub parse_code { | ||
| 42 | my ($self, $code) = @_; | ||
| 43 | |||
| 44 | local %INCLUDED; | ||
| 45 | |||
| 46 | $self->{parser} //= Sidef::Parser->new( | ||
| 47 | opt => $self->{opt}, | ||
| 48 | file_name => $self->{name} // '-', | ||
| 49 | script_name => $self->{name} // '-', | ||
| 50 | ($self->{parser_opt} ? (%{$self->{parser_opt}}) : ()), | ||
| 51 | ); | ||
| 52 | |||
| 53 | my $ast = $self->{parser}->parse_script(code => \$code); | ||
| 54 | |||
| 55 | # Check for optimization | ||
| 56 | if (defined(my $level = $self->{opt}{O})) { | ||
| 57 | |||
| 58 | # Optimize the AST | ||
| 59 | if ($level >= 1) { | ||
| 60 | $ast = $self->optimize_ast($ast); | ||
| 61 | } | ||
| 62 | |||
| 63 | # Deparse the AST into code, then parse the code again. | ||
| 64 | if ($level >= 2) { | ||
| 65 | my $sidef = Sidef->new( | ||
| 66 | opt => $self->{opt}, | ||
| 67 | name => $self->{name}, | ||
| 68 | parser_opt => $self->{parser_opt}, | ||
| 69 | ); | ||
| 70 | |||
| 71 | local $sidef->{opt}{O} = 1; | ||
| 72 | return $sidef->parse_code($self->compile_ast($ast, 'Sidef')); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return $ast; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub optimize_ast { | ||
| 80 | my ($self, $ast) = @_; | ||
| 81 | my $optimizer = Sidef::Optimizer->new; | ||
| 82 | scalar {$optimizer->optimize($ast)}; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub execute_code { | ||
| 86 | my ($self, $code) = @_; | ||
| 87 | $self->execute_perl($self->compile_code($code, 'Perl')); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub execute_perl { | ||
| 91 | my ($self, $code) = @_; | ||
| 92 | local $Sidef::PARSER = $self->{parser}; | ||
| 93 | local $Sidef::DEPARSER = $self->{Perl}{deparser}; | ||
| 94 | eval($code); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub get_sidef_config_dir { | ||
| 98 | my ($self) = @_; | ||
| 99 | |||
| 100 | $self->{sidef_config_dir} //= $ENV{SIDEF_CONFIG_DIR} | ||
| 101 | || File::Spec->catdir( | ||
| 102 | $ENV{XDG_CONFIG_DIR} | ||
| 103 | || ( | ||
| 104 | $ENV{HOME} | ||
| 105 | || $ENV{LOGDIR} | ||
| 106 | || ( | ||
| 107 | $^O eq 'MSWin32' | ||
| 108 | ? '\Local Settings\Application Data' | ||
| 109 | : eval { ((getpwuid($<))[7] || `echo -n ~`) } | ||
| 110 | ) | ||
| 111 | || File::Spec->curdir() | ||
| 112 | ), | ||
| 113 | '.config', | ||
| 114 | 'sidef' | ||
| 115 | ); | ||
| 116 | |||
| 117 | if (not -d $self->{sidef_config_dir}) { | ||
| 118 | require File::Path; | ||
| 119 | eval { File::Path::make_path($self->{sidef_config_dir}) } | ||
| 120 | ## or warn "[WARNING] Can't create directory <<$self->{sidef_config_dir}>>: $!"; | ||
| 121 | } | ||
| 122 | |||
| 123 | return $self->{sidef_config_dir}; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub get_sidef_vdir { | ||
| 127 | my ($self) = @_; | ||
| 128 | $self->{_sidef_vdir} //= File::Spec->catdir($self->get_sidef_config_dir, "v$VERSION"); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub has_dbm_driver { | ||
| 132 | my ($self) = @_; | ||
| 133 | |||
| 134 | if (exists $self->{dbm_driver}) { | ||
| 135 | return $self->{dbm_driver}; | ||
| 136 | } | ||
| 137 | |||
| 138 | if (eval { require DB_File; 1 }) { | ||
| 139 | return ($self->{dbm_driver} = 'bdbm'); | ||
| 140 | } | ||
| 141 | |||
| 142 | if (eval { require GDBM_File; 1 }) { | ||
| 143 | return ($self->{dbm_driver} = 'gdbm'); | ||
| 144 | } | ||
| 145 | |||
| 146 | warn "No supported database driver was found...\n"; | ||
| 147 | warn "Please install DB_File or GDBM_File in order to use this functionality.\n"; | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _init_db { | ||
| 152 | my ($self, $hash, $db_file) = @_; | ||
| 153 | dbmopen(%$hash, $db_file, 0640); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _init_time_db { | ||
| 157 | my ($self, $lang) = @_; | ||
| 158 | |||
| 159 | if (not exists $self->{$lang}{_time_hash}) { | ||
| 160 | $self->{$lang}{_time_hash} = {}; | ||
| 161 | $self->_init_db($self->{$lang}{_time_hash}, $self->{$lang}{time_db}); | ||
| 162 | |||
| 163 | if (not exists $self->{$lang}{_time_hash}{sanitized}) { | ||
| 164 | $self->{$lang}{_time_hash}{sanitized} = time; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _init_code_db { | ||
| 170 | my ($self, $lang) = @_; | ||
| 171 | |||
| 172 | if (not exists $self->{$lang}{_code_hash}) { | ||
| 173 | $self->{$lang}{_code_hash} = {}; | ||
| 174 | $self->_init_db($self->{$lang}{_code_hash}, $self->{$lang}{code_db}); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | sub dbm_lookup { | ||
| 179 | my ($self, $lang, $md5) = @_; | ||
| 180 | |||
| 181 | $self->_init_time_db($lang) | ||
| 182 | if not exists($self->{$lang}{_time_hash}); | ||
| 183 | |||
| 184 | if (exists($self->{$lang}{_time_hash}{$md5})) { | ||
| 185 | |||
| 186 | $self->_init_code_db($lang) | ||
| 187 | if not exists($self->{$lang}{_code_hash}); | ||
| 188 | |||
| 189 | if (time - $self->{$lang}{_time_hash}{$md5} >= UPDATE_SEC) { | ||
| 190 | $self->{$lang}{_time_hash}{$md5} = time; | ||
| 191 | } | ||
| 192 | |||
| 193 | my $compressed_code = $self->{$lang}{_code_hash}{$md5}; | ||
| 194 | |||
| 195 | state $_x = require IO::Uncompress::RawInflate; | ||
| 196 | IO::Uncompress::RawInflate::rawinflate(\$compressed_code => \my $decompressed_code) | ||
| 197 | or die "rawinflate failed: $IO::Uncompress::RawInflate::RawInflateError"; | ||
| 198 | |||
| 199 | return Encode::decode_utf8($decompressed_code); | ||
| 200 | } | ||
| 201 | |||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub dbm_store { | ||
| 206 | my ($self, $lang, $md5, $code) = @_; | ||
| 207 | |||
| 208 | $self->_init_code_db($lang) | ||
| 209 | if not exists($self->{$lang}{_code_hash}); | ||
| 210 | |||
| 211 | # Sanitize the database, by removing old entries | ||
| 212 | if (time - $self->{$lang}{_time_hash}{sanitized} >= SANITIZE_SEC) { | ||
| 213 | |||
| 214 | $self->{$lang}{_time_hash}{sanitized} = time; | ||
| 215 | |||
| 216 | my @delete_keys; | ||
| 217 | while (my ($key, $value) = each %{$self->{$lang}{_time_hash}}) { | ||
| 218 | if (time - $value >= DELETE_SEC) { | ||
| 219 | push @delete_keys, $key; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | if (@delete_keys) { | ||
| 224 | delete @{$self->{$lang}{_time_hash}}{@delete_keys}; | ||
| 225 | delete @{$self->{$lang}{_code_hash}}{@delete_keys}; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | state $_x = require IO::Compress::RawDeflate; | ||
| 230 | IO::Compress::RawDeflate::rawdeflate(\$code => \my $compressed_code) | ||
| 231 | or die "rawdeflate failed: $IO::Compress::RawDeflate::RawDeflateError"; | ||
| 232 | |||
| 233 | $self->{$lang}{_time_hash}{$md5} = time; | ||
| 234 | $self->{$lang}{_code_hash}{$md5} = $compressed_code; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub compile_code { | ||
| 238 | my ($self, $code, $lang) = @_; | ||
| 239 | |||
| 240 | $lang //= 'Sidef'; | ||
| 241 | |||
| 242 | if ( | ||
| 243 | $self->{opt}{s} | ||
| 244 | ##and length($$code) > 1024 | ||
| 245 | and $self->has_dbm_driver | ||
| 246 | ) { | ||
| 247 | |||
| 248 | my $db_dir = ($self->{$lang}{db_dir} //= File::Spec->catdir($self->get_sidef_vdir(), $lang)); | ||
| 249 | |||
| 250 | if (not -e $db_dir) { | ||
| 251 | require File::Path; | ||
| 252 | File::Path::make_path($db_dir); | ||
| 253 | } | ||
| 254 | |||
| 255 | state $_x = do { | ||
| 256 | require Encode; | ||
| 257 | require Digest::MD5; | ||
| 258 | }; | ||
| 259 | |||
| 260 | my $md5 = Digest::MD5::md5_hex(Encode::encode_utf8($code)); | ||
| 261 | |||
| 262 | $self->{$lang}{time_db} //= File::Spec->catfile($db_dir, 'Sidef_Time_' . $self->{dbm_driver} . '.db'); | ||
| 263 | $self->{$lang}{code_db} //= File::Spec->catfile($db_dir, 'Sidef_Code_' . $self->{dbm_driver} . '.db'); | ||
| 264 | |||
| 265 | if (defined(my $cached_code = $self->dbm_lookup($lang, $md5))) { | ||
| 266 | return $cached_code; | ||
| 267 | } | ||
| 268 | |||
| 269 | my $evals_num = keys(%EVALS); | ||
| 270 | |||
| 271 | local $self->{environment_name} = 'Sidef::Runtime' . $md5; | ||
| 272 | my $deparsed = $self->compile_ast($self->parse_code($code), $lang); | ||
| 273 | |||
| 274 | if ($lang eq 'Perl') { | ||
| 275 | $deparsed = "package $self->{environment_name} {$deparsed}\n"; | ||
| 276 | } | ||
| 277 | |||
| 278 | # Don't store code that contains eval() | ||
| 279 | if (keys(%EVALS) == $evals_num) { | ||
| 280 | $self->dbm_store($lang, $md5, Encode::encode_utf8($deparsed)); | ||
| 281 | } | ||
| 282 | |||
| 283 | return $deparsed; | ||
| 284 | } | ||
| 285 | |||
| 286 | state $count = 0; | ||
| 287 | local $self->{environment_name} = 'Sidef::Runtime' . (CORE::abs($count++) || ''); | ||
| 288 | |||
| 289 | my $deparsed = $self->compile_ast($self->parse_code($code), $lang); | ||
| 290 | |||
| 291 | if ($lang eq 'Perl') { | ||
| 292 | $deparsed = "package $self->{environment_name} {$deparsed}\n"; | ||
| 293 | } | ||
| 294 | |||
| 295 | return $deparsed; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub compile_ast { | ||
| 299 | my ($self, $ast, $lang) = @_; | ||
| 300 | |||
| 301 | $lang //= 'Sidef'; | ||
| 302 | |||
| 303 | my $module = "Sidef::Deparse::$lang"; | ||
| 304 | my $pm = ($module =~ s{::}{/}gr . '.pm'); | ||
| 305 | |||
| 306 | require $pm; | ||
| 307 | $self->{$lang}{deparser} = $module->new(opt => $self->{opt}, | ||
| 308 | environment_name => $self->{environment_name} // '',); | ||
| 309 | |||
| 310 | scalar $self->{$lang}{deparser}->deparse($ast); | ||
| 311 | } | ||
| 312 | |||
| 313 | # | ||
| 314 | ## Util functions | ||
| 315 | # | ||
| 316 | |||
| 317 | sub normalize_type { | ||
| 318 | my ($type) = @_; | ||
| 319 | |||
| 320 | $type // return ''; | ||
| 321 | |||
| 322 | if (index($type, 'Sidef::') == 0) { | ||
| 323 | |||
| 324 | if ($type =~ /::[0-9]+::/) { | ||
| 325 | $type = substr($type, $+[0]); | ||
| 326 | } | ||
| 327 | else { | ||
| 328 | $type = substr($type, rindex($type, '::') + 2); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | $type =~ s/^main:://r; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub normalize_method { | ||
| 336 | my ($type, $method) = ($_[0] =~ /^(.*[^:])::(.*)$/); | ||
| 337 | normalize_type($type) . '#' . ($method // ''); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub jaro { | ||
| 341 | my ($s, $t) = @_; | ||
| 342 | |||
| 343 | my $s_len = length($s); | ||
| 344 | my $t_len = length($t); | ||
| 345 | |||
| 346 | my $match_distance = int(List::Util::max($s_len, $t_len) / 2) - 1; | ||
| 347 | |||
| 348 | my @s_matches; | ||
| 349 | my @t_matches; | ||
| 350 | |||
| 351 | my @s = split(//, $s); | ||
| 352 | my @t = split(//, $t); | ||
| 353 | |||
| 354 | my $matches = 0; | ||
| 355 | foreach my $i (0 .. $s_len - 1) { | ||
| 356 | |||
| 357 | my $start = List::Util::max(0, $i - $match_distance); | ||
| 358 | my $end = List::Util::min($i + $match_distance + 1, $t_len); | ||
| 359 | |||
| 360 | foreach my $j ($start .. $end - 1) { | ||
| 361 | $t_matches[$j] and next; | ||
| 362 | $s[$i] eq $t[$j] or next; | ||
| 363 | $s_matches[$i] = 1; | ||
| 364 | $t_matches[$j] = 1; | ||
| 365 | $matches++; | ||
| 366 | last; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | return 0 if $matches == 0; | ||
| 371 | |||
| 372 | my $k = 0; | ||
| 373 | my $trans = 0; | ||
| 374 | |||
| 375 | foreach my $i (0 .. $s_len - 1) { | ||
| 376 | $s_matches[$i] or next; | ||
| 377 | until ($t_matches[$k]) { ++$k } | ||
| 378 | $s[$i] eq $t[$k] or ++$trans; | ||
| 379 | ++$k; | ||
| 380 | } | ||
| 381 | |||
| 382 | #<<< | ||
| 383 | (($matches / $s_len) + ($matches / $t_len) | ||
| 384 | + (($matches - $trans / 2) / $matches)) / 3; | ||
| 385 | #>>> | ||
| 386 | } | ||
| 387 | |||
| 388 | sub jaro_winkler { | ||
| 389 | my ($s, $t) = @_; | ||
| 390 | |||
| 391 | my $distance = jaro($s, $t); | ||
| 392 | |||
| 393 | my $prefix = 0; | ||
| 394 | foreach my $i (0 .. List::Util::min(3, length($s), length($t))) { | ||
| 395 | substr($s, $i, 1) eq substr($t, $i, 1) ? ++$prefix : last; | ||
| 396 | } | ||
| 397 | |||
| 398 | $distance + $prefix * 0.1 * (1 - $distance); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub best_matches { | ||
| 402 | my ($name, $set) = @_; | ||
| 403 | |||
| 404 | my $max = 0; | ||
| 405 | my @best; | ||
| 406 | foreach my $elem (@$set) { | ||
| 407 | my $dist = sprintf("%.4f", jaro_winkler($elem, $name)); | ||
| 408 | $dist >= 0.8 or next; | ||
| 409 | if ($dist > $max) { | ||
| 410 | $max = $dist; | ||
| 411 | @best = (); | ||
| 412 | } | ||
| 413 | push(@best, $elem) if $dist == $max; | ||
| 414 | } | ||
| 415 | |||
| 416 | @best; | ||
| 417 | } | ||
| 418 | |||
| 419 | }; | ||
| 420 | |||
| 421 | use utf8; | ||
| 422 | use 5.016; | ||
| 423 | |||
| 424 | our $AUTOLOAD; | ||
| 425 | |||
| 426 | # | ||
| 427 | ## UNIVERSAL methods | ||
| 428 | # | ||
| 429 | |||
| 430 | *UNIVERSAL::get_value = sub { | ||
| 431 | ref($_[0]) eq 'Sidef::Module::OO' || ref($_[0]) eq 'Sidef::Module::Func' | ||
| 432 | ? $_[0]->{module} | ||
| 433 | : $_[0]; | ||
| 434 | }; | ||
| 435 | |||
| 436 | *UNIVERSAL::DESTROY = sub { }; | ||
| 437 | |||
| 438 | *UNIVERSAL::AUTOLOAD = sub { | ||
| 439 | my ($self, @args) = @_; | ||
| 440 | |||
| 441 | $self = ref($self) if ref($self); | ||
| 442 | |||
| 443 | if (index($self, 'Sidef::') == 0 and index($self, 'Sidef::Runtime') != 0) { | ||
| 444 | |||
| 445 | eval { require $self =~ s{::}{/}rg . '.pm' }; | ||
| 446 | |||
| 447 | if ($@) { | ||
| 448 | if (defined(&main::__load_sidef_module__)) { | ||
| 449 | main::__load_sidef_module__($self); | ||
| 450 | } | ||
| 451 | else { | ||
| 452 | die "[AUTOLOAD] $@"; | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | if (defined(&$AUTOLOAD)) { | ||
| 457 | goto &$AUTOLOAD; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | |||
| 461 | my @caller = caller(1); | ||
| 462 | my $from = Sidef::normalize_method($caller[3]); | ||
| 463 | $from = $from eq '#' ? 'main()' : "$from()"; | ||
| 464 | |||
| 465 | my $table = do { no strict 'refs'; \%{$self . '::'} }; | ||
| 466 | my @methods = grep { !ref($table->{$_}) and defined(&{$table->{$_}}) } keys(%$table); | ||
| 467 | |||
| 468 | my $method = Sidef::normalize_method($AUTOLOAD); | ||
| 469 | my $name = substr($method, rindex($method, '#') + 1); | ||
| 470 | |||
| 471 | my @candidates = Sidef::best_matches($name, \@methods); | ||
| 472 | |||
| 473 | die( "[AUTOLOAD] Undefined method `" | ||
| 474 | . $method . q{'} | ||
| 475 | . " called from $from\n" | ||
| 476 | . (@candidates ? ("[?] Did you mean: " . join("\n" . (' ' x 18), sort(@candidates)) . "\n") : '')); | ||
| 477 | return; | ||
| 478 | }; | ||
| 479 | |||
| 480 | 1; | ||