-
Notifications
You must be signed in to change notification settings - Fork 0
/
Forms.c
66 lines (61 loc) · 1.76 KB
/
Forms.c
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
#include <assert.h>
#include "SymbolValue.h"
#include "StructValue.h"
#include "Eval.h"
#include "Forms.h"
#include "Error.h"
static Struct* quoteForm(Struct* form) {
Struct* x = NULL;
if (get_size(form) == 1) {
x = (Struct*)get_field(form, 0);
} else {
x = malformed(get_tag(form));
}
return x;
}
static Struct* constructForm(Struct* form) {
Struct* x = NULL;
size_t size = get_size(form);
if (size > 0) {
Struct* shouldBeTag = (Struct*)get_field(form, 0);
if (get_tag(shouldBeTag) == SYMBOL_SYMBOL) {
Symbol tag = asSymbol(shouldBeTag);
switch (size) {
case 1 : x = atomic_struct(tag ); break;
case 2 : x = singleton_struct(tag, get_field(form, 1)); break;
default: x = new_struct(tag, size-1, get_fields(form)+1); break;
}
x = quote(x);
}
}
if (x == NULL) {
x = malformed(get_tag(form));
}
return x;
}
Struct* matchForm(Struct* form) {
Struct* x = NULL;
Symbol tag = get_tag(form);
switch (tag) {
case 4831888 /* quote */: x = quoteForm(form) ; break;
case 20981506192834 /* construct */: x = constructForm(form); break;
default : x = noSuchForm(tag) ; break;
}
return x;
}
Map* define(Map* env, Struct* form) {
assert(get_size(form) == 2);
Struct* nameShouldBeSymbol = (Struct*)get_field(form, 0);
assert(get_tag(nameShouldBeSymbol) == SYMBOL_SYMBOL);
Symbol name = asSymbol(nameShouldBeSymbol);
Struct* value = (Struct*)get_field(form, 1);
return insert(name, value, env);
}
Map* matchTopLevel(Map* env, Struct* form) {
Map* m = NULL;
switch (get_tag(form)) {
case 148116611 /* define */: m = define(env, form); break;
default : m = NULL ; break;
}
return m;
}