changeset 225:262f5ae1bb1b

Added a new binaryOps:withHigherPrec macro to simplify specification of binary operator precedence level rules in the grammar
author Michael Pavone <pavone@retrodev.com>
date Sun, 29 Dec 2013 14:39:54 -0800
parents 97c3e33cd3f4
children 6055f56d0e45
files modules/parser.tp
diffstat 1 files changed, 22 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/modules/parser.tp	Sun Dec 29 14:38:56 2013 -0800
+++ b/modules/parser.tp	Sun Dec 29 14:39:54 2013 -0800
@@ -423,6 +423,28 @@
 		}
 	}
 
+	binaryOps:withHigherPrec <- macro: :oplist :higher {
+		quote: (match: Left . Pieces where: {
+			Left <- match: higher
+			Pieces <- zeroPlus: (match: hws . Op . Right where: {
+			Op <- matchOne: oplist
+			Right <- match: higher
+			} yield: {
+				#{
+					op <- Op
+					right <- Right
+				}
+			})
+		} yield: {
+			_processOpPieces: Left Pieces
+		})
+	}
+
+	addsub <- binaryOps: ["+" "-" "."] withHigherPrec: muldiv
+	muldiv <- binaryOps: ["*" "/" "%"] withHigherPrec: primlitsym
+
+	//TODO: Implement operator expressions
+	opexpr <- match: addsub
 
 	_alpha <- charClass: "a-zA-Z"
 	alpha <- zeroPlus: _alpha
@@ -712,39 +734,6 @@
 		}
 	}
 
-	addsub <- match: Left . Pieces where: {
-		Left <- match: muldiv
-		Pieces <- zeroPlus: (match: hws . Op . Right where: {
-			Op <- matchOne: ["+" "-" "."]
-			Right <- match: muldiv
-		} yield: {
-			#{
-				op <- Op
-				right <- Right
-			}
-		})
-	} yield: {
-		_processOpPieces: Left Pieces
-	}
-
-	muldiv <- match: Left . Pieces where: {
-		Left <- match: primlitsym
-		Pieces <- zeroPlus: (match: hws . Op . Right where: {
-			Op <- matchOne: ["*" "/" "%"]
-			Right <- match: primlitsym
-		} yield: {
-			#{
-				op <- Op
-				right <- Right
-			}
-		})
-	} yield: {
-		_processOpPieces: Left Pieces
-	}
-
-	//TODO: Implement operator expressions
-	opexpr <- match: addsub
-
 	expr <- match: (hws . Expr . ws) where: {
 		Expr <- matchOne: [
 			funcall