shithub: mc

Download patch

ref: f4a939a2cfdc00643b02c404b0acbed382964dd0
parent: b4252aeded2d15592fe940e20f533f113000c61d
author: Ori Bernstein <[email protected]>
date: Sat Dec 30 14:23:12 EST 2017

Start cleaning up libstd.

	minor formatting, factoring out iterutil.

--- a/lib/bld.sub
+++ b/lib/bld.sub
@@ -6,6 +6,7 @@
 	fileutil
 	http
 	inifile
+	iter
 	json
 	regex
 	std
--- /dev/null
+++ b/lib/iter/bld.sub
@@ -1,0 +1,7 @@
+lib iter =
+	chunk.myr
+	enum.myr
+	ref.myr
+	reverse.myr
+	zip.myr
+;;
--- /dev/null
+++ b/lib/iter/chunk.myr
@@ -1,0 +1,34 @@
+use std
+
+pkg iter =
+	type chunkiter(@a) = struct
+		idx	: std.size
+		blksz	: std.size
+		sl	: @a[:]
+	;;
+
+	impl iterable chunkiter(@a) -> @a[:]
+	generic bychunk	: (a : @a[:], chunk : std.size -> chunkiter(@a))
+;;
+
+generic bychunk = {a, sz
+	-> [.sl = a, .idx = 0, .blksz = sz]
+}
+
+impl iterable chunkiter(@a) -> @a[:] =
+	__iternext__ = {itp, valp
+		var len
+
+		len = std.min(itp.blksz, itp.sl.len - itp.idx)
+		if itp.idx + len == itp.sl.len
+			-> false
+		;;
+		valp# = itp.sl[itp.idx: itp.idx + len]
+		itp.idx += itp.blksz
+		-> true
+	}
+
+	__iterfin__ = {itp, valp
+	}
+;;
+
--- /dev/null
+++ b/lib/iter/enum.myr
@@ -1,0 +1,30 @@
+use std
+
+pkg iter =
+	type enumiter(@a) = struct
+		idx	: std.size
+		sl	: @a[:]
+	;;
+
+	impl iterable enumiter(@a) -> (std.size, @a)
+	generic byenum	: (a : @a[:] -> enumiter(@a))
+;;
+
+generic byenum = {a
+	-> [.sl = a, .idx = 0]
+}
+
+impl iterable enumiter(@a) -> (std.size, @a) =
+	__iternext__ = {itp, valp
+		if itp.idx == itp.sl.len
+			-> false
+		;;
+		valp# = (itp.idx, itp.sl[itp.idx])
+		itp.idx++
+		-> true
+	}
+
+	__iterfin__ = {itp, valp
+	}
+;;
+
--- /dev/null
+++ b/lib/iter/ref.myr
@@ -1,0 +1,29 @@
+use std
+
+pkg iter =
+	type refiter(@a) = struct
+		idx	: std.size
+		sl	: @a[:]
+	;;
+	impl iterable refiter(@a) -> @a#
+	generic byref 	: (sl : @a[:] -> refiter(@a))
+;;
+
+generic byref = {a
+	-> [.sl = a, .idx = 0]
+}
+
+impl iterable refiter(@a) -> @a# =
+	__iternext__ = {itp, valp
+		if itp.idx == itp.sl.len
+			-> false
+		;;
+		valp# = &itp.sl[itp.idx]
+		itp.idx++
+		-> true
+	}
+
+	__iterfin__ = {itp, valp
+	}
+;;
+
--- /dev/null
+++ b/lib/iter/reverse.myr
@@ -1,0 +1,30 @@
+use std
+
+pkg iter =
+	type reverseiter(@a) = struct
+		sl	: @a[:]
+		idx	: std.size
+	;;
+	impl iterable reverseiter(@a) -> @a
+	generic byreverse 	: (sl : @a[:] -> reverseiter(@a))
+;;
+
+generic byreverse = {sl
+	-> [.sl = sl, .idx = sl.len]
+}
+
+impl iterable reverseiter(@a) -> @a =
+	__iternext__ = {itp, valp
+		if itp.idx > 0
+			valp# = itp.sl[itp.idx - 1]
+			itp.idx--
+			-> true
+		else
+			-> false
+		;;
+	}
+
+	__iterfin__ = {itp, valp
+	}
+;;
+
--- /dev/null
+++ b/lib/iter/test/enum.myr
@@ -1,0 +1,12 @@
+use std
+
+const main = {
+	var n
+
+	n = 0
+	for (x, i) : std.byenum([1,3,5,7,9][:])
+		std.assert(x == n, "invalid enum idx {}", x)
+		std.assert(i == n*2 + 1, "invalid enum val {}", i)
+		n++
+	;;
+}
--- /dev/null
+++ b/lib/iter/test/reverse.myr
@@ -1,0 +1,14 @@
+use std
+use iter
+
+const main = {
+	var n, l : int[:]
+
+	n = 0
+	l = [3, 2, 1, 0][:]
+	for x : iter.byreverse(l)
+		std.assert(x == n, "invalid reversed value {}, expected {}", x, n)
+		n++
+	;;
+
+}
--- /dev/null
+++ b/lib/iter/test/zip.myr
@@ -1,0 +1,15 @@
+use std
+use iter
+
+const main = {
+	var n, la : int[:], lb : int[:]
+
+	n = 0
+	la = [0,2,4,6,8][:]
+	lb = [2,4][:]
+	for (a, b) : iter.byzip(la, lb)
+		std.assert(a == n*2, "invalid val from a: {}", a)
+		std.assert(b == n*2 + 2, "invalid val from b: {}", b)
+		n++
+	;;
+}
--- /dev/null
+++ b/lib/iter/zip.myr
@@ -1,0 +1,30 @@
+pkg iter =
+	type zipiter(@a, @b) = struct
+		s1	: @a[:]
+		s2	: @b[:]
+	;;
+
+	impl iterable zipiter(@a, @b) -> (@a, @b)
+	generic byzip	: (a : @a[:], b : @b[:]	 -> zipiter(@a, @b))
+;;
+
+generic byzip = {a, b
+	-> [.s1 = a, .s2 = b]
+}
+
+impl iterable zipiter(@a, @b) -> (@a, @b) =
+	__iternext__ = {itp, valp
+		if itp.s1.len > 0 && itp.s2.len > 0
+			valp# = (itp.s1[0], itp.s2[0])
+			itp.s1 = itp.s1[1:]
+			itp.s2 = itp.s2[1:]
+			-> true
+		else
+			-> false
+		;;
+	}
+
+	__iterfin__ = {itp, valp
+	}
+;;
+
--- a/lib/std/alloc.myr
+++ b/lib/std/alloc.myr
@@ -31,15 +31,15 @@
 */
 
 pkg std =
