# HG changeset patch # User Michael Pavone # Date 1389066095 28800 # Node ID 8c81afd6d2d37bb227024b4bd7028a128150c799 # Parent 3590ecca6bc977d09856b5db93f534c72f89cd04 Refactor some of the AST node object constructors into a separate AST module diff -r 3590ecca6bc9 -r 8c81afd6d2d3 modules/ast.tp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/ast.tp Mon Jan 06 19:41:35 2014 -0800 @@ -0,0 +1,84 @@ +{ + _binary <- 0 + _string <- 1 + _int <- 2 + + #{ + binary <- { _binary } + stringlit <- { _string } + intlit <- { _int } + + + binaryOp:withArgs <- :opname :_left _right { + #{ + nodeType <- { _binary } + left <- _left + op <- opname + right <- _right + + leftAssociative? <- { + op != "|" + } + stringIndent <- :indent { + (left stringIndent: indent) . " " . op . (right stringIndent: indent) + } + string <- { + stringIndent: "" + } + } + } + + stringLit <- :_val { + #{ + nodeType <- { _string } + val <- _val + stringIndent <- :indent { + "\"" . val . "\"" + } + string <- { + stringIndent: "" + } + } + } + + intLit:withBits:andBase:signed? <- :_val :_bits :_base :_signed? { + #{ + nodeType <- { _int } + val <- _val + base <- _base + bits <- _bits + signed? <- _signed? + stringIndent <- :indent { + suffix <- "" + if: bits != 32 || (not: signed?) { + suffix <- (if: signed? {"i"} else: {"u"}) . bits + } + if: base = 16 { + "0x" . (hex: val) . suffix + } else: { + if: base = 2 { + str <- "0b" + i <- bits - 1 + printzero <- false + while: { i >= 0 } do: { + str <- str . (if: (lshift: 1 by: i) and val > 0 { + printzero <- true + "1" + } else: { + if: printzero {"0"} else: {""} + }) + i <- i - 1 + } + str . suffix + } else: { + (string: val) . suffix + } + } + } + string <- { + stringIndent: "" + } + } + } + } +} diff -r 3590ecca6bc9 -r 8c81afd6d2d3 modules/parser.tp --- a/modules/parser.tp Mon Jan 06 01:03:18 2014 -0800 +++ b/modules/parser.tp Mon Jan 06 19:41:35 2014 -0800 @@ -558,20 +558,11 @@ Left <- match: addsub Right <- match: maybecons } yield: { - #{ - left <- Left - op <- "|" - right <- Right - string <- { - (string: left) . " " . op . " " . right - } - } + ast binaryOp: "|" withArgs: Left Right } addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv muldiv <- binaryOps: ["*" "/" "%"] withHigherPrec: primlitsym - //TODO: Implement operator expressions - _alpha <- charClass: "a-zA-Z" alpha <- zeroPlus: _alpha @@ -607,7 +598,7 @@ if: (Chars length) = 0 { Chars <- [] } - Chars join: "" + ast stringLit: (Chars join: "") } bdigit <- matchOne: [ @@ -655,29 +646,7 @@ } litbits <- (Suffix from: 1) int32 } - #{ - litval <- num - signed? <- signed - bits <- litbits - string <- { - str <- "0b" - i <- bits - 1 - printzero <- false - while: { i >= 0 } do: { - str <- str . (if: (lshift: 1 by: i) and num > 0 { - printzero <- true - "1" - } else: { - if: printzero {"0"} else: {""} - }) - i <- i - 1 - } - if: (not: signed?) || bits != 32 { - str <- str . (if: signed { "i" } else: { "u" }) . bits - } - str - } - } + ast intLit: num withBits: litbits andBase: 2 signed?: signed } decimal <- match: Sign . Digits . Suffix where: { @@ -702,18 +671,7 @@ } litbits <- (Suffix from: 1) int32 } - #{ - litval <- num - signed? <- signed - bits <- litbits - string <- { - str <- string: litval - if: (not: signed?) || bits != 32 { - str <- str . (if: signed? {"i"} else: {"u"}) . bits - } - str - } - } + ast intLit: num withBits: litbits andBase: 10 signed?: signed } hexlit <- match: "0x" . Digits . Suffix where: { @@ -734,18 +692,7 @@ } litbits <- (Suffix from: 1) int32 } - #{ - litval <- num - signed? <- signed - bits <- litbits - string <- { - str <- "0x" . (hex: litval) - if: (not: signed?) || bits != 32 { - str <- str . (if: signed? {"i"} else: {"u"}) . bits - } - str - } - } + ast intLit: num withBits: litbits andBase: 16 signed?: signed } symexpr <- match: Name where: { @@ -1050,13 +997,13 @@ lambda ] - testmatchintlit <- :val matchfun { - res <- matchfun: val + testmatchintlit <- :tomatch matchfun { + res <- matchfun: tomatch if: res { y <- res yield - print: val . " matched with litval " . (y litval) . ", bits " . (y bits) . " and singned? " . (y signed?) . "\n" + print: tomatch . " matched with litval " . (y val) . ", bits " . (y bits) . " and singned? " . (y signed?) . "\n" } else: { - print: val . " did not match\n" + print: tomatch . " did not match\n" } }