changeset 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 7435367a932a
children e48c74a7539e
files modules/parser.tp
diffstat 1 files changed, 42 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/modules/parser.tp	Thu Jan 02 21:02:11 2014 -0800
+++ b/modules/parser.tp	Thu Jan 02 21:04:10 2014 -0800
@@ -427,8 +427,8 @@
 		quote: (match: Left . Pieces where: {
 			Left <- match: higher
 			Pieces <- zeroPlus: (match: hws . Op . Right where: {
-			Op <- matchOne: oplist
-			Right <- match: higher
+				Op <- matchOne: oplist
+				Right <- match: higher
 			} yield: {
 				#{
 					op <- Op
@@ -762,6 +762,7 @@
 			funcall
 			methcall
 			unarymeth
+			assignment
 			opexpr
 		]
 	} yield: {
@@ -838,12 +839,50 @@
 		}
 	}
 
+	argname <- match: hws . Pre . Initial . Rest where: {
+		Pre <- matchOne: [":" ""]
+		Initial <- onePlus: (charClass: "a-zA-Z_!?@")
+		Rest <- zeroPlus: (charClass: "a-zA-Z_!?@0-9")
+	} yield: {
+		Pre . Initial . Rest
+	}
+
+	lambda <- match: hws . Arglist . hws . "{" . ws . Exprs . "}" where: {
+		Arglist <- matchOne: [
+			match: ":" . First . Rest where: {
+				First <- match: symexpr
+				Rest <- zeroPlus: argname
+			} yield: {
+				if: (Rest length) = 0 {
+					Rest <- []
+				}
+				":" . (First name) | Rest
+			}
+			match: "" yield: { [] }
+		]
+		Exprs <- zeroPlus: expr
+	} yield: {
+		if: (Exprs length) = 0 {
+			Exprs <- []
+		}
+		#{
+			args <- Arglist
+			expressions <- Exprs
+			string <- {
+				(args join: " ") . "{\n\t" .
+					((expressions map: :el { string: el }) join: "\n\t") .
+				"}"
+			}
+		}
+	}
+
 	primlitsym <- match: hws . Lit where: {
 		Lit <- matchOne: [
 			hexlit
 			binary
 			decimal
 			symexpr
+			lambda
 			object
 			listlit
 			arraylit
@@ -901,7 +940,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 quine <- 123 | [4 5 6 fiddle sticks]\nquizzle <- #[receiver meth: arg]}"
+		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 }}"
 		codem <- expr: code
 		if: (codem matched?) {
 			print: code . "\nmatched with yield:\n" . (codem yield) . "\n"