Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
[nqp]: Add ternary (infix:<?? !!>) operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 27, 2009
1 parent d9b2cc4 commit d22d3e4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/NQP/Actions.pm
Expand Up @@ -252,6 +252,7 @@ NQP::Grammar.O(':prec<r=>, :assoc<list>', '%concatenation');
NQP::Grammar.O(':prec<m=>, :assoc<left>', '%relational');
NQP::Grammar.O(':prec<l=>, :assoc<left>', '%tight_and');
NQP::Grammar.O(':prec<k=>, :assoc<left>', '%tight_or');
NQP::Grammar.O(':prec<j=>, :assoc<right>', '%conditional');
NQP::Grammar.O(':prec<i=>, :assoc<right>', '%assignment');
NQP::Grammar.O(':prec<g=>, :assoc<list>, :nextterm<nulltermish>', '%comma');
NQP::Grammar.O(':prec<f=>, :assoc<list>', '%list_infix');
Expand Down
12 changes: 11 additions & 1 deletion src/NQP/Grammar.pm
@@ -1,6 +1,8 @@
grammar NQP::Grammar is HLL::Grammar;

token TOP { <comp_unit> }
token TOP {
<comp_unit>
}

token comp_unit {
<.newpad>
Expand Down Expand Up @@ -219,6 +221,14 @@ token infix:sym<&&> { <sym> <O('%tight_and, :pasttype<if>')> }
token infix:sym<||> { <sym> <O('%tight_or, :pasttype<unless>')> }
token infix:sym<//> { <sym> <O('%tight_or, :pasttype<def_or>')> }

token infix:sym<?? !!> {
'??'
<.ws>
<EXPR('i=')>
'!!'
<O('%conditional, :reducecheck<ternary>, :pasttype<if>')>
}

token infix:sym<:=> { <sym> <O('%assignment, :pasttype<bind>')> }
token infix:sym<::=> { <sym> <O('%assignment, :pasttype<bind>')> }

Expand Down
12 changes: 12 additions & 0 deletions src/cheats/hll-grammar.pir
Expand Up @@ -424,6 +424,13 @@ An operator precedence parser.
=cut

.sub 'EXPR' :method
.param string preclim :optional
.param int has_preclim :opt_flag

if has_preclim goto have_preclim
preclim = ''
have_preclim:

.const 'Sub' reduce = 'EXPR_reduce'
.local string termish
termish = 'termish'
Expand Down Expand Up @@ -520,6 +527,7 @@ An operator precedence parser.
.local string inprec, inassoc, opprec
inprec = inO['prec']
unless inprec goto err_inprec
if inprec <= preclim goto term_done
inassoc = inO['assoc']

reduce_loop:
Expand Down Expand Up @@ -591,6 +599,10 @@ An operator precedence parser.
left = pop termstack
op[0] = left
op[1] = right
$S0 = opO['reducecheck']
unless $S0 goto op_infix_1
self.$S0(op)
op_infix_1:
self.'!reduce'('EXPR', 'INFIX', op)
goto done

Expand Down
12 changes: 12 additions & 0 deletions src/cheats/nqp-builtins.pir
Expand Up @@ -48,3 +48,15 @@
$P0 = box 0
set_global '$test_counter', $P0
.end


.namespace ['NQP';'Grammar']

.sub 'ternary' :method
.param pmc match
$P0 = match[1]
$P1 = match['infix']
$P1 = $P1['EXPR']
match[1] = $P1
match[2] = $P0
.end
23 changes: 23 additions & 0 deletions t/nqp/16-ternary.t
@@ -0,0 +1,23 @@
#!./parrot nqp.pbc

# the ternary ?? !! operator

plan(8);

ok( 1 ?? 1 !! 0 );
ok( 0 ?? 0 !! 1 );

my $a := 1 ?? 'yes' !! 'no';
ok( $a eq 'yes' );

my $b := 0 ?? 'yes' !! 'no';
ok( $b eq 'no' );

my $c := 1 ?? 'yes' !! ( $a := 'no' );
ok( $c eq 'yes' );
ok( $a eq 'yes' );

my $d := 0 ?? ( $a := 'no' ) !! 'yes';
ok( $d eq 'yes' );
ok( $a eq 'yes' );

0 comments on commit d22d3e4

Please sign in to comment.