-	generic alloc	: (		-> @a#)
-	generic zalloc	: (		-> @a#)
-	generic free	: (v:@a#	-> void)
+	generic alloc		: ( -> @a#)
+	generic zalloc		: ( -> @a#)
+	generic free		: (v:@a# -> void)
 
-	generic slalloc	: (len : size	-> @a[:])
-	generic slzalloc	: (len : size	-> @a[:])
-	generic slgrow	: (sl : @a[:]#, len : size	-> @a[:])
-	generic slzgrow	: (sl : @a[:]#, len : size	-> @a[:])
-	generic slfree	: (sl : @a[:]	-> void)
+	generic slalloc		: (len : size -> @a[:])
+	generic slzalloc	: (len : size -> @a[:])
+	generic slgrow		: (sl : @a[:]#, len : size -> @a[:])
+	generic slzgrow		: (sl : @a[:]#, len : size -> @a[:])
+	generic slfree		: (sl : @a[:] -> void)
 ;;
 
 type slheader = struct
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -38,7 +38,6 @@
 	intparse.myr
 	introspect.myr
 	ipparse.myr
-	iterutil.myr
 	mk.myr
 	mkpath.myr
 	mktemp.myr
--- a/lib/std/bytealloc.myr
+++ b/lib/std/bytealloc.myr
@@ -1,4 +1,5 @@
 use sys
+
 use "die"
 use "extremum"
 use "memops"
@@ -14,17 +15,16 @@
 	const startalloctrace	: (f : byte[:] -> void)
 	const endalloctrace	: (-> void)
 
-	/* public for testing */
-	pkglocal const zbytealloc	: (sz:size -> byte#)
-	const bytealloc			: (sz:size -> byte#)
-	const bytefree			: (m:byte#, sz:size -> void)
-
 	/* null pointers. only used internally. */
 	pkglocal const Zsliceptr	= (0 : byte#)
 	pkglocal const Align		= 16	/* minimum allocation alignment */
 
-	pkglocal const align	: (m : std.size, align : std.size -> std.size)
-	pkglocal const allocsz	: (sz : std.size -> std.size)
+	/* exported for testing */
+	const bytealloc			: (sz:size -> byte#)
+	const bytefree			: (m:byte#, sz:size -> void)
+	pkglocal const zbytealloc	: (sz:size -> byte#)
+	pkglocal const align		: (m : std.size, align : std.size -> std.size)
+	pkglocal const allocsz		: (sz : std.size -> std.size)
 
 ;;
 
--- a/lib/std/clear.myr
+++ b/lib/std/clear.myr
@@ -1,4 +1,5 @@
 use "slfill"
+
 pkg std =
 	generic clear	: (p : @a#	-> void)
 ;;
--- a/lib/std/cmp.myr
+++ b/lib/std/cmp.myr
@@ -10,9 +10,9 @@
 		`After
 	;;
 
-	generic numcmp	: (a : @a, b : @a -> order)
-	const strcmp	: (a : byte[:], b : byte[:] -> order)
-	const strncmp	: (a : byte[:], b : byte[:], n : size -> order)
+	generic numcmp		: (a : @a, b : @a -> order)
+	const strcmp		: (a : byte[:], b : byte[:] -> order)
+	const strncmp		: (a : byte[:], b : byte[:], n : size -> order)
 	const strcasecmp	: (a : byte[:], b : byte[:] -> order)
 ;;
 
--- a/lib/std/dialparse+posixy.myr
+++ b/lib/std/dialparse+posixy.myr
@@ -13,8 +13,8 @@
 
 pkg std =
 	pkglocal const parseport	: (port : byte[:] -> option(uint16))
-	pkglocal const getaddr	: (addr : byte[:] -> result(netaddr, byte[:]))
-	pkglocal const nameseg	: (str : byte[:] -> option((byte[:], byte[:])))
+	pkglocal const getaddr		: (addr : byte[:] -> result(netaddr, byte[:]))
+	pkglocal const nameseg		: (str : byte[:] -> option((byte[:], byte[:])))
 ;;
 
 const parseport = {port
--- a/lib/std/fltfmt.myr
+++ b/lib/std/fltfmt.myr
@@ -10,7 +10,7 @@
 use "memops"
 
 pkg std =
-	pkglocal const MNormal = 0
+	pkglocal const MNormal	= 0
 	pkglocal const MAbsolute = 1
 	pkglocal const MRelative = 2
 
--- a/lib/std/getcwd.myr
+++ b/lib/std/getcwd.myr
@@ -1,9 +1,9 @@
-
 use "alloc"
 use "errno"
 use "extremum"
 use "syswrap"
 use "syswrap-ss"
+
 pkg std =
 	const getcwd : (-> byte[:])
 ;;
--- a/lib/std/hasprefix.myr
+++ b/lib/std/hasprefix.myr
@@ -1,4 +1,5 @@
 use "cmp"
+
 pkg std =
 	const hasprefix	: (s : byte[:], pre : byte[:] -> bool)
 ;;
--- a/lib/std/hassuffix.myr
+++ b/lib/std/hassuffix.myr
@@ -1,4 +1,5 @@
 use "sleq"
+
 pkg std =
 	const hassuffix	: (s : byte[:], suff : byte[:] -> bool)
 ;;
--- a/lib/std/ipparse.myr
+++ b/lib/std/ipparse.myr
@@ -15,7 +15,6 @@
 use "utf"
 
 pkg std =
-
 	type netaddr = union
 		`Ipv4	byte[4]
 		`Ipv6	byte[16]
--- a/lib/std/iterutil.myr
+++ /dev/null
@@ -1,140 +1,0 @@
-use "types"
-use "fmt"
-use "extremum"
-
-pkg std =
-	type zipiter(@a, @b) = struct
-		s1	: @a[:]
-		s2	: @b[:]
-	;;
-
-	type reverseiter(@a) = struct
-		sl	: @a[:]
-		idx	: size
-	;;
-
-	type enumiter(@a) = struct
-		idx	: size
-		sl	: @a[:]
-	;;
-
-	type refiter(@a) = struct
-		idx	: size
-		sl	: @a[:]
-	;;
-
-	type chunkiter(@a) = struct
-		idx	: size
-		blksz	: size
-		sl	: @a[:]
-	;;
-
-	impl iterable zipiter(@a, @b) -> (@a, @b)
-	impl iterable enumiter(@a) -> (size, @a)
-	impl iterable reverseiter(@a) -> @a
-	impl iterable refiter(@a) -> @a#
-	impl iterable chunkiter(@a) -> @a[:]
-
-	generic byzip	: (a : @a[:], b : @b[:]	 -> zipiter(@a, @b))
-	generic byenum	: (a : @a[:] -> enumiter(@a))
-	generic byreverse 	: (sl : @a[:] -> reverseiter(@a))
-	generic byref 	: (sl : @a[:] -> refiter(@a))
-	generic bychunk	: (a : @a[:], chunk : size -> chunkiter(@a))
-;;
-
-generic byzip = {a, b
-	-> [.s1 = a, .s2 = b]
-}
-
-impl iterable zipiter(@a, @b) -> (@a, @b) =
-	__iternext__ = {itp, valp
-		if itp.s1.len > 0 && itp.s2.len > 0
-			valp# = (itp.s1[0], itp.s2[0])
-			itp.s1 = itp.s1[1:]
-			itp.s2 = itp.s2[1:]
-			-> true
-		else
-			-> false
-		;;
-	}
-
-	__iterfin__ = {itp, valp
-	}
-;;
-
-generic byreverse = {sl
-	-> [.sl = sl, .idx = sl.len]
-}
-
-impl iterable reverseiter(@a) -> @a =
-	__iternext__ = {itp, valp
-		if itp.idx > 0
-			valp# = itp.sl[itp.idx - 1]
-			itp.idx--
-			-> true
-		else
-			-> false
-		;;
-	}
-
-	__iterfin__ = {itp, valp
-	}
-;;
-
-generic byenum = {a
-	-> [.sl = a, .idx = 0]
-}
-
-impl iterable enumiter(@a) -> (size, @a) =
-	__iternext__ = {itp, valp
-		if itp.idx == itp.sl.len
-			-> false
-		;;
-		valp# = (itp.idx, itp.sl[itp.idx])
-		itp.idx++
-		-> true
-	}
-
-	__iterfin__ = {itp, valp
-	}
-;;
-
-generic byref = {a
-	-> [.sl = a, .idx = 0]
-}
-
-impl iterable refiter(@a) -> @a# =
-	__iternext__ = {itp, valp
-		if itp.idx == itp.sl.len
-			-> false
-		;;
-		valp# = &itp.sl[itp.idx]
-		itp.idx++
-		-> true
-	}
-
-	__iterfin__ = {itp, valp
-	}
-;;
-
-generic bychunk = {a, sz
-	-> [.sl = a, .idx = 0, .blksz = sz]
-}
-
-impl iterable chunkiter(@a) -> @a[:] =
-	__iternext__ = {itp, valp
-		var len
-
-		len = min(itp.blksz, itp.sl.len - itp.idx)
-		if itp.idx + len == itp.sl.len
-			-> false
-		;;
-		valp# = itp.sl[itp.idx: itp.idx + len]
-		itp.idx += itp.blksz
-		-> true
-	}
-
-	__iterfin__ = {itp, valp
-	}
-;;
-
--- a/lib/std/listen+posixy.myr
+++ b/lib/std/listen+posixy.myr
@@ -23,7 +23,6 @@
 
 	const announce	: (ds : byte[:] -> result(announce#, byte[:]))
 	const aclose	: (a : announce# -> void)
-
 	const accept	: (a : announce# -> result(fd, byte[:]))
 ;;
 
--- a/lib/std/mktemp.myr
+++ b/lib/std/mktemp.myr
@@ -13,10 +13,10 @@
 use "types"
 
 pkg std =
-	const mktemp	: (base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
-	const mktempat	: (dir : byte[:], base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
-	const mkdtemp	: (base : byte[:], mode : int64 -> std.result(byte[:], errno))
-	const mkdtempat	: (dir : byte[:], base : byte[:], mode : int64 -> std.result(byte[:], errno))
+	const mktemp		: (base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
+	const mktempat		: (dir : byte[:], base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
+	const mkdtemp		: (base : byte[:], mode : int64 -> std.result(byte[:], errno))
+	const mkdtempat		: (dir : byte[:], base : byte[:], mode : int64 -> std.result(byte[:], errno))
 	const mktemppath	: (base : byte[:] -> byte[:])
 ;;
 
--- a/lib/std/resolve+posixy.myr
+++ b/lib/std/resolve+posixy.myr
@@ -58,8 +58,8 @@
 		rtype	: rectype
 	;;
 
-	const resolve	: (host : byte[:]	-> result(hostinfo[:], resolveerr))
-	const resolvemx	: (host : byte[:]	-> result(hostinfo[:], resolveerr))
+	const resolve		: (host : byte[:]	-> result(hostinfo[:], resolveerr))
+	const resolvemx		: (host : byte[:]	-> result(hostinfo[:], resolveerr))
 	const resolverec	: (host : byte[:], t : rectype[:]	-> result(hostinfo[:], resolveerr))
 ;;
 
--- a/lib/std/slpush.myr
+++ b/lib/std/slpush.myr
@@ -7,12 +7,12 @@
 
 generic slpush = {sl, elt
 	/*
-	slpush relies on implementation details
-	of slgrow for efficiency. Because bucket
-	sizes come in powers of two for all buckets
-	<= 32k, and by powers of 1.5 for larger
-	slices, this is effectively growing the
-	slice by powers of two.
+	  slpush relies on implementation details
+	  of slgrow for efficiency. Because bucket
+	  sizes come in powers of two for all buckets
+	  <= 32k, and by powers of 1.5 for larger
+	  slices, this is effectively growing the
+	  slice by powers of two.
 	*/
 	slgrow(sl, sl#.len + 1)
 	sl#[sl#.len - 1] = elt
--- a/lib/std/slput.myr
+++ b/lib/std/slput.myr
@@ -3,8 +3,8 @@
 use "die"
 
 pkg std =
-	generic slput	: (sl : @a[:]#, idx : size, elt : @a	-> @a[:])
-	generic sldel	: (sl : @a[:]#, idx : size	-> @a[:])
+	generic slput	: (sl : @a[:]#, idx : size, elt : @a -> @a[:])
+	generic sldel	: (sl : @a[:]#, idx : size -> @a[:])
 ;;
 
 generic slput = {sl, idx, elt
--- a/lib/std/slurp.myr
+++ b/lib/std/slurp.myr
@@ -7,8 +7,8 @@
 use "types"
 
 pkg std =
-	const slurp : (path : byte[:] -> result(byte[:], errno))
-	const fslurp : (path : fd -> result(byte[:], errno))
+	const slurp	: (path : byte[:] -> result(byte[:], errno))
+	const fslurp	: (path : fd -> result(byte[:], errno))
 ;;
 
 const Bufstart = 4096
--- a/lib/std/test/iterutil.myr
+++ /dev/null
@@ -1,26 +1,0 @@
-use std
-
-const main = {
-	var n
-
-	n = 0
-	for (x, i) : std.byenum([1,3,5,7,9][:])
-		std.assert(x == n, "invalid enum idx {}", x)
-		std.assert(i == n*2 + 1, "invalid enum val {}", i)
-		n++
-	;;
-
-	n = 0
-	for (a, b) : std.byzip([0,2,4,6,8][:], [2,4][:])
-		std.assert(a == n*2, "invalid val from a: {}", a)
-		std.assert(b == n*2 + 2, "invalid val from b: {}", b)
-		n++
-	;;
-
-	n = 0
-	for x : std.byreverse([3, 2, 1, 0][:])
-		std.assert(x == n, "invalid reversed value {}, expected {}", x, n)
-		n++
-	;;
-
-}
--- a/lib/std/try.myr
+++ b/lib/std/try.myr
@@ -3,10 +3,10 @@
 use "fmt"
 
 pkg std =
-	generic try : (v : result(@a, @b) -> @a)
-	generic tryv : (v : result(@a, @b), d : @a -> @a)
-	generic get : (v : option(@a) -> @a)
-	generic getv : (v : option(@a), d : @a -> @a)
+	generic try	: (v : result(@a, @b) -> @a)
+	generic tryv	: (v : result(@a, @b), d : @a -> @a)
+	generic get	: (v : option(@a) -> @a)
+	generic getv	: (v : option(@a), d : @a -> @a)
 	generic canget	: (o : option(@a) -> bool)
 ;;
 
--- a/lib/std/varargs.myr
+++ b/lib/std/varargs.myr
@@ -6,12 +6,12 @@
 pkg std =
 	type valist
 
-	const vastart	: (args : ...# -> valist)
-	const vatype	: (ap : valist# -> byte[:])
-	const vabytes	: (ap : valist# -> byte[:])
-	const vaenter	: (ap : valist# -> valist)
+	const vastart		: (args : ...# -> valist)
+	const vatype		: (ap : valist# -> byte[:])
+	const vabytes		: (ap : valist# -> byte[:])
+	const vaenter		: (ap : valist# -> valist)
 	const vaenterunion	: (ap : valist#, elt : int32 -> valist)
-	generic vanext	: (ap : valist# -> @a)
+	generic vanext		: (ap : valist# -> @a)
 ;;
 
 type valist = struct
--- a/lib/std/writeall.myr
+++ b/lib/std/writeall.myr
@@ -14,10 +14,8 @@
 	sz = src.len
 	while src.len != 0
 		match std.write(fd, src)
-		| `Ok n:
-			src = src[n:]
-		| `Err e:
-			-> (sz - src.len, `Some e)
+		| `Ok n:	src = src[n:]
+		| `Err e:	-> (sz - src.len, `Some e)
 		;;
 	;;
 	-> (sz, `None)