Skip to content

Commit

Permalink
[Yapsi] tried a subtype approach to Yapsi::IO
Browse files Browse the repository at this point in the history
Instead of a derivable class (which would have worked too), we try a
duck-typing variant in which we only require the object stored in the
attribute $!io to have a .say method. A bit lighter.
  • Loading branch information
Carl Masak committed Apr 3, 2010
1 parent c87b635 commit d6b91c7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
11 changes: 3 additions & 8 deletions lib/Yapsi.pm
Expand Up @@ -201,15 +201,10 @@ class Yapsi::Compiler {
}
}

class Yapsi::Runtime::IO {
method say($message) {
# RAKUDO: Can't use say in a method say [perl #74014]
print $message, "\n";
}
}
subset Yapsi::IO where { .can('say') }

class Yapsi::Runtime {
has Yapsi::Runtime::IO $!IO;
has Yapsi::IO $!io = $*OUT;

method run(@sic) {
my @r;
Expand Down Expand Up @@ -252,7 +247,7 @@ class Yapsi::Runtime {
%pad{~$0} = { :type<immediate>, :value(+$1) };
}
when /^ 'say $'(\d+) $/ {
$!IO.say: @r[+$0];
$!io.say: @r[+$0];
}
default { die "Couldn't handle instruction `$_`" }
}
Expand Down
2 changes: 1 addition & 1 deletion yapsi
Expand Up @@ -16,7 +16,7 @@ else {

try {
my Yapsi::Compiler $compiler .= new;
my Yapsi::Runtime $runtime .= new(IO => Yapsi::Runtime::IO.new);
my Yapsi::Runtime $runtime .= new;

my @sic = $compiler.compile($program);
$runtime.run(@sic);
Expand Down

0 comments on commit d6b91c7

Please sign in to comment.