| 1 | #ifndef CL_BRIDGE_UTILS_HPP |
| 2 | #define CL_BRIDGE_UTILS_HPP |
| 3 | #include <iostream> |
| 4 | #include <string> |
| 5 | #ifdef slots |
| 6 | #undef slots |
| 7 | #endif |
| 8 | #include <ecl/ecl.h> |
| 9 | |
| 10 | using std::string; |
| 11 | using lisp_expr = std::string; |
| 12 | using std::cout; |
| 13 | using std::endl; |
| 14 | //extern string CL_MAIN_FASB ; |
| 15 | //extern string CL_MAIN_PACKAGE_NAME ; |
| 16 | |
| 17 | cl_object lispfy(string str); /* convert a std::string to cl_object */ |
| 18 | |
| 19 | |
| 20 | |
| 21 | /* add spaces among several strings. */ |
| 22 | string __spc_expr(string first); |
| 23 | template <typename ...str> |
| 24 | string __spc_expr (string first, str ... next){ |
| 25 | return first+" "+__spc_expr(next...); |
| 26 | } |
| 27 | |
| 28 | /* encapsule expressions in parenthesis. */ |
| 29 | /* to create lisp expr. */ |
| 30 | template<typename ...str> |
| 31 | lisp_expr par_expr(str... all){ |
| 32 | return "("+__spc_expr(all...)+")"; |
| 33 | } |
| 34 | |
| 35 | /* turn the sequence into a lisp list expr. */ |
| 36 | /* ex: par_list("hello", "lisp", "world"); |
| 37 | * -> '("hello" "lisp" "world") */ |
| 38 | template<typename ...str> |
| 39 | lisp_expr par_list(str... all){ |
| 40 | return "'"+par_expr(all...); |
| 41 | } |
| 42 | |
| 43 | /* an enhanced version of cl_eval */ |
| 44 | template<typename ...str> |
| 45 | cl_object cl_eval(str... all){ |
| 46 | std::cout<<par_expr(all...)<<std::endl; |
| 47 | return cl_eval(lispfy(par_expr(all...))); |
| 48 | } |
| 49 | |
| 50 | auto cl_list_traverse=[](auto& cl_lst, auto fn){ |
| 51 | while(!Null(cl_lst)) |
| 52 | { |
| 53 | fn(cl_car(cl_lst)); |
| 54 | cl_lst=cl_cdr(cl_lst); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | /* enhanced cl_object in c++ */ |
| 60 | class cl_obj { |
| 61 | private: |
| 62 | cl_object __obj; |
| 63 | public: |
| 64 | |
| 65 | cl_obj(cl_object &&obj){this->__obj=obj;} |
| 66 | cl_obj(const cl_object &obj){this->__obj=obj;} |
| 67 | |
| 68 | /* list index */ |
| 69 | inline cl_obj car(){return cl_obj(cl_car(this->__obj));} |
| 70 | inline cl_obj cdr(){return cl_obj(cl_cdr(this->__obj));} |
| 71 | inline cl_obj cadr(){return this->cdr().car();} |
| 72 | inline cl_obj caar(){return this->car().car();} |
| 73 | inline cl_obj cddr(){return this->cdr().cdr();} |
| 74 | |
| 75 | /* predicates */ |
| 76 | inline bool nullp(){return Null(this->__obj);} |
| 77 | inline bool atomp(){return ECL_ATOM(this->__obj);} |
| 78 | inline bool listp(){return ECL_LISTP(this->__obj);} |
| 79 | inline bool symbolp(){return ECL_SYMBOLP(this->__obj);} |
| 80 | |
| 81 | inline int to_int(){return ecl_to_int(this->__obj);} |
| 82 | inline char to_char(){return ecl_to_char(this->__obj);} |
| 83 | |
| 84 | /* turn the cl_object into string. */ |
| 85 | inline std::string to_std_string(){ |
| 86 | std::string val; |
| 87 | auto & str=this->__obj->string; |
| 88 | for(unsigned long i=0;i<str.fillp;i+=1) |
| 89 | val+=*(typeof(str.elttype) *)(str.self+i); |
| 90 | return val; |
| 91 | } |
| 92 | |
| 93 | /* traverse the cl_object as a list. */ |
| 94 | template<typename function> |
| 95 | inline void list_traverse(function fn){cl_list_traverse(this->__obj, fn);} |
| 96 | |
| 97 | inline cl_obj operator=(cl_object &&obj){return cl_obj(obj);} |
| 98 | |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | |
| 103 | #endif // CL_BRIDGE_UTILS_HPP |