aboutsummaryrefslogtreecommitdiff
path: root/c/types.h
blob: 271a899170b5ad49a93e994bf2fe243ff49dd52e (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
#ifndef __MAL_TYPES__
#define __MAL_TYPES__

#include <glib.h>

// State

struct MalVal; // pre-declare
extern struct MalVal *mal_error;

#define abort(format, ...) \
    { _error(format, ##__VA_ARGS__); return NULL; }

#define assert(test, format, ...) \
    if (!(test)) { \
        _error(format, ##__VA_ARGS__); \
        return NULL; \
    }

#define assert_type(mv, typ, format, ...) \
    if (!(mv->type & (typ))) { \
        _error(format, ##__VA_ARGS__); \
        return NULL; \
    }

typedef enum {
    MAL_NIL = 1,
    MAL_TRUE = 2,
    MAL_FALSE = 4,
    MAL_INTEGER = 8,
    MAL_FLOAT = 16,
    MAL_SYMBOL = 32,
    MAL_STRING = 64,
    MAL_LIST = 128,
    MAL_VECTOR = 256,
    MAL_HASH_MAP = 512,
    MAL_ATOM = 1024,
    MAL_FUNCTION_C = 2048,
    MAL_FUNCTION_MAL = 4096,
} MalType;


// Predeclare Env
typedef struct Env Env;

typedef struct MalVal {
    MalType type;
    struct MalVal *metadata;
    union {
        gint64 intnum;
        gdouble floatnum;
        char *string;
        GArray *array;
        GHashTable *hash_table;
        struct MalVal *atom_val;
        void *(*f0) ();
        void *(*f1) (void*);
        void *(*f2) (void*,void*);
        void *(*f3) (void*,void*,void*);
        void *(*f4) (void*,void*,void*,void*);
        void *(*f5) (void*,void*,void*,void*,void*);
        void *(*f6) (void*,void*,void*,void*,void*,void*);
        void *(*f7) (void*,void*,void*,void*,void*,void*,void*);
        void *(*f8) (void*,void*,void*,void*,void*,void*,void*,void*);
        void *(*f9) (void*,void*,void*,void*,void*,void*,void*,void*,void*);
        void *(*f10)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*);
        void *(*f11)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*);
        void *(*f12)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*);
        void *(*f13)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*);
        void *(*f14)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*);
        void *(*f15)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*,void*);
        void *(*f16)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*,void*,void*);
        void *(*f17)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*,void*,void*,void*);
        void *(*f18)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*,void*,void*,void*,void*);
        void *(*f19)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*,void*,void*,void*,void*,void*);
        void *(*f20)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,
                     void*,void*,void*,void*,void*,void*,void*,void*,void*,void*);
        struct {
            struct MalVal *(*evaluator)(struct MalVal *, Env *);
            struct MalVal *args;
            struct MalVal *body;
            struct Env    *env;
        } func;
    } val;
    int func_arg_cnt;
    int ismacro;
} MalVal;

// Constants

extern MalVal mal_nil;
extern MalVal mal_true;
extern MalVal mal_false;


// Declare functions used internally (by other C code).
// Mal visible functions are "exported" in types_ns

MalVal *malval_new(MalType type, MalVal *metadata);
int malval_free(MalVal *mv);
MalVal *malval_new_integer(gint64 val);
MalVal *malval_new_float(gdouble val);
MalVal *malval_new_string(char *val);
MalVal *malval_new_symbol(char *val);
MalVal *malval_new_list(MalType type, GArray *val);
MalVal *malval_new_function(void *(*func)(void *), int arg_cnt, MalVal* metadata);

MalVal *hash_map(MalVal *args);
void _error(const char *fmt, ...);
MalVal *_list(int count, ...);

MalVal *apply(MalVal *f, MalVal *el);

char *_pr_str(MalVal *args, int print_readably);

MalVal *first(MalVal* seq);
MalVal *last(MalVal* seq);
MalVal *_slice(MalVal *seq, int start, int end);
MalVal *_nth(MalVal *seq, int idx);
MalVal *rest(MalVal *seq);

MalVal *_map2(MalVal *(*func)(void*, void*), MalVal *lst, void *arg2);

// These are just used by step2 and step3 before then type_ns environment is
// imported

MalVal *int_plus(MalVal *a, MalVal *b);
MalVal *int_minus(MalVal *a, MalVal *b);
MalVal *int_multiply(MalVal *a, MalVal *b);
MalVal *int_divide(MalVal *a, MalVal *b);

// Env

typedef struct Env {
    struct Env *outer;
    GHashTable *table;
} Env;

Env *new_env(Env *outer, MalVal* binds, MalVal *exprs);
Env *env_find(Env *env, char *key);
MalVal *env_get(Env *env, char *key);
Env *env_set(Env *env, char *key, MalVal *val);

// namespace of type functions
typedef struct {
    char *name;
    void *(*func)(void*);
    int arg_cnt;
} types_ns_entry;

extern types_ns_entry types_ns[49];

#endif