summaryrefslogtreecommitdiff
path: root/lexer.c
blob: 54e6e9b381add4156ad8730207a3b6b08c14fb4b (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <u.h>
#include <libc.h>
#include <bio.h>

#include "apl9.h"

enum {
	InputTypeBio,
	InputTypeString,
};

typedef struct InputStream InputStream;
struct InputStream
{
	int tag;
	int offset;
	Rune last;
	Biobuf *bio;
	Rune *string;
};

int inputEOF(InputStream *);
Rune getrune(InputStream *);
void ungetrune(InputStream *);

Statement *lexline(InputStream *, int);

Statement *
lexlinebio(Biobuf *bio, int toplevel)
{
	InputStream in;
	in.tag = InputTypeBio;
	in.offset = 0;
	in.last = 0;
	in.bio = bio;
	in.string = nil;
	return lexline(&in, toplevel);
}

Statement *
lexlinestr(Rune *str, int toplevel)
{
	InputStream in;
	in.tag = InputTypeString;
	in.offset = 0;
	in.last = 0;
	in.bio = nil;
	in.string = str;
	return lexline(&in, toplevel);	
}

Statement *
lexline(InputStream *input, int toplevel)
{
	Statement *stmt = emalloc(sizeof(Statement));
	stmt->ntoks = 0;
	stmt->toks = mallocz(sizeof(Datum) * MAX_LINE_TOKENS, 1);
	stmt->guard = nil;
	stmt->next = nil;

	Rune peek = getrune(input);
	while(!inputEOF(input) && (peek != '\n' || toplevel == 0)){
		Rune *p;
		if(isspacerune(peek) && peek != '\n'){
			peek = getrune(input);
			continue;
		}else if(runestrchr(L"←⋄\n⍝⍬", peek)){
			switch(peek){
			case L'←': stmt->toks[stmt->ntoks].tag = ArrowTag; break;
			case L'\n':
			case L'⋄':
				if(stmt->ntoks == 0)
					stmt = lexline(input, toplevel);
				else
					stmt->next = lexline(input, toplevel);
				goto end;
			case L'⍝': goto end;
			case L'⍬':
				stmt->toks[stmt->ntoks].tag = ArrayTag;
				stmt->toks[stmt->ntoks].array = allocarray(AtypeInt, 1, 0);
				stmt->toks[stmt->ntoks].array->shape[0] = 0;
				break;
			}
		}else if(!toplevel && peek == ':'){
			Rune buf[MAX_LINE_LENGTH];
			Rune *p = buf;
			while((peek = getrune(input)) != L'⋄' && peek != '\n' && !inputEOF(input))
				*p++ = peek;
			*p = 0;
			ungetrune(input);
			stmt->guard = lexlinestr(buf, toplevel);
			stmt->ntoks--;
		}else if(peek == '{'){
			int unclosed = 1;
			int oplevel = 0; /* 1 = monadic operator, 2 = dyadic operator */
			Rune buf[MAX_LINE_LENGTH];
			Rune *p = buf;
			while(((peek = getrune(input)) != '}' || unclosed > 1) && !inputEOF(input)){
				if(unclosed == 1 && peek == L'⍶' && oplevel == 0)
					oplevel = 1;
				else if(unclosed && peek == L'⍹')
					oplevel = 2;
				if(peek == '{')
					unclosed++;
				else if(peek == '}')
					unclosed--;
				*p++ = peek;
			}
			if(peek != '}')
				goto syntax_error;
			*p = 0;
			if(oplevel == 0){
				stmt->toks[stmt->ntoks].tag = FunctionTag;
				stmt->toks[stmt->ntoks].func.type = FunctypeDfn;
				stmt->toks[stmt->ntoks].func.dfn = runestrdup(buf);
			}else{
				stmt->toks[stmt->ntoks].tag = oplevel == 1 ? MonadicOpTag : DyadicOpTag;
				stmt->toks[stmt->ntoks].operator.type = OperatortypeDop;
				stmt->toks[stmt->ntoks].operator.dyadic = oplevel == 2;
				stmt->toks[stmt->ntoks].operator.dop = runestrdup(buf);
			}
		}else if(peek == '('){
			int unclosed = 1;
			Rune buf[MAX_LINE_LENGTH];
			Rune *p = buf;
			while(((peek = getrune(input)) != ')' || unclosed > 1) && !inputEOF(input) && peek != '\n'){
				if(peek == '(')
					unclosed++;
				else if(peek == ')')
					unclosed--;
				*p++ = peek;
			}
			if(peek != ')')
				goto syntax_error;
			*p = 0;
			stmt->toks[stmt->ntoks].tag = LParTag;
			stmt->toks[stmt->ntoks].stmt = *lexlinestr(buf, toplevel);
			stmt->ntoks++;
			stmt->toks[stmt->ntoks].tag = RParTag;
		}else if(p = runestrchr(primfuncnames, peek)){
			stmt->toks[stmt->ntoks].tag = FunctionTag;
			stmt->toks[stmt->ntoks].func.type = FunctypePrim;
			stmt->toks[stmt->ntoks].func.code = p-primfuncnames;
		}else if(p = runestrchr(primmonopnames, peek)){
			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;
		}else if(p = runestrchr(primdyadopnames, peek)){
			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;
		}else if(p = runestrchr(primhybridnames, peek)){
			stmt->toks[stmt->ntoks].tag = HybridTag;
			stmt->toks[stmt->ntoks].hybrid = p-primhybridnames;
		}else if(isdigitrune(peek) || peek == L'¯'){
			char buf[64];
			char *p = buf;
			int floating = 0;
			if(peek == L'¯'){
				*p++ = '-';
				peek = getrune(input);
			}
get_digits:
			while(isdigitrune(peek)){
				p += runetochar(p, &peek);
				peek = getrune(input);
			}
			if(!floating && peek == '.'){
				p += runetochar(p, &peek);
				peek = getrune(input);
				floating = 1;
				goto get_digits;
			}
			*p = 0;
			ungetrune(input);
			stmt->toks[stmt->ntoks].tag = ArrayTag;
			stmt->toks[stmt->ntoks].array = floating ? mkscalarfloat(atof(buf)) : mkscalarint(atoll(buf));
		}else if(runestrchr(L"⍺⍵⍶⍹", peek)){
			Rune name[2] = {peek, 0};
			stmt->toks[stmt->ntoks].tag = NameTag;
			stmt->toks[stmt->ntoks].symbol = getsym(name, 1);
		}else if(isalpharune(peek)){
			Rune buf[64];
			Rune *p = buf;
			while(isalpharune(peek) || isdigitrune(peek)){
				*p++ = peek;
				peek = getrune(input);
			}
			*p = 0;
			ungetrune(input);
			stmt->toks[stmt->ntoks].tag = NameTag;
			stmt->toks[stmt->ntoks].symbol = getsym(buf, 0);
		}else if(runestrchr(L"⎕⍞", peek)){
			/* quad names */
			Rune buf[64];
			Rune *p = buf;
			*p++ = peek;
			peek = getrune(input);
			while(isalpharune(peek)){
				*p++ = toupperrune(peek);
				peek = getrune(input);
			}
			*p = 0;
			ungetrune(input);
			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)
				goto syntax_error;
		}else if(peek == '\''){
			Rune buf[1024]; /* stupid limit on literal string lengths */
			Rune *b = buf;
			int done = 0;
			peek = getrune(input);
			while(!done && !inputEOF(input)){
				if(peek == '\''){
					peek = getrune(input);
					if(peek != '\''){
						*b = 0;
						done = 1;
						ungetrune(input);
					}else{
						*b++ = '\'';
						peek = getrune(input);
					}
				}else{
					*b++ = peek;
					peek = getrune(input);
				}
			}
			if(!done)
				goto syntax_error;
			stmt->toks[stmt->ntoks].tag = ArrayTag;
			if(runestrlen(buf) == 1)
				stmt->toks[stmt->ntoks].array = mkscalarrune(buf[0]);
			else
				stmt->toks[stmt->ntoks].array = mkrunearray(buf);
		}else{
			Rune *err;
syntax_error:
			err = runesmprint("Can't lex");
			free(stmt->toks);
			free(stmt);
			throwerror(err, ESyntax);
		}
		/* print("Got token: %S\n", ppdatum(stmt->toks[stmt->ntoks])); */
		stmt->ntoks++;
		peek = getrune(input);
	}
end:
	stmt->toks = realloc(stmt->toks, sizeof(Datum) * stmt->ntoks);
	return stmt;
}

int
inputEOF(InputStream *i)
{
	int eof;
	if(i->tag == InputTypeBio)
		eof = i->last == Beof;
	else
		eof = i->last == 0;
	/*if(eof) print("EOF\n");*/
	return eof;
}

Rune
getrune(InputStream *i)
{
	Rune r;
	if(i->tag == InputTypeBio)
		r = Bgetrune(i->bio);
	else{
		if(i->string[i->offset] == 0)
			r = 0;
		else
			r = i->string[i->offset++];
	}
	/* print("Get rune: '%C' (%d)\n", r, r); */
	i->last = r;
	return r;
}

void
ungetrune(InputStream *i)
{
	/* print("Unget rune: '%C' (%d)\n", i->last, i->last); */
	if(inputEOF(i))
		return;

	if(i->tag == InputTypeBio)
		Bungetrune(i->bio);
	else
		i->offset--;
}