Skip to content

Commit 2c5b656

Browse files
committed
Insane refactoring.
1 parent 1e01f34 commit 2c5b656

20 files changed

+899
-744
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
./lisp

alloc.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdlib.h>
2+
3+
#include "util.h"
4+
#include "obj.h"
5+
#include "machine.h"
6+
#include "alloc.h"
7+
8+
9+
obj_t * alloc_obj(type)
10+
obj_type_t type;
11+
{
12+
obj_t *o;
13+
14+
if (VM->alloc_offset == VM->semispace_size) gc();
15+
if (VM->alloc_offset == VM->semispace_size) fuck("game over");
16+
17+
o = &VM->from_space[++VM->alloc_offset];
18+
o->type = type;
19+
return o;
20+
}
21+
22+
obj_t *alloc_int(val)
23+
int val;
24+
{
25+
obj_t * x = alloc_obj(TINT);
26+
x->value.i = val;
27+
return x;
28+
}
29+
30+
obj_t * alloc_string(s)
31+
char * s;
32+
{
33+
obj_t *x = alloc_obj(TSTRING);
34+
x->value.str = s;
35+
return x;
36+
}
37+
38+
obj_t * alloc_cons(ca, cd)
39+
obj_t *ca, *cd;
40+
{
41+
obj_t *x = alloc_obj(TCONS);
42+
CAR(x) = ca;
43+
CDR(x) = cd;
44+
return x;
45+
}
46+
47+
obj_t *alloc_primitive(code)
48+
primitive_t code;
49+
{
50+
obj_t *x = alloc_obj(TPRIMITIVE);
51+
x->value.prim.code = code;
52+
return x;
53+
}
54+
55+
obj_t *alloc_function(params, body, env)
56+
obj_t *params, *body, *env;
57+
{
58+
obj_t *x = alloc_obj(TFUNCTION);
59+
x->value.fun.params = params;
60+
x->value.fun.body = body;
61+
x->value.fun.env = env;
62+
return x;
63+
}

alloc.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
obj_t *alloc_obj();
3+
obj_t *alloc_int();
4+
obj_t *alloc_string();
5+
obj_t *alloc_cons();
6+
obj_t *alloc_primitive();
7+
obj_t *alloc_function();

env.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdlib.h>
2+
#include <assert.h>
3+
4+
#include "util.h"
5+
#include "obj.h"
6+
#include "alloc.h"
7+
#include "lisp.h"
8+
#include "symbol.h"
9+
#include "env.h"
10+
11+
obj_t *lookup_env(env, sym)
12+
obj_t *env, *sym;
13+
{
14+
obj_t *entry;
15+
assert(TSYMBOL == sym->type);
16+
for (; nil != env; env = CDR(env)) {
17+
entry = CAR(env);
18+
assert(TCONS == entry->type);
19+
if (sym == CAR(entry))
20+
return entry;
21+
}
22+
return NULL;
23+
}
24+
25+
obj_t *push_env(env, sym, val)
26+
obj_t *env, *sym, *val;
27+
{
28+
obj_t *entry = alloc_cons(sym, val);
29+
return alloc_cons(entry, env);
30+
}
31+
32+
/* syms: list of symbols
33+
vals: list of evaluated values */
34+
obj_t *augment_env(env, syms, vals)
35+
obj_t *env, *syms, *vals;
36+
{
37+
obj_t *entry;
38+
obj_t *aug_env = env;
39+
if (list_length(syms) != list_length(vals)) fuck("fun/arg mismatch");
40+
while (nil != syms) {
41+
entry = alloc_cons(CAR(syms), CAR(vals));
42+
aug_env = alloc_cons(entry, aug_env);
43+
syms = CDR(syms);
44+
vals = CDR(vals);
45+
}
46+
assert(nil == syms);
47+
assert(nil == vals);
48+
return aug_env;
49+
}
50+
51+
obj_t *pop_env(env)
52+
obj_t *env;
53+
{
54+
/* memory leak */
55+
if (nil == env) return nil;
56+
return CDR(env);
57+
}

env.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
obj_t *lookup_env();
2+
obj_t *push_env();
3+
obj_t *augment_env();
4+
obj_t *pop_env();

lisp

23.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)