summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apl9.h1
-rw-r--r--functions.c42
2 files changed, 42 insertions, 1 deletions
diff --git a/apl9.h b/apl9.h
index cf2c352..ace4722 100644
--- a/apl9.h
+++ b/apl9.h
@@ -318,6 +318,7 @@ Array *fnReverseLast(Array *);
Array *fnReverseFirst(Array *);
Array *fnTranspose(Array *);
Array *fnExecute(Array *);
+Array *fnFormat(Array *);
Array *fnSelfReference1(Array *);
/* Dyadic functions from function.c */
diff --git a/functions.c b/functions.c
index 0f40a9a..a0090a9 100644
--- a/functions.c
+++ b/functions.c
@@ -58,7 +58,7 @@ fnmonad monadfunctiondefs[] = {
fnReverseFirst, /* ⊖ */
fnTranspose, /* ⍉ */
fnExecute, /* ⍎ */
- 0, /* ⍕ */
+ fnFormat, /* ⍕ */
fnSelfReference1, /* ∇ */
};
@@ -841,6 +841,46 @@ fnExecute(Array *right)
}
Array *
+fnFormat(Array *right)
+{
+ if(right->type == AtypeArray)
+ throwerror(L"Format for nested arrays", ENotImplemented);
+ /* TODO add support for formatting nested arrays */
+
+ right = right->rank == 0 ? fnRavel(right) : fnSame(right);
+
+ Rune *str = pparray(right);
+ int maxwidth = 0;
+ int tmpwidth = 0;
+ for(int i = 0; str[i] != 0; i++){
+ if(str[i] != '\n')
+ tmpwidth++;
+ if(str[i] == '\n' || str[i+1] == 0){
+ if(tmpwidth > maxwidth)
+ maxwidth = tmpwidth;
+ tmpwidth = 0;
+ }
+ }
+ vlong size;
+ if(right->shape[right->rank-1] == 0)
+ size = 0;
+ else
+ size = maxwidth * right->size / right->shape[right->rank-1];
+
+ Array *result = allocarray(AtypeRune, right->rank, size);
+ result->shape[result->rank-1] = maxwidth;
+ for(int i = 0; i < result->rank-1; i++)
+ result->shape[i] = right->shape[i];
+ int offset = 0;
+ for(int i = 0; str[i] != 0; i++)
+ if(str[i] != '\n')
+ result->runedata[offset++] = str[i];
+ free(str);
+ freearray(right);
+ return result;
+}
+
+Array *
fnSelfReference1(Array *right)
{
DfnFrame *dfn = getcurrentdfn();