diff options
author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-12 00:09:12 +0000 |
---|---|---|
committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-12 00:09:12 +0000 |
commit | 85aa2ad424c68343ef09e5f6df243ad6499e47d5 (patch) | |
tree | 8712baf633e82ffdba79b3a59b17734de0eaf4d7 | |
parent | 2ff4b85fc73f0cf82034e2a19ea8c5d677812c9c (diff) |
Add a small version of monadic ⍳ and some simple form of ⎕IO
-rw-r--r-- | apl9.h | 5 | ||||
-rw-r--r-- | functions.c | 15 | ||||
-rw-r--r-- | lexer.c | 10 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | symbol.c | 14 |
5 files changed, 40 insertions, 5 deletions
@@ -113,12 +113,14 @@ Datum *eval(Datum *, int *); /* symbol.c */ Symbol *getsym(Symtab *, Rune *); Symtab *newsymtab(void); +vlong globalIO(void); /* Monadic functions from functions.h */ Array *fnSame(Array *); Array *fnTally(Array *); Array *fnEnclose(Array *); Array *fnNest(Array *); +Array *fnIndexGenerator(Array *); Array *fnRavel(Array *); Array *fnShape(Array *); @@ -138,4 +140,5 @@ extern Rune primdyadopnames[]; /* lexer.c */ extern Rune primhybridnames[]; /* lexer.c */ extern fnmonad monadfunctiondefs[]; /* function.c */ extern fndyad dyadfunctiondefs[]; /* function.c */ -extern Symtab *globalsymtab; /* symbol.c */
\ No newline at end of file +extern Symtab *globalsymtab; /* symbol.c */ +extern Symtab *currentsymtab; /* symbol.c */
\ No newline at end of file diff --git a/functions.c b/functions.c index cf5402a..4d9a69f 100644 --- a/functions.c +++ b/functions.c @@ -44,7 +44,7 @@ fnmonad monadfunctiondefs[] = { 0, /* ⌷ */ 0, /* ⍋ */ 0, /* ⍒ */ - 0, /* ⍳ */ + fnIndexGenerator, /* ⍳ */ 0, /* ⍸ */ 0, /* ∊ */ 0, /* ⍷ */ @@ -143,6 +143,19 @@ fnEnclose(Array *right) } Array * +fnIndexGenerator(Array *right) +{ + /* TODO only works for creating vectors */ + vlong n = right->intdata[0]; + Array *res = mkarray(AtypeInt, 1, n); + res->shape[0] = n; + vlong io = globalIO(); + for(vlong i = 0; i < n; i++) + res->intdata[i] = i + io; + return res; +} + +Array * fnNest(Array *right) { if(simplearray(right)) @@ -53,11 +53,15 @@ lexline(Rune *line, int *ntoks, Symtab *symtab) *p = 0; tokens[*ntoks].tag = ArrayTag; tokens[*ntoks].array = mkscalarint(atoll(buf)); - }else if(isalpharune(line[offset])){ + }else if(isalpharune(line[offset]) || line[offset] == L'⎕'){ + int quadname = L'⎕' == line[offset]; Rune buf[64]; Rune *p = buf; - while(isalpharune(line[offset])){ - *p = line[offset]; + while(isalpharune(line[offset]) || (line[offset] == L'⎕' && p == buf)){ + if(quadname) + *p = toupperrune(line[offset]); + else + *p = line[offset]; p++; offset++; } @@ -15,6 +15,7 @@ main(int argc, char *argv[]) int off = 0; stdin = Bfdopen(0, OREAD); globalsymtab = newsymtab(); + currentsymtab = globalsymtab; traceeval = 0; ARGBEGIN{ @@ -5,6 +5,7 @@ #include "apl9.h" Symtab *globalsymtab; +Symtab *currentsymtab; Symbol * getsym(Symtab *tab, Rune *name) @@ -27,5 +28,18 @@ newsymtab(void) Symtab *tab = malloc(sizeof(Symtab)); tab->nsyms = 0; tab->syms = nil; + + Symbol *io = getsym(tab, L"⎕IO"); + io->value.tag = ArrayTag; + io->value.array = mkscalarint(1); + io->undefined = 0; + return tab; +} + +vlong +globalIO(void) +{ + Symbol *s = getsym(currentsymtab, L"⎕IO"); + return s->value.array->intdata[0]; }
\ No newline at end of file |