summaryrefslogtreecommitdiff
path: root/lexer.c
blob: 40c7f35660d67928ce6261c52b36c31384ad1727 (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
#include <u.h>
#include <libc.h>
#include <bio.h>

#include "apl9.h"

Rune primmonopnames[] = L"¨⍨⌸⌶&";
Rune primdyadopnames[] = L"⍣.∘⍤⍥@⍠⌺";
Rune primhybridnames[] = L"/\⌿⍀";

Datum *
lexline(Rune *line, int *ntoks)
{
	int offset = 0;
	int len = runestrlen(line);
	Datum *tokens = mallocz(sizeof(Datum) * MAX_LINE_TOKENS, 1);
	*ntoks = 0;
	while(offset < len){
		Rune *p;
		if(isspacerune(line[offset])){
			offset++;
			continue;
		}else if(runestrchr(L"(){}[]", line[offset])){
			switch(line[offset]){
			case '(': tokens[*ntoks].tag = LParTag; break;
			case ')': tokens[*ntoks].tag = RParTag; break;
			case '{': tokens[*ntoks].tag = LCurlTag; break;
			case '}': tokens[*ntoks].tag = RCurlTag; break;
			case '[': tokens[*ntoks].tag = LBracketTag; break;
			case ']': tokens[*ntoks].tag = RBracketTag; break;
			}
			offset++;
		}else if(p = runestrchr(primfuncnames, line[offset])){
			tokens[*ntoks].tag = FunctionTag;
			tokens[*ntoks].code = p-primfuncnames;
			offset++;
		}else if(p = runestrchr(primmonopnames, line[offset])){
			tokens[*ntoks].tag = MonadicOpTag;
			tokens[*ntoks].code = p-primmonopnames;
			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];
			char *p = buf;
			while(isdigitrune(line[offset])){
				p += runetochar(p, &line[offset]);
				offset++;
			}
			*p = 0;
			tokens[*ntoks].tag = ArrayTag;
			tokens[*ntoks].array = mkscalarint(atoll(buf));
		}else{
			print("Can't lex: %S\n", &line[offset]);
			*ntoks = 0;
			break;
		}
		(*ntoks)++;
	}
	return tokens;
}