summaryrefslogtreecommitdiff
path: root/lib/std/fmt.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/fmt.myr')
-rw-r--r--lib/std/fmt.myr99
1 files changed, 79 insertions, 20 deletions
diff --git a/lib/std/fmt.myr b/lib/std/fmt.myr
index b7155f6..4178524 100644
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -283,43 +283,43 @@ const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
sbputc(sb, val)
| `Tyint8:
var val : int8 = vanext(ap)
- intfmt(sb, intparams(params), true, (val : uint64), 8)
+ intfmt(sb, intparams(ap, params), true, (val : uint64), 8)
| `Tyint16:
var val : int16 = vanext(ap)
- intfmt(sb, intparams(params), true, (val : uint64), 16)
+ intfmt(sb, intparams(ap, params), true, (val : uint64), 16)
| `Tyint:
var val : int = vanext(ap)
- intfmt(sb, intparams(params), true, (val : uint64), 32)
+ intfmt(sb, intparams(ap, params), true, (val : uint64), 32)
| `Tyint32:
var val : int32 = vanext(ap)
- intfmt(sb, intparams(params), true, (val : uint64), 32)
+ intfmt(sb, intparams(ap, params), true, (val : uint64), 32)
| `Tyint64:
var val : int64 = vanext(ap)
- intfmt(sb, intparams(params), true, (val : uint64), 64)
+ intfmt(sb, intparams(ap, params), true, (val : uint64), 64)
| `Tybyte:
var val : byte = vanext(ap)
- intfmt(sb, intparams(params), false, (val : uint64), 8)
+ intfmt(sb, intparams(ap, params), false, (val : uint64), 8)
| `Tyuint8:
var val : uint8 = vanext(ap)
- intfmt(sb, intparams(params), false, (val : uint64), 8)
+ intfmt(sb, intparams(ap, params), false, (val : uint64), 8)
| `Tyuint16:
var val : uint16 = vanext(ap)
- intfmt(sb, intparams(params), false, (val : uint64), 16)
+ intfmt(sb, intparams(ap, params), false, (val : uint64), 16)
| `Tyuint:
var val : uint = vanext(ap)
- intfmt(sb, intparams(params), false, (val : uint64), 32)
+ intfmt(sb, intparams(ap, params), false, (val : uint64), 32)
| `Tyuint32:
var val : uint32 = vanext(ap)
- intfmt(sb, intparams(params), false, (val : uint64), 32)
+ intfmt(sb, intparams(ap, params), false, (val : uint64), 32)
| `Tyuint64:
var val : uint64 = vanext(ap)
- intfmt(sb, intparams(params), false, (val : uint64), 64)
+ intfmt(sb, intparams(ap, params), false, (val : uint64), 64)
| `Tyflt32:
var val : flt32 = vanext(ap)
- flt32bfmt(sb, fltparams(params), val)
+ flt32bfmt(sb, fltparams(ap, params), val)
| `Tyflt64:
var val : flt64 = vanext(ap)
- flt64bfmt(sb, fltparams(params), val)
+ flt64bfmt(sb, fltparams(ap, params), val)
| `Tyvalist:
sbputs(sb, "...")
@@ -334,7 +334,7 @@ const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
match typedesc(desc)
| `Tybyte:
var val : byte[:] = vanext(ap)
- strfmt(sb, val, params)
+ strfmt(sb, val, ap, params)
| _:
subap = vaenter(ap)
fmtslice(sb, subap, params)
@@ -449,7 +449,7 @@ const fmtslice = {sb, subap, params
;;
}
-const fltparams = {params
+const fltparams = {ap, params
var fp : fltparams
fp = [
@@ -463,7 +463,12 @@ const fltparams = {params
for p : params
match p
| ("e", ""): fp.scientific = true
- | ("w", wid): fp.padto = getint(wid, "fmt: width must be integer")
+ | ("w", wid):
+ if eq(wid, "?")
+ fp.padto = pullint(ap, "fmt: width = ? must be integer")
+ else
+ fp.padto = getint(wid, "fmt: width must be integer")
+ ;;
| ("p", pad): fp.padfill = decode(pad)
| ("s", sig):
fp.mode = MRelative
@@ -480,7 +485,7 @@ const fltparams = {params
-> fp
}
-const intparams = {params
+const intparams = {ap, params
var ip : intparams
ip = [
@@ -493,7 +498,12 @@ const intparams = {params
match p
| ("b", bas): ip.base = getint(bas, "fmt: base must be integer")
| ("x", ""): ip.base = 16
- | ("w", wid): ip.padto = getint(wid, "fmt: width must be integer")
+ | ("w", wid):
+ if eq(wid, "?")
+ ip.padto = pullint(ap, "fmt: width = ? must be integer")
+ else
+ ip.padto = getint(wid, "fmt: width must be integer")
+ ;;
| ("p", pad): ip.padfill = decode(pad)
| (opt, arg):
std.write(2, "fmt: ")
@@ -507,7 +517,7 @@ const intparams = {params
-> ip
}
-const strfmt = {sb, str, params
+const strfmt = {sb, str, ap, params
var w, p, i, raw, esc
p = ' '
@@ -517,7 +527,12 @@ const strfmt = {sb, str, params
for pp : params
match pp
- | ("w", wid): w = getint(wid, "fmt: width must be integer")
+ | ("w", wid):
+ if eq(wid, "?")
+ w = pullint(ap, "fmt: width = ? must be integer")
+ else
+ w = getint(wid, "fmt: width must be integer")
+ ;;
| ("p", pad): p = decode(pad)
| ("r", ""): raw = true
| ("e", ""): esc = true
@@ -576,3 +591,47 @@ const getint = {s, msg
| `None: die(msg)
;;
}
+
+const pullint = {ap, msg
+ match typedesc(vatype(ap))
+ | `Tyint8:
+ var val : int8 = vanext(ap)
+ -> val < 0 ? 0 : (val : size)
+ | `Tyint16:
+ var val : int16 = vanext(ap)
+ -> val < 0 ? 0 : (val : size)
+ | `Tyint:
+ var val : int = vanext(ap)
+ -> val < 0 ? 0 : (val : size)
+ | `Tyint32:
+ var val : int32 = vanext(ap)
+ -> val < 0 ? 0 : (val : size)
+ | `Tyint64:
+ var val : int64 = vanext(ap)
+ -> val < 0 ? 0 : (val : size)
+ | `Tyuint8:
+ var val : uint8 = vanext(ap)
+ -> (val : size)
+ | `Tyuint16:
+ var val : uint16 = vanext(ap)
+ -> (val : size)
+ | `Tyuint:
+ var val : uint = vanext(ap)
+ -> (val : size)
+ | `Tyuint32:
+ var val : uint32 = vanext(ap)
+ -> (val : size)
+ | `Tyuint64:
+ var val : uint64 = vanext(ap)
+ -> (val : size)
+ | `Tyname (_, desc):
+ /* This is primarily for handling std.size */
+ var subap = vaenter(ap)
+ var ret = pullint(&subap, msg)
+ vabytes(ap) /* Pull one element out to keep ap synchronized with subap */
+ -> ret
+ | _: die(msg)
+ ;;
+
+ -> 0
+}