-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotaru.h
182 lines (148 loc) · 3.38 KB
/
hotaru.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef HOTARU_H_
#define HOTARU_H_
#include <stdint.h>
#include "hvm.h"
#include "utils.h"
typedef enum hResult {
HRES_OK = 0,
HRES_INVALID_VARIABLE,
} hResult;
typedef enum hLogLevel {
HLOG_FATAL = 0,
HLOG_ERROR,
HLOG_WARN,
HLOG_INFO,
COUNT_HLOG_LEVELS,
} hLogLevel;
typedef struct hPosition {
uint32_t row, col;
} hPosition;
typedef struct hExpr hExpr;
typedef enum hBinOpType {
HBINOP_NONE = 0,
HBINOP_ADD,
HBINOP_SUB,
HBINOP_MUL,
HBINOP_EQ,
HBINOP_NE,
HBINOP_GT,
HBINOP_GE,
HBINOP_LT,
HBINOP_LE,
COUNT_HBINOP_TYPES,
} hBinOpType;
typedef enum hExprType {
HEXPR_NONE = 0,
HEXPR_INT_LITERAL,
HEXPR_FLOAT_LITERAL,
HEXPR_BINOP,
HEXPR_VAR_READ,
} hExprType;
typedef struct hBinOpExpr {
hPosition pos;
hBinOpType type;
hExpr *left;
hExpr *right;
} hBinOpExpr;
struct hExpr {
hPosition pos;
hExprType type;
union {
int64_t int_literal;
double float_literal;
hBinOpExpr binop;
struct {
StringView name;
} var_read;
} as;
};
typedef struct hStmt hStmt;
typedef struct hBlock {
hPosition pos;
hStmt *items;
uint32_t count;
uint32_t capacity;
} hBlock;
typedef enum hStmtType {
HSTMT_NONE = 0,
HSTMT_VAR_INIT,
HSTMT_VAR_ASSIGN,
HSTMT_WHILE,
HSTMT_IF,
HSTMT_FUNC_DEF,
HSTMT_DUMP,
} hStmtType;
typedef struct hVarInitAndAssignStmt {
hPosition pos;
StringView name;
hExpr value;
} hVarInitAndAssignStmt;
typedef struct hWhileStmt {
hPosition pos;
hExpr condition;
hBlock body;
} hWhileStmt;
typedef struct hElifBlock {
hPosition pos;
hExpr condition;
hBlock body;
} hElifBlock;
typedef struct hFuncDef {
hBlock body;
} hFuncDef;
typedef struct hIfStmt {
hPosition pos;
hExpr condition;
hBlock body;
// TODO: maybe linked list will be more efficient
struct {
hElifBlock *items;
uint32_t count;
uint32_t capacity;
} _elif;
hBlock _else;
} hIfStmt;
struct hStmt {
hPosition pos;
hStmtType type;
union {
hVarInitAndAssignStmt var_init;
hVarInitAndAssignStmt var_assign;
hWhileStmt _while;
hIfStmt _if;
hExpr dump;
hExpr expr;
} as;
};
typedef struct hVarBinding {
StringView name;
uint32_t pos; // addr or absolute index in stack
} hVarBinding;
typedef struct hScope hScope;
struct hScope {
hScope *prev;
hVarBinding *items;
ut_size count;
ut_size capacity;
};
typedef struct hState {
HVM vm;
Arena arena;
hScope global;
HVM_Module mod;
uint32_t vsp; // virtual stack pointer
uint32_t vss; // virtual stack scope
hScope *current;
} hState;
void hlog_message(hLogLevel level, const char *fmt, ...);
hVarBinding *hscope_find(const hScope *scope, StringView name);
hVarBinding *hscope_append(hScope *scope, hVarBinding binding, Arena *arena);
void hstate_init(hState *state);
void hstate_deinit(hState *state);
hResult hstate_exec_expr(hState *state, const hExpr *expr);
hResult hstate_exec_stmt(hState *state, const hStmt *stmt);
hResult hstate_exec_source(hState *state, const char *source);
hResult hstate_compile_expr(hState *state, const hExpr *expr);
hResult hstate_compile_stmt(hState *state, const hStmt *stmt);
hResult hstate_compile_source(hState *state, const char *source);
#endif // HOTARU_H_