-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_nodes.h
123 lines (95 loc) · 2.12 KB
/
ast_nodes.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
/*
* An Abstract Syntax Tree is the ideal in-memory representation of a program
* Contains types and methods for printing, deallocating, and converting to bytecodes
*/
#pragma once
#include <stdbool.h>
#include "list.h"
#include "vm.h"
#include "bytecodes.h"
#define MAKE_EXPR
typedef List ExprList;
// just a placeholder to prevent circular declaration
struct nExpression;
typedef struct {
char* name;
struct nExpression* val;
} NAssignment;
typedef struct {
char* name;
ExprList* args;
} NCall;
typedef struct {
struct nExpression* left;
int op;
struct nExpression* right;
} NBinaryOp;
typedef struct {
char* name;
} NLookup;
// TODO: There's some duplication here. Remove?
typedef struct {
struct nExpression* expr;
ExprList* block;
} NIfStructure;
typedef struct {
struct nExpression* expr;
ExprList* block;
} NWhileStructure;
// typedef struct {
// char* name;
// } NForStructure;
typedef struct {
char* name;
List* arg_list;
ExprList* block;
} NFuncDef;
typedef struct {
List* arg_list;
ExprList* block;
} NAnonFuncDef;
// expression superclass
typedef enum {
NNULL, // if the default is NASSIGNMENT, mistakes lead to segfaults
NASSIGNMENT,
NINTEGER,
NDOUBLE,
NSTRING,
NBOOL,
NARRAY,
NCALL,
NBINARYOP,
NLOOKUP,
NIFSTRUCTURE,
NWHILESTRUCTURE,
// NFORSTRUCTURE
NFUNCDEF,
NANONFUNCDEF,
} ExprType;
typedef struct nExpression {
ExprType type;
union {
NAssignment assignment;
int integer;
double tdouble;
char* string;
bool tbool;
ExprList* array;
NCall call;
NBinaryOp binary_op;
NLookup lookup;
NIfStructure if_structure;
NWhileStructure while_structure;
// NForStructure for_structure;
NFuncDef func_def;
NAnonFuncDef anon_func_def;
};
} NExpression;
NExpression* ast_expr_new(ExprType type);
void ast_list_print(ExprList* list, int indent);
void ast_exp_print(NExpression* expr, int indent);
void ast_list_free(ExprList* list);
void ast_expr_free(NExpression* expr);
bool ast_compile(ExprList* root, file_blob_t* output);
bool ast_list_compile(ExprList* list, List* output, file_blob_t* blob);
bool ast_expr_compile(NExpression* expr, List* output, file_blob_t* blob);