aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: ed9c4130ac5c0e6d5fc925cd80a7edc79d650e5e (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
#include "opt.h"
#include "err.h"
#include "tok.h"

#include <stdio.h>
#include <errno.h>




int main(int argc, char **argv)
{
	FILE *fi = stdin;
	struct tok_s t;

	opt_parse(argc, argv);

	if (opt_infile_str() != NULL) {
		fi = fopen(opt_infile_str(), "r");
		TRY(fi != NULL, "could not read file `%s`", opt_infile_str());
	}

	while (1) {
		t = tok_get(fi);

		if (t.type == TOK_UNKNWN || t.type == TOK_END)
			break;

		printf("%s:%zu:%zu: ", (fi == stdin ? "stdin" : opt_infile_str()),
				t.line, t.col);

		if (t.type > TOK_QMARK) {
			printf("%u, `%s`\n", t.type, t.val);
		} else {
			printf("%u\n", t.type);
		}
	};

	if (t.type == TOK_UNKNWN) {
		printf("%s:%zu:%zu: error: unrecognised token `%s`\n",
				(fi == stdin ? "stdin" : opt_infile_str()),
				t.line, t.col, t.val);
	}

	fclose(fi);

	return 0;
}