shithub: mc

Download patch

ref: 627ef0fa8e965ac37ba79db1eae7e7d6b78c53b7
parent: bfb7664b7f66e0d9e406672fa92bdee35bced70b
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Sep 30 17:45:49 EDT 2015

Add support for duplicating functions.

--- a/lib/std/bitset.myr
+++ b/lib/std/bitset.myr
@@ -18,8 +18,8 @@
 
 	const bsmax	: (a : bitset# -> size)
 
-	generic bsput	: (bs : bitset#, v : @a::(integral,numeric) -> void)
-	generic bsdel	: (bs : bitset#, v : @a::(integral,numeric) -> void)
+	generic bsput	: (bs : bitset#, v : @a::(integral,numeric) -> bool)
+	generic bsdel	: (bs : bitset#, v : @a::(integral,numeric) -> bool)
 	generic bshas	: (bs : bitset#, v : @a::(integral,numeric) -> bool)
 
 	const bsdiff	: (a : bitset#, b : bitset# -> void)
@@ -55,25 +55,28 @@
 }
 
 generic bsput = {bs, v
-	var idx
-	var off
+	var idx, off, has
 
 	idx = (v castto(size)) / (8*sizeof(size))
 	off = (v castto(size)) % (8*sizeof(size))
 	ensurelen(bs, idx)
+
+	has = (bs.bits[idx] & (1 << off)) != 0
 	bs.bits[idx] |= (1 << off)
+	-> has
 }
 
 generic bsdel = {bs, v
-	var idx
-	var off
+	var idx, off, had
 
+	had = false
 	idx = (v castto(size)) / (8*sizeof(size))
 	off = (v castto(size)) % (8*sizeof(size))
-	if idx >= bs.bits.len
-		->
+	if idx < bs.bits.len
+		had = (bs.bits[idx] & (1 << off)) != 0
+		bs.bits[idx] &= ~(1 << off)
 	;;
-	bs.bits[idx] &= ~(1 << off)
+	-> had
 }
 
 generic bshas = {bs, v
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -20,6 +20,7 @@
 	fltfmt.myr
         fmt.myr
 	fmtfuncs.myr
+	fndup.myr
 	getcwd.myr
 	getint.myr
 	hashfuncs.myr
--- /dev/null
+++ b/lib/std/fndup.myr
@@ -1,0 +1,34 @@
+use "alloc.use"
+use "die.use"
+use "sldup.use"
+use "types.use"
+
+pkg std =
+	generic fndup	: (fn : @fn::function -> @fn::function)
+	generic fnfree	: (fn : @fn::function -> void)
+;;
+
+generic fndup = {fn
+	var repr : intptr[2]
+
+	repr = (&fn castto(intptr[2]#))#
+	repr[0] = sldup(envslice(repr[0])) castto(intptr)
+	-> (&repr castto(@fn::function#))#
+}
+
+generic fnfree = {fn
+	var repr : intptr[2]
+
+	repr = (&fn castto(intptr[2]#))#
+	std.slfree(envslice(repr[0]))
+}
+
+const envslice = {ep : intptr
+	var env : byte#
+	var szp : intptr#
+
+	env = ep castto(byte#)
+	szp = env castto(intptr#)
+	-> env[:szp#]
+}
+
--- a/mbld/deps.myr
+++ b/mbld/deps.myr
@@ -14,7 +14,7 @@
 	var usepat	: regex.regex#
 ;;
 
-const Abiversion = 7
+const Abiversion = 8
 
 var usepat	: regex.regex#
 
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -4,7 +4,7 @@
 #	define FATAL
 #endif
 
-#define Abiversion 7
+#define Abiversion 8
 
 typedef uint8_t         byte;
 typedef unsigned int    uint;
--- a/parse/trait.def
+++ b/parse/trait.def
@@ -4,3 +4,4 @@
 Tc(Tcfloat,     "floating")     /* behaves like a float, defaults to float as fallback */
 Tc(Tcidx,       "indexable")    /* indexable */
 Tc(Tcslice,     "sliceable")    /* sliceable */
+Tc(Tcfunc,      "function")     /* behaves like a function */
--- a/parse/type.c
+++ b/parse/type.c
@@ -904,6 +904,9 @@
     traits[Tyarray][0] = traittab[Tcidx];
     traits[Tyarray][1] = traittab[Tcslice];
 
+    /* @a::function */
+    traits[Tyfunc][0] = traittab[Tcfunc];
+
 /* Definining and registering the types has to go after we define the
  * constraints, otherwise they will have no constraints set on them. */
 #define Ty(t, n, stk) \