summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apl9.h8
-rw-r--r--eval.c18
-rw-r--r--functions.c57
-rw-r--r--lexer.c11
4 files changed, 85 insertions, 9 deletions
diff --git a/apl9.h b/apl9.h
index e487b63..41ad5e2 100644
--- a/apl9.h
+++ b/apl9.h
@@ -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
diff --git a/eval.c b/eval.c
index 9c737ff..da49137 100644
--- a/eval.c
+++ b/eval.c
@@ -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)
{
diff --git a/lexer.c b/lexer.c
index 21a5da8..40c7f35 100644
--- a/lexer.c
+++ b/lexer.c
@@ -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];