diff options
author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-26 09:40:50 +0000 |
---|---|---|
committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-26 09:40:50 +0000 |
commit | bcaf7f25f42b21067a26895e097ada73765ba7d5 (patch) | |
tree | ccfb887cf08619c17b3afa3b517bfc3dcdfa18ad /print.c | |
parent | 57a86f761605b6261d1045558c9cb7c83d723b60 (diff) |
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
Diffstat (limited to 'print.c')
-rw-r--r-- | print.c | 38 |
1 files changed, 22 insertions, 16 deletions
@@ -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; |