summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2013-04-03 17:05:14 -0400
committerOri Bernstein <ori@eigenstate.org>2013-04-03 17:05:14 -0400
commit2a3b64a5642729ab3bf83a21ea6bf59fc2a88f2c (patch)
treefd4acf8ac0990b1bf299422dec90ca04de35259e
parent480b380507201923c5b0d4e8610499e23ee2bea6 (diff)
downloadmc-2a3b64a5642729ab3bf83a21ea6bf59fc2a88f2c.tar.gz
Add functions for buffered formatting.
-rw-r--r--libstd/alloc.myr1
-rw-r--r--libstd/fmt.myr48
2 files changed, 36 insertions, 13 deletions
diff --git a/libstd/alloc.myr b/libstd/alloc.myr
index 7ab8a68..2b91e9b 100644
--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -2,7 +2,6 @@ use "die.use"
use "sys.use"
use "types.use"
use "extremum.use"
-use "fmt.use"
/*
The allocator implementation here is based on Bonwick's slab allocator.
diff --git a/libstd/fmt.myr b/libstd/fmt.myr
index 4b2f51a..ae2699a 100644
--- a/libstd/fmt.myr
+++ b/libstd/fmt.myr
@@ -1,3 +1,4 @@
+use "alloc.use"
use "die.use"
use "sys.use"
use "types.use"
@@ -19,36 +20,59 @@ use "extremum.use"
*/
pkg std =
- const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> size)
- const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist -> size)
const put : (fmt : byte[:], args : ... -> size)
const putv : (fmt : byte[:], ap : valist -> size)
const fatal : (status : int, fmt : byte[:], args : ... -> void)
const fatalv : (status : int, fmt : byte[:], ap : valist -> void)
+ const fmt : (fmt : byte[:], args : ... -> byte[:])
+ const fmtv : (fmt : byte[:], ap : valist -> byte[:])
+ const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> size)
+ const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist -> size)
;;
+/* Writes a string of text up to 2 kb in size to stdout */
+const put = {fmt, args
+ -> putv(fmt, vastart(&args))
+}
+
+/* Writes a string of text up to 2kb long to stdout, using a valist
+ as the source of the arguments */
+const putv = {fmt, ap
+ var buf : byte[2048]
+ var n
+
+ n = bfmtv(buf[:], fmt, ap)
+ write(1, buf[:n])
+ -> n
+}
+
+/* same as 'put', but exits the program after printing */
const fatal = {status, fmt, args
putv(fmt, vastart(&args))
exit(status)
}
+/* same as 'putv', but exits the program after printing */
const fatalv = {status, fmt, ap
putv(fmt, ap)
exit(status)
}
-const put = {fmt, args
- -> putv(fmt, vastart(&args))
+/* formats a string, allocating the slice. FIXME: calculate the
+ size needed. */
+const fmt = {fmt, args
+ -> fmtv(fmt, vastart(&args))
}
-/* Writes a string of text up to 2 kb in size to stdout */
-const putv = {fmt, ap
- var buf : byte[2048]
- var n
-
- n = bfmtv(buf[:], fmt, ap)
- write(1, buf[:n])
- -> n
+/* formats a string, allocating the slice. FIXME: calculate the
+ size needed. Takes a valist as it's last argument. */
+const fmtv = {fmt, ap
+ var buf
+ var sz
+
+ buf = slalloc(2048)
+ sz = bfmtv(buf, fmt, ap)
+ -> buf[:sz]
}
/* formats a string of text as specified by 'fmt' into 'buf' */