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

static void unexpected(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 + 1);
			break;
		case TOK_KW_VAR:
			tok = keyword_var(tok + 1);
			break;
		case TOK_KW_CONST:
			tok = keyword_const(tok + 1);
			break;
		default:
			unexpected(tok);
			return 1;
		}
	}
	return 0;
}

static void unexpected(const Token *tok)
{
	fprintf(stderr, "unexpected %s %s%s%sat %u:%u\n",
		token_type_str(tok->type),
		(tok->s != NULL) ? "(" : "",
		(tok->s != NULL) ? tok->s : "",
		(tok->s != NULL) ? ") " : "",
		tok->line, tok->column);
}

static const Token *keyword_fn(const Token *tok)
{
	return tok;
}

static const Token *keyword_var(const Token *tok)
{
	return tok;
}

static const Token *keyword_const(const Token *tok)
{
	return tok;
}