changeset 227:8c16ef123aee

Implement list literals in grammar
author Michael Pavone <pavone@retrodev.com>
date Sun, 29 Dec 2013 17:09:21 -0800
parents 6055f56d0e45
children decdf28a8517
files modules/parser.tp
diffstat 1 files changed, 45 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/modules/parser.tp	Sun Dec 29 14:56:46 2013 -0800
+++ b/modules/parser.tp	Sun Dec 29 17:09:21 2013 -0800
@@ -454,6 +454,9 @@
 			left <- Left
 			op <- "|"
 			right <- Right
+			string <- {
+				(string: left) . " " . op . " " . right
+			}
 		}
 	}
 	addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv
@@ -703,16 +706,20 @@
 		}
 	}
 
+	unarymeth <- match: Receiver . hws . Method where: {
+		Receiver <- match: opexpr
+		Method <- match: symexpr
+	} yield: {
+		#{
+			receiver <- Receiver
+			name <- Method name
+			args <- []
+		}
+	}
+
 	methcall <- match: Receiver . hws . Rest where: {
 		Receiver <- match: opexpr
-		Rest <- matchOne: [funcall (
-			match: Name where: { Name <- match: symexpr } yield: {
-				#{
-					name <- Name name
-					args <- []
-				}
-			}
-		)]
+		Rest <- match: funcall
 	} yield: {
 		#{
 			receiver <- Receiver
@@ -754,6 +761,17 @@
 		Expr <- matchOne: [
 			funcall
 			methcall
+			unarymeth
+			opexpr
+		]
+	} yield: {
+		Expr
+	}
+
+	lexpr <- match: (hws . Expr . ws) where: {
+		Expr <- matchOne: [
+			funcall
+			methcall
 			opexpr
 		]
 	} yield: {
@@ -786,6 +804,23 @@
 		}
 	}
 
+	listlit <- match: "[" . ws . Els . "]" where: {
+		Els <- zeroPlus: lexpr
+	} yield: {
+		//Handle limitation of zeroPlus macro
+		if: (Els length) = 0 {
+			Els <- []
+		}
+		#{
+			litval <- Els
+			string <- {
+				"[\n\t". ((litval map: :el {
+					string: el
+				}) join: "\n\t") . "\n]"
+			}
+		}
+	}
+
 	primlitsym <- match: hws . Lit where: {
 		Lit <- matchOne: [
 			hexlit
@@ -793,6 +828,7 @@
 			decimal
 			symexpr
 			object
+			listlit
 		]
 	} yield: {
 		Lit
@@ -847,7 +883,7 @@
 		testmatchintlit: "0x20" :s {hexlit: s}
 		testmatchintlit: "0x42u64" :s {hexlit: s}
 		testmatchintlit: "0b10101" :s {binary: s}
-		code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n}"
+		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}"
 		codem <- expr: code
 		if: (codem matched?) {
 			print: code . "\nmatched with yield:\n" . (codem yield) . "\n"