LambLisp 01 Red Fox Alpha
Lisp For Real-Time Control
Loading...
Searching...
No Matches
LambLisp.h
1#ifndef LL_LAMB_H
2#define LL_LAMB_H
3
4#include "ll_platform_generic.h"
5
15
18class LL_Port;
19typedef LL_Port Port_t;
20typedef Port_t *Portst_t;
22
24typedef void CPPDeleter(void *cpp_obj);
25typedef CPPDeleter *CPPDeleterPtr;
26
27class Cell; //forward
28typedef Cell *Sexpr_t;
29
40extern Sexpr_t NIL;
41extern Sexpr_t HASHT;
42extern Sexpr_t HASHF;
43extern Sexpr_t OBJ_EOF;
44extern Sexpr_t OBJ_UNDEF;
45extern Sexpr_t OBJ_VOID;
46extern Sexpr_t OBJ_SYSERROR;
48
104class Cell {
105public:
113 Cell() {}
114
115 Cell(Word_t typ, Word_t w1, Word_t w2) { type(typ); rplaca(w1); rplacd(w2); }
116 Cell(Word_t typ, Int_t w1, Sexpr_t w2) { type(typ); rplaca((Word_t) w1); rplacd((Word_t) w2); }
117 Cell(Word_t typ, Sexpr_t w1, Sexpr_t w2) { type(typ); rplaca((Word_t) w1); rplacd((Word_t) w2); }
119
135 enum {
136 //Complex atoms first
139
141
146
149
152
156
157 //Simple atoms next
163
164 //Interface atoms to C++ functions. No garbage collection finalization required.
167
168 //T_VOID and T_UNDEF are singletons
171
177
191
193 };
195
198 static const int F_GC01 = 0x01;
199 static const int F_GC02 = 0x02;
200 static const int F_GC04 = 0x04;
201 static const int F_TAIL = 0x08;
202 static const int F_0x10 = 0x10;
203 static const int F_0x20 = 0x20;
204 static const int F_0x40 = 0x40;
205 static const int F_0x80 = 0x80;
206 static const int GC_STATE_MASK = F_GC01 | F_GC02 | F_GC04;
208
217#define _type_ (_contents._byte[0])
218#define _flags_ (_contents._byte[1])
219#define _car_ (_contents._word[1])
220#define _cdr_ (_contents._word[2])
221#define _car_addr_ (&(_car_))
222#define _int_ptr_ ((Int_t *) _car_addr_)
223#define _real_ptr_ ((Real_t *) _car_addr_)
224#define _byte2_ptr_ (&(_contents._byte[2]))
225
226 void zero() { _contents._word[0] = _contents._word[1] = _contents._word[2] = 0; }
227 Int_t type(void) { return _type_; }
228 void type(Int_t t) { _type_ = t; }
229
230 Int_t flags(void) { return _flags_; }
231 void flags_set(Int_t f) { _flags_ |= f; }
232 void flags_clr(Int_t f) { _flags_ &= ~f; }
233
234 void rplaca(Word_t p) { _car_ = p; }
235 void rplacd(Word_t p) { _cdr_ = p; }
237
254
255 typedef struct {
256 Int_t typ;
257 bool is_any_pair, is_any_svec, is_any_svec2n, is_any_str, is_any_sym, is_any_bvec;
258 const char *type_name;
259 } CellFeatures;
260
261 static const CellFeatures features[Ntypes];
262
263 void init_static_data();
264
265 Bool_t is_atom(void) { return _type_ <= T_NIL; }
266 Bool_t is_pair(void) { return _type_ == T_PAIR; }
267
268 //Note sometimes faster to check the features, other times faster to check the type directly. For 3 checks the feature table wins, but for 2 it is not always obvious.
269 Bool_t is_any_pair(void) { return _type_ > T_NIL; }
270 Bool_t is_any_svec_atom() { return features[_type_].is_any_svec; }
271 Bool_t is_any_svec2n_atom() { return (_type_ == T_SVEC2N_HEAP) || (_type_ == T_SVEC_IMM); }
272 Bool_t is_any_str_atom(void) { return features[_type_].is_any_str; }
273 Bool_t is_any_sym_atom(void) { return (_type_ == T_SYM_HEAP) || (_type_ == T_GENSYM); }
274 Bool_t is_any_bvec_atom(void) { return features[_type_].is_any_bvec; }
275
277
296 enum { gcst_idle, gcst_issued, gcst_stacked, gcst_marked, gcst_free, Ngcstates };
297
298 Int_t gc_state(void) { return _flags_ & GC_STATE_MASK; }
299 Sexpr_t gc_state(Int_t st) { _flags_ = (_flags_ & ~GC_STATE_MASK) | (st & GC_STATE_MASK); return this; }
300 Int_t tail_state(void) { return _flags_ & F_TAIL; }
301 Sexpr_t tail_state_set(void) { _flags_ |= F_TAIL; return this; }
302 Sexpr_t tail_state_clr(void) { _flags_ &= ~F_TAIL; return this; }
304
307 Ptr_t get_car_addr() { return _car_addr_; }
308 Word_t get_car(void) { return _car_; }
309 Word_t get_cdr(void) { return _cdr_; }
310
311 Bool_t as_Bool_t() { return (Bool_t) _car_; }
312 Char_t as_Char_t() { return (Char_t) _car_; }
313 Int_t as_Int_t() { return *_int_ptr_; }
314 Real_t as_Real_t() { return *_real_ptr_; }
315
316 Ptr_t as_Ptr_t() { return (Ptr_t) _cdr_; }
317 Charst_t as_Charst_t() { return (Charst_t) as_Ptr_t(); }
318 Bytest_t as_Bytest_t() { return (Bytest_t) as_Ptr_t(); }
319 CharVec_t as_CharVec_t() { return (CharVec_t) as_Ptr_t(); }
320 ByteVec_t as_ByteVec_t() { return (ByteVec_t) as_Ptr_t(); }
321 Portst_t as_Portst_t() { return (Portst_t) as_Ptr_t(); }
322
323 Int_t as_numerator() { return (Int_t) _car_; }
324 Int_t as_denominator() { return (Int_t) _cdr_; }
325
327
336 Int_t hash_sexpr(void);
337 Int_t hash_contents(void);
338 Int_t hash(void) { return (type() == T_SYM_HEAP) ? prechecked_sym_heap_get_hash() : hash_sexpr(); }
340
343 Sexpr_t set(Int_t typ, Word_t w1, Word_t w2) { _type_ = typ; _car_ = w1; _cdr_ = w2; return this; }
344 Sexpr_t set(Int_t typ, Int_t a, Sexpr_t b) { _type_ = typ; _car_ = (Word_t) a; _cdr_ = (Word_t) b; return this; }
345 Sexpr_t set(Int_t typ, Sexpr_t a, Sexpr_t b) { _type_ = typ; _car_ = (Word_t) a; _cdr_ = (Word_t) b; return this; }
346
347 Sexpr_t set(Bool_t b) { return set(T_BOOL, (Word_t) b, (Word_t) 0); }
348 Sexpr_t set(Char_t c) { return set(T_CHAR, (Word_t) c, (Word_t) 0); }
349 Sexpr_t set(Int_t i) { _type_ = T_INT; _car_ = _cdr_ = 0; *_int_ptr_ = i; return this; }
350 Sexpr_t set(Real_t r) { _type_ = T_REAL; _car_ = _cdr_ = 0; *_real_ptr_ = r; return this; }
351 Sexpr_t set(Port_t &p) { return set(T_PORT_HEAP, (Word_t) 0, (Word_t) &p); }
352 //Sexpr_t set(Sexpr_t a, Sexpr_t b) { return set(T_PAIR, a, b); } //!<Set cell as pair
353
354 Sexpr_t set(Int_t typ, Int_t a, Charst_t b) { return set(typ, (Word_t) a, (Word_t) b); }
355 Sexpr_t set(Int_t typ, Int_t a, Bytest_t b) { return set(typ, (Word_t) a, (Word_t) b); }
356 Sexpr_t set(Int_t typ, Int_t a, CharVec_t b) { return set(typ, (Word_t) a, (Word_t) b); }
357 Sexpr_t set(Int_t typ, Int_t a, ByteVec_t b) { return set(typ, (Word_t) a, (Word_t) b); }
359
360 Sexpr_t mk_error(const char *fmt, ...) CHECKPRINTF_pos2;
361 Sexpr_t mk_error(Sexpr_t irritants, const char *fmt, ...) CHECKPRINTF_pos3;
362
368 Sexpr_t prechecked_anypair_get_car() { return (Sexpr_t) get_car(); }
369 Sexpr_t prechecked_anypair_get_cdr() { return (Sexpr_t) get_cdr(); }
370
371 Int_t prechecked_sym_heap_get_hash() { return as_Int_t(); }
372 Charst_t prechecked_sym_heap_get_chars() { return (Charst_t) as_Ptr_t(); }
373 void prechecked_sym_heap_get_info(Int_t &hsh, Charst_t &chars) { hsh = as_Int_t(); chars = (Charst_t) as_Ptr_t(); }
374
377 CharVec_t prechecked_str_imm_get_chars() { return (CharVec_t) _byte2_ptr_; }
378
379 void prechecked_bvec_imm_set_length(Int_t l) { _contents._byte[2] = l; }
380
381 Charst_t prechecked_gensym_get_chars() { static String s = ""; s = toString("G%s", ascii.hex(_cdr_)); return s.c_str(); }
382 void prechecked_gensym_get_info(Int_t &hsh, Charst_t &chars) { hsh = as_Int_t(); chars = prechecked_gensym_get_chars(); }
383
384 Sexpr_t prechecked_error_get_irritants() { return (Sexpr_t) get_cdr(); }
385 Sexpr_t prechecked_error_get_str() { return (Sexpr_t) get_car(); }
386 Charst_t prechecked_error_get_chars() { return prechecked_error_get_str()->any_str_get_chars(); }
388
393#define THROW_BAD_TYPE { throw mk_error("%s Bad type %s", me, dump().c_str()); }
395 Bool_t mustbe_Bool_t() { ME("Cell::mustbe_Bool_t()"); if (type() == T_BOOL) return as_Bool_t(); THROW_BAD_TYPE; }
396 Char_t mustbe_Char_t() { ME("Cell::mustbe_Char_t()"); if (type() == T_CHAR) return as_Char_t(); THROW_BAD_TYPE; }
397 Int_t mustbe_Int_t() { ME("Cell::mustbe_Int_t()"); if (type() == T_INT) return as_Int_t(); THROW_BAD_TYPE; }
398 Real_t mustbe_Real_t() { ME("Cell::mustbe_Real_t()"); if (type() == T_REAL) return as_Real_t(); THROW_BAD_TYPE; }
399
400 Sexpr_t mustbe_any_str_t() { ME("Cell::mustbe_any_str_t()"); if (is_any_str_atom()) return this; THROW_BAD_TYPE; }
401 Sexpr_t mustbe_cppobj_t() { ME("Cell::mustbe_cppobj_t()"); if (type() == T_CPP_HEAP) return this; THROW_BAD_TYPE; }
402
403 CPPDeleterPtr prechecked_cppobj_get_deleter() { return (CPPDeleterPtr) _car_; }
404 Ptr_t prechecked_cppobj_get_ptr() { return as_Ptr_t(); }
405
406 CPPDeleterPtr any_cppobj_get_deleter() { return mustbe_cppobj_t()->prechecked_cppobj_get_deleter(); }
407 Ptr_t any_cppobj_get_ptr() { return mustbe_cppobj_t()->prechecked_cppobj_get_ptr(); }
408 void any_cppobj_get_info(CPPDeleterPtr &d, Ptr_t &p) { Sexpr_t o = mustbe_cppobj_t(); d = o->prechecked_cppobj_get_deleter(); p = o->prechecked_cppobj_get_ptr(); }
409
410 Real_t coerce_Real_t()
411 {
412 ME("Cell::coerce_Real_t()");
413 Int_t typ = type();
414 if (typ == T_REAL) return as_Real_t();
415 else if (typ == T_INT) return (Real_t) as_Int_t();
416 else if (typ == T_CHAR) return (Real_t) as_Char_t();
417 THROW_BAD_TYPE;
418 }
419
420 Real_t coerce_Int_t()
421 {
422 ME("Cell::coerce_Int_t()");
423 Int_t typ = type();
424 if (typ == T_INT) return as_Int_t();
425 else if (typ == T_REAL) return (Int_t) as_Real_t();
426 else if (typ == T_CHAR) return (Int_t) as_Char_t();
427 THROW_BAD_TYPE;
428 }
429
430 Sexpr_t anypair_get_car() { ME("Cell::anypair_get_car()"); if (is_any_pair()) return prechecked_anypair_get_car(); THROW_BAD_TYPE; }
431 Sexpr_t anypair_get_cdr() { ME("Cell::anypair_get_cdr()"); if (is_any_pair()) return prechecked_anypair_get_cdr(); THROW_BAD_TYPE; }
432
433 Sexpr_t error_get_str() { ME("Cell::error_get_str()"); if (_type_ == T_ERROR) return prechecked_error_get_str(); THROW_BAD_TYPE; }
434 Sexpr_t error_get_irritants() { ME("Cell::error_get_irritants()"); if (_type_ == T_ERROR) return prechecked_error_get_irritants(); THROW_BAD_TYPE; }
435 Charst_t error_get_chars() { ME("Cell::error_get_chars()"); if (_type_ == T_ERROR) return prechecked_error_get_chars(); THROW_BAD_TYPE; }
436
437 Int_t any_sym_get_hash() { ME("any_sym_get_hash()"); if (is_any_sym_atom()) return as_Int_t(); THROW_BAD_TYPE; }
438
439 Charst_t any_sym_get_chars() {
440 ME("Cell::any_sym_get_chars()");
441 if (_type_ == T_SYM_HEAP) return prechecked_sym_heap_get_chars();
442 else if (_type_ == T_GENSYM) return prechecked_gensym_get_chars();
443 else THROW_BAD_TYPE;
444 }
445
446 void any_sym_get_info(Int_t &hsh, Charst_t &chars) {
447 ME("Cell::any_sym_get_info()");
448 if (_type_ == T_SYM_HEAP) prechecked_sym_heap_get_info(hsh, chars);
449 else if (_type_ == T_GENSYM) prechecked_gensym_get_info(hsh, chars);
450 else THROW_BAD_TYPE;
451 }
452
454
466
468 CharVec_t any_str_get_chars() {
469 ME("Cell::anystring_get_chars()");
470 if (_type_ == T_STR_IMM) return prechecked_str_imm_get_chars();
471 else if (_type_ == T_STR_HEAP) return prechecked_str_heap_get_chars();
472 else if (_type_ == T_STR_EXT) return prechecked_str_ext_get_chars();
473 else THROW_BAD_TYPE;
474 }
475
476 Int_t any_str_get_length() { return strlen(any_str_get_chars()); }
477 void any_str_get_info(Int_t &len, CharVec_t &chars) { len = strlen(chars = any_str_get_chars()); }
479
505
507 void any_svec_get_info(Int_t &Nelems, Sexpr_t *&elems) {
508 ME("Cell::any_svec_get_info()");
509 Int_t typ = _type_;
510 if (typ <= T_SVEC2N_HEAP) {
511 Nelems = as_Int_t();
512 elems = (Sexpr_t *) as_Ptr_t();
513 }
514 else if (typ == T_SVEC_IMM) { //could be 0, 1 or 2 elems
515 Nelems = 2; //assume
516 if (prechecked_anypair_get_cdr() == OBJ_VOID) Nelems--;
517 if (prechecked_anypair_get_car() == OBJ_VOID) Nelems--;
518 elems = (Sexpr_t *) _car_addr_;
519 }
520 else THROW_BAD_TYPE;
521 }
522
523 Sexpr_t *any_svec_get_elems()
524 {
525 ME("Cell::any_svec_get_elems()");
526 Sexpr_t *res = 0;
527 Int_t typ = _type_;
528
529 if (typ <= T_SVEC2N_HEAP) res = (Sexpr_t *) as_Ptr_t();
530 else if (typ == T_SVEC_IMM) res = (Sexpr_t *) _car_addr_;
531 else THROW_BAD_TYPE;
532
533 return res;
534 }
535
536 Int_t any_bvec_get_length()
537 {
538 ME("Cell::any_bvec_get_length()");
539 Int_t Nelems = -1;
540 if (_type_ == T_BVEC_IMM) {
541 ByteVec_t b = (ByteVec_t) _byte2_ptr_;
542 Nelems = b[0];
543 }
544 else if ((_type_ == T_BVEC_HEAP) || (_type_ == T_BVEC_EXT)) Nelems = as_Int_t();
545 else THROW_BAD_TYPE;
546
547 return Nelems;
548 }
549
550 ByteVec_t any_bvec_get_elems()
551 {
552 ME("Cell::any_bvec_get_elems()");
553 ByteVec_t res = 0;
554 if (_type_ == T_BVEC_IMM) {
555 ByteVec_t b = (ByteVec_t) _byte2_ptr_;
556 res = (ByteVec_t) &(b[1]);
557 }
558 else if ((_type_ == T_BVEC_HEAP) || (_type_ == T_BVEC_EXT)) res = as_ByteVec_t();
559 else THROW_BAD_TYPE;
560 return res;
561 }
562
563 void any_bvec_get_info(Int_t &Nelems, ByteVec_t &elems)
564 {
565 ME("Cell::any_bvec_get_info()");
566 if (_type_ == T_BVEC_IMM) {
567 ByteVec_t b = (ByteVec_t) _byte2_ptr_;
568 Nelems = b[0];
569 elems = (ByteVec_t) &(b[1]);
570 }
571 else if ((_type_ == T_BVEC_HEAP) || (_type_ == T_BVEC_EXT)) {
572 Nelems = as_Int_t();
573 elems = as_ByteVec_t();
574 }
575 else THROW_BAD_TYPE;
576 }
578
579#undef THROW_BAD_TYPE
580#undef _type_
581#undef _flags_
582#undef _car_
583#undef _cdr_
584#undef _car_addr_
585#undef _int_ptr_
586#undef _real_ptr_
587#undef _byte2_ptr_
588
595 String cell_name(void);
596
597 Charst_t type_name(Int_t typ);
598 Charst_t type_name(void) { return type_name(this->type()); }
599 Charst_t gcstate_name(void);
600
601 String dump();
602
603 String str(Sexpr_t sx, Bool_t as_write_or_display, Int_t env_depth, Int_t max_depth);
604 String str(Bool_t as_write_or_display, Int_t env_depth, Int_t max_depth) { return str(this, as_write_or_display, env_depth, max_depth); }
605 String str(Bool_t as_write_or_display, Int_t env_depth) { return str(this, as_write_or_display, env_depth, 10); }
606 String str(Bool_t as_write_or_display) { return str(this, as_write_or_display, 1, 10); }
607 String str(Int_t env_depth) { return str(this, true, env_depth, 10); }
608 String str(void) { return str(this, true, 1, 10); }
610
611private:
612 union {
613 Byte_t _byte[3 * sizeof(Word_t)];
614 Word_t _word[3];
615 } _contents;
616
617};
618
619static_assert(sizeof(Cell) == 3*sizeof(Word_t), "Cell size != 3*Word_t size\n");
620static_assert(sizeof(Cell) == 3*sizeof(Ptr_t), "Cell size != 3*Ptr_t size\n");
621
624extern Sexpr_t LAMB_INPUT;
625extern Sexpr_t LAMB_OUTPUT;
627
628class LambMemoryManager;
629
651class Lamb {
652public:
653
654 Lamb();
655
660 typedef Sexpr_t (*Mop3st_t)(Lamb &lamb, Sexpr_t sexpr, Sexpr_t env_exec);
661
664 Sexpr_t setup(void);
665 Sexpr_t loop(void);
666 void end(void);
668
671 void log(const char *fmt, ...) CHECKPRINTF_pos2;
672 void printf(const char *fmt, ...) CHECKPRINTF_pos2;
673 bool debug(void);
674 void debug(bool onoff);
676
680 unsigned long build_version();
681 unsigned long build_UTC();
682 unsigned long build_pushUTC();
683 const char *build_buildRelease();
684 const char *build_buildDate();
685 const char *build_pushDate();
687
694 void expand();
695 Sexpr_t tcons(Int_t typ, Word_t a, Word_t b, Sexpr_t env_exec);
696 Sexpr_t tcons(Int_t typ, Sexpr_t a, Sexpr_t b, Sexpr_t env_exec);
697
698 Sexpr_t cons(Sexpr_t a, Sexpr_t b, Sexpr_t env_exec) { return tcons(Cell::T_PAIR, a, b, env_exec); }
699
700 Sexpr_t gensym(Sexpr_t env_exec);
701
702 void gc_root_push(Sexpr_t p);
703 void gc_root_pop(Int_t n=1);
705
708 Sexpr_t car(Sexpr_t l);
709 Sexpr_t cdr(Sexpr_t l);
710 Sexpr_t caar(Sexpr_t l) { return car(car(l)); }
711 Sexpr_t cadr(Sexpr_t l) { return car(cdr(l)); }
712 Sexpr_t cdar(Sexpr_t l) { return cdr(car(l)); }
713 Sexpr_t cddr(Sexpr_t l) { return cdr(cdr(l)); }
714 Sexpr_t caddr(Sexpr_t l) { return car(cddr(l)); }
715 Sexpr_t cdddr(Sexpr_t l) { return cdr(cddr(l)); }
716 Sexpr_t cadddr(Sexpr_t l) { return car(cdddr(l)); }
717 Sexpr_t cddddr(Sexpr_t l) { return cdr(cdddr(l)); }
719
722 void set_car_bang(Sexpr_t c, Sexpr_t val);
723 void set_cdr_bang(Sexpr_t c, Sexpr_t val);
724 void vector_set_bang(Sexpr_t vec, Int_t k, Sexpr_t val);
725 Sexpr_t reverse_bang(Sexpr_t l);
727
730 Sexpr_t eq_q(Sexpr_t obj1, Sexpr_t obj2);
731 Sexpr_t eqv_q(Sexpr_t obj1, Sexpr_t obj2);
732 Sexpr_t equal_q(Sexpr_t obj1, Sexpr_t obj2);
733 Sexpr_t assq(Sexpr_t obj, Sexpr_t alist);
735
755
757 Sexpr_t oblist_query(Sexpr_t oblist, const char *identifier, Bool_t force, Sexpr_t env_exec);
758 Sexpr_t oblist_test(Sexpr_t oblist, const char *identifier, Sexpr_t env_exec) { return oblist_query(oblist, identifier, false, env_exec); }
759 Sexpr_t oblist_intern(Sexpr_t oblist, const char *identifier, Sexpr_t env_exec) { return oblist_query(oblist, identifier, true, env_exec); }
760 Sexpr_t oblist_analyze(Sexpr_t oblist, Int_t verbosity, Sexpr_t env_exec);
762
769 Sexpr_t dict_new(Int_t framesize, Sexpr_t env_exec) { return dict_add_empty_frame(NIL, framesize, env_exec); }
770 Sexpr_t dict_new(Sexpr_t env_exec) { return dict_add_empty_frame(NIL, 0, env_exec); }
771
772 Sexpr_t dict_add_empty_frame(Sexpr_t dict, Int_t framesize, Sexpr_t env_exec);
773 Sexpr_t dict_add_empty_frame(Sexpr_t dict, Sexpr_t env_exec) { return dict_add_empty_frame(dict, 0, env_exec); }
774
775 Sexpr_t dict_add_keyval_frame(Sexpr_t dict, Sexpr_t keys, Sexpr_t vals, Sexpr_t env_exec);
776 Sexpr_t dict_add_alist_frame(Sexpr_t dict, Sexpr_t alist, Sexpr_t env_exec) { return mk_dict(alist, dict, env_exec); }
777
778 void dict_bind_bang(Sexpr_t dict, Sexpr_t key, Sexpr_t value, Sexpr_t env_exec);
779 void dict_rebind_bang(Sexpr_t dict, Sexpr_t key, Sexpr_t value, Sexpr_t env_exec);
780 void dict_bind_alist_bang(Sexpr_t dict, Sexpr_t alist, Sexpr_t env_exec);
781 void dict_rebind_alist_bang(Sexpr_t dict, Sexpr_t alist, Sexpr_t env_exec);
782
783 Sexpr_t dict_ref_q(Sexpr_t dict, Sexpr_t key);
784 Sexpr_t dict_ref(Sexpr_t dict, Sexpr_t key);
785
787 Sexpr_t dict_keys(Sexpr_t dict, Sexpr_t env_exec);
788 Sexpr_t dict_values(Sexpr_t dict, Sexpr_t env_exec);
789
790 Sexpr_t dict_to_alist(Sexpr_t dict, Sexpr_t env_exec);
791 Sexpr_t dict_to_2list(Sexpr_t dict, Sexpr_t env_exec);
792 Sexpr_t alist_to_dict(Sexpr_t alist, Sexpr_t env_exec);
793 Sexpr_t twolist_to_dict(Sexpr_t twolist, Sexpr_t env_exec);
794
795 Sexpr_t dict_analyze(Sexpr_t dict, Int_t verbosity = 0);
797
800 Sexpr_t read(LL_Port &src, Sexpr_t env_exec) { return read_sexpr(src, env_exec, false); }
801
802 Sexpr_t write_or_display(Sexpr_t sexpr, Bool_t do_write);
803 Sexpr_t write_simple(Sexpr_t sexpr);
804
805 String sprintf(Charst_t fmt, Sexpr_t sexpr, Sexpr_t env_exec);
806
807 Sexpr_t printf(Sexpr_t args, LL_Port &outp);
808 Sexpr_t printf(Sexpr_t args);
810
813 Sexpr_t eval(Sexpr_t sexpr, Sexpr_t env_exec);
814 Sexpr_t eval_list(Sexpr_t args, Sexpr_t env_exec);
815 Sexpr_t apply_proc_partial(Sexpr_t proc, Sexpr_t sexpr, Sexpr_t env_exec);
816 Sexpr_t map_proc(Sexpr_t proc, Sexpr_t lists, Sexpr_t env_exec);
818
821 Sexpr_t r5_base_environment() { return _r5_base_environment; }
822 Sexpr_t r5_interaction_environment() { return _r5_interaction_environment; }
823 Sexpr_t lamb_oblist() { return _lamb_oblist; }
824
825 Sexpr_t current_input_port() { return _r5_current_input_port; }
826 Sexpr_t current_output_port() { return _r5_current_output_port; }
827 Sexpr_t current_error_port() { return _r5_current_error_port; }
829
830 Sexpr_t load(Charst_t name, Sexpr_t env_exec, Int_t verbosity = 0);
831
834 Sexpr_t append(Sexpr_t sexpr, Sexpr_t env_exec);
835 Sexpr_t append2(Sexpr_t lis, Sexpr_t obj, Sexpr_t env_exec);
836 Sexpr_t list_copy(Sexpr_t sexpr, Sexpr_t env_exec);
837 Sexpr_t list_analyze(Sexpr_t sexpr, Sexpr_t env_exec);
838 Sexpr_t list_to_vector(Sexpr_t l, Sexpr_t env_exec);
839 Sexpr_t vector_to_list(Sexpr_t v, Sexpr_t env_exec);
840 Sexpr_t vector_copy(Sexpr_t from, Sexpr_t env_exec);
842
847 Sexpr_t vector_to_sparsevec(Sexpr_t vec, Sexpr_t skip, Sexpr_t env_exec);
848 Sexpr_t sparsevec_to_vector(Sexpr_t alist, Sexpr_t fill, Sexpr_t env_exec);
850
857 Sexpr_t mk_error(Sexpr_t env_exec, Sexpr_t irritants, const char *fmt, ...) CHECKPRINTF_pos4;
858 Sexpr_t mk_error(Sexpr_t env_exec, const char *fmt, ...) CHECKPRINTF_pos3;
859 Sexpr_t mk_syserror(const char *fmt, ...) CHECKPRINTF_pos2;
860 Sexpr_t mk_syserror(Sexpr_t irritants, const char *fmt, ...) CHECKPRINTF_pos3;
862
869 Sexpr_t mk_bool(Bool_t b, Sexpr_t env_exec) { return tcons(Cell::T_PAIR, NIL, NIL, env_exec)->set(b); }
870 Sexpr_t mk_character(Char_t ch, Sexpr_t env_exec) { return tcons(Cell::T_PAIR, NIL, NIL, env_exec)->set(ch); }
871 Sexpr_t mk_integer(Int_t i, Sexpr_t env_exec) { return tcons(Cell::T_PAIR, NIL, NIL, env_exec)->set(i); }
872 Sexpr_t mk_real(Real_t r, Sexpr_t env_exec) { return tcons(Cell::T_PAIR, NIL, NIL, env_exec)->set(r); }
873
874 Sexpr_t mk_number(Charst_t str, Sexpr_t env_exec);
875 Sexpr_t mk_sharp_const(Charst_t name, Sexpr_t env_exec);
877
904 Sexpr_t mk_string(Sexpr_t env_exec, const char *fmt, ...) CHECKPRINTF_pos3;
905 Sexpr_t mk_string(Int_t k, Charst_t src, Sexpr_t env_exec);
906 Sexpr_t mk_string(String s, Sexpr_t env_exec) { return mk_string(s.length(), s.c_str(), env_exec); }
907
908 Sexpr_t mk_symbol_or_number(Charst_t str, Sexpr_t env_exec);
909 Sexpr_t mk_symbol(Charst_t str, Sexpr_t env_exec) { return oblist_intern(_lamb_oblist, str, env_exec); }
910
911 Sexpr_t mk_bytevector(Int_t k, Sexpr_t env_exec);
912 Sexpr_t mk_bytevector(Int_t k, Bytest_t src, Sexpr_t env_exec);
913 Sexpr_t mk_bytevector(Int_t k, Int_t fill, Sexpr_t env_exec);
914
916 Sexpr_t mk_bytevector_ext(Int_t k, Bytest_t ext, Sexpr_t env_exec) { return tcons(Cell::T_BVEC_EXT, (Word_t) k, (Word_t) ext, env_exec); }
917
919 Sexpr_t mk_intvector(Int_t k, Sexpr_t env_exec);
920 Sexpr_t mk_realvector(Int_t k, Sexpr_t env_exec);
921 Sexpr_t mk_intvector(Int_t k, Int_t fill, Sexpr_t env_exec);
922 Sexpr_t mk_realvector(Int_t k, Real_t fill, Sexpr_t env_exec);
923
924 Sexpr_t mk_vector(Int_t len, Sexpr_t fill, Sexpr_t env_exec);
925 Sexpr_t mk_hashtbl(Int_t len, Sexpr_t fill, Sexpr_t env_exec);
926
927 Sexpr_t mk_serial_port(Sexpr_t env_exec);
928 Sexpr_t mk_input_file_port(Charst_t name, Sexpr_t env_exec);
929 Sexpr_t mk_output_file_port(Charst_t name, Sexpr_t env_exec);
930 Sexpr_t mk_input_string_port(Charst_t inp, Sexpr_t env_exec);
931 Sexpr_t mk_output_string_port(Sexpr_t env_exec);
933
936 Sexpr_t mk_Mop3_procst_t(Mop3st_t f, Sexpr_t env_exec) { return tcons(Cell::T_MOP3_PROC, (Word_t) NIL, (Word_t) f, env_exec); }
937 Sexpr_t mk_Mop3_nprocst_t(Mop3st_t f, Sexpr_t env_exec) { return tcons(Cell::T_MOP3_NPROC, (Word_t) NIL, (Word_t) f, env_exec); }
938 Sexpr_t mk_cppobj(void *obj, CPPDeleterPtr deleter, Sexpr_t env_exec) { return tcons(Cell::T_CPP_HEAP, (Word_t) deleter, (Word_t) obj, env_exec); }
940
954 Sexpr_t mk_macro(Sexpr_t nlam, Sexpr_t env_nlam, Sexpr_t env_exec) { return tcons(Cell::T_MACRO, nlam, env_nlam, env_exec); }
955 Sexpr_t mk_procedure(Sexpr_t formals, Sexpr_t body, Sexpr_t env_proc, Sexpr_t env_exec) { return tcons(Cell::T_PROC, cons(formals, body, env_proc), env_proc, env_exec); }
956 Sexpr_t mk_nprocedure(Sexpr_t formals, Sexpr_t body, Sexpr_t env_nproc, Sexpr_t env_exec) { return tcons(Cell::T_NPROC, cons(formals, body, env_nproc), env_nproc, env_exec); }
957 Sexpr_t mk_thunk_sexpr(Sexpr_t sexpr, Sexpr_t env_thunk, Sexpr_t env_exec) { return tcons(Cell::T_THUNK_SEXPR, sexpr, env_thunk, env_exec); }
958 Sexpr_t mk_thunk_body(Sexpr_t body, Sexpr_t env_thunk, Sexpr_t env_exec) { return tcons(Cell::T_THUNK_BODY, body, env_thunk, env_exec); }
959 Sexpr_t mk_dict(Sexpr_t frame, Sexpr_t base, Sexpr_t env_exec) { return tcons(Cell::T_DICT, frame, base, env_exec); }
960 Sexpr_t mk_dict(Int_t framesize, Sexpr_t env_exec);
962
968 Sexpr_t macroexpand(Sexpr_t form, Sexpr_t env_exec);
969 Sexpr_t macroexpand1(Sexpr_t form, Sexpr_t env_exec);
970
971 Sexpr_t macroexpand(Sexpr_t proc, Sexpr_t args, Sexpr_t env_exec);
972 Sexpr_t macroexpand1(Sexpr_t proc, Sexpr_t args, Sexpr_t env_exec);
974
975 /* put this comment after the last documented member to start new page in doxygen detailed section.
976 \latexonly \newpage \endlatexonly
977 */
978
979private:
980
981 LambMemoryManager *mem;
982
983 bool _debug_in_progress;
984 Int_t _verbosity;
985
986 //Symbols and bindings
987 static const Int_t _lamb_oblist_size = 2048;
988 static const Int_t _r5_base_frame_size = 1024;
989 static const Int_t _r5_interaction_frame_size = 512;
990
991 Sexpr_t _lamb_oblist;
992 Sexpr_t _r5_base_environment;
993 Sexpr_t _r5_interaction_environment;
994
995 Sexpr_t _r5_current_input_port;
996 Sexpr_t _r5_current_output_port;
997 Sexpr_t _r5_current_error_port;
998
999 //Reader
1000 const char *DELIMITERS = "()\";\f\t\v\n\r ";
1001
1002 enum {
1003 TOK_EOF,
1004 TOK_LPAREN,
1005 TOK_RPAREN,
1006 TOK_DOT,
1007 TOK_ATOM,
1008 TOK_SQUOTE,
1009 TOK_DQUOTE,
1010 TOK_BQUOTE,
1011 TOK_COMMA,
1012 TOK_COMMA_AT,
1013 TOK_SHARP,
1014 TOK_SHARP_CONST,
1015 TOK_VECTOR,
1016 TOK_DCOLON,
1017 Ntokens
1018 };
1019
1020 void report();
1021
1022 Int_t produce_token(LL_Port &src);
1023 Sexpr_t consume_token(Int_t tok, LL_Port &src, Sexpr_t env_exec, bool quoted);
1024
1025 //Tokens that the reader recognizes and converts directly to symbols.
1026 Sexpr_t sym_squote; //single quote '
1027 Sexpr_t sym_qquote; //backquote `
1028 Sexpr_t sym_unquote; //comma ,
1029 Sexpr_t sym_uqsplice; //comma-at ,@
1030
1031 //Symbols inserted into the reader output to complete multi-step operations
1032 //e.g., a quoted vector '#(a b c) becomes (apply vector (quote (a b c)))
1033 Sexpr_t sym_apply;
1034 Sexpr_t sym_vector;
1035
1036 //Magic reader tokens that turn into magic symbols.
1037 Sexpr_t sym_colon_hook; //double colon ::
1038 Sexpr_t sym_sharp_hook; //sharp # (not constant or vector)
1039
1040 Sexpr_t sym_ellipsis; //three dot ellipsis ...
1041 Sexpr_t sym_fatarrow; //aka "feed through" =>
1042
1043 //Loop-based control
1044 Sexpr_t sym_loop;
1045
1046 Sexpr_t vector_eqv_q(Sexpr_t obj1, Sexpr_t obj2);
1047 Sexpr_t hashtbl_eqv_q(Sexpr_t obj1, Sexpr_t obj2);
1048 Sexpr_t bytevector_eqv_q(Sexpr_t obj1, Sexpr_t obj2);
1049
1050 //Bindings: Symbols, Variables and Environments
1051 Sexpr_t dump_frame(Sexpr_t frame);
1052 Sexpr_t dump_env_stack(Sexpr_t env_stack);
1053
1055 const char *token2name(Int_t tok);
1056
1057 Sexpr_t read_sexpr(LL_Port &src, Sexpr_t &env_exec, bool quoted);
1058 Sexpr_t read_atom(LL_Port &src, Sexpr_t &env_exec);
1059
1060 Sexpr_t read_any_quote(Sexpr_t symbol, LL_Port &src, Sexpr_t &env_exec);
1061 Sexpr_t read_squote(LL_Port &src, Sexpr_t env_exec) { return read_any_quote(sym_squote, src, env_exec); }
1062 Sexpr_t read_qquote(LL_Port &src, Sexpr_t env_exec) { return read_any_quote(sym_qquote, src, env_exec); }
1063 Sexpr_t read_unquote(LL_Port &src, Sexpr_t env_exec) { return read_any_quote(sym_unquote, src, env_exec); }
1064 Sexpr_t read_uqsplice(LL_Port &src, Sexpr_t env_exec) { return read_any_quote(sym_uqsplice, src, env_exec); }
1065
1066 Sexpr_t read_sharp_const(LL_Port &src, Sexpr_t &env_exec);
1067 Sexpr_t read_sharp(LL_Port &src, Sexpr_t &env_exec);
1068 Sexpr_t read_vector(LL_Port &src, Sexpr_t &env_exec);
1069 Sexpr_t read_quotedvector(LL_Port &src, Sexpr_t &env_exec);
1070 Sexpr_t read_list(LL_Port &src, Sexpr_t &env_exec, bool quoted);
1071 Sexpr_t read_string(LL_Port &src, Sexpr_t &env_exec);
1072
1073};
1074
1075//With error check, inlined for speed.
1076inline Sexpr_t Lamb::car(Sexpr_t c) {
1077 ME("Lamb::car()");
1078 if (c->type() >= Cell::T_PAIR) return c->prechecked_anypair_get_car();
1079 embedded_debug_catcher();
1080 throw mk_syserror("%s Bad type %s", me, c->dump().c_str());
1081}
1082
1083inline Sexpr_t Lamb::cdr(Sexpr_t c) {
1084 ME("Lamb::cdr()");
1085 if (c->type() >= Cell::T_PAIR) return c->prechecked_anypair_get_cdr();
1086 embedded_debug_catcher();
1087 throw mk_syserror("%s Bad type %s", me, c->dump().c_str());
1088}
1089
1090#endif
Definition LambLisp.h:104
Sexpr_t set(Int_t typ, Int_t a, Bytest_t b)
Set cell as a immutable bytevector.
Definition LambLisp.h:355
static const int F_GC02
gc flag
Definition LambLisp.h:199
Sexpr_t set(Bool_t b)
Set cell as boolean.
Definition LambLisp.h:347
Real_t as_Real_t()
Return the value of this cell as a real number.
Definition LambLisp.h:314
Bool_t is_any_pair(void)
Return true if the cell is any pair type.
Definition LambLisp.h:269
Sexpr_t set(Int_t typ, Int_t a, CharVec_t b)
Set cell as a mutable string.
Definition LambLisp.h:356
CharVec_t any_str_get_chars()
If the cell is any kind of string, return a pointer to the zero-terminated character array.
Definition LambLisp.h:468
void flags_set(Int_t f)
Set the selected flags.
Definition LambLisp.h:231
void rplaca(Word_t p)
Replace the cell car field. Called rplaca for historical reasons, and to distinguish it from set-car!...
Definition LambLisp.h:234
Sexpr_t mustbe_cppobj_t()
Return this cell if it is a CPP object.
Definition LambLisp.h:401
Sexpr_t set(Int_t typ, Int_t a, ByteVec_t b)
Set cell as a mutable bytevector.
Definition LambLisp.h:357
Bool_t is_any_str_atom(void)
Return true if the cell is any kind of string.
Definition LambLisp.h:272
CharVec_t prechecked_str_ext_get_chars()
Return a pointer to the character array located outside the heap.
Definition LambLisp.h:376
ByteVec_t as_ByteVec_t()
Return the value of this cell as a pointer to an array of bytes.
Definition LambLisp.h:320
Bool_t is_any_svec2n_atom()
Return true if the cell is Sexpr_t vector of size 2^n.
Definition LambLisp.h:271
String cell_name(void)
A convenience feature to produce a string name for cells which are "well known" like NIL....
Sexpr_t set(Int_t i)
Set cell as integer.
Definition LambLisp.h:349
Word_t get_car(void)
Return the value of the cell car.
Definition LambLisp.h:308
void rplacd(Word_t p)
Replace the cell cdr field. Called rplacd for historical reasons, and to distinguish it from set-cdr!...
Definition LambLisp.h:235
Ptr_t get_car_addr()
Return the address of the cell car.
Definition LambLisp.h:307
static const int F_GC04
gc flag
Definition LambLisp.h:200
Real_t mustbe_Real_t()
Return the value of this cell as a real number.
Definition LambLisp.h:398
CharVec_t as_CharVec_t()
Return the value of this cell as a pointer to a zero-terminated character array.
Definition LambLisp.h:319
Char_t as_Char_t()
Return the value of this cell as a character.
Definition LambLisp.h:312
Char_t mustbe_Char_t()
Return the value of this cell as a character.
Definition LambLisp.h:396
Sexpr_t tail_state_set(void)
Set the tail state flag on this cell, and return the cell.
Definition LambLisp.h:301
Bool_t is_any_bvec_atom(void)
Return true if the cell is any kind of bytevector.
Definition LambLisp.h:274
Sexpr_t set(Real_t r)
Set cell as real.
Definition LambLisp.h:350
Int_t type(void)
Return the type of the cell as a small integer.
Definition LambLisp.h:227
Charst_t type_name(Int_t typ)
Return a pointer to the C string corresponding to the cell type.
Int_t as_Int_t()
Return the value of this cell as an integer.
Definition LambLisp.h:313
static const int F_0x20
spare
Definition LambLisp.h:203
static const int F_0x40
spare
Definition LambLisp.h:204
static const int GC_STATE_MASK
Mask for obtaining garbage collection state bits from Cell flag byte.
Definition LambLisp.h:206
Ptr_t as_Ptr_t()
Return the value of this cell as a generic pointer (i.e., void*).
Definition LambLisp.h:316
Bool_t is_atom(void)
Return true if the cell is an atom.
Definition LambLisp.h:265
Bool_t as_Bool_t()
Return the value of this cell as a boolean.
Definition LambLisp.h:311
Int_t as_numerator()
Return the value of this cell as the numerator of a rational number.
Definition LambLisp.h:323
Sexpr_t set(Int_t typ, Int_t a, Charst_t b)
Set cell as a immutable string.
Definition LambLisp.h:354
Sexpr_t mustbe_any_str_t()
Return this cell if it is a string.
Definition LambLisp.h:400
void flags_clr(Int_t f)
Clear the selected flags.
Definition LambLisp.h:232
Word_t get_cdr(void)
Return the value of the cell cdr.
Definition LambLisp.h:309
static const int F_GC01
gc flag
Definition LambLisp.h:198
Bool_t is_any_svec_atom()
Return true if the cell is any kind of Sexpr_t vector.
Definition LambLisp.h:270
Charst_t as_Charst_t()
Return the value of this cell as a pointer to a zero-terminated character array.
Definition LambLisp.h:317
static const int F_0x80
spare
Definition LambLisp.h:205
Bool_t is_pair(void)
Return true if the cell is a cons pair.
Definition LambLisp.h:266
Int_t gc_state(void)
Return the garbage collection stat eof this cell.
Definition LambLisp.h:298
CharVec_t prechecked_str_imm_get_chars()
Return a pointer to the character array embedded in this cell.
Definition LambLisp.h:377
void zero()
Set all cell bits to zero.
Definition LambLisp.h:226
Int_t hash_sexpr(void)
For symbols, the hash of its characters; for numbers, the hash of the number; otherwise the hash of t...
Sexpr_t gc_state(Int_t st)
Set the garbage collection state of this cell, and return the cell.
Definition LambLisp.h:299
@ T_STR_EXT
(reserved Charst_t) Same as T_STR but externally allocated; the character array is not freed at GC ti...
Definition LambLisp.h:151
@ T_ERROR
(Sexpr_t Sexpr_t) An error cell; car is a T_STRING, cdr is a pointer to irritants.
Definition LambLisp.h:190
@ T_CHAR
(Char_t reserved) Character atom
Definition LambLisp.h:159
@ T_NEEDS_FINALIZING
Any types less than or equal to this have data stored in the heap, and need specialized finalizing at...
Definition LambLisp.h:148
@ T_BVEC_IMM
Special format: type and flag bytes as usual, byte 2 is vector length, remaining bytes are vector ele...
Definition LambLisp.h:153
@ T_PORT_HEAP
(reserved Ptr_t) car is unused, cdr is ptr to underlying C++ port instance
Definition LambLisp.h:147
@ T_BVEC_EXT
(Int_t Bytest_t) Same as T_BYTEVEC but externally allocated; the byte array is not freed at GC time.
Definition LambLisp.h:150
@ T_SVEC_HEAP
(Int_t Sexpr_t[]) A vector of Sexprs is a (len Sexpr_t*) pair.
Definition LambLisp.h:137
@ T_VOID
(don't care) VOID has its own type and a singleton instance OBJ_VOID
Definition LambLisp.h:169
@ T_INT
(Int_t reserved) Integer atom
Definition LambLisp.h:160
@ T_PAIR
(Sexpr_t Sexpr_t) Normal untyped cons cell. All C++ types < T_PAIR are atoms.
Definition LambLisp.h:182
@ T_GENSYM
Runtime symbol generation with no heap operations.
Definition LambLisp.h:155
@ T_SVEC2N_HEAP
(Int_t Sexpr_t[]) Same as vector, but length must be a power of 2; useful for hash tables.
Definition LambLisp.h:138
@ T_MACRO
(Sexpr_t Sexpr_t) Macro; car is transformer (proc of 1 arg) and cdr is env.
Definition LambLisp.h:186
@ T_UNDEF
(ERROR ERROR) UNDEF has its own type and a singleton instance OBJ_UNDEF
Definition LambLisp.h:170
@ T_THUNK_SEXPR
(Sexpr_t Sexpr_t) S-expression thunk pair; car is sexpr, cdr is environment with all variable binding...
Definition LambLisp.h:188
@ T_CPP_HEAP
(ptr-to-cpp-deleter ptr-to-cpp-object) Deleter is a function void f(void *cpp_obj) of the appropriate...
Definition LambLisp.h:145
@ T_THUNK_BODY
(Sexpr_t Sexpr_t) Code body thunk pair; car is body (i.e., list of sexprs), cdr is environment with a...
Definition LambLisp.h:189
@ T_NIL
(ERROR ERROR) NIL has its own type and a singleton instance.
Definition LambLisp.h:176
@ T_BVEC_HEAP
(Int_t Bytest_t) Bytevector is a (len byte*) pair.
Definition LambLisp.h:143
@ T_STR_HEAP
(reserved Charst_t) The reserved field may be used in future to store the string length (not possible...
Definition LambLisp.h:144
@ T_SYM_HEAP
(Int_t Charst_t) Symbol is a (hash char*) pair.
Definition LambLisp.h:142
@ T_MOP3_NPROC
(reserved *Mop3st_t) pointer to native macro processor - args are not evaluated before calling
Definition LambLisp.h:166
@ Ntypes
Number of LambLisp virtual machine types.
Definition LambLisp.h:192
@ T_SVEC_IMM
(Sexpr_t Sexpr_t) Vector of 0, 1 or 2 elements.
Definition LambLisp.h:183
@ T_REAL
(Special format: Real_t uses double word) Real number atom
Definition LambLisp.h:161
@ T_PROC
(Sexpr_t Sexpr_t) Procedure pair; car is lambda (formals + body containing free variables),...
Definition LambLisp.h:184
@ T_BOOL
(Bool_t reserved) Boolean atom
Definition LambLisp.h:158
@ T_DICT
(Sexpr_t Sexpr_t) Dictionary pair; car is local frame, cdr is list of parent frames,...
Definition LambLisp.h:187
@ T_ANY_HEAP_SVEC
Any types less than or equal to this are heap-stored vectors of S-expressions. They need specialized ...
Definition LambLisp.h:140
@ T_NPROC
(Sexpr_t Sexpr_t) Non-evaluating procedure pair; car is nlambda (formals + body containing free varia...
Definition LambLisp.h:185
@ T_MOP3_PROC
(reserved *Mop3st_t) pointer to native function - args are evaluated before calling
Definition LambLisp.h:165
@ T_STR_IMM
Special format: type and flag bytes as usual, remaining bytes are 0-terminated string embedded in the...
Definition LambLisp.h:154
@ T_RATIONAL
(Int_t Int_t) The fields are numerator and denominator of a rational number.
Definition LambLisp.h:162
static const int F_TAIL
trampoline tail marker
Definition LambLisp.h:201
Sexpr_t set(Int_t typ, Word_t w1, Word_t w2)
This is the lowest-level generic "set" function.
Definition LambLisp.h:343
Bytest_t as_Bytest_t()
Return the value of this cell as a pointer to an array of bytes.
Definition LambLisp.h:318
Sexpr_t set(Int_t typ, Sexpr_t a, Sexpr_t b)
This is a convenience function for common cases.
Definition LambLisp.h:345
Ptr_t prechecked_cppobj_get_ptr()
Return a pointer to a C++ object obtained earlier.
Definition LambLisp.h:404
Bool_t is_any_sym_atom(void)
Return true if the cell is any kind of symbol.
Definition LambLisp.h:273
Sexpr_t tail_state_clr(void)
Clear the tail state flag on this cell, and return the cell.
Definition LambLisp.h:302
Sexpr_t set(Int_t typ, Int_t a, Sexpr_t b)
This is a convenience function for common cases.
Definition LambLisp.h:344
Int_t tail_state(void)
Return the taill state of this cell.
Definition LambLisp.h:300
Bool_t mustbe_Bool_t()
Return the value of this cell as a boolean.
Definition LambLisp.h:395
Sexpr_t mk_error(const char *fmt,...) CHECKPRINTF_pos2
Fills and returns the single Cell-level T_ERROR object.
Portst_t as_Portst_t()
Return the value of this cell as a pointer to an instance of the system underlying "port" implementat...
Definition LambLisp.h:321
Int_t hash(void)
Return the hash value of this cell.
Definition LambLisp.h:338
CPPDeleterPtr prechecked_cppobj_get_deleter()
Return the function to be called at garbage collection time to recycle the C++ object.
Definition LambLisp.h:403
static const int F_0x10
spare
Definition LambLisp.h:202
Sexpr_t set(Char_t c)
Set cell as character.
Definition LambLisp.h:348
Int_t hash_contents(void)
For symbols, the hash of its characters; for numbers, strings, vectors, and bytevectors,...
Int_t mustbe_Int_t()
Return the value of this cell as an integer.
Definition LambLisp.h:397
Int_t flags(void)
Return the entire set of cell flags.
Definition LambLisp.h:230
Sexpr_t set(Port_t &p)
Set cell as port.
Definition LambLisp.h:351
Charst_t gcstate_name(void)
Return a pointer to a C string corresponding to the given GC state.
String dump()
Return a printable representation of the Cell internals.
Int_t as_denominator()
Return the value of this cell as the denominator of a rational number.
Definition LambLisp.h:324
CharVec_t prechecked_str_heap_get_chars()
Return a pointer to the character array in the heap.
Definition LambLisp.h:375
void type(Int_t t)
Set the type of this cell.
Definition LambLisp.h:228
Sexpr_t mk_bytevector_ext(Int_t k, Bytest_t ext, Sexpr_t env_exec)
Injection of externally allocated memory, which will not be freed at GC time.
Definition LambLisp.h:916
Sexpr_t apply_proc_partial(Sexpr_t proc, Sexpr_t sexpr, Sexpr_t env_exec)
Evaluate the function arguments and then apply the function to them. The result may be a tail requiri...
Sexpr_t assq(Sexpr_t obj, Sexpr_t alist)
Search an association list for a matching key, and return the (key . value) pair, or false if not fou...
Sexpr_t alist_to_dict(Sexpr_t alist, Sexpr_t env_exec)
Convert an alist into a dictionary. The resulting dictionary has no parent.
Sexpr_t(* Mop3st_t)(Lamb &lamb, Sexpr_t sexpr, Sexpr_t env_exec)
Definition LambLisp.h:660
const char * build_buildDate()
Return a pointer to a character array containing the build date.
Sexpr_t mk_realvector(Int_t k, Sexpr_t env_exec)
Allocates a bytevector from the heap to ensure proper alignment (no IMM type).
Sexpr_t loop(void)
Run often to maintain control. The main responsibility of C++ Lamb::loop() is to call the LambLisp fu...
Sexpr_t gensym(Sexpr_t env_exec)
Produce a new unique symbol.
Sexpr_t eqv_q(Sexpr_t obj1, Sexpr_t obj2)
Return true if 1 cells are the same cell, or are atoms with the same value.
Sexpr_t tcons(Int_t typ, Word_t a, Word_t b, Sexpr_t env_exec)
Generic cell constructor with no GC protection for its arguments.
void printf(const char *fmt,...) CHECKPRINTF_pos2
C-level printf feature with a limit on the length of strings produced.
Sexpr_t eq_q(Sexpr_t obj1, Sexpr_t obj2)
Return true if 2 cells are the same cell, or are atoms with the same value.
Sexpr_t cons(Sexpr_t a, Sexpr_t b, Sexpr_t env_exec)
Cell constructor for normal pairs.
Definition LambLisp.h:698
Sexpr_t dict_values(Sexpr_t dict, Sexpr_t env_exec)
Return a list of all the values in this dictionary. If the dictionary has parents,...
Sexpr_t mk_bytevector(Int_t k, Bytest_t src, Sexpr_t env_exec)
Heap allocation with initialization.
Sexpr_t mk_bytevector(Int_t k, Sexpr_t env_exec)
Simplest heap allocation with no initialization.
unsigned long build_version()
Return the build version as a long integer (implemented as a UTC time stamp).
Sexpr_t reverse_bang(Sexpr_t l)
Reverse the list in-place and return the new list head (the former list tail).
Sexpr_t list_analyze(Sexpr_t sexpr, Sexpr_t env_exec)
Traverse the list and return either false (if circular), the list length (if a proper list),...
Sexpr_t mk_bytevector(Int_t k, Int_t fill, Sexpr_t env_exec)
Heap allocation with initialization.
const char * build_pushDate()
Return a pointer to a character array containing the date this repo was last pushed.
void gc_root_pop(Int_t n=1)
Release the preserved cells for normal GC processing.
const char * build_buildRelease()
Return a pointer to a character array containing the release description.
void set_cdr_bang(Sexpr_t c, Sexpr_t val)
Replace the cdr field in the cell with val. GC flags will be maintained as required.
Sexpr_t twolist_to_dict(Sexpr_t twolist, Sexpr_t env_exec)
Convert a list of 2-element lists into a dictionary. The resulting dictionary has no parent.
Sexpr_t dict_to_2list(Sexpr_t dict, Sexpr_t env_exec)
Convert the dictionary to a list of 2-element lists, retaining only the top-level (key value) sublist...
Sexpr_t dict_add_empty_frame(Sexpr_t dict, Sexpr_t env_exec)
Returns a new dictionary with an empty top frame, and an existing dictionary as parent.
Definition LambLisp.h:773
Sexpr_t equal_q(Sexpr_t obj1, Sexpr_t obj2)
Returns true when obj1 and obj2 are eqv?, and also all their descendants.
Sexpr_t dict_ref_q(Sexpr_t dict, Sexpr_t key)
Returns (key value) pair, or false if key is unbound.
Sexpr_t mk_intvector(Int_t k, Sexpr_t env_exec)
Note that integer vectors and real vectors are just bytevectors underneath.
unsigned long build_UTC()
Return the UTC time of this build.
Sexpr_t dict_to_alist(Sexpr_t dict, Sexpr_t env_exec)
Convert the dictionary to an alist, retaining only the top-level (key . value) pairs.
void expand()
Add another block of cells to the population.
Sexpr_t mk_syserror(const char *fmt,...) CHECKPRINTF_pos2
Fill the system error object with the info given, and return it.
Sexpr_t eval_list(Sexpr_t args, Sexpr_t env_exec)
Evaluate the list of S-expressions in the environment provided, and return a lkist of results.
void log(const char *fmt,...) CHECKPRINTF_pos2
C-level printf feature with a limit on the length of strings produced. Takes care of log prompt.
void end(void)
Release resources used by the Lamb virtual machine.
bool debug(void)
Return the state of the Lamb internal debug flag.
void dict_bind_alist_bang(Sexpr_t dict, Sexpr_t alist, Sexpr_t env_exec)
Modify the target dictionary; binding keys in the alist to their corresponding values.
Sexpr_t dict_keys(Sexpr_t dict, Sexpr_t env_exec)
Note that keys and values are guaranteed to return in the corresponding order in dict_keys and dict_v...
Sexpr_t dict_add_alist_frame(Sexpr_t dict, Sexpr_t alist, Sexpr_t env_exec)
Returns a new dictionary with the alist bindings added in a new frame on top of the base dictionary.
Definition LambLisp.h:776
Sexpr_t dict_ref(Sexpr_t dict, Sexpr_t key)
Returns the value associated with the key in the given dictionary; throws error if key is unbound.
void dict_rebind_alist_bang(Sexpr_t dict, Sexpr_t alist, Sexpr_t env_exec)
Modify the target dictionary; rebinding keys in the alist to their corresponding values.
Sexpr_t dict_new(Sexpr_t env_exec)
Return a new empty dictionary with alist top frame.
Definition LambLisp.h:770
void dict_bind_bang(Sexpr_t dict, Sexpr_t key, Sexpr_t value, Sexpr_t env_exec)
Modify the target dictionary; value is re-assigned to keyif found, else created in the top frame.
void gc_root_push(Sexpr_t p)
Preserve the cell given from GC, until popped.
Sexpr_t dict_add_keyval_frame(Sexpr_t dict, Sexpr_t keys, Sexpr_t vals, Sexpr_t env_exec)
Returns a new dictionary with a new top frame containing the keys bound to the values.
void dict_rebind_bang(Sexpr_t dict, Sexpr_t key, Sexpr_t value, Sexpr_t env_exec)
Modify the target dictionary; value is assigned to key wherever first found in env,...
Sexpr_t dict_analyze(Sexpr_t dict, Int_t verbosity=0)
Internal integrity check.
Sexpr_t eval(Sexpr_t sexpr, Sexpr_t env_exec)
Evaluate the S-expression in the environment provided.
unsigned long build_pushUTC()
Return the UTC time this repo was last pushed.
Sexpr_t dict_new(Int_t framesize, Sexpr_t env_exec)
Return a new empty dictionary with the given top frame size.
Definition LambLisp.h:769
Sexpr_t append2(Sexpr_t lis, Sexpr_t obj, Sexpr_t env_exec)
Destructively attach obj to the end of lis, which must be a proper list.
void vector_set_bang(Sexpr_t vec, Int_t k, Sexpr_t val)
Replace the specified vector element with val. GC flags will be maintained as required.
bool build_isDebug()
Return true if this build is not checked in. This is unrelated to the runtime debug flag.
Sexpr_t mk_error(Sexpr_t env_exec, Sexpr_t irritants, const char *fmt,...) CHECKPRINTF_pos4
Fill a new error object with the information given, and return it.
void set_car_bang(Sexpr_t c, Sexpr_t val)
Replace the car field in the cell with val. GC flags will be maintained as required.
Sexpr_t load(Charst_t name, Sexpr_t env_exec, Int_t verbosity=0)
Load and evaluate S-expressions from the named file.
Sexpr_t map_proc(Sexpr_t proc, Sexpr_t lists, Sexpr_t env_exec)
Apply the procecure to the lists per R5RS.
Sexpr_t setup(void)
Run once after base platform has started. In particular, Serial should be initialized before calling ...