| 1 | package Language::LispPerl::Role::BuiltIns::Coro; | ||||||||||
| 2 | 1 | $Language::LispPerl::Role::BuiltIns::Coro::VERSION = '0.007'; | |||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Coro; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Language::LispPerl::Atom; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Language::LispPerl::Seq; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Language::LispPerl::Role::BuiltIns::Coro - A role with coro primitives for the BuiltIns objects. | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | my $lisp = Language::LispPerl::Evaler->new(); | ||||||||||
| 17 | |||||||||||
| 18 | $lisp->builtins()->apply_role('Language::LispPerl::Role::BuiltIns::Coro'); | ||||||||||
| 19 | |||||||||||
| 20 | .. lisp now implements the coro functions. | ||||||||||
| 21 | |||||||||||
| 22 | =head2 FUNCTIONS | ||||||||||
| 23 | |||||||||||
| 24 | To be documented. Look at the source code for now.. | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 1 | my $_CORO_FUNCTIONS = { | |||||||||
| 29 | |||||||||||
| 30 | # Coro stuff | ||||||||||
| 31 | "coro" => \&_impl_coro, | ||||||||||
| 32 | "coro-suspend" => \&_impl_coro_suspend, | ||||||||||
| 33 | "coro-sleep" => \&_impl_coro_sleep, | ||||||||||
| 34 | "coro-yield" => \&_impl_coro_yield, | ||||||||||
| 35 | "coro-resume" => \&_impl_coro_resume, | ||||||||||
| 36 | "coro-wake" => \&_impl_coro_wake, | ||||||||||
| 37 | "coro-join" => \&_impl_coro_join, | ||||||||||
| 38 | "coro-current" => \&_impl_coro_current, | ||||||||||
| 39 | "coro-main" => \&_impl_coro_main, | ||||||||||
| 40 | }; | ||||||||||
| 41 | |||||||||||
| 42 | 1300 | ▶ | around 'has_function' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 43 | 1300 | my ( $orig, $self, $fname, @rest ) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 1300 | ▶ | if ( my $f = $_CORO_FUNCTIONS->{$fname} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 14 | return $f; | |||||||||
| 47 | } | ||||||||||
| 48 | 1286 | return $self->$orig( $fname, @rest ); | |||||||||
| 49 | 1 | }; | |||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | sub _impl_coro { | ||||||||
|
Subroutine
_impl_coro: called CC=3 SCAR=11.1
| |||||||||||
| 52 | 1 | my ( $self, $ast, $symbol ) = @_; | |||||||||
| 53 | 1 | ▶ | $ast->error("coro expects 1 argument") if $ast->size() != 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 1 | my $b = $self->evaler()->_eval( $ast->second() ); | |||||||||
| 55 | 1 | ▶ | $ast->error( "core expects a function as argument but got " . $b->type() ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | if $b->type() ne "function"; | ||||||||||
| 57 | 1 | ▶ | my $coro = new Coro sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | my $evaler = $self->evaler()->new_instance(); | |||||||||
| 59 | 1 | my $fc = Language::LispPerl::Seq->new({ type => "list" }); | |||||||||
| 60 | 1 | $fc->append($b); | |||||||||
| 61 | 1 | $evaler->_eval($fc); | |||||||||
| 62 | 1 | }; | |||||||||
| 63 | 1 | $coro->ready(); | |||||||||
| 64 | 1 | return Language::LispPerl::Atom->new({type => "coroutine", value => $coro }); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub _impl_coro_suspend { | ||||||||
|
Subroutine
_impl_coro_suspend: not called CC=3 SCAR=24.8
| |||||||||||
| 68 | 0 | my ( $self, $ast, $symbol ) = @_; | |||||||||
| 69 | 0 | ▶ | $ast->error("coro-suspend expects 1 argument") if $ast->size() != 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | my $coro = $self->evaler()->_eval( $ast->second() ); | |||||||||
| 71 | 0 | ▶ | $ast->error( "coro-suspend expects a coroutine as argument but got " | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | . $coro->type() ) | ||||||||||
| 73 | if $coro->type() ne "coroutine"; | ||||||||||
| 74 | 0 | $coro->value()->suspend(); | |||||||||
| 75 | 0 | return $coro; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 1 | ▶ | sub _impl_coro_sleep { | ||||||||
|
Subroutine
_impl_coro_sleep: called CC=2 SCAR=7.0
| |||||||||||
| 79 | 1 | my ( $self, $ast ) = @_; | |||||||||
| 80 | 1 | ▶ | $ast->error("coro-sleep expects 0 argument") if $ast->size != 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 1 | $Coro::current->suspend(); | |||||||||
| 82 | 1 | cede(); | |||||||||
| 83 | 1 | return Language::LispPerl::Atom->new({ type => "coroutine", value => $Coro::current }); | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | sub _impl_coro_yield { | ||||||||
|
Subroutine
_impl_coro_yield: not called CC=2 SCAR=17.9
| |||||||||||
| 87 | 0 | my ( $self, $ast ) = @_; | |||||||||
| 88 | 0 | ▶ | $ast->error("coro-yield expects 0 argument") if $ast->size() != 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | cede; | |||||||||
| 90 | 0 | return Language::LispPerl::Atom->new({ type => "coroutine", value => $Coro::current }); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | ▶ | sub _impl_coro_resume { | ||||||||
|
Subroutine
_impl_coro_resume: called CC=3 SCAR=11.2
| |||||||||||
| 94 | 2 | my ( $self, $ast ) = @_; | |||||||||
| 95 | 2 | ▶ | $ast->error("coro-resume expects 1 argument") if $ast->size() != 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 2 | my $coro = $self->evaler()->_eval( $ast->second() ); | |||||||||
| 97 | 2 | ▶ | $ast->error( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | "coro-resume expects a coroutine as argument but got " . $coro->type() ) | ||||||||||
| 99 | if $coro->type() ne "coroutine"; | ||||||||||
| 100 | 2 | $coro->value()->resume(); | |||||||||
| 101 | 2 | $coro->value()->cede_to(); | |||||||||
| 102 | 2 | return $coro; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | sub _impl_coro_wake { | ||||||||
|
Subroutine
_impl_coro_wake: not called CC=3 SCAR=24.8
| |||||||||||
| 106 | 0 | my ( $self, $ast ) = @_; | |||||||||
| 107 | 0 | ▶ | $ast->error("coro-wake expects 1 argument") if $ast->size() != 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | my $coro = $self->evaler()->_eval( $ast->second() ); | |||||||||
| 109 | 0 | ▶ | $ast->error( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | "coro-wake expects a coroutine as argument but got " . $coro->type() ) | ||||||||||
| 111 | if $coro->type() ne "coroutine"; | ||||||||||
| 112 | 0 | $coro->value()->resume(); | |||||||||
| 113 | 0 | return $coro; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub _impl_coro_join { | ||||||||
|
Subroutine
_impl_coro_join: not called CC=3 SCAR=24.8
| |||||||||||
| 117 | 0 | my ( $self, $ast ) = @_; | |||||||||
| 118 | 0 | ▶ | $ast->error("join-coro expects 1 argument") if $ast->size() != 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | my $coro = $self->evaler()->_eval( $ast->second() ); | |||||||||
| 120 | 0 | ▶ | $ast->error( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | "join-coro expects a coroutine as argument but got " . $coro->type() ) | ||||||||||
| 122 | if $coro->type() ne "coroutine"; | ||||||||||
| 123 | 0 | $coro->value()->join(); | |||||||||
| 124 | 0 | return $coro; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 2 | ▶ | sub _impl_coro_current { | ||||||||
|
Subroutine
_impl_coro_current: called CC=2 SCAR=7.1
| |||||||||||
| 128 | 2 | my ( $self, $ast ) = @_; | |||||||||
| 129 | 2 | ▶ | $ast->error("coro-current expects 0 argument") if $ast->size() != 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 2 | return Language::LispPerl::Atom->new({ type => "coroutine", value => $Coro::current }); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 1 | ▶ | sub _impl_coro_main { | ||||||||
|
Subroutine
_impl_coro_main: called CC=2 SCAR=7.1
| |||||||||||
| 134 | 1 | my ( $self, $ast ) = @_; | |||||||||
| 135 | 1 | ▶ | $ast->error("coro-main expects 0 argument") if $ast->size() != 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 1 | return Language::LispPerl::Atom->new({ type => "coroutine", value => $Coro::main }); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 1 | 1; | |||||||||