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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
#include "function.h"
#include "apply.h"
#include "eval.h"
#include "read.h"
#include "write.h"
lsp_builtin_entry_t lsp_functions[] = {
{"eval", lsp_function_eval},
{"apply", lsp_function_apply},
{"error", lsp_function_error},
{"cons", lsp_function_cons},
{"set-car!", lsp_function_set_car},
{"set-cdr!", lsp_function_set_cdr},
{"number?", lsp_function_is_number},
{"pair?", lsp_function_is_pair},
{"string?", lsp_function_is_string},
{"symbol?", lsp_function_is_symbol},
{"function?", lsp_function_is_function},
{"syntax?", lsp_function_is_syntax},
{"eq?", lsp_function_eq},
{"equal?", lsp_function_equal},
{">", lsp_function_gt},
{"<", lsp_function_lt},
{"+", lsp_function_plus},
{"-", lsp_function_minus},
{"*", lsp_function_multiply},
{"/", lsp_function_divide},
{"read", lsp_function_read},
{"read-u8", lsp_function_read_u8},
{"peek-u8", lsp_function_peek_u8},
{"write", lsp_function_write},
{"write-u8", lsp_function_write_u8},
{"make-string", lsp_function_make_string},
{"string-length", lsp_function_string_length},
{"string-ref", lsp_function_string_ref},
{"string-set!", lsp_function_string_set},
{NULL, NULL}};
lsp_status_t lsp_function_eval(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
return lsp_eval(e, ctx, value);
}
lsp_status_t lsp_function_apply(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t callable;
lsp_addr_t arguments;
lsp_status_t status =
lsp_builtin_get_args_2(e->m, args, &callable, &arguments);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_builtin(e->m, callable) &&
!lsp_mem_is_function_or_syntax(e->m, callable))
return LSP_ERR_ARG_TYPE;
return lsp_apply(e, ctx, callable, arguments);
}
lsp_status_t lsp_function_error(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_number(e->m, value))
return LSP_ERR_ARG_TYPE;
lsp_int32_t code = lsp_mem_get_number(e->m, value);
if (code < 0 || code > 126)
return LSP_ERR_ARG_VALUE;
return LSP_ERR_USER + code;
}
lsp_status_t lsp_function_cons(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t first;
lsp_addr_t second;
lsp_status_t status = lsp_builtin_get_args_2(e->m, args, &first, &second);
if (status != LSP_SUCCESS)
return status;
lsp_addr_t result;
status = lsp_mem_create_pair(e->m, first, second, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_set_car(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t pair;
lsp_addr_t first;
lsp_status_t status = lsp_builtin_get_args_2(e->m, args, &pair, &first);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_pair(e->m, pair))
return LSP_ERR_ARG_TYPE;
if (pair == e->m->nil)
return LSP_ERR_ARG_VALUE;
lsp_mem_set_pair_first(e->m, pair, first);
return lsp_env_set_result_value(e, e->m->nil);
}
lsp_status_t lsp_function_set_cdr(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t pair;
lsp_addr_t second;
lsp_status_t status = lsp_builtin_get_args_2(e->m, args, &pair, &second);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_pair(e->m, pair))
return LSP_ERR_ARG_TYPE;
if (pair == e->m->nil)
return LSP_ERR_ARG_VALUE;
lsp_mem_set_pair_second(e->m, pair, second);
return lsp_env_set_result_value(e, e->m->nil);
}
lsp_status_t lsp_function_is_number(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_number = lsp_mem_is_number(e->m, value);
return lsp_env_set_result_value(e, (is_number ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_is_pair(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_pair = lsp_mem_is_pair(e->m, value);
return lsp_env_set_result_value(e, (is_pair ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_is_string(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_string = lsp_mem_is_string(e->m, value);
return lsp_env_set_result_value(e, (is_string ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_is_symbol(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_symbol = lsp_mem_is_symbol(e->m, value);
return lsp_env_set_result_value(e, (is_symbol ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_is_function(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_function = lsp_mem_is_function(e->m, value) ||
lsp_mem_is_builtin_function(e->m, value);
return lsp_env_set_result_value(e, (is_function ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_is_syntax(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_syntax = lsp_mem_is_syntax(e->m, value) ||
lsp_mem_is_builtin_syntax(e->m, value);
return lsp_env_set_result_value(e, (is_syntax ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_eq(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t first;
lsp_addr_t second;
lsp_status_t status = lsp_builtin_get_args_2(e->m, args, &first, &second);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_eq = lsp_mem_eq(e->m, first, second);
return lsp_env_set_result_value(e, (is_eq ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_equal(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t first;
lsp_addr_t second;
lsp_status_t status = lsp_builtin_get_args_2(e->m, args, &first, &second);
if (status != LSP_SUCCESS)
return status;
lsp_bool_t is_equal = lsp_mem_equal(e->m, first, second);
return lsp_env_set_result_value(e, (is_equal ? e->m->one : e->m->zero));
}
lsp_status_t lsp_function_gt(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t last_value = e->m->nil;
while (args != e->m->nil) {
lsp_addr_t value = lsp_mem_get_pair_first(e->m, args);
if (!lsp_mem_is_number(e->m, value))
return LSP_ERR_ARG_TYPE;
if (last_value != e->m->nil && lsp_mem_get_number(e->m, last_value) <=
lsp_mem_get_number(e->m, value))
return lsp_env_set_result_value(e, e->m->zero);
last_value = value;
args = lsp_mem_get_pair_second(e->m, args);
}
return lsp_env_set_result_value(e, e->m->one);
}
lsp_status_t lsp_function_lt(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t last_value = e->m->nil;
while (args != e->m->nil) {
lsp_addr_t value = lsp_mem_get_pair_first(e->m, args);
if (!lsp_mem_is_number(e->m, value))
return LSP_ERR_ARG_TYPE;
if (last_value != e->m->nil && lsp_mem_get_number(e->m, last_value) >=
lsp_mem_get_number(e->m, value))
return lsp_env_set_result_value(e, e->m->zero);
last_value = value;
args = lsp_mem_get_pair_second(e->m, args);
}
return lsp_env_set_result_value(e, e->m->one);
}
lsp_status_t lsp_function_plus(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_int32_t value = 0;
while (args != e->m->nil) {
lsp_addr_t i = lsp_mem_get_pair_first(e->m, args);
if (!lsp_mem_is_number(e->m, i))
return LSP_ERR_ARG_TYPE;
value += lsp_mem_get_number(e->m, i);
args = lsp_mem_get_pair_second(e->m, args);
}
lsp_addr_t result;
lsp_status_t status = lsp_mem_create_number(e->m, value, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_minus(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_int32_t value;
lsp_uint16_t counter = 0;
while (args != e->m->nil) {
lsp_addr_t i = lsp_mem_get_pair_first(e->m, args);
if (!lsp_mem_is_number(e->m, i))
return LSP_ERR_ARG_TYPE;
if (++counter > 1) {
value -= lsp_mem_get_number(e->m, i);
} else {
value = lsp_mem_get_number(e->m, i);
}
args = lsp_mem_get_pair_second(e->m, args);
}
if (!counter)
return LSP_ERR_ARG_COUNT;
if (counter < 2)
value = -value;
lsp_addr_t result;
lsp_status_t status = lsp_mem_create_number(e->m, value, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_multiply(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_int32_t value = 1;
while (args != e->m->nil) {
lsp_addr_t i = lsp_mem_get_pair_first(e->m, args);
if (!lsp_mem_is_number(e->m, i))
return LSP_ERR_ARG_TYPE;
value *= lsp_mem_get_number(e->m, i);
args = lsp_mem_get_pair_second(e->m, args);
}
lsp_addr_t result;
lsp_status_t status = lsp_mem_create_number(e->m, value, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_divide(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_int32_t value;
lsp_uint16_t counter = 0;
while (args != e->m->nil) {
lsp_addr_t i = lsp_mem_get_pair_first(e->m, args);
if (!lsp_mem_is_number(e->m, i))
return LSP_ERR_ARG_TYPE;
if (++counter > 1) {
value /= lsp_mem_get_number(e->m, i);
} else {
value = lsp_mem_get_number(e->m, i);
}
args = lsp_mem_get_pair_second(e->m, args);
}
if (!counter)
return LSP_ERR_ARG_COUNT;
if (counter < 2)
value = 1 / value;
lsp_addr_t result;
lsp_status_t status = lsp_mem_create_number(e->m, value, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_read(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
if (args != e->m->nil)
return LSP_ERR_ARG_COUNT;
lsp_addr_t value;
lsp_status_t status = lsp_read(e->m, e->in, &value);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, value);
lsp_mem_dec_ref(e->m, value);
return status;
}
lsp_status_t lsp_function_read_u8(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
if (args != e->m->nil)
return LSP_ERR_ARG_COUNT;
lsp_uint8_t c;
lsp_status_t status = lsp_in_stream_read(e->in, &c);
if (status != LSP_SUCCESS)
return status;
lsp_addr_t result;
status = lsp_mem_create_number(e->m, c, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_peek_u8(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
if (args != e->m->nil)
return LSP_ERR_ARG_COUNT;
lsp_uint8_t c;
lsp_status_t status = lsp_in_stream_peek(e->in, &c);
if (status != LSP_SUCCESS)
return status;
lsp_addr_t result;
status = lsp_mem_create_number(e->m, c, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_write(lsp_env_t *e, lsp_addr_t ctx, lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
status = lsp_write(e->m, e->out, value);
if (status != LSP_SUCCESS)
return status;
return lsp_env_set_result_value(e, e->m->nil);
}
lsp_status_t lsp_function_write_u8(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_number(e->m, value))
return LSP_ERR_ARG_TYPE;
lsp_uint8_t c = lsp_mem_get_number(e->m, value);
status = lsp_out_stream_write(e->out, c);
if (status != LSP_SUCCESS)
return status;
return lsp_env_set_result_value(e, e->m->nil);
}
lsp_status_t lsp_function_make_string(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t value;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &value);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_number(e->m, value))
return LSP_ERR_ARG_TYPE;
lsp_uint16_t str_len = lsp_mem_get_number(e->m, value);
lsp_addr_t result;
status = lsp_mem_create_string(e->m, str_len, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_string_length(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t str;
lsp_status_t status = lsp_builtin_get_args_1(e->m, args, &str);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_string(e->m, str))
return LSP_ERR_ARG_TYPE;
lsp_uint16_t str_len = lsp_mem_get_string_len(e->m, str);
lsp_addr_t result;
status = lsp_mem_create_number(e->m, str_len, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_string_ref(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t str;
lsp_addr_t index;
lsp_status_t status = lsp_builtin_get_args_2(e->m, args, &str, &index);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_string(e->m, str) || !lsp_mem_is_number(e->m, index))
return LSP_ERR_ARG_TYPE;
lsp_uint16_t i = lsp_mem_get_number(e->m, index);
lsp_uint16_t str_len = lsp_mem_get_string_len(e->m, str);
if (i >= str_len)
return LSP_ERR_ARG_VALUE;
lsp_uint8_t c = lsp_mem_get_string_data(e->m, str, i);
lsp_addr_t result;
status = lsp_mem_create_number(e->m, c, &result);
if (status != LSP_SUCCESS)
return status;
status = lsp_env_set_result_value(e, result);
lsp_mem_dec_ref(e->m, result);
return status;
}
lsp_status_t lsp_function_string_set(lsp_env_t *e, lsp_addr_t ctx,
lsp_addr_t args) {
lsp_addr_t str;
lsp_addr_t index;
lsp_addr_t value;
lsp_status_t status =
lsp_builtin_get_args_3(e->m, args, &str, &index, &value);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_string(e->m, str) || !lsp_mem_is_number(e->m, index) ||
!lsp_mem_is_number(e->m, value))
return LSP_ERR_ARG_TYPE;
lsp_uint16_t i = lsp_mem_get_number(e->m, index);
lsp_uint16_t str_len = lsp_mem_get_string_len(e->m, str);
if (i >= str_len)
return LSP_ERR_ARG_VALUE;
lsp_mem_set_string_data(e->m, str, i, lsp_mem_get_number(e->m, value));
return lsp_env_set_result_value(e, e->m->nil);
}
|