aboutsummaryrefslogtreecommitdiff
path: root/dat.h
blob: 4e0a120e93201dc29e2c77c9a3e3f03a9a8bc224 (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
135
136
137
138
139
/*
 * Copyright ©️ 2022 Mario Forzanini <mf@marioforzanini.com>
 *
 * This file is part of dwrt.
 *
 * Dwrt is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * Dwrt is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dwrt. If not, see <https://www.gnu.org/licenses/>.
 *
 */

#define LEN(x) (sizeof((x)) / sizeof((x)[0]))
#define KNOWN_FUNCS 8
#define KNOWN_OPERATORS 5

enum lex_states {
	LS_ERROR,
	LS_NUMBER,
	LS_PAREN,
	LS_SYMBOL,
	LS_WS
};

enum lexeme_types {
	LE_ERROR,
	LE_EOF,
	LE_NUMBER,
	LE_OPERATOR,
	LE_SYMBOL,
	LE_RPAREN,
	LE_LPAREN
};

enum symbol_type {
	S_FUNC,
	S_LPAREN,
	S_NUM,
	S_OP,
	S_RPAREN,
	S_VAR
};

enum funcs {
	COS = 0x00,
	COSH,
	EXP,
	LOG,
	SIN,
	SINH,
	TAN,
	TANH
};

static struct func_to_bit {
	char *func;
	uint8_t bit;
} known_funcs[] = {
	{"cos", COS},
	{"cosh", COSH},
	{"exp", EXP},
	{"log", LOG},
	{"sin", SIN},
	{"sinh", SINH},
	{"tan", TAN},
	{"tanh", TANH}
};

enum operators {
	EXPT = 0x00,
	FRAC = 0x10,
	MUL = 0x20,
	SUB = 0x30,
	SUM = 0x40
};

static struct op_to_bit {
	char op;
	uint8_t bit;
} known_operators[] = {
	{'^', EXPT},
	{'/', FRAC},
	{'*', MUL},
	{'-', SUB},
	{'+', SUM}
};

#define IS_FUNC 0x0F;
#define IS_OP 0xF0;

typedef struct Lexeme Lexeme;
typedef struct Lexer Lexer;
typedef struct Node Node;
typedef struct Parser Parser;
typedef struct Symbol Symbol;

typedef Node* (*Derivative)(Node*, char);

struct Lexer {
	size_t len; /* data length */
	enum lex_states state; /* where was I? */
	char *filename, *err;
	char *data, *pos; /* contents of filename, current position */
};

struct Lexeme {
	enum lexeme_types type;
	size_t len;
	char *lexeme;
};

struct Node {
	Node *left, *right;
	Symbol *sym;
};

struct Parser {
	char *err;
	Lexer *l;
	Node *ast;
};

struct Symbol {
	enum symbol_type type;
	union {
		uint8_t func;
		double num;
		char var;
	} content;
};