summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS. Gilles <sgilles@umd.edu>2019-11-24 18:20:51 -0500
committerOri Bernstein <ori@eigenstate.org>2019-12-04 22:27:28 -0800
commit6a113c23192c0877185aadddbc58b943ed68dd7b (patch)
tree48c9c3514c45a21c39d514e7371cf0083e1cdd00
parent03ef9a246d5f6f8013c19285224323e64206c415 (diff)
downloadmc-6a113c23192c0877185aadddbc58b943ed68dd7b.tar.gz
Make vanext decrement tc.nelt unconditionally
This allows formatting functions to pass off the valist to type-specific formatters, which may themselves consume elements from the valist, and still check for correct argument counts without passing around counter variables.
-rw-r--r--lib/std/fmt.myr29
-rw-r--r--lib/std/introspect.myr1
2 files changed, 15 insertions, 15 deletions
diff --git a/lib/std/fmt.myr b/lib/std/fmt.myr
index c4c3797..b7155f6 100644
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -165,11 +165,9 @@ const sbfmt = {sb, fmt, args
const sbfmtv = {sb, fmt, ap -> size
var buf : byte[256], param : (byte[:], byte[:])[8]
var state, startp, endp, starta, nbuf
- var nfmt, nvarargs, nparam
+ var nparam
var b, i
- nvarargs = ap.tc.nelt
- nfmt = 0
startp = 0
starta = 0
nparam = 0
@@ -216,8 +214,7 @@ const sbfmtv = {sb, fmt, ap -> size
if startp != nbuf
param[nparam++] = (buf[startp:nbuf], "")
;;
- nfmt++
- if nfmt > nvarargs
+ if ap.tc.nelt < 1
die("too few values for fmt\n")
;;
fmtval(sb, vatype(ap), ap, param[:nparam])
@@ -237,15 +234,17 @@ const sbfmtv = {sb, fmt, ap -> size
if startp != nbuf
param[nparam++] = (buf[startp:endp], buf[starta:nbuf])
;;
+ if ap.tc.nelt < 1
+ die("too few values for fmt\n")
+ ;;
fmtval(sb, vatype(ap), ap, param[:nparam])
- nfmt++
| (`ParamArg, '\\'):
buf[nbuf++] = fmt[i++]
| (`ParamArg, chr):
buf[nbuf++] = b
;;
;;
- if nfmt != nvarargs
+ if ap.tc.nelt != 0
die("too many values for fmt\n")
;;
-> sb.len
@@ -367,26 +366,26 @@ const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
| `Tytuple tc:
subap = vaenter(ap)
sbfmt(sb, "(")
- for var i = 0; i < subap.tc.nelt; i++
+ var extracomma = subap.tc.nelt == 1
+ while subap.tc.nelt != 0
fmtval(sb, vatype(&subap), &subap, [][:])
- if subap.tc.nelt == 1
- sbfmt(sb, ",")
- elif i != subap.tc.nelt -1
+ if subap.tc.nelt > 0
sbfmt(sb, ", ")
;;
;;
+ if extracomma
+ sbfmt(sb, ",")
+ ;;
sbfmt(sb, ")")
vabytes(ap)
| `Tystruct nc:
subap = vaenter(ap)
sbfmt(sb, "[")
- for var i = 0; i < subap.tc.nelt; i++
+ while subap.tc.nelt != 0
(subname, subenc) = ncpeek(&subap.tc)
sbfmt(sb, ".{}=", subname)
fmtval(sb, vatype(&subap), &subap, [][:])
- if subap.tc.nelt == 1
- sbfmt(sb, ",")
- elif i != subap.tc.nelt -1
+ if subap.tc.nelt > 0
sbfmt(sb, ", ")
;;
;;
diff --git a/lib/std/introspect.myr b/lib/std/introspect.myr
index aec9e69..5198aeb 100644
--- a/lib/std/introspect.myr
+++ b/lib/std/introspect.myr
@@ -189,6 +189,7 @@ const ncnext = {tc
(n, sz) = getipacked(tc.rem)
enc = tc.rem[sz:sz+n]
tc.rem = tc.rem[sz+n:]
+ tc.nelt--
-> (name, enc)
}