changeset 242:0e7982adc76b

Make the successful return value from a match expression be truthy and the failure value false. This avoids an extra method call when checking the result and avoids allocating a new object when a match fails.
author Mike Pavone <pavone@retrodev.com>
date Sun, 05 Jan 2014 20:56:25 -0800
parents c50f77de41d1
children 5b830147c1cd
files modules/parser.tp
diffstat 1 files changed, 96 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/modules/parser.tp	Sun Jan 05 20:54:22 2014 -0800
+++ b/modules/parser.tp	Sun Jan 05 20:56:25 2014 -0800
@@ -5,30 +5,32 @@
 _matchString <- :str tomatch {
 	if: (tomatch isString?) {
 		if: (tomatch length) < (str length) {
-			#{
-				matched? <- { false }
-			}
+			false
 		} else: {
 			if: (tomatch length) > (str length) {
 				tomatch <- tomatch from: 0 withLength: (str length)
 			}
 			if: str = tomatch {
 				#{
-					matched? <- { true }
+					if <- :self trueblock {
+						trueblock:
+					}
+					ifnot <- :self falseblock {
+						self
+					}
+					if:else <- :self trueblock :elseblock {
+						trueblock:
+					}
 					matchlen <- { str length }
 					basicYield? <- { true }
 					yield <- { str }
 				}
 			} else: {
-				#{
-					matched? <- { false }
-				}
+				false
 			}
 		}
 	} else: {
-		#{
-			matched? <- { false }
-		}
+		false
 	}
 }
 _makeMatchCall <- :matchexpr {
@@ -58,14 +60,22 @@
 							valid? <- { true }
 							matchcall <- quote: (_applyMatch: :tomatch {
 								lm <- left
-								if: (lm matched?) {
+								if: lm {
 									orig <- tomatch
 									tomatch <- tomatch from: (lm matchlen)
 									rm <- right
-									if: (rm matched?) {
+									if: rm {
 										total <- (rm matchlen) + (lm matchlen)
 										#{
-											matched? <- { true }
+											if <- :self trueblock {
+												trueblock:
+											}
+											ifnot <- :self falseblock {
+												self
+											}
+											if:else <- :self trueblock :elseblock {
+												trueblock:
+											}
 											matchlen <- { total }
 											basicYield? <- { true }
 											yield <- { orig from: 0 withLength: total }
@@ -110,8 +120,7 @@
 			yieldvals <- []
 			while: { _match && cur < n } do: {
 				res <- mcall
-				_match <- res matched?
-				if: _match {
+				_match <- if: res {
 					count <- count + 1
 					//TODO: Use some kind of lightweight substring wrapper here
 					tomatch <- tomatch from: (res matchlen)
@@ -128,12 +137,21 @@
 					}
 					allBasic? <- allBasic? && (res basicYield?)
 					cur <- cur + (res matchlen)
+					true
 				}
 			}
 			if: count >= min {
 				if: allBasic? {
 					#{
-						matched? <- { true }
+						if <- :self trueblock {
+							trueblock:
+						}
+						ifnot <- :self falseblock {
+							self
+						}
+						if:else <- :self trueblock :elseblock {
+							trueblock:
+						}
 						matchlen <- { cur }
 						basicYield? <- { true }
 						yield <- { orig from: 0 withLength: cur }
@@ -141,16 +159,22 @@
 				} else: {
 					yieldvals <- yieldvals reverse
 					#{
-						matched? <- { true }
+						if <- :self trueblock {
+							trueblock:
+						}
+						ifnot <- :self falseblock {
+							self
+						}
+						if:else <- :self trueblock :elseblock {
+							trueblock:
+						}
 						matchlen <- { cur }
 						basicYield? <- { false }
 						yield <- { yieldvals }
 					}
 				}
 			} else: {
-				#{
-					matched? <- { false }
-				}
+				false
 			}
 		}
 	} else: {
@@ -227,6 +251,7 @@
 	}
 }
 _charClass <- :chars {
+	orig <- chars
 	chars <- _expandClass: chars
 	charmap <- ""
 	char <- 0
@@ -248,31 +273,28 @@
 		if: (tomatch isString?) {
 			if: (charmap byte: (tomatch byte: 0)) = t {
 				#{
-					matched? <- { true }
+					if <- :self trueblock {
+						trueblock:
+					}
+					ifnot <- :self falseblock {
+						self
+					}
+					if:else <- :self trueblock :elseblock {
+						trueblock:
+					}
 					matchlen <- { 1 }
 					basicYield? <- { true }
 					yield <- { tomatch from: 0 withLength: 1 }
 				}
 			} else: {
-				#{
-					matched? <- { false }
-				}
+				false
 			}
 		} else: {
-			#{
-				matched? <- { false }
-			}
+			false
 		}
 	}
 }
 #{
