-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
160 lines (123 loc) · 3.37 KB
/
memory.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
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
#include <stdlib.h>
#include <stdio.h>
#include "jscheme.h"
OBJ
newInteger(jscheme_int64 iVal){
struct jschemeInteger *theJSInt;
theJSInt = (struct jschemeInteger*)(malloc( sizeof( struct jschemeInteger)));
theJSInt->tag = T_INTEGER;
theJSInt->intVal = iVal;
return (OBJ) theJSInt;
}
OBJ
newFileStream(FILE* file){
struct jschemeFileStream *fileStream;
fileStream = (struct jschemeFileStream*)(malloc( sizeof( struct jschemeFileStream)));
fileStream->tag = T_FILESTREAM;
fileStream->file = file;
fileStream->peekChar = 0;
return (OBJ)fileStream;
}
OBJ
newStringStream(char *buffer){
struct jschemeStringStream *stringStream;
stringStream = (struct jschemeStringStream*)(malloc( sizeof( struct jschemeStringStream)));
stringStream->tag = T_STRINGSTREAM;
stringStream->buffer = buffer;
stringStream->index = 0;
return (OBJ)stringStream;
}
OBJ
newSymbol(char *chars){
struct jschemeSymbol *theSymbol;
theSymbol = (struct jschemeSymbol*)(malloc( sizeof( struct jschemeSymbol)));
theSymbol->tag = T_SYMBOL;
theSymbol->symbolVal = chars;
return (OBJ)theSymbol;
}
OBJ
newString(char *chars){
struct jschemeString *theString;
theString = (struct jschemeString*)(malloc( sizeof( struct jschemeString)));
theString->tag = T_STRING;
theString->stringVal = chars;
return (OBJ)theString;
}
OBJ
newCons(OBJ car, OBJ cdr){
struct jschemeCons *theCons;
theCons = (struct jschemeCons*)(malloc( sizeof( struct jschemeCons)));
theCons->tag = T_CONS;
theCons->car = car;
theCons->cdr = cdr;
return (OBJ) theCons;
}
OBJ
newBuiltinFunction(char *internalName, OBJFUNC theCode){
struct jsBuiltinFunction *theFunction;
theFunction = (struct jsBuiltinFunction *)(malloc( sizeof(struct jsBuiltinFunction)));
theFunction->tag = T_BUILTINFUNCTION;
theFunction->internalName = internalName;
theFunction->theCode = theCode;
return (OBJ) theFunction;
}
OBJ
newBuiltinSyntax(char *internalName, OBJFUNC theCode){
struct jsBuiltinSyntax *theSyntax;
theSyntax = (struct jsBuiltinSyntax *)(malloc( sizeof(struct jsBuiltinSyntax)));
theSyntax->tag = T_BUILTINSYNTAX;
theSyntax->internalName = internalName;
theSyntax->theCode = theCode;
return (OBJ) theSyntax;
}
OBJ
CP_newBuiltinSyntax(char *internalName, VOIDPTRFUNC theCode){
struct CP_jsBuiltinSyntax *theSyntax;
theSyntax = (struct CP_jsBuiltinSyntax *)(malloc( sizeof(struct CP_jsBuiltinSyntax)));
theSyntax->tag = T_BUILTINSYNTAX;
theSyntax->internalName = internalName;
theSyntax->theCode = theCode;
return (OBJ) theSyntax;
}
// length helper method
int
length(OBJ list) {
int len = 0;
while( list != js_nil){
if( !ISCONS(list)){
js_error("bad element in list", list);
}
len++;
list = CDR(list);
}
return len;
}
OBJ
newUserDefinedFunction(char *internalName, OBJ argList, OBJ bodyList){
struct jsUserDefinedFunction *theUDF;
theUDF = (struct jsUserDefinedFunction *)(malloc (sizeof(struct jsUserDefinedFunction)));
theUDF->tag = T_USERDEFINEDFUNCTION;
theUDF->internalName = internalName;
theUDF->argList = argList;
theUDF->bodyList = bodyList;
theUDF->numArgs = length(argList);
theUDF->home = js_nil;
return (OBJ) theUDF;
}
const char* tag_lookup[15] = {
"T_NIL",
"T_TRUE",
"T_FALSE",
"T_INTEGER",
"T_SYMBOL",
"T_STRING",
"T_FILESTREAM",
"T_STRINGSTREAM",
"T_CONS",
"T_BUILTINFUNCTION",
"T_BUILTINSYNTAX",
"T_VOID",
"T_GLOBALENVIRONMENT",
"T_LOCALENVIRONMENT",
"T_USERDEFINEDFUNCTION",
};