/* 88888888 888888888888 88888888888888 8888888888888888 888888888888888888 888888 8888 888888 88888 88 88888 888888 8888 888888 88888888888888888888 88888888888888888888 8888888888888888888888 8888888888888888888888888888 88888888888888888888888888888888 88888888888888888888 888888888888888888888888 888888 8888888888 888888 888 8888 8888 888 888 888 OCTOBANANA Licensed under the MIT License Copyright (c) 2019 Brett Robinson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // TODO static link binary // TODO add type system #include "ob/term.hh" #include "ob/text.hh" // TODO find replacement with permissible licence for static linking #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; namespace iom = OB::Term::iomanip; namespace aec = OB::Term::ANSI_Escape_Codes; using namespace std::string_literals; struct Typ; extern std::vector typ_str; extern std::unordered_map paren_begin; extern std::unordered_map paren_end; using u8 = std::uint8_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; using i8 = std::int8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using f32 = float; using f64 = double; using Int = boost::multiprecision::mpz_int; using Rat = boost::multiprecision::mpq_rational; using Flo = boost::multiprecision::mpfr_float; using Num = std::variant; using Sym = std::string; using Str = OB::Text::String; using Atm = std::variant; struct Xpr; using Lst = std::list; struct Env; struct Fun { using Fn = std::function)>; Lst args {}; Fn fn {nullptr}; std::shared_ptr env {nullptr}; std::optional yield {std::nullopt}; // std::shared_ptr> memo {std::make_shared>()}; std::shared_ptr bind(Sym const& sym, Lst& l, std::shared_ptr e); }; struct Xpr : std::variant {}; struct Val { enum : u64 { nil = 0, evaled = 1<<0, constant = 1<<1, pure = 1<<2, lazy = 1<<3, }; Xpr xpr; std::shared_ptr env {nullptr}; u64 ctx {nil}; }; struct Env { using Inner = std::map; using Outer = std::shared_ptr; Inner inner {}; Outer outer {nullptr}; Outer current {nullptr}; std::shared_ptr> memo {std::make_shared>()}; Env(Outer outer = nullptr, Outer current = nullptr) : outer {outer}, current {current} {} Val& operator[](Sym const& sym); std::optional find(Sym const& sym); std::optional find_inner(Sym const& sym); std::optional find_outer(Sym const& sym); std::optional find_current(Sym const& sym); std::optional find_current_inner(Sym const& sym); void dump(); void dump_inner(); void list(Xpr& x); }; using Tok = std::string; using Tks = std::deque; Tks str_tks(std::string_view& str); Atm tok_atm(Tok const& tk); std::optional tks_xpr(Tks& ts); std::optional read(std::string_view& str); std::optional read(std::string_view&& str); std::size_t type(Xpr const& x); template std::string print(T const& t); std::string show(Xpr const& x); std::string print(Xpr const& x); std::string cprint(Xpr const& x); bool find_sym(Xpr const& xpr, Sym const& sym); void resolve_sym(Xpr& xpr, Sym const& sym, Xpr const& rpl); Xpr eval_impl(Xpr&, std::shared_ptr); Xpr eval(Xpr& xr, std::shared_ptr ev); Xpr eval(Xpr&& xr, std::shared_ptr ev); void env_init(std::shared_ptr ev, int argc, char** argv); void repl(int argc, char** argv); #define xpr_atm(x) (std::get_if(x)) #define xpr_num(x) (std::get_if(std::get_if(x))) #define xpr_int(x) (std::get_if(std::get_if(std::get_if(x)))) #define xpr_rat(x) (std::get_if(std::get_if(std::get_if(x)))) #define xpr_flo(x) (std::get_if(std::get_if(std::get_if(x)))) #define xpr_sym(x) (std::get_if(std::get_if(x))) #define xpr_str(x) (std::get_if(std::get_if(x))) #define xpr_lst(x) (std::get_if(x)) #define xpr_fun(x) (std::get_if(x)) #define atm_num(x) (std::get_if(x)) #define atm_sym(x) (std::get_if(x)) #define atm_str(x) (std::get_if(x)) #define atm_int(x) (std::get_if(std::get_if(x))) #define atm_rat(x) (std::get_if(std::get_if(x))) #define atm_flo(x) (std::get_if(std::get_if(x))) #define num_int(x) (std::get_if(x)) #define num_rat(x) (std::get_if(x)) #define num_flo(x) (std::get_if(x)) #define fun_xpr(x) (Xpr{Fun{(x)}}) #define num_atm(x) (Atm{Num{(x)}}) #define num_xpr(x) (Xpr{Atm{Num{(x)}}}) #define sym_atm(x) (Atm{Sym{(x)}}) #define sym_xpr(x) (Xpr{Atm{Sym{(x)}}}) #define str_atm(x) (Atm{Str{(x)}}) #define str_xpr(x) (Xpr{Atm{Str{(x)}}}) #define str_lst(x) std::get(*read((x))) #define holds(x, y) (std::holds_alternative(x))