summaryrefslogtreecommitdiff
path: root/parse.c
blob: e059eb5a3cf081ef4a42b5731753edb41dc3e567 (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
#include "parse.h"
#include "Token.h"
#include "Scope.h"
#include <stdio.h>
#include <stdlib.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(Scope *scope, const Token *tok);
static const Token *keyword_const(Scope *scope, const Token *tok);

int parse(const Token *tok)
{
	Scope *scope = scope_create(NULL);
	if (scope == NULL)
		return 1;
	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(scope, tok);
			break;
		case TOK_KW_CONST:
			tok = keyword_const(scope, tok);
			break;
		default:
			unexpected(tok, -1);
			return free(scope), 1;
		}
		if (tok == NULL)
			return free(scope), 1;
	}
	return free(scope), 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(Scope *scope, 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) {
		if (scope_insert(scope, name->s, 0))
			return NULL;
		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);
	if (scope_insert(scope, name->s, 0))
		return NULL;
	return tok + 1;
}

/* constant variable declaration */
static const Token *keyword_const(Scope *scope, 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);
	if (scope_insert(scope, name->s, 0))
		return NULL;
	return tok + 1;
}