summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-10-02 23:56:21 -0700
committerOri Bernstein <ori@eigenstate.org>2015-10-02 23:56:21 -0700
commit9dbef7492971df74e2ecd58ee02f5fe69e979149 (patch)
treeed000fb05640fea2ba8776fa5eb63417db816296
parent08442dd9c3426e2dbe598ef1f6fb10f2f67638aa (diff)
downloadmc-9dbef7492971df74e2ecd58ee02f5fe69e979149.tar.gz
Make assert able to take format args.
This is useful outside of libstd. Libstd mostly can't use it due to dependency loops, so we get iassert for that.
-rw-r--r--lib/std/alloc.myr2
-rw-r--r--lib/std/assert.myr18
-rw-r--r--lib/std/bigint.myr4
-rw-r--r--lib/std/bld.sub1
-rw-r--r--lib/std/die.myr5
-rw-r--r--lib/std/fmt.myr4
-rw-r--r--lib/std/putint.myr2
-rw-r--r--lib/std/rand.myr1
-rw-r--r--lib/std/slcp.myr2
-rw-r--r--lib/std/strbuf.myr2
-rw-r--r--lib/std/test/slcp.myr6
11 files changed, 32 insertions, 15 deletions
diff --git a/lib/std/alloc.myr b/lib/std/alloc.myr
index 5f4c4f6..91e0440 100644
--- a/lib/std/alloc.myr
+++ b/lib/std/alloc.myr
@@ -142,7 +142,7 @@ const checkhdr = {p
addr = p castto(size)
addr -= align(sizeof(slheader), Align)
phdr = addr castto(slheader#)
- assert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
+ iassert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
}
/* Frees a slice */
diff --git a/lib/std/assert.myr b/lib/std/assert.myr
new file mode 100644
index 0000000..58ad9d3
--- /dev/null
+++ b/lib/std/assert.myr
@@ -0,0 +1,18 @@
+use "fmt.use"
+use "syswrap.use"
+use "varargs.use"
+
+pkg std =
+ const assert : (cond : bool, fmt : byte[:], args : ... -> void)
+;;
+
+const assert = {cond, msg, args
+ var ap
+
+ ap = vastart(&args)
+ if !cond
+ std.fputv(2, msg, &ap)
+ suicide()
+ ;;
+}
+
diff --git a/lib/std/bigint.myr b/lib/std/bigint.myr
index db09e66..0e22fca 100644
--- a/lib/std/bigint.myr
+++ b/lib/std/bigint.myr
@@ -647,7 +647,7 @@ generic bigshli = {a, s : @a::(numeric,integral)
var off, shift
var t, carry
- assert(s >= 0, "shift amount must be positive")
+ iassert(s >= 0, "shift amount must be positive")
off = (s castto(uint64)) / 32
shift = (s castto(uint64)) % 32
@@ -678,7 +678,7 @@ generic bigshri = {a, s
var off, shift
var t, carry
- assert(s >= 0, "shift amount must be positive")
+ iassert(s >= 0, "shift amount must be positive")
off = (s castto(uint64)) / 32
shift = (s castto(uint64)) % 32
diff --git a/lib/std/bld.sub b/lib/std/bld.sub
index 1fc4578..6609263 100644
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -3,6 +3,7 @@ lib std {inc=.} =
# portable files
alloc.myr
+ assert.myr
bigint.myr
bitset.myr
blat.myr
diff --git a/lib/std/die.myr b/lib/std/die.myr
index 87135f1..ababd82 100644
--- a/lib/std/die.myr
+++ b/lib/std/die.myr
@@ -3,7 +3,7 @@ use "types.use"
pkg std =
$noret const die : (msg : byte[:] -> void)
- const assert : (cond : bool, msg : byte[:] -> void)
+ pkglocal const iassert : (cond : bool, msg : byte[:] -> void)
;;
const die = {msg
@@ -11,9 +11,8 @@ const die = {msg
suicide()
}
-const assert = {cond, msg
+const iassert = {cond, msg
if !cond
die(msg)
;;
}
-
diff --git a/lib/std/fmt.myr b/lib/std/fmt.myr
index 70b0f7f..0b66bb5 100644
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -440,7 +440,7 @@ const intparams = {params
| _: std.die("unreachable")
;;
;;
- std.assert(ip.padto >= 0, "pad must be >= 0")
+ iassert(ip.padto >= 0, "pad must be >= 0")
std.slfree(opts)
-> ip
}
@@ -461,7 +461,7 @@ const strfmt = {sb, str, params
| _: std.die("unreachable")
;;
;;
- std.assert(p >= 0, "pad must be >= 0")
+ iassert(p >= 0, "pad must be >= 0")
std.slfree(opts)
for i = 0; i < w - graphemewidth(str); i++
sbputc(sb, p)
diff --git a/lib/std/putint.myr b/lib/std/putint.myr
index 9ff92dd..2bf7b12 100644
--- a/lib/std/putint.myr
+++ b/lib/std/putint.myr
@@ -1,5 +1,5 @@
+use "assert.use"
use "types.use"
-use "die.use"
pkg std =
generic putle64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
diff --git a/lib/std/rand.myr b/lib/std/rand.myr
index 89858db..351bf8d 100644
--- a/lib/std/rand.myr
+++ b/lib/std/rand.myr
@@ -1,4 +1,5 @@
use "die.use"
+use "assert.use"
use "types.use"
use "alloc.use"
use "now.use"
diff --git a/lib/std/slcp.myr b/lib/std/slcp.myr
index a099bf3..5e40702 100644
--- a/lib/std/slcp.myr
+++ b/lib/std/slcp.myr
@@ -7,6 +7,6 @@ pkg std =
;;
generic slcp = {a : @a[:], b : @a[:]
- assert(a.len == b.len, "arguments to slcp() must be of equal length\n")
+ iassert(a.len == b.len, "arguments to slcp() must be of equal length\n")
memblit(a castto(byte#), b castto(byte#), a.len * sizeof(@a))
}
diff --git a/lib/std/strbuf.myr b/lib/std/strbuf.myr
index 097d1ce..3c4b51c 100644
--- a/lib/std/strbuf.myr
+++ b/lib/std/strbuf.myr
@@ -87,7 +87,7 @@ const sbputb = {sb, v
}
const sbtrim = {sb, len
- assert(abs(len) <= sb.len, "trim out of range\n")
+ iassert(abs(len) <= sb.len, "trim out of range\n")
if len < 0
sb.len -= abs(len)
else
diff --git a/lib/std/test/slcp.myr b/lib/std/test/slcp.myr
index 8eea596..006e2d0 100644
--- a/lib/std/test/slcp.myr
+++ b/lib/std/test/slcp.myr
@@ -9,8 +9,6 @@ const main = {
std.slcp(a[:a.len-2], a[2:])
std.slcp(b[2:], b[:b.len-2])
- std.put("a: {}, a_cped: {}\n", a[:], a_cped[:])
- std.put("b: {}, b_cped: {}\n", b[:], b_cped[:])
- std.assert(std.sleq(a[:], a_cped[:]), "slcp of a failed")
- std.assert(std.sleq(b[:], b_cped[:]), "slcp of b failed")
+ std.assert(std.sleq(a[:], a_cped[:]), "slcp of a failed: got {}, wanted {}", a[:], a_cped[:])
+ std.assert(std.sleq(b[:], b_cped[:]), "slcp of b failed: got {}, wanted {}", a[:], a_cped[:])
}