1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "apl9.h"
Rune primhybridnames[] = L"/\⌿⍀";
Statement *
lexline(Rune *line)
{
int offset = 0;
int len = runestrlen(line);
Statement *stmt = emalloc(sizeof(Statement));
stmt->ntoks = 0;
stmt->toks = mallocz(sizeof(Datum) * MAX_LINE_TOKENS, 1);
stmt->next = nil;
while(offset < len){
Rune *p;
if(isspacerune(line[offset])){
offset++;
continue;
}else if(runestrchr(L"[]←⋄⍝", line[offset])){
switch(line[offset]){
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]); goto end;
case L'⍝': goto end;
}
offset++;
}else if(line[offset] == '{'){
Rune buf[MAX_LINE_LENGTH];
Rune *p = buf;
offset++;
while(line[offset] != '}' && offset < len){
*p = line[offset];
p++;
offset++;
}
if(line[offset] != '}')
goto syntax_error;
*p = 0;
offset++;
stmt->toks[stmt->ntoks].tag = FunctionTag;
stmt->toks[stmt->ntoks].func.type = FunctypeDfn;
stmt->toks[stmt->ntoks].func.dfn = runestrdup(buf);
}else if(line[offset] == '('){
int unclosed = 1;
Rune buf[MAX_LINE_LENGTH];
Rune *p = buf;
offset++;
while((line[offset] != ')' || unclosed > 1) && offset < len){
if(line[offset] == '(')
unclosed++;
else if(line[offset] == ')')
unclosed--;
*p = line[offset];
p++;
offset++;
}
if(line[offset] != ')')
goto syntax_error;
*p = 0;
offset++;
stmt->toks[stmt->ntoks].tag = LParTag;
stmt->toks[stmt->ntoks].stmt = *lexline(buf);
stmt->ntoks++;
stmt->toks[stmt->ntoks].tag = RParTag;
}else if(p = runestrchr(primfuncnames, line[offset])){
stmt->toks[stmt->ntoks].tag = FunctionTag;
stmt->toks[stmt->ntoks].func.type = FunctypePrim;
stmt->toks[stmt->ntoks].func.code = p-primfuncnames;
offset++;
}else if(p = runestrchr(primmonopnames, line[offset])){
stmt->toks[stmt->ntoks].tag = MonadicOpTag;
stmt->toks[stmt->ntoks].operator.type = OperatortypePrim;
stmt->toks[stmt->ntoks].operator.dyadic = 0;
stmt->toks[stmt->ntoks].operator.code = p-primmonopnames;
offset++;
}else if(p = runestrchr(primdyadopnames, line[offset])){
stmt->toks[stmt->ntoks].tag = DyadicOpTag;
stmt->toks[stmt->ntoks].operator.type = OperatortypePrim;
stmt->toks[stmt->ntoks].operator.dyadic = 1;
stmt->toks[stmt->ntoks].operator.code = p-primdyadopnames;
offset++;
}else if(isdigitrune(line[offset])){
char buf[64];
char *p = buf;
int floating = 0;
get_digits:
while(isdigitrune(line[offset]))
p += runetochar(p, &line[offset++]);
if(!floating && line[offset] == '.'){
p += runetochar(p, &line[offset++]);
floating = 1;
goto get_digits;
}
*p = 0;
stmt->toks[stmt->ntoks].tag = ArrayTag;
stmt->toks[stmt->ntoks].array = floating ? mkscalarfloat(atof(buf)) : 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])){
Rune buf[64];
Rune *p = buf;
while(isalpharune(line[offset])){
*p = line[offset];
p++;
offset++;
}
*p = 0;
stmt->toks[stmt->ntoks].tag = NameTag;
stmt->toks[stmt->ntoks].symbol = getsym(currentsymtab, buf);
}else if(runestrchr(L"⎕⍞", line[offset])){
/* quad names */
Rune buf[64];
Rune *p = buf;
*p++ = line[offset++];
while(isalpharune(line[offset]))
*p++ = toupperrune(line[offset++]);
*p = 0;
int valid = 0;
for(int i = 0; quadnames[i].name != nil && !valid; i++){
if(runestrcmp(buf, quadnames[i].name) != 0)
continue;
valid = 1;
stmt->toks[stmt->ntoks] = quadnamedatum(quadnames[i]);
}
if(!valid){
offset -= runestrlen(buf);
goto syntax_error;
}
}else{
syntax_error:
print("Can't lex: %S\n", &line[offset]);
free(stmt->toks);
free(stmt);
return 0;
}
stmt->ntoks++;
}
end:
stmt->toks = realloc(stmt->toks, sizeof(Datum) * stmt->ntoks);
return stmt;
}
|