-
Notifications
You must be signed in to change notification settings - Fork 0
/
lake.h
190 lines (159 loc) · 3.87 KB
/
lake.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* lake.h
* Lake Scheme
*
* Copyright 2011 Sami Samhuri
* MIT License
*
*/
#ifndef _LAKE_LAKE_H
#define _LAKE_LAKE_H
#include "common.h"
#include <stdlib.h>
#define LAKE_VERSION "0.1"
typedef int LakeType;
#define TYPE_SYM 1
#define TYPE_BOOL 2
#define TYPE_INT 3
#define TYPE_STR 4
#define TYPE_LIST 5
#define TYPE_DLIST 6
#define TYPE_PRIM 7
#define TYPE_FN 8
#define TYPE_COMM 9
#define VAL(x) ((LakeVal *)x)
#define SYM(x) ((LakeSym *)x)
#define BOOL(x) ((LakeBool *)x)
#define INT(x) ((LakeInt *)x)
#define STR(x) ((LakeStr *)x)
#define LIST(x) ((LakeList *)x)
#define DLIST(x) ((LakeDottedList *)x)
#define PRIM(x) ((LakePrimitive *)x)
#define FN(x) ((LakeFn *)x)
#define COMM(x) ((LakeComment *)x)
struct lake_val
{
LakeType type;
size_t size;
};
typedef struct lake_val LakeVal;
struct lake_sym
{
LakeVal base;
size_t n;
char *s;
unsigned long hash;
};
typedef struct lake_sym LakeSym;
struct lake_bool
{
LakeVal base;
bool val;
};
typedef struct lake_bool LakeBool;
struct lake_int
{
LakeVal base;
int val;
};
typedef struct lake_int LakeInt;
#define INT_VAL(x) (x->val)
struct lake_str
{
LakeVal base;
size_t n;
char *s;
};
typedef struct lake_str LakeStr;
#define STR_N(str) (str->n)
#define STR_S(str) (str->s)
struct lake_list
{
LakeVal base;
size_t cap;
size_t n;
LakeVal **vals;
};
typedef struct lake_list LakeList;
#define LIST_N(list) (list->n)
#define LIST_VALS(list) (list->vals)
#define LIST_VAL(list, i) (i >= 0 && i < list->n ? list->vals[i] : NULL)
struct lake_dlist
{
LakeVal base;
LakeList *head;
LakeVal *tail;
};
typedef struct lake_dlist LakeDottedList;
#include "env.h"
#include "hash.h"
/* Execution context */
struct lake_ctx
{
Env *toplevel;
lake_hash_t *symbols;
lake_hash_t *special_form_handlers;
LakeBool *T;
LakeBool *F;
};
typedef struct lake_ctx LakeCtx;
typedef LakeVal *(*lake_prim)(LakeCtx *ctx, LakeList *args);
struct lake_primitive
{
LakeVal base;
char *name;
int arity;
lake_prim fn;
};
typedef struct lake_primitive LakePrimitive;
#define PRIM_ARITY(x) (x->arity)
#define ARITY_VARARGS -1
struct lake_fn
{
LakeVal base;
LakeList *params;
LakeSym *varargs;
LakeList *body;
Env *closure;
};
typedef struct lake_fn LakeFn;
#define CALLABLE(x) (lake_is_type(TYPE_FN, x) || lake_is_type(TYPE_PRIM, x))
struct lake_comment
{
LakeVal base;
LakeStr *text;
};
typedef struct lake_comment LakeComment;
#define COMM_TEXT(x) (x->text)
LakeCtx *lake_init(void);
int lake_val_size(void *x);
int lake_is_type(LakeType t, void *x);
bool lake_is_nil(LakeVal *x);
bool lake_is(LakeVal *a, LakeVal *b);
bool lake_equal(LakeVal *a, LakeVal *b);
char *lake_repr(void *val);
#include <stdio.h>
#define ERR(...) \
do \
{ \
fprintf(stderr, "error: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
#define DIE(...) \
do \
{ \
ERR(__VA_ARGS__); \
exit(1); \
} while (0)
#define OOM() DIE("%s:%d out of memory", __FILE__, __LINE__)
#include "bool.h"
#include "comment.h"
#include "dlist.h"
#include "fn.h"
#include "int.h"
#include "list.h"
#include "primitive.h"
#include "str.h"
#include "sym.h"
#endif