blob: efecaa08fbb4798d626f559faef6b4139a4e8d25 (
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
|
#ifndef CRBC_ABSTR
#define CRBC_ABSTR
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// ===== EXPRESSIONS =====
enum ExprKind {
EXPR_INT, EXPR_BOOL,
EXPR_CHAR, EXPR_STRING,
EXPR_IDENT
};
typedef struct Expr {
enum ExprKind kind;
void *as;
} Expr;
typedef struct ExprInt {
int value;
} ExprInt;
//
typedef struct ExprChar {
char value;
} ExprChar;
//
typedef struct ExprBool {
bool value;
} ExprBool;
//
typedef struct ExprString {
char *value;
} ExprString;
//
typedef struct ExprIdent {
char *name;
} ExprIdent;
// ===== STATEMENTS =====
typedef struct Stmt {
int _dummy;
} Stmt;
typedef struct Block {
int _dummy;
} Block;
// ===== DECLARATIONS =====
enum DeclKind {
DECL_VAR,
DECL_FUNC,
DECL_TYPE_STRUCT,
DECL_TYPE_ENUM,
DECL_TYPE_UNION
};
typedef struct Decl {
enum DeclKind kind;
void *as;
} Decl;
// variable declaration
typedef struct DeclVar {
char *ident;
char *type; // TODO: replace with Type*
Expr *init; // NULL => declaration only
} DeclVar;
// function declaration
typedef struct DeclFunc {
char *ident;
char *return_type; // NULL => inferred
char *params; // TODO: param list
Block *body; // NULL => forward declaration
} DeclFunc;
// struct type declaration
typedef struct DeclTypeStruct {
char *ident;
char *members; // TODO: member list
} DeclTypeStruct;
// enum type declaration
typedef struct EnumMember {
char *name;
char *value; // NULL => implicit
} EnumMember;
typedef struct DeclTypeEnum {
char *ident;
EnumMember *members;
} DeclTypeEnum;
// union type declaration
typedef struct DeclTypeUnion {
char *ident;
char *members; // TODO: member list
} DeclTypeUnion;
// ===== FUNCTIONS =====
static inline Decl *DeclareVariable(const char *ident, const char *type, Expr *init) {
Decl *decl = malloc(sizeof *decl);
DeclVar *var = malloc(sizeof *var);
var->ident = strdup(ident);
var->type = type ? strdup(type) : NULL;
var->init = init;
decl->kind = DECL_VAR;
decl->as = var;
return decl;
}
static inline Expr *ExprIntLiteral(int value) {
Expr *expr = malloc(sizeof *expr);
ExprInt *lit = malloc(sizeof *lit);
lit->value = value;
expr->kind = EXPR_INT;
expr->as = lit;
return expr;
}
#endif
|