summaryrefslogtreecommitdiff
path: root/parse.c
blob: d2e1bcd9a6fab83c715fe2f85dfe58d78bb1b992 (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
#include "parse.h"
#include "Token.h"
#include <stdio.h>

static void *unexpected(const Token *tok, int n);
static const Token *expression(const Token *tok);
static const Token *keyword_fn(const Token *tok);
static const Token *keyword_var(const Token *tok);
static const Token *keyword_const(const Token *tok);

int parse(const Token *tok)
{
	while (tok != NULL && tok->type != TOK_NONE) {
		switch (tok->type) {
		case TOK_KW_FN:
			tok = keyword_fn(tok);
			break;
		case TOK_KW_VAR:
			tok = keyword_var(tok);
			break;
		case TOK_KW_CONST:
			tok = keyword_const(tok);
			break;
		default:
			unexpected(tok, -1);
			return 1;
		}
		if (tok == NULL)
			return 1;
	}
	return 0;
}

/* write unexpected token error to stderr before returning NULL,
 * `n` is used to mark the error in code */
static void *unexpected(const Token *tok, int n)
{
	fprintf(stderr, "[%d] unexpected %s %s%s%sat %u:%u\n",
		n, token_type_str(tok->type),
		(tok->s != NULL) ? "(" : "",
		(tok->s != NULL) ? tok->s : "",
		(tok->s != NULL) ? ") " : "",
		tok->line, tok->column);
	return NULL;
}

/* any chain of instructions that results in a value */
static const Token *expression(const Token *tok)
{
	if (tok->type == TOK_INTEGER)
		return tok + 1;
	if (tok->type == TOK_STRING)
		return tok + 1;
	if (tok->type == TOK_WORD)
		return tok + 1;
	return unexpected(tok, 20);
}

static const Token *keyword_fn(const Token *tok)
{
	tok += 1;
	if (tok->type != TOK_WORD)
		return unexpected(tok, 0);
	const Token *name = tok;
	tok += 1;
	(void)name;
	if (tok->type != TOK_PAREN_OPEN)
		return unexpected(tok, 1);
	tok += 1;
	size_t args = 0;
	while (tok->type == TOK_WORD) {
		tok += 1;
		args += 1;
		if (tok->type == TOK_COMMA)
			tok += 1;
	}
	if (tok->type != TOK_PAREN_CLOS)
		return unexpected(tok, 2);
	printf("%zu args\n", args);
	return tok + 1;
}

/* mutable variable declaration */
static const Token *keyword_var(const Token *tok)
{
	tok += 1;
	if (tok->type != TOK_WORD)
		return unexpected(tok, 10);
	const Token *name = tok;
	(void)name;
	tok += 1;
	/* no initialization */
	if (tok->type == TOK_SEMICOLON)
		return tok + 1;
	if (tok->type != TOK_ASSIGN)
		return unexpected(tok, 11);
	tok = expression(tok + 1);
	if (tok == NULL)
		return NULL;
	if (tok->type != TOK_SEMICOLON)
		return unexpected(tok, 12);
	return tok + 1;
}

/* constant variable declaration */
static const Token *keyword_const(const Token *tok)
{
	tok += 1;
	if (tok->type != TOK_WORD)
		return unexpected(tok, 10);
	const Token *name = tok;
	(void)name;
	tok += 1;
	if (tok->type != TOK_ASSIGN)
		return unexpected(tok, 11);
	tok = expression(tok + 1);
	if (tok == NULL)
		return NULL;
	if (tok->type != TOK_SEMICOLON)
		return unexpected(tok, 12);
	return tok + 1;
}