summaryrefslogtreecommitdiff
path: root/parser.c
blob: 987f26d14e3bdd2725e1236d6be6b8c21c4408ee (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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lexer.h"
#include "common.h"
#include "error.h"
#include "logic.h"
#include "gate.h"


static FILE* fd;
static struct token *cursor;

#ifdef emit_error
#warning "Remember to remove the global emit_error"
#undef emit_error
#endif /* ifdef emit_error */

#define emit_error(...) fprintf(stderr, "Error (%zd,%zd): ", cursor->loc.line, cursor->loc.column);\
                        fprintf(stderr, __VA_ARGS__);\
                        indicate_file_area(fd, cursor->loc.line, cursor->loc.column, cursor->span)


//static struct op_lookup uop_handlers[] = {
//	{.str = "not",   .handler = logic_nand},
//};
//
//static struct op_lookup bop_handlers[] = {
//	{.str = "and",   .handler = logic_and},
//	{.str = "or",    .handler = logic_or},
//	{.str = "nand",  .handler = logic_nand},
//	{.str = "nor",   .handler = logic_nor},
//	{.str = "xor",   .handler = logic_xor}
//};
//
//static struct tok_lookup tok_handlers[] = {
//	{.str = "input",   .handler = parse_input},
//	{.str = "module", .handler = parse_module},
//	{.str = "expr",   .handler = parse_expr}
//};

static int
expect(enum TOKEN_TYPE e) {
	char *expected_desc = "(internal error)";
	char *observed_desc = "(internal error)";

	if (cursor->type != e) {
		expected_desc = get_token_description(e);
		observed_desc = get_token_description(cursor->type);
		emit_error("Expected %s, got %s\n", expected_desc, observed_desc);
		return 1;
	}

	return 0;
}

static void
kerchunk() {
	cursor = cursor->next;
}

int
parse(FILE *f, struct token *t) {
	char *token_desc = "(internal error)";
	fd = f;
	cursor = t;

	if (expect(TOK_MODULE)) {
		return 1;
	}
	kerchunk();

	if (expect(TOK_IDENT)) {
		return 1;
	}
	emit_error("Debug: module is named %s\n", cursor->value);
	kerchunk();

	if (expect(TOK_EOL)) {
		return 1;
	}
	kerchunk();

	while (cursor) {
		switch(cursor->type) {
			case TOK_EOL:
				kerchunk();
				break;
			/* FIXME implement everything */
			default:
				token_desc = get_token_description(cursor->type);
				emit_error("Unexpected %s\n", token_desc);
				return 1;
		}
	}
	return 0;
}