changeset 261:074f885596ed

Merge
author Michael Pavone <pavone@retrodev.com>
date Sun, 01 Jun 2014 22:39:33 -0700
parents 56409de95f55 (current diff) be224817a14b (diff)
children 9d93e65a34be
files
diffstat 4 files changed, 104 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/interp.js	Sun Jun 01 22:39:25 2014 -0700
+++ b/interp.js	Sun Jun 01 22:39:33 2014 -0700
@@ -345,6 +345,31 @@
 	return this;
 };
 
+intlit.prototype.isconstant =
+	floatlit.prototype.isconstant =
+	strlit.prototype.isconstant = 
+	lambda.prototype.isconstant = function() {
+	return true;
+};
+
+object.prototype.isconstant =
+	symbol.prototype.isconstant =
+	funcall.prototype.isconstant = 
+	op.prototype.isconstant = 
+	assignment.prototype.isconstant = function() {
+	return false;
+}
+
+arraylit.prototype.isconstant =
+	listlit.prototype.isconstant = function() {
+	for (var idx = 0; idx < this.val.length; ++idx) {
+		if (!this.val[idx].isconstant()) {
+			return false;
+		}
+	}
+	return true;
+};
+
 intlit.prototype.eval =
 	floatlit.prototype.eval =
 	strlit.prototype.eval =
@@ -740,10 +765,18 @@
 			} else {
 				env.syms[expr.symbol.cleanName()] = {};
 				this.expressions[i] = expr.macroexpand(env);
-				if (this.expressions[i].expression instanceof lambda) {
-					env.syms[expr.symbol.cleanName()] = this.expressions[i].expression.eval(env);
-				} else {
-					env.syms[expr.symbol.cleanName()] = null;
+				try {
+					if (this.expressions[i].expression.isconstant()) {
+						env.syms[expr.symbol.cleanName()] = this.expressions[i].expression.eval(env);
+					} else {
+						env.syms[expr.symbol.cleanName()] = null;
+					}
+				} catch(e) {
+					var msg = 'Error, \n\t' + e.message.split('\n').join('\n\t') + '\nevaling node ' + this.expressions[i].expression
+					if (typeof this.expressions[i].expression == 'object') {
+						msg += ' with keys ' + JSON.stringify(Object.keys(this.expressions[i].expression) + ' and constructor ' + this.expressions[i].expression.constructor.name);
+					}
+					throw new Error(msg);
 				}
 			}
 		} else {
--- a/modules/ast.tp	Sun Jun 01 22:39:25 2014 -0700
+++ b/modules/ast.tp	Sun Jun 01 22:39:33 2014 -0700
@@ -157,7 +157,7 @@
 				fold:with <- :acc :fun {
 					acc <- fun: acc self
 					_args fold: acc with: :acc el {
-						fun: acc el
+						el fold: acc with: fun
 					}
 				}
 			}
@@ -179,7 +179,7 @@
 				fold:with <- :acc :fun {
 					acc <- fun: acc self
 					messages fold: acc with: :acc el {
-						fun: acc el
+						el fold: acc with: fun
 					}
 				}
 			}
@@ -202,7 +202,7 @@
 				fold:with <- :acc :fun {
 					acc <- fun: acc self
 					els fold: acc with: :acc el {
-						fun: acc el
+						el fold: acc with: fun
 					}
 				}
 			}
@@ -221,8 +221,8 @@
 				}
 				fold:with <- :acc :fun {
 					acc <- fun: acc self
-					acc <- fun: acc _sym
-					fun: acc _expr
+					acc <- _sym fold: acc with: fun
+					_expr fold: acc with: fun
 				}
 			}
 		}
@@ -248,7 +248,7 @@
 				fold:with <- :acc :fun {
 					acc <- fun: acc self
 					expressions fold: acc with: :acc el {
-						fun: acc el
+						el fold: acc with: fun
 					}
 				}
 			}
--- a/modules/dict.tp	Sun Jun 01 22:39:25 2014 -0700
+++ b/modules/dict.tp	Sun Jun 01 22:39:33 2014 -0700
@@ -127,6 +127,7 @@
 								notdone <- false
 							}
 						}
