Mercurial > repos > tabletprog
comparison modules/parser.tp @ 230:195f02ba349b
Implement lambdas in grammar. Make assignments an expression in grammar.
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 02 Jan 2014 21:04:10 -0800 |
parents | decdf28a8517 |
children | e48c74a7539e |
comparison
equal
deleted
inserted
replaced
229:7435367a932a | 230:195f02ba349b |
---|---|
425 | 425 |
426 binaryOps:withHigherPrec <- macro: :oplist :higher { | 426 binaryOps:withHigherPrec <- macro: :oplist :higher { |
427 quote: (match: Left . Pieces where: { | 427 quote: (match: Left . Pieces where: { |
428 Left <- match: higher | 428 Left <- match: higher |
429 Pieces <- zeroPlus: (match: hws . Op . Right where: { | 429 Pieces <- zeroPlus: (match: hws . Op . Right where: { |
430 Op <- matchOne: oplist | 430 Op <- matchOne: oplist |
431 Right <- match: higher | 431 Right <- match: higher |
432 } yield: { | 432 } yield: { |
433 #{ | 433 #{ |
434 op <- Op | 434 op <- Op |
435 right <- Right | 435 right <- Right |
436 } | 436 } |
760 expr <- match: (hws . Expr . ws) where: { | 760 expr <- match: (hws . Expr . ws) where: { |
761 Expr <- matchOne: [ | 761 Expr <- matchOne: [ |
762 funcall | 762 funcall |
763 methcall | 763 methcall |
764 unarymeth | 764 unarymeth |
765 assignment | |
765 opexpr | 766 opexpr |
766 ] | 767 ] |
767 } yield: { | 768 } yield: { |
768 Expr | 769 Expr |
769 } | 770 } |
836 }) join: "\n\t") . "\n]" | 837 }) join: "\n\t") . "\n]" |
837 } | 838 } |
838 } | 839 } |
839 } | 840 } |
840 | 841 |
842 argname <- match: hws . Pre . Initial . Rest where: { | |
843 Pre <- matchOne: [":" ""] | |
844 Initial <- onePlus: (charClass: "a-zA-Z_!?@") | |
845 Rest <- zeroPlus: (charClass: "a-zA-Z_!?@0-9") | |
846 } yield: { | |
847 Pre . Initial . Rest | |
848 } | |
849 | |
850 lambda <- match: hws . Arglist . hws . "{" . ws . Exprs . "}" where: { | |
851 Arglist <- matchOne: [ | |
852 match: ":" . First . Rest where: { | |
853 First <- match: symexpr | |
854 Rest <- zeroPlus: argname | |
855 } yield: { | |
856 if: (Rest length) = 0 { | |
857 Rest <- [] | |
858 } | |
859 ":" . (First name) | Rest | |
860 } | |
861 match: "" yield: { [] } | |
862 ] | |
863 Exprs <- zeroPlus: expr | |
864 } yield: { | |
865 if: (Exprs length) = 0 { | |
866 Exprs <- [] | |
867 } | |
868 #{ | |
869 args <- Arglist | |
870 expressions <- Exprs | |
871 string <- { | |
872 (args join: " ") . "{\n\t" . | |
873 ((expressions map: :el { string: el }) join: "\n\t") . | |
874 "}" | |
875 } | |
876 } | |
877 } | |
878 | |
841 primlitsym <- match: hws . Lit where: { | 879 primlitsym <- match: hws . Lit where: { |
842 Lit <- matchOne: [ | 880 Lit <- matchOne: [ |
843 hexlit | 881 hexlit |
844 binary | 882 binary |
845 decimal | 883 decimal |
846 symexpr | 884 symexpr |
885 lambda | |
847 object | 886 object |
848 listlit | 887 listlit |
849 arraylit | 888 arraylit |
850 ] | 889 ] |
851 } yield: { | 890 } yield: { |
899 testmatchintlit: "-567" :s {decimal: s} | 938 testmatchintlit: "-567" :s {decimal: s} |
900 testmatchintlit: "123u16" :s {decimal: s} | 939 testmatchintlit: "123u16" :s {decimal: s} |
901 testmatchintlit: "0x20" :s {hexlit: s} | 940 testmatchintlit: "0x20" :s {hexlit: s} |
902 testmatchintlit: "0x42u64" :s {hexlit: s} | 941 testmatchintlit: "0x42u64" :s {hexlit: s} |
903 testmatchintlit: "0b10101" :s {binary: s} | 942 testmatchintlit: "0b10101" :s {binary: s} |
904 code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n quine <- 123 | [4 5 6 fiddle sticks]\nquizzle <- #[receiver meth: arg]}" | 943 code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n quine <- 123 | [4 5 6 fiddle sticks]\n quizzle <- #[receiver meth: arg]\n blah <- :arg arg2 :arg3 { arg + arg2 + arg3 }}" |
905 codem <- expr: code | 944 codem <- expr: code |
906 if: (codem matched?) { | 945 if: (codem matched?) { |
907 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" | 946 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" |
908 } else: { | 947 } else: { |
909 print: code . "\ndid not match\n" | 948 print: code . "\ndid not match\n" |