diff 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
line wrap: on
line diff
--- 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"
 		}
 	}