Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

EXPR_reduce Q:PIR -> nqp

  • Loading branch information...
commit bb128431766ebab3c2cd75601b9f730e645d8114 1 parent 23b8c3e
kboga authored

Showing 1 changed file with 35 additions and 84 deletions. Show diff stats Hide diff stats

  1. +35 84 src/HLL/Grammar.pm
119 src/HLL/Grammar.pm
@@ -713,90 +713,41 @@ An operator precedence parser.
713 713 };
714 714 }
715 715
716   - method EXPR_reduce($termstack, $opstack) {
717   - Q:PIR {
718   - .local pmc self, termstack, opstack
719   - self = find_lex 'self'
720   - termstack = find_lex '$termstack'
721   - opstack = find_lex '$opstack'
722   -
723   - .local pmc op, opOPER, opO
724   - .local string opassoc
725   - op = pop opstack
726   -
727   - # Give it a fresh capture list, since we'll have assumed it has
728   - # no positional captures and not taken them.
729   - .local pmc cap_class
730   - cap_class = find_lex 'NQPCapture'
731   - $P0 = new ['ResizablePMCArray']
732   - setattribute op, cap_class, '@!array', $P0
733   -
734   - opOPER = op['OPER']
735   - opO = opOPER['O']
736   - $P0 = opO['assoc']
737   - opassoc = $P0
738   - if opassoc == 'unary' goto op_unary
739   - if opassoc == 'list' goto op_list
740   - op_infix:
741   - .local pmc right, left
742   - right = pop termstack
743   - left = pop termstack
744   - op[0] = left
745   - op[1] = right
746   - $P0 = opO['reducecheck']
747   - if null $P0 goto op_infix_1
748   - $S0 = $P0
749   - self.$S0(op)
750   - op_infix_1:
751   - self.'!reduce_with_match'('EXPR', 'INFIX', op)
752   - goto done
753   -
754   - op_unary:
755   - .local pmc arg, afrom, ofrom
756   - arg = pop termstack
757   - op[0] = arg
758   - afrom = arg.'from'()
759   - ofrom = op.'from'()
760   - if afrom < ofrom goto op_postfix
761   - op_prefix:
762   - self.'!reduce_with_match'('EXPR', 'PREFIX', op)
763   - goto done
764   - op_postfix:
765   - self.'!reduce_with_match'('EXPR', 'POSTFIX', op)
766   - goto done
767   -
768   - op_list:
769   - .local string sym
770   - sym = ''
771   - $P0 = opOPER['sym']
772   - if null $P0 goto op_list_1
773   - sym = $P0
774   - op_list_1:
775   - arg = pop termstack
776   - unshift op, arg
777   - op_sym_loop:
778   - unless opstack goto op_sym_done
779   - $S0 = ''
780   - $P0 = opstack[-1]
781   - $P0 = $P0['OPER']
782   - $P0 = $P0['sym']
783   - if null $P0 goto op_sym_1
784   - $S0 = $P0
785   - op_sym_1:
786   - if sym != $S0 goto op_sym_done
787   - arg = pop termstack
788   - unshift op, arg
789   - $P0 = pop opstack
790   - goto op_sym_loop
791   - op_sym_done:
792   - arg = pop termstack
793   - unshift op, arg
794   - self.'!reduce_with_match'('EXPR', 'LIST', op)
795   - goto done
796   -
797   - done:
798   - push termstack, op
799   - };
  716 + method EXPR_reduce(@termstack, @opstack) {
  717 + my $op := nqp::pop(@opstack);
  718 + # Give it a fresh capture list, since we'll have assumed it has
  719 + # no positional captures and not taken them.
  720 + nqp::bindattr($op, NQPCapture, '@!array', nqp::list());
  721 + my %opOPER := $op<OPER>;
  722 + my %opO := %opOPER<O>;
  723 + my $opassoc := ~%opO<assoc>;
  724 + my $key;
  725 +
  726 + if $opassoc eq 'unary' {
  727 + my $arg := nqp::pop(@termstack);
  728 + $op[0] := $arg;
  729 + $key := $arg.from() < $op.from() ?? 'POSTFIX' !! 'PREFIX';
  730 + }
  731 + elsif $opassoc eq 'list' {
  732 + my $sym := ~%opOPER<sym>;
  733 + nqp::unshift($op, nqp::pop(@termstack));
  734 + while @opstack {
  735 + last if $sym ne @opstack[+@opstack-1]<OPER><sym>;
  736 + nqp::unshift($op, nqp::pop(@termstack));
  737 + nqp::pop(@opstack);
  738 + }
  739 + nqp::unshift($op, nqp::pop(@termstack));
  740 + $key := 'LIST';
  741 + }
  742 + else { # infix op assoc: left|right|ternary|...
  743 + $op[1] := nqp::pop(@termstack); # right
  744 + $op[0] := nqp::pop(@termstack); # left
  745 + my $reducecheck := %opO<reducecheck>;
  746 + self."$reducecheck"($op) if $reducecheck ne '';
  747 + $key := 'INFIX';
  748 + }
  749 + self.'!reduce_with_match'('EXPR', $key, $op);
  750 + nqp::push(@termstack, $op);
800 751 }
801 752
802 753 method ternary($match) {

0 comments on commit bb12843

Please sign in to comment.
Something went wrong with that request. Please try again.