summaryrefslogtreecommitdiff
path: root/print.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-11 18:23:19 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-11 18:23:19 +0000
commit9f01c03fd60ed1f745cb1ae3ff6f1e27910531af (patch)
tree9edf386ee88b84200d7d06bd51f40bfa66339225 /print.c
parent4a92ef13f83ec4fe79d6dd815f1b9fe492a404fe (diff)
Add a new array printer
Diffstat (limited to 'print.c')
-rw-r--r--print.c52
1 files changed, 34 insertions, 18 deletions
diff --git a/print.c b/print.c
index 7166888..e0443b3 100644
--- a/print.c
+++ b/print.c
@@ -42,24 +42,40 @@ ppdatums(Datum *ds, int n)
Rune *
pparray(Array *a)
{
- /* can only print integer vectors and scalars for now, also nested */
- if(a->type == AtypeArray){
- Rune *res = runesmprint("[");
- Rune *tmp;
- for(int i = 0; i < a->size; i++){
- tmp = res;
- res = runesmprint("%S%S%s", res, pparray(a->arraydata[i]), (i+1<a->size)?";":"]");
- free(tmp);
- }
- return res;
- }else{
- Rune *res = runesmprint("");
- Rune *tmp;
- for(int i = 0; i < a->size; i++){
+ Rune **elemstrs = malloc(sizeof(Rune *) * a->size);
+ for(int i = 0; i < a->size; i++){
+ if(a->type == AtypeArray){
+ 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]);
+ }
+
+ /* Should do some width and height padding here */
+ Rune *res = runesmprint("");
+ Rune *tmp;
+ for(int i = 0; i < a->size; i++){
+ tmp = res;
+ res = runesmprint("%S%S", res, elemstrs[i]);
+ free(tmp);
+ free(elemstrs[i]);
+
+ int j = 1;
+ int spaceprinted = 0;
+ for(int dim = 0; dim < a->rank && i+1 != a->size; dim++){
+ j *= a->shape[dim];
tmp = res;
- res = runesmprint("%S%lld%s", res, a->intdata[i], (i+1<a->size)?" ":"");
- free(tmp);
- }
- return res;
+ if((i+1)%j == 0){
+ spaceprinted = 1;
+ res = runesmprint("%S\n", res);
+ free(tmp);
+ }else if(!spaceprinted){
+ spaceprinted = 1;
+ res = runesmprint("%S ", res);
+ free(tmp);
+ }
+ }
}
+ return res;
} \ No newline at end of file