comparison modules/parser.tp @ 246:8c81afd6d2d3

Refactor some of the AST node object constructors into a separate AST module
author Michael Pavone <pavone@retrodev.com>
date Mon, 06 Jan 2014 19:41:35 -0800
parents 3590ecca6bc9
children b76f683d076e
comparison
equal deleted inserted replaced
245:3590ecca6bc9 246:8c81afd6d2d3
556 ] 556 ]
557 consop <- match: Left . hws . "|" . Right where: { 557 consop <- match: Left . hws . "|" . Right where: {
558 Left <- match: addsub 558 Left <- match: addsub
559 Right <- match: maybecons 559 Right <- match: maybecons
560 } yield: { 560 } yield: {
561 #{ 561 ast binaryOp: "|" withArgs: Left Right
562 left <- Left
563 op <- "|"
564 right <- Right
565 string <- {
566 (string: left) . " " . op . " " . right
567 }
568 }
569 } 562 }
570 addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv 563 addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv
571 muldiv <- binaryOps: ["*" "/" "%"] withHigherPrec: primlitsym 564 muldiv <- binaryOps: ["*" "/" "%"] withHigherPrec: primlitsym
572
573 //TODO: Implement operator expressions
574 565
575 566
576 _alpha <- charClass: "a-zA-Z" 567 _alpha <- charClass: "a-zA-Z"
577 alpha <- zeroPlus: _alpha 568 alpha <- zeroPlus: _alpha
578 alphaNum <- zeroPlus: (charClass: "a-zA-Z0-9") 569 alphaNum <- zeroPlus: (charClass: "a-zA-Z0-9")
605 ]) 596 ])
606 } yield: { 597 } yield: {
607 if: (Chars length) = 0 { 598 if: (Chars length) = 0 {
608 Chars <- [] 599 Chars <- []
609 } 600 }
610 Chars join: "" 601 ast stringLit: (Chars join: "")
611 } 602 }
612 603
613 bdigit <- matchOne: [ 604 bdigit <- matchOne: [
614 (match: "0" yield: {0i64}) 605 (match: "0" yield: {0i64})
615 (match: "1" yield: {1i64}) 606 (match: "1" yield: {1i64})
653 if: (Suffix from: 0 withLength: 1) = "u" { 644 if: (Suffix from: 0 withLength: 1) = "u" {
654 signed <- false 645 signed <- false
655 } 646 }
656 litbits <- (Suffix from: 1) int32 647 litbits <- (Suffix from: 1) int32
657 } 648 }
658 #{ 649 ast intLit: num withBits: litbits andBase: 2 signed?: signed
659 litval <- num
660 signed? <- signed
661 bits <- litbits
662 string <- {
663 str <- "0b"
664 i <- bits - 1
665 printzero <- false
666 while: { i >= 0 } do: {
667 str <- str . (if: (lshift: 1 by: i) and num > 0 {
668 printzero <- true
669 "1"
670 } else: {
671 if: printzero {"0"} else: {""}
672 })
673 i <- i - 1
674 }
675 if: (not: signed?) || bits != 32 {
676 str <- str . (if: signed { "i" } else: { "u" }) . bits
677 }
678 str
679 }
680 }
681 } 650 }
682 651
683 decimal <- match: Sign . Digits . Suffix where: { 652 decimal <- match: Sign . Digits . Suffix where: {
684 Sign <- matchOne: ["-" ""] 653 Sign <- matchOne: ["-" ""]
685 Digits <- onePlus: digit 654 Digits <- onePlus: digit
700 if: (Suffix from: 0 withLength: 1) = "u" { 669 if: (Suffix from: 0 withLength: 1) = "u" {
701 signed <- false 670 signed <- false
702 } 671 }
703 litbits <- (Suffix from: 1) int32 672 litbits <- (Suffix from: 1) int32
704 } 673 }
705 #{ 674 ast intLit: num withBits: litbits andBase: 10 signed?: signed
706 litval <- num
707 signed? <- signed
708 bits <- litbits
709 string <- {
710 str <- string: litval
711 if: (not: signed?) || bits != 32 {
712 str <- str . (if: signed? {"i"} else: {"u"}) . bits
713 }
714 str
715 }
716 }
717 } 675 }
718 676
719 hexlit <- match: "0x" . Digits . Suffix where: { 677 hexlit <- match: "0x" . Digits . Suffix where: {
720 Digits <- onePlus: hdigit 678 Digits <- onePlus: hdigit
721 Suffix <- matchOne: [ 679 Suffix <- matchOne: [
732 if: (Suffix from: 0 withLength: 1) = "u" { 690 if: (Suffix from: 0 withLength: 1) = "u" {
733 signed <- false 691 signed <- false
734 } 692 }
735 litbits <- (Suffix from: 1) int32 693 litbits <- (Suffix from: 1) int32
736 } 694 }
737 #{ 695 ast intLit: num withBits: litbits andBase: 16 signed?: signed
738 litval <- num
739 signed? <- signed
740 bits <- litbits
741 string <- {
742 str <- "0x" . (hex: litval)
743 if: (not: signed?) || bits != 32 {
744 str <- str . (if: signed? {"i"} else: {"u"}) . bits
745 }
746 str
747 }
748 }
749 } 696 }
750 697
751 symexpr <- match: Name where: { 698 symexpr <- match: Name where: {
752 Name <- match: (onePlus: (charClass: "a-zA-Z_@!?")) . (zeroPlus: ((matchOne: [":" ""]) . (charClass: "a-zA-Z_@!?0-9"))) 699 Name <- match: (onePlus: (charClass: "a-zA-Z_@!?")) . (zeroPlus: ((matchOne: [":" ""]) . (charClass: "a-zA-Z_@!?0-9")))
753 } yield: { 700 } yield: {
1048 top <- matchOne: [ 995 top <- matchOne: [
1049 object 996 object
1050 lambda 997 lambda
1051 ] 998 ]
1052 999
1053 testmatchintlit <- :val matchfun { 1000 testmatchintlit <- :tomatch matchfun {
1054 res <- matchfun: val 1001 res <- matchfun: tomatch
1055 if: res { 1002 if: res {
1056 y <- res yield 1003 y <- res yield
1057 print: val . " matched with litval " . (y litval) . ", bits " . (y bits) . " and singned? " . (y signed?) . "\n" 1004 print: tomatch . " matched with litval " . (y val) . ", bits " . (y bits) . " and singned? " . (y signed?) . "\n"
1058 } else: { 1005 } else: {
1059 print: val . " did not match\n" 1006 print: tomatch . " did not match\n"
1060 } 1007 }
1061 } 1008 }
1062 1009
1063 main <- :args { 1010 main <- :args {
1064 cmatch <- alpha: "czx0123" 1011 cmatch <- alpha: "czx0123"