summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apl9.h4
-rw-r--r--array.c22
-rw-r--r--lexer.c27
-rw-r--r--print.c10
4 files changed, 61 insertions, 2 deletions
diff --git a/apl9.h b/apl9.h
index f15cd77..f6ea693 100644
--- a/apl9.h
+++ b/apl9.h
@@ -23,6 +23,7 @@ typedef enum
{
AtypeInt,
AtypeFloat,
+ AtypeRune,
AtypeArray,
} arrayDataType;
@@ -60,6 +61,7 @@ struct Array
char *rawdata;
vlong *intdata;
double *floatdata;
+ Rune *runedata;
Array **arraydata;
};
};
@@ -157,6 +159,8 @@ Statement *lexline(Rune *);
/* array.c */
Array *mkscalarint(vlong);
Array *mkscalarfloat(double);
+Array *mkscalarrune(Rune);
+Array *mkrunearray(Rune *);
Array *duparray(Array *);
int simplearray(Array *);
int simplescalar(Array *);
diff --git a/array.c b/array.c
index 4208d9f..5fb2a32 100644
--- a/array.c
+++ b/array.c
@@ -9,6 +9,7 @@ Array *inttofloatarray(Array *);
int datasizes[] = {
[AtypeInt] = sizeof(vlong),
[AtypeFloat] = sizeof(double),
+ [AtypeRune] = sizeof(Rune),
[AtypeArray] = sizeof(Array *)
};
@@ -29,6 +30,24 @@ mkscalarfloat(double f)
}
Array *
+mkscalarrune(Rune r)
+{
+ Array *a = allocarray(AtypeRune, 0, 1);
+ a->runedata[0] = r;
+ return a;
+}
+
+Array *
+mkrunearray(Rune *str)
+{
+ Array *a = allocarray(AtypeRune, 1, runestrlen(str));
+ a->shape[0] = a->size;
+ for(int i = 0; i < a->size; i++)
+ a->runedata[i] = str[i];
+ return a;
+}
+
+Array *
duparray(Array *a)
{
Array *b = allocarray(a->type, a->rank, a->size);
@@ -135,6 +154,9 @@ arrayitem(Array *a, int index)
case AtypeFloat:
res = mkscalarfloat(a->floatdata[index]);
break;
+ case AtypeRune:
+ res = mkscalarrune(a->runedata[index]);
+ break;
case AtypeArray:
res = a->arraydata[index];
incref(res);
diff --git a/lexer.c b/lexer.c
index 76cffc8..25b5d75 100644
--- a/lexer.c
+++ b/lexer.c
@@ -113,7 +113,7 @@ get_digits:
}else if(isalpharune(line[offset])){
Rune buf[64];
Rune *p = buf;
- while(isalpharune(line[offset])){
+ while(isalpharune(line[offset]) || isdigitrune(line[offset])){
*p = line[offset];
p++;
offset++;
@@ -140,6 +140,31 @@ get_digits:
offset -= runestrlen(buf);
goto syntax_error;
}
+ }else if(line[offset] == '\''){
+ Rune buf[1024]; /* stupid limit on literal string lengths */
+ Rune *b = buf;
+ int done = 0;
+ offset++;
+ while(!done && offset < len){
+ if(line[offset] == '\'' && line[offset+1] != '\''){
+ *b = 0;
+ done = 1;
+ }else if(line[offset] == '\'' && line[offset+1] == '\''){
+ *b++ = '\'';
+ offset++;
+ }else
+ *b++ = line[offset];
+ offset++;
+ }
+ if(!done){
+ offset = offset - (b-buf);
+ goto syntax_error;
+ }
+ stmt->toks[stmt->ntoks].tag = ArrayTag;
+ if(runestrlen(buf) == 1)
+ stmt->toks[stmt->ntoks].array = mkscalarrune(buf[0]);
+ else
+ stmt->toks[stmt->ntoks].array = mkrunearray(buf);
}else{
syntax_error:
print("Can't lex: %S\n", &line[offset]);
diff --git a/print.c b/print.c
index 15e5f2a..da60c7b 100644
--- a/print.c
+++ b/print.c
@@ -81,6 +81,8 @@ pparray(Array *a)
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]);
@@ -124,7 +126,13 @@ pparray(Array *a)
char *fmt = smprint("%%%s%dS", align, widths[x]);
Rune *line = strline(elemstrs[i], y);
Rune *padded = runesmprint(fmt, line);
- Rune *spacing = boxing ? L"│" : (x == lastdim-1) ? L"" : L" ";
+ Rune *spacing;
+ if(boxing)
+ spacing = L"│";
+ else if(x == lastdim - 1 || a->type == AtypeRune)
+ spacing = L"";
+ else
+ spacing = L" ";
rowstrs[row] = runesmprint("%S%S%S", tmp, padded, spacing);
free(tmp);
free(fmt);