+						i <- i + 1
 					}
 					if: (ret empty?) ifnot else: {
 						ifpres: (ret v)
--- a/modules/parser.tp	Sun Jun 01 22:39:25 2014 -0700
+++ b/modules/parser.tp	Sun Jun 01 22:39:33 2014 -0700
@@ -1,7 +1,39 @@
 {
+	_matchid <- 0
+	getMatchId <- {
+		id <- _matchid
+		_matchid <- _matchid + 1
+		id
+	}
+	matchMemo <- {
+		_posdata <- #[]
+		_checkInitData <- :len {
+			len <- len + 1
+			while: { (_posdata length) < len } do: {
+				_posdata append: (dict hash)
+			}
+		}
+		#{
+			memo:at:withId:length <- :val :at :id :len {
+				_checkInitData: len
+				(_posdata get: at) set: id val
+				self
+			}
+			
+			getMemo:at:else <- :id :at :else {
+				if: (_posdata length) > at {
+					(_posdata get: at) ifget: id :val {
+						val
+					} else: else
+				} else: else
+			}
+		}
+	}
 light:from:withLength <- :_base :_start :_len {
+	_matchmemo <- matchMemo:
 	if: (not: (_base isBasicString?)) {
 		_start <- _start + (_base start)
+		_matchmemo <- _base memoData
 		_base <- _base base
 	}
 	_needsflat? <- true
@@ -53,6 +85,16 @@
 		isBasicString? <- { false }
 		base <- { _base }
 		start <- { _start }
+		memoData <- { _matchmemo }
+		
+		memo:at:withId <- :val :at :id {
+			_matchmemo memo: val at: (at + _start) withId: id length: (_base length)
+			self
+		}
+		
+		getMemo:at:else <- :id :at :else {
+			_matchmemo getMemo: id at: (at + _start) else: else
+		}
 	}
 }
 
@@ -371,6 +413,7 @@
 	}
 
 	matchOne <- macro: :options {
+		myid <- getMatchId:
 		options <- (options value) map: :option {
 			_makeMatchCall: option
 		}
@@ -384,7 +427,12 @@
 			}
 		}
 		quote: :tomatch {
-			body
+			tomatch <- light: tomatch from: 0
+			tomatch getMemo: myid at: 0 else: {
+				ret <- body
+				tomatch memo: ret at: 0 withId: myid
+				ret
+			}
 		}
 	}
 
@@ -431,8 +479,9 @@
 	}
 
 	match:where:yield <- macro: :matchexpr :whereclause :ylambda {
+		myid <- getMatchId:
 		syms <- []
-		withwhere <- (whereclause expressions) fold: (quote: :tomatch {}) with: :acc el {
+		withwhere <- (whereclause expressions) fold: (quote: {}) with: :acc el {
 
 			if: (el nodeType) = "assignment" {
 				valassign <- quote: (val <- false)
@@ -523,10 +572,16 @@
 				(quote: (lsym <- rsym)) | acc
 			}
 			successLambda <- successLambda expressions!: sucexp
-			withwhere addExpression: (quote: (if: matchres successLambda else: {
+			withwhere addExpression: (quote: (ret <- if: matchres successLambda else: {
 				matchres
 			}))
-			withwhere
+			withwhere addExpression: (quote: (tomatch memo: ret at: 0 withId: myid))
+			withwhere addExpression: (quote: ret)
+			
+			quote: :tomatch {
+				tomatch <- light: tomatch from: 0
+				tomatch getMemo: myid at: 0 else: withwhere
+			}
 		} else: {
 			print: "#error Error in main match expression of match:where:yield: " . (mcMain message) . "\n"
 		}
@@ -797,12 +852,7 @@
 	opsym <- match: Name where: {
 		Name <- matchOne: ["&&" "||" "<=" ">=" "<" ">" "=" "!=" "=" "-" "." "*" "/" "%" "|"]
 	} yield: {
-		#{
-			name <- Name
-			string <- {
-				name
-			}
-		}
+		ast symbol: Name
 	}
 
 	assignment <- match: ws . Symbol . hws . "<-" . Expr where: {