summaryrefslogtreecommitdiff
path: root/lexer.c
blob: be87bf81c1b4e7dfb9a064c02c66f80266194ac3 (plain)
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
#include "lexer.h"
#include "slice.h"
#include "token.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

static int
iswordy(int c)
{
	if (isalpha(c) || c == '_')
		return 1;
	return 0;
}

static unsigned int
simple_token(char c)
{
	switch (c) {
	case '{': return TOK_OPEN_CURL;
	case '}': return TOK_CLOS_CURL;
	case '(': return TOK_OPEN_PAREN;
	case ')': return TOK_CLOS_PAREN;
	case '[': return TOK_OPEN_SQUAR;
	case ']': return TOK_CLOS_SQUAR;
	case '=': return TOK_ASSIGN;
	case ';': return TOK_END;
	default:  return TOK_NONE;
	}
}

static Slice
slice_match(Slice slice, int (*isfun)(int c))
{
	for (int i = slice.begin; i < slice.end; i++)
		if (!isfun(slice.str[i]))
			return slice_sub(slice, slice.begin, i);
	return slice;
}

Token *
lexer(Slice slice)
{
	Token *toks = NULL;
	int i = slice.begin;
	while (i < slice.end) {
		// Skip whitespaces.
		if (isspace(slice.str[i])) {
			i += 1;
			continue;
		}

		// Ignore comments.
		if (slice.str[i] == '/' && slice.str[i + 1] == '/') {
			while (i < slice.end && slice.str[i] != '\n')
				i += 1;
			continue;
		}

		// Single character tokens.
		if (simple_token(slice.str[i]) != TOK_NONE) {
			Slice sub = slice_sub(slice, i, i + 1);
			token_append(&toks, token_create(sub,
			             simple_token(slice.str[i])));
			if (slice.str[i] == '}')
				token_append(&toks,
				             token_create(sub, TOK_END));
			i += 1;
			continue;
		}

		// Number token.
		if (isdigit(slice.str[i])) {
			Slice number;
			number = slice_match(slice_sub(slice, i, slice.end),
			                     isdigit);
			token_append(&toks, token_create(number, TOK_NUMBER));
			i = number.end;
			continue;
		}

		// Word token, and keywords by extension.
		if (iswordy(slice.str[i])) {
			Slice word = slice_match(slice_sub(slice, i, slice.end),
			                         iswordy);

			// Keywords.
			unsigned int type = TOK_WORD;
			if (slice_equal(word, slice_from_str("while")))
				type = TOK_KW_WHILE;
			else if (slice_equal(word, slice_from_str("if")))
				type = TOK_KW_IF;
			else if (slice_equal(word, slice_from_str("else")))
				type = TOK_KW_ELSE;
			else if (slice_equal(word, slice_from_str("let")))
				type = TOK_KW_LET;
			else if (slice_equal(word, slice_from_str("return")))
				type = TOK_KW_RETURN;

			token_append(&toks, token_create(word, type));
			i = word.end;
			continue;
		}

		// String token.
		if (slice.str[i] == '"') {
			int close = i + 1;
			while (close < slice.end && slice.str[close] != '"')
				close += 1;
			if (slice.str[close] != '"') {
				// XXX: Streamline this kind of error handling.
				fprintf(stderr, "unclosed string\n");
				token_destroy(toks);
				return NULL;
			}
			Slice string = slice_sub(slice, i + 1, close);
			token_append(&toks, token_create(string, TOK_STRING));
			i = close + 1;
			continue;
		}

		// Character token.
		if (slice.str[i] == '\'') {
			if (slice.str[i + 1] == '\0' ||
			    slice.str[i + 2] != '\'') {
				fprintf(stderr, "unclosed character\n");
				token_destroy(toks);
				return NULL;
			}
			Slice character = slice_sub(slice, i + 1, i + 2);
			token_append(&toks, token_create(character, TOK_CHAR));
			i += 3;
			continue;
		}

		printf("skipping '%c'\n", slice.str[i]);
		i += 1;
	}
	return toks;
}