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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
import std.stdio;
import std.string;
import std.conv;
import parser;
import dbg;
enum ObjType {
FUNCTION,
SCRIPT,
}
abstract class Obj {
ObjType type;
}
class Function : Obj {
Chunk chunk;
int arity;
string name;
ObjType type;
this(ObjType type, string name = "") {
this.type = type;
this.chunk = new Chunk();
this.arity = 0;
this.name = name;
}
override string toString() {
if (type == ObjType.SCRIPT) {
return "<neb>";
} else {
return format("<fn %s>", name);
}
}
}
enum SeqType {
LIST,
STRING,
}
abstract class Seq {
SeqType type;
abstract Value first();
abstract Seq rest();
abstract Seq most();
abstract Value last();
abstract Seq reverse();
abstract int length();
abstract bool isIn(Value val);
}
class String : Seq {
string str;
this(Value value) {
this.str = value.as.str;
this.type = SeqType.STRING;
}
this(string str) {
this.str = str;
this.type = SeqType.STRING;
}
override Value first() {
string ret = to!string(this.str[0]);
return makeSeqValue(new String(ret));
}
override Seq rest() {
return new String(this.str[1..$]);
}
override Seq most() {
return new String(this.str[0..$ - 1]);
}
override Seq reverse() {
string s = "";
int end = to!int(this.str.length) - 1;
for (int i = end; i >= 0; i--) {
s ~= this.str[i];
}
return new String(s);
}
override Value last() {
string ret = to!string(this.str[$ - 1]);
return makeSeqValue(new String(ret));
}
override int length() {
return to!int(str.length);
}
Seq concat(Seq seq) {
if (seq.type != SeqType.STRING) {
// how do i throw an error here?
writeln("must concat strings to strings!");
return this;
}
String strSeq = cast(String)seq;
return new String(this.str ~ strSeq.str);
}
override bool isIn(Value val) {
if (val.type != ValueType.SEQ) {
// how do i throw an error here?
writeln("a non-string can't be 'in?' a string (this should be a compile error)");
return false;
}
Seq seq = val.as.seq;
if (seq.type != SeqType.STRING) {
// how do i throw an error here?
writeln("a non-string can't be 'in?' a string (this should be a compile error)");
return false;
}
String strVal = cast(String)seq;
return (indexOf(this.str, strVal.str) != -1);
}
override string toString() {
return format("\"%s\"", this.str);
}
}
class List : Seq {
Value[] inner;
this(int length) {
this.inner = new Value[length];
this.type = SeqType.LIST;
}
void addItemAtIndex(Value item, int idx) {
this.inner[idx] = item;
}
override Value first() {
return this.inner[0]; // this fails on NIL
}
override Seq rest() {
if (this.inner.length == 0) {
return new List(0);
}
List ret = new List(to!int(this.inner.length) - 1);
for (int i = 1; i < this.inner.length; i++) {
ret.addItemAtIndex(this.inner[i], i - 1);
}
return ret;
}
override Seq most() {
if (this.inner.length == 0) {
return new List(0);
}
int newLength = to!int(this.inner.length) - 1;
List ret = new List(newLength);
for (int i = 0; i < newLength; i++) {
ret.addItemAtIndex(this.inner[i], i);
}
return ret;
}
override Seq reverse() {
int length = to!int(this.inner.length);
List ret = new List(length);
for (int i = 0; i < length; i++) {
ret.addItemAtIndex(this.inner[i], length - i - 1);
}
return ret;
}
override Value last() {
return this.inner[$ - 1]; // this fails on NIL
}
override int length() {
return to!int(this.inner.length);
}
Seq append(Value val) {
int length = to!int(this.inner.length);
List ret = new List(length + 1);
for (int i = 0; i < length; i++) {
ret.addItemAtIndex(this.inner[i], i);
}
ret.addItemAtIndex(val, length);
return ret;
}
override bool isIn(Value val) {
foreach(Value mine ; this.inner) {
if (areValuesEqual(mine, val)) {
return true;
}
}
return false;
}
override string toString() {
if (this.inner.length == 0) {
return "nil";
} else {
string[] ret;
foreach (Value val ; this.inner) {
ret ~= printableValue(val);
}
return format("(%s)", join(ret, " "));
}
}
}
enum OpCode {
// MATH
OP_ADD,
OP_DIVIDE,
OP_INCREMENT,
OP_MULTIPLY,
OP_NEGATE,
OP_SUBTRACT,
OP_ZERO,
// BOOLEAN
OP_EQUAL,
OP_GREATER,
OP_LESS,
OP_NOT,
// LISTS
OP_LIST,
OP_LIST_N,
// SEQUENCES
OP_CONCAT, // No?
OP_APPEND,
OP_FIRST, // No?
OP_LENGTH,
OP_LAST,
OP_MEMBER,
OP_MOST,
OP_REST,
OP_REVERSE,
// DEFINITIONS/VARIABLES
OP_CONSTANT,
OP_DEF_GLOBAL,
OP_DEF_LOCAL,
OP_GET_GLOBAL,
OP_GET_LOCAL,
OP_SET_GLOBAL,
OP_SET_LOCAL,
// JUMPS
OP_JUMP,
OP_JUMP_IF_FALSE,
OP_JUMP_IF_TRUE,
OP_JUMP_TO,
// STACK THINGS
OP_DUPLICATE,
OP_DUPLICATE_2,
OP_POP,
OP_POPB,
OP_POP_SCOPE,
OP_ROTATE_N,
// RANDOM
OP_NIL,
OP_PRINT,
// FUNCTIONS
OP_CALL,
OP_RETURN,
// TYPES
OP_IS_NIL,
OP_TYPE_CHECK_BOOLEAN,
OP_TYPE_CHECK_LIST,
OP_TYPE_CHECK_NUMBER,
OP_TYPE_CHECK_SEQ,
OP_TYPE_CHECK_STRING,
}
class Chunk {
int count = 0;
ubyte[] code;
int[] lines;
Value[] constants;
int writeOp(ubyte opCode, int line) {
this.code ~= opCode;
this.lines ~= line;
this.count++;
assert(this.code.length == count);
assert(this.lines.length == count);
return count;
}
int addConstant(Value value) {
this.constants ~= value;
return to!int(this.constants.length - 1);
}
}
|