From bcaf7f25f42b21067a26895e097ada73765ba7d5 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Wed, 26 Jan 2022 09:40:50 +0000 Subject: Implement a new "mixed" type which can be either of the three scalar types: int, float, rune. This allows scalar arrays with mixed scalar types --- print.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'print.c') diff --git a/print.c b/print.c index e46866c..3c119dd 100644 --- a/print.c +++ b/print.c @@ -63,24 +63,30 @@ pparray(Array *a) Rune *arrstr = pparray(a->arraydata[i]); elemstrs[i] = runesmprint("%S", arrstr); free(arrstr); - }else if(a->type == AtypeInt) - elemstrs[i] = runesmprint("%lld", a->intdata[i]); - else if(a->type == AtypeRune) - elemstrs[i] = runesmprint("%C", a->runedata[i]); - else if(a->type == AtypeFloat){ - char *fmt = smprint("%%.%df", printprecision); - elemstrs[i] = runesmprint(fmt, a->floatdata[i]); - free(fmt); - Rune *p = &elemstrs[i][runestrlen(elemstrs[i])-1]; - int done = 0; - while((*p == '0' || *p == '.') && !done){ - if(*p == '.') - done = 1; - *p-- = 0; /* remove trailing 0's */ + }else{ + Array *e = arrayitem(a, i); /* a scalar */ + if(e->type == AtypeInt) + elemstrs[i] = runesmprint("%lld", e->intdata[0]); + else if(e->type == AtypeRune) + elemstrs[i] = runesmprint("%C", e->runedata[0]); + else if(e->type == AtypeFloat){ + char *fmt = smprint("%%.%df", printprecision); + elemstrs[i] = runesmprint(fmt, e->floatdata[0]); + free(fmt); + Rune *p = &elemstrs[i][runestrlen(elemstrs[i])-1]; + int done = 0; + while((*p == '0' || *p == '.') && !done){ + if(*p == '.') + done = 1; + *p-- = 0; /* remove trailing 0's */ + } } + + if(elemstrs[i][0] == '-' && (e->type == AtypeInt || e->type == AtypeFloat)) + elemstrs[i][0] = L'¯'; + freearray(e); } - if(elemstrs[i][0] == '-' && (a->type == AtypeInt || a->type == AtypeFloat)) - elemstrs[i][0] = L'¯'; + } int lastdim = a->rank ? a->shape[a->rank-1] : 1; -- cgit v1.2.3