summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-06-06 17:58:36 -0700
committerOri Bernstein <ori@eigenstate.org>2015-06-06 18:13:07 -0700
commit8fef0e65ac05920b30d13ef4816dd166c3c83c47 (patch)
treeb480f1fce49020d3e1621a8dbc107e82566b4047 /libstd
parent4ca93982fdef1ddb426b5595f814f3099adfdc65 (diff)
downloadmc-8fef0e65ac05920b30d13ef4816dd166c3c83c47.tar.gz
Support formatting params for ints.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/fmt2.myr99
-rw-r--r--libstd/varargs.myr1
2 files changed, 75 insertions, 25 deletions
diff --git a/libstd/fmt2.myr b/libstd/fmt2.myr
index 9ce32d4..081fb93 100644
--- a/libstd/fmt2.myr
+++ b/libstd/fmt2.myr
@@ -85,19 +85,31 @@ const f2sbfmt = {sb, fmt, args
const f2sbfmtv = {sb, fmt, ap -> size
var c, params
+ var nfmt, nparams
+ nparams = ap.tc.nelt
+ nfmt = 0
while fmt.len != 0
(c, fmt) = striter(fmt)
match c
| '%':
+ nfmt++
+ if nfmt > nparams
+ die("too few params for fmt\n")
+ ;;
+
if fmt.len > 1 && fmt[0] == '{' castto(byte)
(params, fmt) = f2getparams(fmt[1:])
;;
ap = fallbackfmt(sb, params, vatype(ap), ap)
+
| chr:
sbputc(sb, chr)
;;
;;
+ if nfmt != nparams
+ die("too many params for fmt\n")
+ ;;
-> sb.len
}
@@ -136,44 +148,44 @@ const fallbackfmt = {sb, params, tyenc, ap
sbputc(sb, c_val)
| `Tyint8:
(b_val, ap) = vanext(ap)
- intfmt(sb, params, true, b_val)
+ intfmt(sb, intparams(params), true, b_val)
| `Tyint16:
(w_val, ap) = vanext(ap)
- intfmt(sb, params, true, w_val)
+ intfmt(sb, intparams(params), true, w_val)
| `Tyint:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, true, i_val)
+ intfmt(sb, intparams(params), true, i_val)
| `Tyint32:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, true, i_val)
+ intfmt(sb, intparams(params), true, i_val)
| `Tyint64:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, true, l_val)
+ intfmt(sb, intparams(params), true, l_val)
| `Tylong:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, true, l_val)
+ intfmt(sb, intparams(params), true, l_val)
| `Tybyte:
(ub_val, ap) = vanext(ap)
- intfmt(sb, params, false, ub_val)
+ intfmt(sb, intparams(params), false, ub_val)
| `Tyuint8:
(ub_val, ap) = vanext(ap)
- intfmt(sb, params, false, ub_val)
+ intfmt(sb, intparams(params), false, ub_val)
| `Tyuint16:
(w_val, ap) = vanext(ap)
- intfmt(sb, params, false, uw_val)
+ intfmt(sb, intparams(params), false, uw_val)
| `Tyuint:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, false, ui_val)
+ intfmt(sb, intparams(params), false, ui_val)
| `Tyuint32:
(i_val, ap) = vanext(ap)
- intfmt(sb, params, false, ui_val)
+ intfmt(sb, intparams(params), false, ui_val)
| `Tyuint64:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, false, ul_val)
+ intfmt(sb, intparams(params), false, ul_val)
| `Tyulong:
(l_val, ap) = vanext(ap)
- intfmt(sb, params, false, ul_val)
+ intfmt(sb, intparams(params), false, ul_val)
| `Tyflt32:
(l_val, ap) = vanext(ap)
sbputs(sb, "flt32")
@@ -185,7 +197,10 @@ const fallbackfmt = {sb, params, tyenc, ap
/* compound types */
| `Typtr desc:
(p_val, ap) = vanext(ap)
- intfmt(sb, params, false, p_val castto(intptr))
+ sbputs(sb, "0x")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
| `Tyslice desc:
match typedesc(desc)
| `Tybyte:
@@ -195,10 +210,14 @@ const fallbackfmt = {sb, params, tyenc, ap
sbputs(sb, "slice[:]")
;;
| `Tyfunc tc:
- sbputs(sb, "func")
+ (p_val, ap) = vanext(ap)
+ sbputs(sb, "func{")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
+ sbputs(sb, "}")
| `Tyarray (sz, data):
sbputs(sb, "array")
-
/* aggregate types */
| `Tytuple typecursor:
sbputs(sb, "tuple")
@@ -212,6 +231,11 @@ const fallbackfmt = {sb, params, tyenc, ap
-> ap
}
+type intparams = struct
+ base : size
+ padto : size
+ padfill : char
+;;
const f2getparams = {fmt
var i
@@ -226,6 +250,32 @@ const f2getparams = {fmt
-> (fmt[:i], fmt[i+1:])
}
+const intparams = {params
+ var ip : intparams
+ var c
+
+ ip = [
+ .base = 10,
+ .padfill = ' ',
+ .padto = 0
+ ]
+
+ while params.len > 0
+ (c, params) = striter(params)
+ match c
+ | 'x': ip.base = 16
+ | '0': ip.padfill = '0'
+ | chr:
+ while isdigit(c)
+ ip.padto = ip.padto*10 + charval(c, 10)
+ (c, params) = striter(params)
+ ;;
+ ;;
+ ;;
+ -> ip
+
+}
+
const digitchars = [
'0','1','2','3','4',
'5','6','7','8','9',
@@ -236,12 +286,10 @@ generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
var val
var b : char[32]
var i, j, npad
- var base, padto, padfill
+ var base
- i = 0
- base = 10
- padto = 0
- padfill = '0'
+ base = opts.base castto(uint64)
+ std.put("base = %l\n", base)
if signed && bits < 0
val = -bits castto(uint64)
isneg = true
@@ -251,6 +299,7 @@ generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
isneg = false
;;
+ i = 0
if val == 0
b[0] = '0'
i++
@@ -261,17 +310,17 @@ generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
i++
;;
- npad = clamp(padto - i, 0, padto)
+ npad = clamp(opts.padto - i, 0, opts.padto)
if isneg
npad--
;;
- if padfill == '0' && isneg
+ if opts.padfill == '0' && isneg
sbputc(sb, '-')
;;
for j = 0; j < npad; j++
- sbputc(sb, padfill)
+ sbputc(sb, opts.padfill)
;;
- if padfill != '0' && isneg
+ if opts.padfill != '0' && isneg
sbputc(sb, '-')
;;
for j = i; j != 0; j--
diff --git a/libstd/varargs.myr b/libstd/varargs.myr
index 06e6644..d311585 100644
--- a/libstd/varargs.myr
+++ b/libstd/varargs.myr
@@ -8,6 +8,7 @@ pkg std =
const vastart : (args : ...# -> valist)
const vatype : (ap : valist -> byte[:])
+ const vaskip : (ap : valist -> byte[:])
generic vanext : (ap : valist -> (@a, valist))
;;