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
|
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
typedef enum ASTtype{
AST_BLOCK,
AST_OP,
AST_NUM,
AST_STR,
AST_VAR,
AST_CALL,
AST_IF,
AST_WHILE,
AST_FUNC,
AST_PROGRAM,
} ASTtype;
typedef struct AST AST;
typedef struct ASTblock{
int len;
AST **exprs;
} ASTblock;
typedef struct ASTop{
const char *op; // Constant string, does not need to be freed
AST *left,*right;
} ASTop;
typedef struct ASTnum{
bool isint;
union {
int64_t i;
double d;
};
} ASTnum;
typedef struct ASTstr{
int len; // Excludes terminating null char
char *str; // May contain null chars before terminating null char
} ASTstr;
typedef struct ASTvar{
char *name;
} ASTvar;
typedef struct ASTcall{
char *func;
int nargs;
AST **args;
} ASTcall;
typedef struct ASTif{
AST *cond;
AST *thenb,*elseb;
} ASTif;
typedef struct ASTwhile{
AST *cond;
AST *body;
} ASTwhile;
typedef struct ASTfunc{
char *name;
int nargs;
char **args;
AST *body;
} ASTfunc;
typedef struct ASTprogram{
int nfuncs;
AST **funcs;
} ASTprogram;
typedef struct AST{
ASTtype type;
union {
ASTblock b;
ASTop o;
ASTnum n;
ASTstr s;
ASTvar v;
ASTcall c;
ASTif i;
ASTwhile w;
ASTfunc f;
ASTprogram p;
};
} AST;
typedef enum Associativity{
AS_PREFIX,
AS_SUFFIX,
AS_LEFT,
AS_RIGHT,
AS_NONASSOC
} Associativity;
AST* parse(const char *source,char **errmsg);
void ast_debug(FILE *stream,const AST *ast);
void ast_free(AST *ast);
|