changeset 311:dfd204c82849

Merge
author Michael Pavone <pavone@retrodev.com>
date Fri, 01 Aug 2014 18:56:50 -0700
parents 2308336790d4 (current diff) 56deb4a102db (diff)
children 8511aac1ce82
files modules/list.tp
diffstat 6 files changed, 96 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/compile	Fri Aug 01 18:56:39 2014 -0700
+++ b/compile	Fri Aug 01 18:56:50 2014 -0700
@@ -16,4 +16,4 @@
 bin=`echo $1 | sed 's/\.tp//'`
 shift
 echo gcc $@ -o $bin $cname runtime/object.c -lgc
-gcc $@ -o $bin $cname runtime/object.c -lgc -ldl -lSDL2
+gcc $@ -o $bin $cname runtime/object.c -lgc
--- a/modules/file.tp	Fri Aug 01 18:56:39 2014 -0700
+++ b/modules/file.tp	Fri Aug 01 18:56:50 2014 -0700
@@ -76,6 +76,7 @@
 				iter
 			}
 			#{
+				fd <- { _fd }
 				read <- :bytes {
 					while: { bytes > _buffered && (not: _eof?)} do: {
 						_addBuffer:
@@ -109,6 +110,12 @@
 					_buffers <- nextBuffers
 					pieces join: ""
 				}
+				readAll <- {
+					while: { not: _eof? } do: {
+						_addBuffer:
+					}
+					read: _buffered
+				}
 				write <- :data {
 					//TODO: write buffering for small writes
 					os write: _fd data
--- a/modules/list.tp	Fri Aug 01 18:56:39 2014 -0700
+++ b/modules/list.tp	Fri Aug 01 18:56:50 2014 -0700
@@ -4,6 +4,8 @@
 		empty? <- { true }
 		fold:with <- :acc :fun { acc }
 		foldr:with <- :acc :fun { acc }
+		filter <- :pred { self }
+		foreach <- :self fun { self }
 		map <- :fun { self }
 		| <- :val {
 			list node: val withTail: self
@@ -38,6 +40,11 @@
 				foldr:with <- :acc fun {
 					fun: (_tail foldr: acc with: fun) _val
 				}
+				filter <- :pred {
+					reverse: (fold: [] with: :acc el {
+						if: (pred: el) { el | acc } else: { acc }
+					})
+				}
 				map <- :fun {
 					node: (fun: _val) withTail: (_tail map: fun)
 				}
--- a/modules/string.tp	Fri Aug 01 18:56:39 2014 -0700
+++ b/modules/string.tp	Fri Aug 01 18:56:50 2014 -0700
@@ -211,6 +211,12 @@
 			start <- bytes
 		}
 		clampedLen <- tocopy num
+		if: clampedLen < 0 {
+			clampedLen <- bytes - clampedLen
+			if: clampedLen < 0 {
+				clampedLen <- 0
+			}
+		}
 		if: start + clampedLen > bytes {
 			clampedLen <- bytes - start
 		}
@@ -219,6 +225,7 @@
 		memcpy: (ret data) data + start clampedLen
 		ret len!: clampedLen
 		ret bytes!: clampedLen
+		(ret data) set: clampedLen 0
 		ret
 	}
 
@@ -292,16 +299,22 @@
 		} do: {
 			start <- start + 1
 		}
-		end <- l
+		end <- l - 1
 		while: {
-			if: end > 0 {
-				b <- byte: end - 1
+			if: end > start {
+				b <- byte: end
 				b = space || b = tab || b = nl || b = cr
 			}
 		} do: {
 			end <- end - 1
 		}
-		from: start withLength: (end - start)
+		from: start withLength: (end + 1 - start)
+	}
+	
+	startsWith? <- :prefix {
+		if: (prefix length) <= length {
+			0 = (compareSub: prefix 0 0 (prefix length))
+		}
 	}
 
 	endsWith? <- :suffix {
--- a/modules/symbols.tp	Fri Aug 01 18:56:39 2014 -0700
+++ b/modules/symbols.tp	Fri Aug 01 18:56:50 2014 -0700
@@ -3,6 +3,9 @@
 		find:else <- :_ :else {
 			else:
 		}
+		ifDefined:else <- :name ifdef :else {
+			else:
+		}
 	}
 	_local <- 0
 	_closedover <- 1
@@ -19,17 +22,63 @@
 			name <- { _name }
 			id <- { _id }
 			string <- { "method " . _name . "(" . _id . ")" }
+			isMethod? <- { true }
+			isLocal? <- { false }
+		}
+	}
+	_local <- :_name _def {
+		#{
+			name <- { _name }
+			string <- { "local " . _name }
+			def <- { _def }
+			isMethod? <- { false }
+			closedOver? <- false
+			isLocal? <- { true }
+		}
+	}
+	_upvar <- :parent {
+		if: (parent isLocal?) {
+			parent closedOver?!: true
+			#{
+				name <- { parent name }
+				def <- { parent def }
+				closedOver? <- { parent closedOver? }
+				isMethod? <- { parent isMethod? }
+				string <- { "upvar " . name}
+				isLocal? <- { false }
+				depth <- 1
+			}
+		} else: {
+			parent depth!: (parent depth) + 1
 		}
 	}
 	#{
 		nullTable <- { _null }
 
-		tablewithParent <- :_parent {
+		tableWithParent <- :_parent {
 			_symbols <- dict hash
 			#{
+				ifDefined:else <- :name ifdef :else {
+					_symbols ifget: name :sym {
+						ifdef: sym
+					} else: {
+						_parent ifDefined: name :sym {
+							ifdef: (_upvar: sym)
+						} else: else
+					}
+				}
 				find:else <- :name :else {
 					_symbols get: name else: {
-						_parent find: name else: else
+						_parent ifDefined: name :sym {
+							if: (sym isMethod?) {
+								//TODO: methods on parent objects
+								sym
+							} else: {
+								_upvar: sym
+							}
+						} else: {
+							else:
+						}
 					}
 				}
 				defineMethod <- :name {
@@ -38,6 +87,16 @@
 					}
 					self
 				}
+				define <- :name def {
+					s <- (_local: name def)
+					_symbols set: name s
+					s
+				}
+				find:elseDefine <- :name :def {
+					find: name else: {
+						define: name def
+					}
+				}
 				print <- {
 					foreach: _symbols :name info {
 						print: name . ": " . info . "\n"
@@ -47,7 +106,7 @@
 		}
 
 		table <- {
-			tablewithParent: _null
+			tableWithParent: _null
 		}
 
 		buildMethodTable <- :tree {
--- a/samples/stringops.tp	Fri Aug 01 18:56:39 2014 -0700
+++ b/samples/stringops.tp	Fri Aug 01 18:56:50 2014 -0700
@@ -21,6 +21,8 @@
 		print: (string: ("12abcDEF" parseHex32)) . "\n"
 		print: (string: ("FFFFFFFFFF" parseHex64)) . "\n"
 		print: "'" . (" 	\nfoobar baz  " ltrim) . "'\n"
+		print: "'" . (" 	\nfoobar baz  " trim) . "'\n"
+		print: "'" . (" 	\n\t\r" trim) . "'\n"
 		print: (72 utf8) . (101 utf8) . (108 utf8) . (108 utf8) . (111 utf8) . (0x1F604 utf8) . "\n"
 	}
 }