diff options
-rw-r--r-- | apl9.h | 5 | ||||
-rw-r--r-- | eval.c | 19 | ||||
-rw-r--r-- | lexer.c | 12 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | symbol.c | 3 |
5 files changed, 33 insertions, 8 deletions
@@ -99,13 +99,16 @@ typedef Array* (*fnmonad)(Array*); typedef Array* (*fndyad)(Array*, Array*); /* Function prototypes for the different source files */ +/* main.c */ +Datum *evalline(Rune *); + /* print.c */ Rune *ppdatum(Datum); Rune *ppdatums(Datum *, int); Rune *pparray(Array *); /* lexer.c */ -Statement *lexline(Rune *, Symtab *); +Statement *lexline(Rune *); /* array.c */ Array *mkarray(int, int, int); @@ -150,10 +150,25 @@ monadfun(Datum left, Datum right) traceprint("Monadic function application\n"); Datum result; result.tag = ArrayTag; + result.shy = 0; if(left.func.type == FunctypeDfn){ - print("Can't evaluate dfns yet: %S %S\n", ppdatum(left), ppdatum(right)); - exits(nil); + Symtab *tmpsymtab = currentsymtab; + currentsymtab = newsymtab(); + if(left.func.left){ + Symbol *alpha = getsym(currentsymtab, L"⍺"); + alpha->value.tag = ArrayTag; + alpha->value.array = left.func.left; + alpha->undefined = 0; + } + + Symbol *omega = getsym(currentsymtab, L"⍵"); + omega->value = right; + omega->undefined = 0; + + Datum *dfnres = evalline(left.func.dfn); + currentsymtab = tmpsymtab; + return *dfnres; /* TODO what if the evaluation failed */ }else{ /* TODO handle undefined functions here */ if(left.func.left) @@ -9,7 +9,7 @@ Rune primdyadopnames[] = L"⍣.∘⍤⍥@⍠⌺"; Rune primhybridnames[] = L"/\⌿⍀"; Statement * -lexline(Rune *line, Symtab *symtab) +lexline(Rune *line) { int offset = 0; int len = runestrlen(line); @@ -30,7 +30,7 @@ lexline(Rune *line, Symtab *symtab) case '[': stmt->toks[stmt->ntoks].tag = LBracketTag; break; case ']': stmt->toks[stmt->ntoks].tag = RBracketTag; break; case L'←': stmt->toks[stmt->ntoks].tag = ArrowTag; break; - case L'⋄': stmt->next = lexline(&line[offset+1], symtab); goto end; + case L'⋄': stmt->next = lexline(&line[offset+1]); goto end; case L'⍝': goto end; } offset++; @@ -73,6 +73,12 @@ lexline(Rune *line, Symtab *symtab) *p = 0; stmt->toks[stmt->ntoks].tag = ArrayTag; stmt->toks[stmt->ntoks].array = mkscalarint(atoll(buf)); + }else if(runestrchr(L"⍺⍵", line[offset])){ + Rune *name = L"?"; + name[0] = line[offset]; + stmt->toks[stmt->ntoks].tag = NameTag; + stmt->toks[stmt->ntoks].symbol = getsym(currentsymtab, name); + offset++; }else if(isalpharune(line[offset]) || line[offset] == L'⎕'){ int quadname = L'⎕' == line[offset]; Rune buf[64]; @@ -87,7 +93,7 @@ lexline(Rune *line, Symtab *symtab) } *p = 0; stmt->toks[stmt->ntoks].tag = NameTag; - stmt->toks[stmt->ntoks].symbol = getsym(symtab, buf); + stmt->toks[stmt->ntoks].symbol = getsym(currentsymtab, buf); }else{ syntax_error: print("Can't lex: %S\n", &line[offset]); @@ -59,7 +59,7 @@ prompt(Rune *pr) Datum * evalline(Rune *line) { - Statement *stmts = lexline(line, globalsymtab); + Statement *stmts = lexline(line); Datum *result = eval(stmts); if(result) return result; @@ -31,7 +31,8 @@ newsymtab(void) Symbol *io = getsym(tab, L"⎕IO"); io->value.tag = ArrayTag; - io->value.array = mkscalarint(1); + io->value.array = mkscalarint(currentsymtab ? globalIO() : 1); + io->value.shy = 0; io->undefined = 0; return tab; |