summaryrefslogtreecommitdiff
path: root/src/heads/abstract.h
blob: 3140742c9196c9251aee3586e40a4ed5647de052 (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
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
#ifndef CRBC_ABSTR_H
#define CRBC_ABSTR_H

#include <stdio.h> // FILE, fprintf()
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

// ===== EXPRESSIONS =====
enum ExprKind {
    EXPR_INT, EXPR_BOOL,
    EXPR_CHAR, EXPR_STRING,
    EXPR_IDENT
};

// fwd.
typedef struct Expr Expr;  
typedef struct ExprInt ExprInt;
typedef struct ExprChar ExprChar;
typedef struct ExprBool ExprBool;
typedef struct ExprString ExprString;
typedef struct ExprIdent ExprIdent;
typedef struct ExprBinary ExprBinary;

struct Expr {
    enum ExprKind kind;
    union {
        ExprInt    *intLiteral;
        ExprBool   *boolLiteral;
        ExprChar   *charLiteral;
        ExprString *stringLiteral;
        ExprIdent  *ident;
    } as;
};

struct ExprInt {
    int value;
};
//
struct ExprChar {
    char value;
};
//
struct ExprBool {
    bool value;
};
//
struct ExprString {
    char *value;
};
//
struct ExprIdent {
    char *name;
};
//
struct ExprBinary {
    Expr *left;
    Expr *right;
    char op;
};

// ===== STATEMENTS =====

typedef struct Stmt Stmt;
typedef struct Block Block;

struct Stmt {
    int _dummy;
};

struct Block {
    int _dummy;
};

// ===== DECLARATIONS =====
enum DeclKind {
    DECL_VAR,
    DECL_FUNC,
    DECL_TYPE_STRUCT,
    DECL_TYPE_ENUM,
    DECL_TYPE_UNION
};

// fwd.
typedef struct Decl Decl;
typedef struct DeclVar DeclVar;
typedef struct DeclFunc DeclFunc;
typedef struct DeclTypeStruct DeclTypeStruct;
typedef struct EnumMember EnumMember;
typedef struct DeclTypeEnum DeclTypeEnum;
typedef struct DeclTypeUnion DeclTypeUnion;

struct Decl {
    enum DeclKind kind;
    union {
        DeclVar        *var;
        DeclFunc       *func;
        DeclTypeStruct *typeStruct;
        DeclTypeEnum   *typeEnum;
        DeclTypeUnion  *typeUnion;
    } as;
};

// variable declaration
struct DeclVar {
    char *ident;
    char *type;      // TODO: replace with Type*
    Expr *init;      // NULL => declaration only
};

struct DeclFunc {
    char *ident;
    char *return_type;  // NULL => inferred
    char *params;       // TODO: param list
    Block *body;        // NULL => forward declaration
};

struct DeclTypeStruct {
    char *ident;
    char *members;   // TODO: member list
};

struct EnumMember {
    char *name;
    char *value;     // NULL => implicit
};

struct DeclTypeEnum {
    char *ident;
    EnumMember *members;
};

// union type declaration
struct DeclTypeUnion {
    char *ident;
    char *members;   // TODO: member list
};

// ===== 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 = 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.intLiteral = lit;

    return expr;
}

#endif