-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.h
106 lines (92 loc) · 1.99 KB
/
structure.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
#ifndef __STRUCTURE_H__
#define __STRUCTURE_H__
#include <stdbool.h>
#include "symbol.h"
enum NodeType {
SYMBOL,
PAIR,
LIST,
EMPTY,
VECTOR,
BOOL,
COMPLEX,
CHAR,
STRING,
FIX_LAMBDA,
VAR_LAMBDA,
BUI_LAMBDA,
MACRO,
UNSPECIFIED,
// macro,
BUILTIN_MAC,
DUMMY,
LISTELL,
VECTORELL,
MARG,
};
typedef struct Node {
enum NodeType type;
} Node;
typedef struct SymNode {
enum NodeType type;
Symbol sym;
unsigned int fromLevel;
} SymNode;
typedef struct PairNode {
enum NodeType type;
struct Node * a;
struct Node * b;
} PairNode;
typedef struct VecNode {
enum NodeType type;
unsigned int len;
Node ** vec;
} VecNode;
typedef struct StringNode {
enum NodeType type;
unsigned int len;
char * str;
} StringNode;
typedef struct CharNode {
enum NodeType type;
char value;
} CharNode;
typedef struct Env Env;
typedef struct LambdaNode {
enum NodeType type;
Node * formal;
Env * env;
Node * body;
} LambdaNode;
Node empty;
Node boolTrue;
Node boolFalse;
Node * newSymbol(const char *);
Node * newVector(Node **, unsigned int);
Node * newString(const char *, unsigned int);
Node * newBool(bool);
Node * newChar(char);
Node * newLambda(Node *, Node *, Env *);
Node * newMarg(unsigned int, unsigned int);
Node * newMacro(Node * lit, Node * ps, Node * ts, Env * env);
Node * newComplex(const char *);
unsigned int length(Node *);
Node * cons(Node *, Node *);
bool equal(Node *, Node *);
#define toPair(p) ((PairNode *)(p))
#define toSym(p) ((SymNode *)(p))
#define toVec(p) ((VecNode *)(p))
#define toChar(p) ((CharNode *)(p))
#define toString(p) ((StringNode *)(p))
#define toComplex(p) ((ComplexNode *)(p))
#define toMarg(p) ((MargNode *)(p))
#define toMacro(p) ((Macro *)(p))
#define toLambda(p) ((LambdaNode *)(p))
#define toBuiLambda(p) ((BuiltinLambda *)(p))
#define LIST1(a) cons((a), &empty)
#define LIST2(a, b) cons((a), LIST1(b))
#define LIST3(a, b, c) cons((a), LIST2((b), (c)))
#define LIST4(a, b, c, d) cons((a), LIST3((b), (c), (d)))
#define car(p) toPair(p)->a
#define cdr(p) toPair(p)->b
#endif