Skip to content

Commit

Permalink
implement the throw statement
Browse files Browse the repository at this point in the history
This was `die` for a short while in a branch, but #65 says we
should call it `throw`.

Also introduces the Val::Exception type, which is the only
thing you can throw. (In the branch it was Val::Str, but that's
deprecated now with extreme prejudice.)
  • Loading branch information
Carl Masak committed Mar 11, 2016
1 parent 7b9b75d commit 342d99d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/_007/Parser/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class _007::Parser::Actions {
make Q::Statement::Return.new(:expr($<EXPR> ?? $<EXPR>.ast !! Val::None.new));
}

method statement:throw ($/) {
make Q::Statement::Throw.new(:expr($<EXPR> ?? $<EXPR>.ast !! Val::None.new));
}

method statement:if ($/) {
my %parameters = $<xblock>.ast;
%parameters<else> = $<else> :exists
Expand Down
5 changes: 5 additions & 0 deletions lib/_007/Parser/Syntax.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ grammar _007::Parser::Syntax {
unless $*insub;
}
}

token statement:throw {
throw [<.ws> <EXPR>]?
}

rule statement:if {
if <xblock>
[ else
Expand Down
12 changes: 12 additions & 0 deletions lib/_007/Q.pm
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,18 @@ class Q::Statement::Return does Q::Statement {
}
}

class Q::Statement::Throw does Q::Statement {
has $.expr = Val::None.new;

method run($runtime) {
my $value = $.expr ~~ Val::None ?? Val::Exception.new(:message("Died")) !! $.expr.eval($runtime);
die X::TypeCheck.new(:got($value), :excpected(Val::Exception))
if $value !~~ Val::Exception;

die $value.message;
}
}

class Q::Statement::Sub does Q::Statement does Q::Declaration {
has $.identifier;
has $.traitlist = Q::TraitList.new;
Expand Down
2 changes: 2 additions & 0 deletions lib/_007/Runtime/Builtins.pm
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class _007::Runtime::Builtins {
Val::Str,
Val::Sub,
Val::Type,
Val::Exception,
;

for @val-types -> $type {
Expand Down Expand Up @@ -413,6 +414,7 @@ class _007::Runtime::Builtins {
Q::Statement::My,
Q::Statement::Return,
Q::Statement::Sub,
Q::Statement::Throw,
Q::Statement::While,
Q::StatementList,
Q::Term::Array,
Expand Down
17 changes: 17 additions & 0 deletions lib/_007/Val.pm
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,20 @@ class Val::Macro is Val::Sub {

method Str { "<macro {$.name}{$.pretty-parameters}>" }
}

class Val::Exception does Val {
has Str $.message;

method Str {
my $message = Val::Str.new(:value($.message));
return "Exception \{message: {$message.quoted-Str}\}";
}

method quoted-Str {
self.Str
}

method truthy {
?%.properties
}
}

0 comments on commit 342d99d

Please sign in to comment.