Skip to content

Commit

Permalink
add die
Browse files Browse the repository at this point in the history
  • Loading branch information
sergot committed Mar 10, 2016
1 parent ef1433b commit dcad411
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/format.007
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ macro format(fmt, args) {
input.suffix(openBracePos + closeBracePos + 1)));
}

if fmt ~~ Q::Literal::Str {
if fmt ~~ Q::Literal::Str && args ~~ Q::Term::Array {
my highestUsedIndex = findHighestIndex(fmt.value);
if args ~~ Q::Term::Array && args.elements.elems() <= highestUsedIndex {
args.elements[highestUsedIndex];
my argCount = args.elements.elems();
if argCount <= highestUsedIndex {
die "Highest index was " ~ str(highestUsedIndex) ~ " but got only " ~ str(argCount) ~ " arguments.";
}
}

Expand Down
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:die ($/) {
make Q::Statement::Die.new(:expr($<EXPR> ?? $<EXPR>.ast !! Val::None.new));
}

method statement:if ($/) {
my %parameters = $<xblock>.ast;
%parameters<else> = $<else> :exists
Expand Down
3 changes: 3 additions & 0 deletions lib/_007/Parser/Syntax.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ grammar _007::Parser::Syntax {
unless $*insub;
}
}
token statement:die {
die [<.ws> <EXPR>]?
}
token statement:if {
if <.ws> <xblock>
[ <.ws> else <.ws>
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::Die does Q::Statement {
has $.expr = Val::None.new;

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

die $value.value;
}
}

class Q::Statement::Sub does Q::Statement does Q::Declaration {
has $.identifier;
has $.traitlist = Q::TraitList.new;
Expand Down

0 comments on commit dcad411

Please sign in to comment.