diff options
-rw-r--r-- | apl9.h | 8 | ||||
-rw-r--r-- | eval.c | 18 | ||||
-rw-r--r-- | functions.c | 57 | ||||
-rw-r--r-- | lexer.c | 11 |
4 files changed, 85 insertions, 9 deletions
@@ -24,6 +24,7 @@ typedef enum AtypeArray } arrayDataType; + /* Data types */ typedef struct Array Array; typedef struct Datum Datum; @@ -50,6 +51,8 @@ struct Datum }; }; +typedef Array* (*fnmonad)(Array*); + /* Function prototypes for the different source files */ /* print.c */ Rune *ppdatum(Datum); @@ -80,7 +83,8 @@ Array *fnCatenateFirst(Array *, Array *); /* Global variables */ extern Rune *errormsg; /* eval.c */ extern int datasizes[]; /* array.c */ -extern Rune primfuncnames[]; /* lexer.c */ +extern Rune primfuncnames[]; /* function.c */ extern Rune primmonopnames[]; /* lexer.c */ extern Rune primdyadopnames[]; /* lexer.c */ -extern Rune primhybridnames[]; /* lexer.c */
\ No newline at end of file +extern Rune primhybridnames[]; /* lexer.c */ +extern fnmonad monadfunctiondefs[]; /* function.c */
\ No newline at end of file @@ -9,11 +9,12 @@ Rune *errormsg; typedef Datum (*evalfn)(Datum, Datum); Datum strand(Datum, Datum); +Datum monadfun(Datum, Datum); int bindingstrengths[12][12] = { /* A F H MO DO AF ( ) { } [ ] */ - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F */ + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A */ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* H */ 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 */ @@ -29,7 +30,7 @@ int bindingstrengths[12][12] = { evalfn evalfns[12][12] = { /* A F H MO DO AF ( ) { } [ ] */ strand, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F */ + monadfun, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* H */ 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 */ @@ -90,4 +91,15 @@ strand(Datum left, Datum right) result.tag = ArrayTag; result.array = fnCatenateFirst(left.array,fnEnclose(right.array)); return result; +} + +Datum +monadfun(Datum left, Datum right) +{ + print("Monadic function application\n"); + Datum result; + result.tag = ArrayTag; + /* TODO handle undefined functions here */ + result.array = monadfunctiondefs[left.code](right.array); + return result; }
\ No newline at end of file diff --git a/functions.c b/functions.c index 6ff653a..1c60a82 100644 --- a/functions.c +++ b/functions.c @@ -4,6 +4,63 @@ #include "apl9.h" +Rune primfuncnames[] = L"+-×÷*⍟⌹○!?|⌈⌊⊥⊤⊣⊢=≠≤<>≥≡≢∨∧⍲⍱↑↓⊂⊃⊆⌷⍋⍒⍳⍸∊⍷∪∩~,⍪⍴⌽⊖⍉⍎⍕"; + +fnmonad monadfunctiondefs[] = { + 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, /* ↓ */ + fnEnclose, /* ⊂ */ + 0, /* ⊃ */ + fnNest, /* ⊆ */ + 0, /* ⌷ */ + 0, /* ⍋ */ + 0, /* ⍒ */ + 0, /* ⍳ */ + 0, /* ⍸ */ + 0, /* ∊ */ + 0, /* ⍷ */ + 0, /* ∪ */ + 0, /* ∩ */ + 0, /* ~ */ + fnRavel, /* , */ + 0, /* ⍪ */ + 0, /* ⍴ */ + 0, /* ⌽ */ + 0, /* ⊖ */ + 0, /* ⍉ */ + 0, /* ⍎ */ + 0, /* ⍕ */ +}; + Array * fnCatenateFirst(Array *left, Array *right) { @@ -4,7 +4,6 @@ #include "apl9.h" -Rune primfuncnames[] = L"+-×÷*⍟⌹○!?|⌈⌊⊥⊤⊣⊢=≠≤<>≥≡≢∨∧⍲⍱↑↓⊂⊃⊆⌷⍋⍒⍳⍸∊⍷∪∩~,⍪⍴⌽⊖⍉⍎⍕"; Rune primmonopnames[] = L"¨⍨⌸⌶&"; Rune primdyadopnames[] = L"⍣.∘⍤⍥@⍠⌺"; Rune primhybridnames[] = L"/\⌿⍀"; @@ -17,6 +16,7 @@ lexline(Rune *line, int *ntoks) Datum *tokens = mallocz(sizeof(Datum) * MAX_LINE_TOKENS, 1); *ntoks = 0; while(offset < len){ + Rune *p; if(isspacerune(line[offset])){ offset++; continue; @@ -30,14 +30,17 @@ lexline(Rune *line, int *ntoks) case ']': tokens[*ntoks].tag = RBracketTag; break; } offset++; - }else if(runestrchr(primfuncnames, line[offset])){ + }else if(p = runestrchr(primfuncnames, line[offset])){ tokens[*ntoks].tag = FunctionTag; + tokens[*ntoks].code = p-primfuncnames; offset++; - }else if(runestrchr(primmonopnames, line[offset])){ + }else if(p = runestrchr(primmonopnames, line[offset])){ tokens[*ntoks].tag = MonadicOpTag; + tokens[*ntoks].code = p-primmonopnames; offset++; - }else if(runestrchr(primdyadopnames, line[offset])){ + }else if(p = runestrchr(primdyadopnames, line[offset])){ tokens[*ntoks].tag = DyadicOpTag; + tokens[*ntoks].code = p-primdyadopnames; offset++; }else if(isdigitrune(line[offset])){ char buf[64]; |