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

Commit

Permalink
Reduce higher predecence operators immediately.
Browse files Browse the repository at this point in the history
Test nqp with multiple levels of precedence.
  • Loading branch information
pmichaud committed Oct 19, 2009
1 parent ecc6416 commit 5b872f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/NQP/Actions.pm
@@ -1,6 +1,8 @@
class NQP::Actions is HLL::Actions;

# These will eventually go in NQP::Grammar.
NQP::Grammar.O(':prec<w=>, :assoc<left>', '%exponentiation');
NQP::Grammar.O(':prec<u=>, :assoc<left>', '%multiplicative');
NQP::Grammar.O(':prec<t=>, :assoc<left>', '%additive');

method TOP($/) { make $<EXPR>.ast; }
Expand Down
8 changes: 6 additions & 2 deletions src/NQP/Grammar.pm
Expand Up @@ -6,8 +6,12 @@ proto token term { <...> }
token term:sym<value> { <value> }

proto token infix { <...> }
token infix:sym<+> { $<sym>=['+'] <O('%additive, :pirop<add>')> }
token infix:sym<-> { $<sym>=['-'] <O('%additive, :pirop<sub>')> }
token infix:sym<**> { $<sym>=['**'] <O('%exponentiation, :pirop<pow>')> }
token infix:sym<*> { $<sym>=['*'] <O('%multiplicative, :pirop<mul>')> }
token infix:sym</> { $<sym>=['/'] <O('%multiplicative, :pirop<div>')> }
token infix:sym<%> { $<sym>=['%'] <O('%multiplicative, :pirop<mod>')> }
token infix:sym<+> { $<sym>=['+'] <O('%additive , :pirop<add>')> }
token infix:sym<-> { $<sym>=['-'] <O('%additive , :pirop<sub>')> }

token value {
| <integer>
Expand Down
8 changes: 8 additions & 0 deletions src/cheats/hll-grammar.pir
Expand Up @@ -393,9 +393,17 @@ An operator precedence parser.
inprec = $P0['prec']
inassoc = $P0['assoc']
unless inprec goto err_inprec

reduce_loop:
$P0 = opstack[-1]
$P0 = $P0['O']
opprec = $P0['prec']
unless opprec > inprec goto reduce_gt_done
capture_lex reduce
self.reduce()
goto reduce_loop
reduce_gt_done:

unless opprec == inprec goto reduce_done
# equal precedence, use associativity to decide
unless inassoc == 'left' goto reduce_done
Expand Down

0 comments on commit 5b872f0

Please sign in to comment.