summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-10 19:32:39 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-10 19:32:39 +0000
commit6c964eb54c0056d14a1b929dc09c75240a585cea (patch)
tree3c57d83be54c151f5a7f5b9596e87b5e425982fc
parent0b8bd8e88f2620992310c7ba41283f5d9120e371 (diff)
Implement ( expr ) and improve stranding
-rw-r--r--apl9.h9
-rw-r--r--array.c1
-rw-r--r--eval.c33
-rw-r--r--functions.c3
-rw-r--r--print.c2
5 files changed, 42 insertions, 6 deletions
diff --git a/apl9.h b/apl9.h
index 41ad5e2..4080a81 100644
--- a/apl9.h
+++ b/apl9.h
@@ -27,6 +27,7 @@ typedef enum
/* Data types */
typedef struct Array Array;
+typedef struct Expr Expr;
typedef struct Datum Datum;
struct Array
@@ -35,6 +36,7 @@ struct Array
int rank;
int *shape;
int size;
+ int stranded; /* 1 if build directly by stranding */
union {
char *rawdata;
vlong *intdata;
@@ -42,12 +44,19 @@ struct Array
};
};
+struct Expr
+{
+ int ntoks;
+ Datum *toks;
+};
+
struct Datum
{
datumTag tag;
union {
Array *array;
int code;
+ Expr expr;
};
};
diff --git a/array.c b/array.c
index 4f904e1..9feb132 100644
--- a/array.c
+++ b/array.c
@@ -16,6 +16,7 @@ mkarray(arrayDataType t, int rank, int size)
a->rank = rank;
a->type = t;
a->size = size;
+ a->stranded = 0;
a->shape = malloc(sizeof(int) * rank);
a->rawdata = malloc(datasizes[t] * size);
a->type = t;
diff --git a/eval.c b/eval.c
index da49137..1ee7033 100644
--- a/eval.c
+++ b/eval.c
@@ -10,6 +10,8 @@ typedef Datum (*evalfn)(Datum, Datum);
Datum strand(Datum, Datum);
Datum monadfun(Datum, Datum);
+Datum lpar(Datum, Datum);
+Datum rpar(Datum, Datum);
int bindingstrengths[12][12] = {
/* A F H MO DO AF ( ) { } [ ] */
@@ -19,7 +21,7 @@ int bindingstrengths[12][12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* MO */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* DO */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* FA */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ( */
+ 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, /* ( */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ) */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* { */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* } */
@@ -35,7 +37,7 @@ evalfn evalfns[12][12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* MO */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* DO */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* FA */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ( */
+ lpar, lpar, lpar, lpar, lpar, lpar, lpar, rpar, lpar, lpar, lpar, lpar, /* ( */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ) */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* { */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* } */
@@ -88,8 +90,11 @@ strand(Datum left, Datum right)
{
print("Stranding\n");
Datum result;
+ Array *leftarr = left.array->stranded ? left.array : fnEnclose(left.array);
+ Array *rightarr = right.array->stranded ? right.array : fnEnclose(right.array);
result.tag = ArrayTag;
- result.array = fnCatenateFirst(left.array,fnEnclose(right.array));
+ result.array = fnCatenateFirst(leftarr, rightarr);
+ result.array->stranded = 1;
return result;
}
@@ -102,4 +107,26 @@ monadfun(Datum left, Datum right)
/* TODO handle undefined functions here */
result.array = monadfunctiondefs[left.code](right.array);
return result;
+}
+
+Datum
+lpar(Datum left, Datum right)
+{
+ /* build up a parthenthesised expression */
+ left.expr.ntoks++;
+ left.expr.toks = realloc(left.expr.toks, sizeof(Datum) * left.expr.ntoks);
+ left.expr.toks[left.expr.ntoks-1] = right;
+ print("LPAR: %S\n", ppdatums(left.expr.toks, left.expr.ntoks));
+ return left;
+}
+
+Datum
+rpar(Datum left, Datum right)
+{
+ /* evaluate a parenthesis expression and return the result */
+ USED(right);
+ print("RPAR: %S\n", ppdatums(left.expr.toks, left.expr.ntoks));
+ Datum *result = eval(left.expr.toks, &left.expr.ntoks);
+ result[0].array->stranded = 0;
+ return result[0]; /* TODO handle error if ntoks != 1 */
} \ No newline at end of file
diff --git a/functions.c b/functions.c
index 1c60a82..ddd5532 100644
--- a/functions.c
+++ b/functions.c
@@ -69,8 +69,7 @@ fnCatenateFirst(Array *left, Array *right)
left = fnRavel(left);
if(right->rank == 0)
right = fnRavel(right);
-
- print("Catenating: %S and %S\n", pparray(left), pparray(right));
+
/* assume two vectors of same type for now */
Array *res = mkarray(left->type, 1, left->size+right->size);
res->shape[0] = left->shape[0] + right->shape[0];
diff --git a/print.c b/print.c
index 7935413..7166888 100644
--- a/print.c
+++ b/print.c
@@ -48,7 +48,7 @@ pparray(Array *a)
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)?" ":"]");
+ res = runesmprint("%S%S%s", res, pparray(a->arraydata[i]), (i+1<a->size)?";":"]");
free(tmp);
}
return res;