| 1 | 34 | ▶ | use strict; use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | package Lingy::Lang::RT; | ||
| 3 | |||
| 4 | 34 | ▶ | use Lingy; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 34 | ▶ | use Lingy::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 34 | ▶ | use Lingy::Eval; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 34 | ▶ | use Lingy::Lang::HashMap; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 34 | ▶ | use Lingy::Lang::Namespace; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 34 | ▶ | use Lingy::Lang::Nil; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 34 | ▶ | use Lingy::Lang::Sequential; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 34 | ▶ | use Lingy::Lang::Symbol; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 34 | ▶ | use Lingy::ReadLine; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use constant LANG => 'Lingy'; | ||
| 15 | use constant HOST => 'perl'; | ||
| 16 | |||
| 17 | use constant env_class => 'Lingy::Env'; | ||
| 18 | use constant printer_class => 'Lingy::Printer'; | ||
| 19 | use constant reader_class => 'Lingy::Reader'; | ||
| 20 | |||
| 21 | my @class_names = ( | ||
| 22 | ATOM, | ||
| 23 | BOOLEAN, | ||
| 24 | CHARACTER, | ||
| 25 | CLASS, | ||
| 26 | COMPILER, | ||
| 27 | FUNCTION, | ||
| 28 | HASHMAP, | ||
| 29 | KEYWORD, | ||
| 30 | LIST, | ||
| 31 | LISTTYPE, | ||
| 32 | MACRO, | ||
| 33 | NIL, | ||
| 34 | NUMBER, | ||
| 35 | REGEX, | ||
| 36 | SEQUENTIAL, | ||
| 37 | STRING, | ||
| 38 | SYMBOL, | ||
| 39 | VAR, | ||
| 40 | VECTOR, | ||
| 41 | NUMBERS, | ||
| 42 | RT, | ||
| 43 | TERM, | ||
| 44 | THREAD, | ||
| 45 | UTIL, | ||
| 46 | ); | ||
| 47 | sub class_names { \@class_names } | ||
| 48 | |||
| 49 | my $current_ns_name; | ||
| 50 | sub current_ns_name { $current_ns_name = $_[1] // $current_ns_name } | ||
| 51 | |||
| 52 | my %namespaces; | ||
| 53 | bless \%namespaces, 'lingy-internal'; | ||
| 54 | sub namespaces { \%namespaces } | ||
| 55 | sub current_ns { $namespaces{$current_ns_name} } | ||
| 56 | |||
| 57 | my %ns_refers; | ||
| 58 | bless \%ns_refers, 'lingy-internal'; | ||
| 59 | sub ns_refers { \%ns_refers } | ||
| 60 | |||
| 61 | my %classes; | ||
| 62 | |||
| 63 | my %meta; | ||
| 64 | sub meta { \%meta } | ||
| 65 | |||
| 66 | my $env; | ||
| 67 | sub env { $env } | ||
| 68 | |||
| 69 | my $reader; | ||
| 70 | sub reader { $reader } | ||
| 71 | |||
| 72 | my $printer; | ||
| 73 | sub printer { $printer } | ||
| 74 | |||
| 75 | my $core_ns; | ||
| 76 | sub core_ns { $core_ns } | ||
| 77 | |||
| 78 | my $user_ns; | ||
| 79 | sub user_ns { $user_ns } | ||
| 80 | |||
| 81 | our $ready = 0; | ||
| 82 | sub ready { $ready } | ||
| 83 | |||
| 84 | sub init { | ||
| 85 | my ($self) = @_; | ||
| 86 | |||
| 87 | for my $package_name (@{RT->class_names}) { | ||
| 88 | eval "require $package_name; 1" or die $@; | ||
| 89 | my $class = CLASS->_new($package_name); | ||
| 90 | my $class_name = $class->_name; | ||
| 91 | $classes{$class_name} = $class; | ||
| 92 | if ($class_name =~ /\.(\w+)$/) { | ||
| 93 | $classes{$1} = $class; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | $env = $self->require_new($self->env_class); | ||
| 98 | $reader = $self->require_new($self->reader_class); | ||
| 99 | $printer = $self->require_new($self->printer_class); | ||
| 100 | |||
| 101 | $core_ns = $self->core_namespace(); | ||
| 102 | $user_ns = $self->user_namespace(); | ||
| 103 | $user_ns->current; | ||
| 104 | |||
| 105 | $ready = 1; | ||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub core_namespace { | ||
| 111 | my ($self) = @_; | ||
| 112 | |||
| 113 | my $ns = NAMESPACE->new( | ||
| 114 | name => 'lingy.core', | ||
| 115 | %classes, | ||
| 116 | )->current; | ||
| 117 | |||
| 118 | $ns->{$_} = $classes{$_} for keys %classes; | ||
| 119 | |||
| 120 | my $argv = @ARGV | ||
| 121 | ? LIST->new([ | ||
| 122 | map STRING->new($_), @ARGV[1..$#ARGV]] | ||
| 123 | ) : NIL->new; | ||
| 124 | |||
| 125 | # Define these functions first for bootstrapping: | ||
| 126 | $env->set(cons => \&cons); | ||
| 127 | $env->set(concat => \&concat); | ||
| 128 | $env->set(eval => sub { evaluate($_[0], $env) }); | ||
| 129 | |||
| 130 | # Clojure dynamic vars: | ||
| 131 | $env->set('*file*', STRING->new( | ||
| 132 | $ARGV[0] || "NO_SOURCE_PATH" | ||
| 133 | )); | ||
| 134 | $env->set('*command-line-args*', $argv); | ||
| 135 | |||
| 136 | # Lingy dynamic vars: | ||
| 137 | $env->set('*ARGV*', $argv); | ||
| 138 | $env->set('*LANG*', STRING->new($self->LANG)); | ||
| 139 | $env->set('*HOST*', STRING->new($self->HOST)); | ||
| 140 | |||
| 141 | $Lingy::VERSION =~ /^(\d+)\.(\d+)\.(\d+)$/; | ||
| 142 | $self->rep(" | ||
| 143 | (def *lingy-version* | ||
| 144 | { | ||
| 145 | :major $1 | ||
| 146 | :minor $2 | ||
| 147 | :incremental $3 | ||
| 148 | :qualifier nil | ||
| 149 | }) | ||
| 150 | |||
| 151 | (def *clojure-version* | ||
| 152 | { | ||
| 153 | :major 1 | ||
| 154 | :minor 11 | ||
| 155 | :incremental 1 | ||
| 156 | :qualifier nil | ||
| 157 | }) | ||
| 158 | "); | ||
| 159 | |||
| 160 | my $core_ly = $INC{'Lingy/Lang/RT.pm'}; | ||
| 161 | $core_ly =~ s/Lang\/RT\.pm$/core.ly/; | ||
| 162 | $self->rep($self->slurp_file($core_ly)); | ||
| 163 | |||
| 164 | return $ns; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub user_namespace { | ||
| 168 | my ($self) = @_; | ||
| 169 | |||
| 170 | NAMESPACE->new( | ||
| 171 | name => 'user', | ||
| 172 | refer => [ | ||
| 173 | $self->core_ns, | ||
| 174 | ], | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub require_new { | ||
| 179 | my $self = shift; | ||
| 180 | my $class = shift; | ||
| 181 | eval "require $class; 1" or | ||
| 182 | die "Can't require '$class':\n$@"; | ||
| 183 | $class->new(@_); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub slurp_file { | ||
| 187 | my ($self, $file) = @_; | ||
| 188 | open my $slurp, '<', "$file" or | ||
| 189 | die "Couldn't read file '$file'"; | ||
| 190 | local $/; | ||
| 191 | <$slurp>; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub is_lingy_class { | ||
| 195 | my ($self, $class) = @_; | ||
| 196 | $class->isa(CLASS) or $class =~ /^Lingy::Lang::\w/; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub rep { | ||
| 200 | my ($self, $str) = @_; | ||
| 201 | map $printer->pr_str(evaluate($_, $env)), | ||
| 202 | $reader->read_str($str); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub repl { | ||
| 206 | my ($self) = @_; | ||
| 207 | |||
| 208 | $self->rep(q< (println (str *LANG* " " (lingy-version) " [" *HOST* "]\n"))>) | ||
| 209 | unless $ENV{LINGY_TEST}; | ||
| 210 | my ($clojure_repl) = $self->rep("(identity *clojure-repl*)"); | ||
| 211 | if ($clojure_repl eq 'true') { | ||
| 212 | require Lingy::ClojureREPL; | ||
| 213 | Lingy::ClojureREPL->start(); | ||
| 214 | } | ||
| 215 | |||
| 216 | while (defined (my $line = Lingy::ReadLine::readline)) { | ||
| 217 | next unless length $line; | ||
| 218 | |||
| 219 | my @forms = eval { $reader->read_str($line, 1) }; | ||
| 220 | if ($@) { | ||
| 221 | print "$@\n"; | ||
| 222 | $Lingy::ReadLine::input = ''; | ||
| 223 | next; | ||
| 224 | } | ||
| 225 | |||
| 226 | for my $form (@forms) { | ||
| 227 | my $ret = eval { | ||
| 228 | $printer->pr_str(evaluate($form, $env)); | ||
| 229 | }; | ||
| 230 | my $err; | ||
| 231 | $err = $ret = $@ if $@; | ||
| 232 | chomp $ret; | ||
| 233 | print "$ret\n"; | ||
| 234 | } | ||
| 235 | |||
| 236 | my $input = $Lingy::ReadLine::input // next; | ||
| 237 | ($clojure_repl) = $self->rep("(identity *clojure-repl*)"); | ||
| 238 | |||
| 239 | if ($input =~ s/^;;;// or $clojure_repl eq 'true') { | ||
| 240 | require Lingy::ClojureREPL; | ||
| 241 | Lingy::ClojureREPL->rep($input); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | print "\n"; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | #------------------------------------------------------------------------------ | ||
| 249 | |||
| 250 | sub all_ns { | ||
| 251 | list([ | ||
| 252 | map { $namespaces{$_} } | ||
| 253 | sort keys %namespaces | ||
| 254 | ]); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub apply { | ||
| 258 | my ($fn, $args) = @_; | ||
| 259 | my $seq = pop(@$args); | ||
| 260 | $seq = list([]) if ref($seq) eq NIL; | ||
| 261 | push @$args, @$seq; | ||
| 262 | ref($fn) eq 'CODE' | ||
| 263 | ? $fn->(@$args) | ||
| 264 | : evaluate($fn->(@$args)); | ||
| 265 | } | ||
| 266 | |||
| 267 | sub assoc { | ||
| 268 | my ($map, $key, $val) = @_; | ||
| 269 | $map->assoc($key, $val); | ||
| 270 | } | ||
| 271 | |||
| 272 | sub atom_ { ATOM->new($_[0]) } | ||
| 273 | |||
| 274 | sub booleanCast { | ||
| 275 | my ($val) = @_; | ||
| 276 | my $type = ref($val); | ||
| 277 | $type eq NIL ? false : | ||
| 278 | ($type eq BOOLEAN and $$val == 0) ? false : | ||
| 279 | true; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub charCast { | ||
| 283 | my ($char) = @_; | ||
| 284 | my $type = ref($char); | ||
| 285 | err "Class '$type' cannot be cast to 'lingy.lang.Character'" | ||
| 286 | unless $type eq SYMBOL or $type eq NUMBER; | ||
| 287 | return CHARACTER->read($char); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub concat { list([map @$_, @_]) } | ||
| 291 | |||
| 292 | sub conj { | ||
| 293 | my ($o, @args) = @_; | ||
| 294 | my $type = ref($o); | ||
| 295 | $type eq LIST ? list([reverse(@args), @$o]) : | ||
| 296 | $type eq VECTOR ? VECTOR->new([@$o, @args]) : | ||
| 297 | $type eq NIL ? nil : | ||
| 298 | throw("conj first arg type '$type' not allowed"); | ||
| 299 | } | ||
| 300 | |||
| 301 | sub cons { list([$_[0], @{$_[1]}]) } | ||
| 302 | |||
| 303 | sub contains { | ||
| 304 | my ($map, $key) = @_; | ||
| 305 | return false unless ref($map) eq HASHMAP; | ||
| 306 | $key = | ||
| 307 | $key->isa(STRING) ? qq<"$key> : | ||
| 308 | $key->isa(SYMBOL) ? qq<$key > : | ||
| 309 | "$key"; | ||
| 310 | BOOLEAN->new(exists $map->{"$key"}); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub count { | ||
| 314 | NUMBER->new(ref($_[0]) eq NIL ? 0 : scalar @{$_[0]}); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub create_ns { | ||
| 318 | my ($name) = @_; | ||
| 319 | err "Invalid ns name '$name'" | ||
| 320 | unless $name =~ /^\w+(\.\w+)*$/; | ||
| 321 | NAMESPACE->new( | ||
| 322 | name => $name, | ||
| 323 | refer => $core_ns, | ||
| 324 | ); | ||
| 325 | } | ||
| 326 | |||
| 327 | sub dec { $_[0] - 1 } | ||
| 328 | |||
| 329 | sub deref { $_[0]->[0] } | ||
| 330 | |||
| 331 | sub dissoc { | ||
| 332 | my ($map, @keys) = @_; | ||
| 333 | @keys = map { | ||
| 334 | $_->isa(STRING) ? qq<"$_> : "$_"; | ||
| 335 | } @keys; | ||
| 336 | $map = { %$map }; | ||
| 337 | delete $map->{$_} for @keys; | ||
| 338 | HASHMAP->new([%$map]); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub find_ns { | ||
| 342 | assert_args(\@_, SYMBOL); | ||
| 343 | $namespaces{$_[0]} // nil; | ||
| 344 | } | ||
| 345 | |||
| 346 | sub first { | ||
| 347 | ref($_[0]) eq NIL | ||
| 348 | ? nil : @{$_[0]} ? $_[0]->[0] : nil; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub get { | ||
| 352 | my ($map, $key, $default) = @_; | ||
| 353 | return nil unless ref($map) eq HASHMAP; | ||
| 354 | $key = qq<"$key> if $key->isa(STRING); | ||
| 355 | $map->{"$key"} // $default // nil; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub getenv { | ||
| 359 | my $val = $ENV{$_[0]}; | ||
| 360 | defined($val) ? string($val) : nil; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub hash_map_ { HASHMAP->new([@_]) } | ||
| 364 | |||
| 365 | sub in_ns { | ||
| 366 | my ($name) = @_; | ||
| 367 | err "Invalid ns name '$name'" | ||
| 368 | unless $name =~ /^\w+(\.\w+)*$/; | ||
| 369 | my $ns = $namespaces{$name} // | ||
| 370 | NAMESPACE->new( | ||
| 371 | name => $name, | ||
| 372 | ); | ||
| 373 | $ns->current; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub inc { $_[0] + 1 } | ||
| 377 | |||
| 378 | sub keys_ { | ||
| 379 | list([ | ||
| 380 | map { | ||
| 381 | s/^"// ? string($_) : | ||
| 382 | s/^:// ? KEYWORD->new($_) : | ||
| 383 | s/ $// ? symbol($_) : | ||
| 384 | symbol("$_"); | ||
| 385 | } keys %{$_[0]} | ||
| 386 | ]); | ||
| 387 | } | ||
| 388 | |||
| 389 | sub keyword_ { KEYWORD->new($_[0]) } | ||
| 390 | |||
| 391 | sub list_ { list([@_]) } | ||
| 392 | |||
| 393 | sub macroexpand { | ||
| 394 | Lingy::Eval::macroexpand($_[0], $Lingy::Eval::ENV); | ||
| 395 | } | ||
| 396 | |||
| 397 | sub map { | ||
| 398 | list([ | ||
| 399 | map apply($_[0], [$_, []]), @{$_[1]} | ||
| 400 | ]); | ||
| 401 | } | ||
| 402 | |||
| 403 | sub meta_get { | ||
| 404 | $meta{"$_[0]"} // nil; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub name { | ||
| 408 | string($_[0] =~ m{(.*?)/(.*)} ? $2 : "$_[0]"); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub namespace { | ||
| 412 | $_[0] =~ m{(.*?)/(.*)} ? string($1) : nil; | ||
| 413 | } | ||
| 414 | |||
| 415 | my $nextID = 1000; | ||
| 416 | sub nextID { | ||
| 417 | return $nextID = $_[1] if @_ == 2; | ||
| 418 | string(++$nextID); | ||
| 419 | } | ||
| 420 | |||
| 421 | sub ns_ { | ||
| 422 | my ($name, $args) = @_; | ||
| 423 | err "Invalid ns name '$name'" | ||
| 424 | unless $name =~ /^\w+(\.\w+)*$/; | ||
| 425 | |||
| 426 | my $ns; | ||
| 427 | $ns = $namespaces{$name} // | ||
| 428 | NAMESPACE->new( | ||
| 429 | name => $name, | ||
| 430 | refer => $core_ns, | ||
| 431 | ); | ||
| 432 | $ns->current; | ||
| 433 | |||
| 434 | for my $arg (@$args) { | ||
| 435 | err "Invalid ns arg" unless | ||
| 436 | $arg->isa(LIST) and | ||
| 437 | @$arg >= 2 and | ||
| 438 | ref($arg->[0]) eq KEYWORD; | ||
| 439 | |||
| 440 | my ($keyword, @args) = @$arg; | ||
| 441 | if ($$keyword eq ':use') { | ||
| 442 | for my $spec (@args) { | ||
| 443 | evaluate( | ||
| 444 | list([ | ||
| 445 | symbol('use'), | ||
| 446 | list([symbol('quote'), $spec]), | ||
| 447 | ]), | ||
| 448 | $Lingy::Eval::ENV, | ||
| 449 | ); | ||
| 450 | } | ||
| 451 | } | ||
| 452 | elsif ($$keyword eq ':import') { | ||
| 453 | my (undef, @args) = @$arg; | ||
| 454 | evaluate( | ||
| 455 | list([ symbol('import'), @args ]), | ||
| 456 | $Lingy::Eval::ENV, | ||
| 457 | ); | ||
| 458 | } | ||
| 459 | else { | ||
| 460 | err "Invalid keyword arg '$keyword' in ns"; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | nil; | ||
| 465 | } | ||
| 466 | |||
| 467 | sub nth { $_[0][$_[1]] } | ||
| 468 | |||
| 469 | sub number_ { NUMBER->new("$_[0]" + 0) } | ||
| 470 | |||
| 471 | sub pos_Q { $_[0] > 0 ? true : false } | ||
| 472 | |||
| 473 | sub pr_str { | ||
| 474 | string(join ' ', map $printer->pr_str($_), @_); | ||
| 475 | } | ||
| 476 | |||
| 477 | sub println { | ||
| 478 | printf "%s\n", join ' ', | ||
| 479 | map $printer->pr_str($_, 1), @_; | ||
| 480 | nil; | ||
| 481 | } | ||
| 482 | |||
| 483 | sub prn { | ||
| 484 | printf "%s\n", join ' ', | ||
| 485 | map $printer->pr_str($_), @_; | ||
| 486 | nil; | ||
| 487 | } | ||
| 488 | |||
| 489 | sub quot { NUMBER->new(int($_[0] / $_[1])) } | ||
| 490 | |||
| 491 | sub read_string { | ||
| 492 | my @forms = Lingy::Reader->new->read_str($_[0]); | ||
| 493 | return @forms ? $forms[0] : nil; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub readline { | ||
| 497 | require Lingy::ReadLine; | ||
| 498 | my $l = Lingy::ReadLine::readline() // return; | ||
| 499 | chomp $l; | ||
| 500 | string($l); | ||
| 501 | } | ||
| 502 | |||
| 503 | sub refer_ { | ||
| 504 | my (@specs) = @_; | ||
| 505 | for my $spec (@specs) { | ||
| 506 | err "'refer' only works with symbols" | ||
| 507 | unless ref($spec) eq SYMBOL; | ||
| 508 | my $refer_ns_name = $$spec; | ||
| 509 | my $current_ns_name = $current_ns_name; | ||
| 510 | my $refer_ns = $namespaces{$refer_ns_name} | ||
| 511 | or err "No namespace: '$refer_ns_name'"; | ||
| 512 | my $refer_map = $ns_refers{$current_ns_name} //= {}; | ||
| 513 | map $refer_map->{$_} = $refer_ns_name, | ||
| 514 | grep /^\S/, keys %$refer_ns; | ||
| 515 | } | ||
| 516 | return nil; | ||
| 517 | } | ||
| 518 | |||
| 519 | sub require { | ||
| 520 | outer: | ||
| 521 | for my $spec (@_) { | ||
| 522 | err "'require' only works with symbols" | ||
| 523 | unless ref($spec) eq SYMBOL; | ||
| 524 | |||
| 525 | return nil if $namespaces{$$spec}; | ||
| 526 | |||
| 527 | my $name = $$spec; | ||
| 528 | |||
| 529 | my $path = $name; | ||
| 530 | $path =~ s/^lingy\.lang\./Lingy.Lang\./; | ||
| 531 | $path =~ s/^lingy\./Lingy\./; | ||
| 532 | my $module = $path; | ||
| 533 | $path =~ s/\./\//g; | ||
| 534 | |||
| 535 | for my $inc (@INC) { | ||
| 536 | $inc =~ s{^([^/.])}{./$1}; | ||
| 537 | my $inc_path = "$inc/$path"; | ||
| 538 | if (-f "$inc_path.ly") { | ||
| 539 | my $ns = $namespaces{$current_ns_name}; | ||
| 540 | RT->rep(qq< (load-file "$inc_path.ly") >); | ||
| 541 | $ns->current; | ||
| 542 | next outer; | ||
| 543 | } | ||
| 544 | } | ||
| 545 | |||
| 546 | err "Can't find library for (require '$name)"; | ||
| 547 | } | ||
| 548 | return nil; | ||
| 549 | } | ||
| 550 | |||
| 551 | sub reset_BANG { $_[0]->[0] = $_[1] } | ||
| 552 | |||
| 553 | sub resolve { | ||
| 554 | my ($symbol) = @_; | ||
| 555 | my ($ns_name, $sym_name, $var); | ||
| 556 | if ($symbol =~ /(.*?)\/(.*)/) { | ||
| 557 | ($ns_name, $sym_name) = ($1, $2); | ||
| 558 | } | ||
| 559 | else { | ||
| 560 | $ns_name = $current_ns_name; | ||
| 561 | $sym_name = $symbol; | ||
| 562 | } | ||
| 563 | |||
| 564 | my $ns = $namespaces{$ns_name} or return nil; | ||
| 565 | if (exists $ns->{$sym_name}) { | ||
| 566 | $var = $ns_name . '/' . $sym_name; | ||
| 567 | } else { | ||
| 568 | my $ref; | ||
| 569 | if (($ref = $ns_refers{$ns_name}) and | ||
| 570 | defined($ns_name = $ref->{$sym_name}) | ||
| 571 | ) { | ||
| 572 | $var = $ns_name . '/' . $sym_name; | ||
| 573 | } else { | ||
| 574 | return nil; | ||
| 575 | } | ||
| 576 | } | ||
| 577 | return VAR->new($var); | ||
| 578 | } | ||
| 579 | |||
| 580 | sub rest { | ||
| 581 | my ($list) = @_; | ||
| 582 | return list([]) if $list->isa(NIL) or not @$list; | ||
| 583 | list([@{$list}[1..(@$list-1)]]); | ||
| 584 | } | ||
| 585 | |||
| 586 | sub seq { | ||
| 587 | my ($o) = @_; | ||
| 588 | $o->can('_to_seq') or | ||
| 589 | err(sprintf "Don't know how to create ISeq from: %s", $o->NAME); | ||
| 590 | $o->_to_seq; | ||
| 591 | } | ||
| 592 | |||
| 593 | sub slurp { string(RT->slurp_file($_[0])) } | ||
| 594 | |||
| 595 | sub sort { | ||
| 596 | list([ | ||
| 597 | CORE::sort @{$_[0]} | ||
| 598 | ]); | ||
| 599 | } | ||
| 600 | |||
| 601 | sub str { | ||
| 602 | string( | ||
| 603 | join '', | ||
| 604 | map $printer->pr_str($_, 1), | ||
| 605 | grep {ref($_) ne NIL} | ||
| 606 | @_ | ||
| 607 | ); | ||
| 608 | } | ||
| 609 | |||
| 610 | sub swap_BANG { | ||
| 611 | my ($atom, $fn, $args) = @_; | ||
| 612 | $atom->[0] = apply($fn, [[$atom->[0], @$args]]); | ||
| 613 | } | ||
| 614 | |||
| 615 | sub symbol_ { symbol("$_[0]") } | ||
| 616 | |||
| 617 | sub the_ns { | ||
| 618 | $_[0]->isa(NAMESPACE) ? $_[0] : | ||
| 619 | $_[0]->isa(SYMBOL) ? do { | ||
| 620 | $namespaces{$_[0]} // | ||
| 621 | err "No namespace: '$_[0]' found"; | ||
| 622 | } : err "Invalid argument for the-ns: '$_[0]'"; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub time_ms { | ||
| 626 | require Time::HiRes; | ||
| 627 | my ($s, $m) = Time::HiRes::gettimeofday(); | ||
| 628 | NUMBER->new($s * 1000 + $m / 1000); | ||
| 629 | } | ||
| 630 | |||
| 631 | sub type_ { | ||
| 632 | CLASS->_new(ref($_[0])); | ||
| 633 | } | ||
| 634 | |||
| 635 | sub with_meta { | ||
| 636 | my ($o, $m) = @_; | ||
| 637 | $o = ref($o) eq 'CODE' ? sub { goto &$o } : $o->clone; | ||
| 638 | $meta{$o} = $m; | ||
| 639 | $o; | ||
| 640 | } | ||
| 641 | |||
| 642 | sub vals { list([ values %{$_[0]} ]) } | ||
| 643 | |||
| 644 | sub var_ { VAR->new($_[0]) } | ||
| 645 | |||
| 646 | sub vec { VECTOR->new([@{$_[0]}]) } | ||
| 647 | |||
| 648 | sub vector_ { VECTOR->new([@_]) } | ||
| 649 | |||
| 650 | 1; | ||