aboutsummaryrefslogtreecommitdiff
path: root/src/parser.h
blob: 62f557b705585684f6bec55e5373b2dff7dc46f6 (plain) (blame)
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
#ifndef ORYX_PARSER_H
#define ORYX_PARSER_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "lexer.h"

enum {
	/* Variable declaration, lhs and rhs may be unused
	   ‘x: lhs = rhs’ */
	ASTDECL,

	/* Constant declaration, lhs and rhs may be unused
	   ‘x: lhs : rhs’ */
	ASTCDECL,

	/* Function prototype
	   ‘(a: b, c: d) rhs’; aux[lhs].fnproto */
	ASTFNPROTO,

	/* Function, lhs is the prototype and rhs is the body block */
	ASTFN,

	/* Braced block, sublist[lhs…rhs] */
	ASTBLK,

	/* Numeric literal */
	ASTNUMLIT,

	/* Typename */
	ASTTYPE,

	/* Return statement, rhs may be unused
	   ‘return rhs’ */
	ASTRET,

	/* Binary add
	   ‘lhs + rhs’ */
	ASTBINADD = '+',

	/* Binary sub
	   ‘lhs - rhs’ */
	ASTBINSUB = '-',
};

typedef uint8_t ast_kind;

#define AST_SOA_BLKSZ (sizeof(ast_kind) + sizeof(size_t) * 4)

struct ast_soa {
	ast_kind *kinds;
	size_t *lexemes;
	struct {
		size_t lhs, rhs;
	} *kids;
	size_t len, cap;
};

#define ast_free(x) free((x).kinds)

/* Parse the tokens in TOKS into an abstract syntax tree */
struct ast_soa parsetoks(struct lexemes_soa toks);

#endif /* !ORYX_PARSER_H */