comparison modules/parser.tp @ 223:25db1c7c7300

Added addition and multiplication precedence level operators to grammar
author Michael Pavone <pavone@retrodev.com>
date Sun, 29 Dec 2013 14:25:38 -0800
parents c6e321a538d4
children 262f5ae1bb1b
comparison
equal deleted inserted replaced
222:c6e321a538d4 223:25db1c7c7300
693 } 693 }
694 str 694 str
695 } 695 }
696 } 696 }
697 } 697 }
698 _processOpPieces <- :Left Pieces {
699 if: (Pieces length) > 0 {
700 Pieces fold: Left with: :acc piece {
701 #{
702 left <- acc
703 op <- piece op
704 right <- piece right
705 string <- {
706 (string: left) . " " . op . " " . right
707 }
708 }
709 }
710 } else: {
711 Left
712 }
713 }
714
715 addsub <- match: Left . Pieces where: {
716 Left <- match: muldiv
717 Pieces <- zeroPlus: (match: hws . Op . Right where: {
718 Op <- matchOne: ["+" "-" "."]
719 Right <- match: muldiv
720 } yield: {
721 #{
722 op <- Op
723 right <- Right
724 }
725 })
726 } yield: {
727 _processOpPieces: Left Pieces
728 }
729
730 muldiv <- match: Left . Pieces where: {
731 Left <- match: primlitsym
732 Pieces <- zeroPlus: (match: hws . Op . Right where: {
733 Op <- matchOne: ["*" "/" "%"]
734 Right <- match: primlitsym
735 } yield: {
736 #{
737 op <- Op
738 right <- Right
739 }
740 })
741 } yield: {
742 _processOpPieces: Left Pieces
743 }
698 744
699 //TODO: Implement operator expressions 745 //TODO: Implement operator expressions
700 opexpr <- match: primlitsym 746 opexpr <- match: addsub
701 747
702 expr <- match: (hws . Expr . ws) where: { 748 expr <- match: (hws . Expr . ws) where: {
703 Expr <- matchOne: [ 749 Expr <- matchOne: [
704 funcall 750 funcall
705 methcall 751 methcall
794 testmatchintlit: "-567" :s {decimal: s} 840 testmatchintlit: "-567" :s {decimal: s}
795 testmatchintlit: "123u16" :s {decimal: s} 841 testmatchintlit: "123u16" :s {decimal: s}
796 testmatchintlit: "0x20" :s {hexlit: s} 842 testmatchintlit: "0x20" :s {hexlit: s}
797 testmatchintlit: "0x42u64" :s {hexlit: s} 843 testmatchintlit: "0x42u64" :s {hexlit: s}
798 testmatchintlit: "0b10101" :s {binary: s} 844 testmatchintlit: "0b10101" :s {binary: s}
799 code <- "#{ foo <- 123\n bar <- 0xABC\n baz <- 0b1010\n qux <- fo: 38 shizzle: bam\n}" 845 code <- "#{ foo <- 123\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n}"
800 codem <- expr: code 846 codem <- expr: code
801 if: (codem matched?) { 847 if: (codem matched?) {
802 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" 848 print: code . "\nmatched with yield:\n" . (codem yield) . "\n"
803 } else: { 849 } else: {
804 print: code . "\ndid not match\n" 850 print: code . "\ndid not match\n"