-	ifmatch:else <- :matchres :elseblock {
-		if: (matchres matched?) {
-			matchres
-		} else: {
-			elseblock:
-		}
-	}
 	charClass <- macro: :rawchars {
 		eval: rawchars :chars {
 			_charClass: chars
@@ -293,12 +315,10 @@
 		options <- (options value) map: :option {
 			_makeMatchCall: option
 		}
-		body <- options foldr: (quote: #{
-			matched? <- { false }
-		}) with: :acc el {
+		body <- options foldr: (quote: false) with: :acc el {
 			if: (el valid?) {
 				mcall <- el matchcall
-				quote: (ifmatch: mcall else: { acc })
+				quote: (ifnot: mcall { acc })
 			} else: {
 				print: "#error Invalid matchOne macro call: " . (el message) . "\n"
 				acc
@@ -327,9 +347,17 @@
 			mcall <- mc matchcall
 			quote: :tomatch {
 				res <- mcall
-				if: (res matched?) {
+				if: res {
 					#{
-						matched? <- { true }
+						if <- :self trueblock {
+							trueblock:
+						}
+						ifnot <- :self falseblock {
+							self
+						}
+						if:else <- :self trueblock :elseblock {
+							trueblock:
+						}
 						matchlen <- { res matchlen }
 						basicYield? <- { false }
 						yield <- ylambda
@@ -368,19 +396,25 @@
 						if: matchsym {
 							if: valsym = tomatch {
 								#{
-									matched? <- { true }
+									if <- :self trueblock {
+										trueblock:
+									}
+									ifnot <- :self falseblock {
+										self
+									}
+									if:else <- :self trueblock :elseblock {
+										trueblock:
+									}
 									matchlen <- { valsym length }
 									basicYield? <- { true } //TODO: Check if this is correct
 									yield <- { valsym }
 								}
 							} else: {
-								#{
-									matched? <- { false }
-								}
+								false
 							}
 						} else: {
 							mr <- mcall
-							if: (mr matched?) {
+							if: mr {
 								matchsym <- true
 								valsym <- (mr yield)
 							}
@@ -410,7 +444,15 @@
 				//Extra assignments will be added here
 				mlen <- matchres matchlen
 				#{
-					matched? <- { true }
+					if <- :self trueblock {
+						trueblock:
+					}
+					ifnot <- :self falseblock {
+						self
+					}
+					if:else <- :self trueblock :elseblock {
+						trueblock:
+					}
 					matchlen <- { mlen }
 					basicYield? <- { false }
 					yield <- ylambda
@@ -422,7 +464,7 @@
 				(quote: (lsym <- rsym)) | acc
 			}
 			successLambda <- successLambda expressions!: sucexp
-			withwhere addExpression: (quote: (if: (matchres matched?) successLambda else: {
+			withwhere addExpression: (quote: (if: matchres successLambda else: {
 				matchres
 			}))
 			withwhere
@@ -928,7 +970,7 @@
 
 	testmatchintlit <- :val matchfun {
 		res <- matchfun: val
-		if: (res matched?) {
+		if: res {
 			y <- res yield
 			print: val . " matched with litval " . (y litval) . ", bits " . (y bits) . " and singned? " . (y signed?) . "\n"
 		} else: {
@@ -939,31 +981,31 @@
 	main <- :args {
 		cmatch <- alpha: "czx0123"
 		zeromatch <- alpha: "01234"
-		if: (cmatch matched?) {
+		if: cmatch {
 			print: "czx0123 matched with length " . (cmatch matchlen) . "\n"
 		} else: {
 			print: "czx0123 didn't match\n"
 		}
-		if: (zeromatch matched?) {
+		if: zeromatch {
 			print: "0123 matched with length " . (zeromatch matchlen) . "\n"
 		} else: {
 			print: "0123 didn't match\n"
 		}
 		zeromatchanum <- alphaNum: "01234"
-		if: (zeromatchanum matched?) {
+		if: zeromatchanum {
 			print: "01234 matched with length " . (zeromatchanum matchlen) . "\n"
 		} else: {
 			print: "01234 didn't match\n"
 		}
 		stuff <- " \t/* blah blah blah * blah */ foo"
 		hwsmatch <- hws: stuff
-		if: (hwsmatch matched?) {
+		if: hwsmatch {
 			print: "'" . (stuff from: (hwsmatch matchlen)) . "' found after hws\n"
 		} else: {
 			print: stuff . " did not match hws rule\n"
 		}
 		tmatch <- digit: "3"
-		if: (tmatch matched?) {
+		if: tmatch {
 			print: "3 matched with yield " . (tmatch yield) . ", yield + 1 = " . ((tmatch yield) + 1) . "\n"
 		} else: {
 			print: "3 did not match\n"
@@ -988,7 +1030,7 @@
 			}
 		}
 		codem <- top: code
-		if: (codem matched?) {
+		if: codem {
 			print: code . "\nmatched with yield:\n" . (codem yield) . "\n"
 		} else: {
 			print: code . "\ndid not match\n"