lispp

A Lisp interpreter in C++.


lispp

/

src

/

main.cc

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
/*
                                    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 <https://octobanana.com/>

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.
*/

#include <cstdint>
#include <list>
#include <deque>
#include <string>
#include <vector>
#include <limits>
#include <utility>
#include <sstream>
#include <variant>
#include <iostream>
#include <iterator>
#include <functional>
#include <type_traits>

using i8 = std::int8_t;
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using f32 = float;
using f64 = double;
using num = std::variant<i64, f64>;
using sym = std::string;
using atm = std::variant<num, sym>;
struct xpr;
using lst = std::list<xpr>;
using fun = std::function<xpr(xpr&)>;
struct xpr : std::variant<lst, fun, atm> {};
using env = std::unordered_map<sym, xpr>;
using Tokens = std::deque<std::string>;

#define xpr_atm(x) (std::get_if<atm>(&(x)))
#define xpr_num(x) (std::get_if<num>(std::get_if<atm>(&(x))))
#define xpr_i64(x) (std::get_if<i64>(std::get_if<num>(std::get_if<atm>(&(x)))))
#define xpr_f64(x) (std::get_if<f64>(std::get_if<num>(std::get_if<atm>(&(x)))))
#define xpr_sym(x) (std::get_if<sym>(std::get_if<atm>(&(x))))
#define xpr_lst(x) (std::get_if<lst>(&(x)))
#define xpr_fun(x) (std::get_if<fun>(&(x)))
#define atm_num(x) (std::get_if<num>(&(x)))
#define atm_sym(x) (std::get_if<sym>(&(x)))
#define atm_i64(x) (std::get_if<i64>(std::get_if<num>(&(x))))
#define atm_f64(x) (std::get_if<f64>(std::get_if<num>(&(x))))
#define num_i64(x) (std::get_if<i64>(&(x)))
#define num_f64(x) (std::get_if<f64>(&(x)))
#define i64_xpr(x) (xpr{atm{num{(x)}}})
#define f64_xpr(x) (xpr{atm{num{(x)}}})
#define sym_atm(x) (atm{sym{(x)}})
#define sym_xpr(x) (xpr{atm{sym{(x)}}})
#define holds(x, y) (std::holds_alternative<y>((x)))

std::string replace(std::string str, std::string const& key, std::string const& val, std::size_t size = std::numeric_limits<std::size_t>::max()) {
  for (std::size_t pos {0}; size-- > 0;) {
    pos = str.find(key, pos);
    if (pos == std::string::npos) {break;}
    str.replace(pos, key.size(), val);
    pos += val.size();
  }
  return str;
}

std::string replace(std::string str, std::vector<std::pair<std::string, std::string>> const& vals) {
  for (auto const& [key, val] : vals) {str = replace(str, key, val);}
  return str;
}

std::deque<std::string> split(std::string const& str, std::string const& delim, std::size_t size = std::numeric_limits<std::size_t>::max()) {
  std::string buf;
  std::deque<std::string> tok;
  std::size_t start {0};
  auto end = str.find(delim);
  while ((size-- > 0) && (end != std::string::npos)) {
    buf = str.substr(start, end - start);
    if (buf.size()) {tok.emplace_back(buf);}
    start = end + delim.size();
    end = str.find(delim, start);
  }
  buf = str.substr(start, end);
  if (buf.size()) {tok.emplace_back(buf);}
  return tok;
}

template<typename T, typename F1, typename F2> void for_each(T& t, F1 const& f1, F2 const& f2) {
  if (t.empty()) {return;}
  auto end = --t.end();
  for (auto i = t.begin(); i != end; ++i) {auto& e = *i; f1(e);}
  f2(t.back());
}

void env_init(env& ev) {
  auto const fc_if64 = [](xpr& x, auto fn) {
    auto& l = std::get<lst>(x);
    if (l.size() != 2) {throw std::runtime_error("unbalanced function arguments");}
    auto n1 = xpr_num(*l.begin());
    auto n2 = xpr_num(*std::next(l.begin(), 1));
    if (!n1 || !n2) {throw std::runtime_error("invalid type");}
    if (n1->index() == n2->index()) {
      if (holds(*n1, i64)) {return fn(*num_i64(*n1), *num_i64(*n2)) ? sym_xpr("#t") : sym_xpr("#f");}
      return fn(*num_f64(*n1), *num_f64(*n2)) ? sym_xpr("#t") : sym_xpr("#f");
    }
    if (holds(*n1, i64)) {return fn(static_cast<f64>(*num_i64(*n1)), *num_f64(*n2)) ? sym_xpr("#t") : sym_xpr("#f");}
    return fn(*num_f64(*n1), static_cast<f64>(*num_i64(*n2))) ? sym_xpr("#t") : sym_xpr("#f");
  };
  auto const fa_i64 = [](xpr& x, auto fn) {
    auto& l = std::get<lst>(x);
    if (l.size() != 2) {throw std::runtime_error("unbalanced function arguments");}
    auto n1 = xpr_i64(*l.begin());
    auto n2 = xpr_i64(*std::next(l.begin(), 1));
    if (!n1 || !n2) {throw std::runtime_error("invalid type");}
    return i64_xpr(fn(*n1, *n2));
  };
  auto const fa_if64 = [](xpr& x, auto fn) {
    auto& l = std::get<lst>(x);
    if (l.size() != 2) {throw std::runtime_error("unbalanced function arguments");}
    auto n1 = xpr_num(*l.begin());
    auto n2 = xpr_num(*std::next(l.begin(), 1));
    if (!n1 || !n2) {throw std::runtime_error("invalid type");}
    if (n1->index() == n2->index()) {
      if (holds(*n1, i64)) {return i64_xpr(fn(*num_i64(*n1), *num_i64(*n2)));}
      return f64_xpr(fn(*num_f64(*n1), *num_f64(*n2)));
    }
    if (holds(*n1, i64)) {return f64_xpr(fn(static_cast<f64>(*num_i64(*n1)), *num_f64(*n2)));}
    return f64_xpr(fn(*num_f64(*n1), static_cast<f64>(*num_i64(*n2))));
  };
#define op(X, Y) (X(x, [](auto const& n1, auto const& n2) {return n1 Y n2;}))
  ev["#t"] = sym_xpr("#t");
  ev["#f"] = sym_xpr("#f");
  ev["nil"] = sym_xpr("nil");
  ev["=="] = xpr{[&](xpr& x) -> xpr {return op(fc_if64, ==);}};
  ev["!="] = xpr{[&](xpr& x) -> xpr {return op(fc_if64, !=);}};
  ev["<"] = xpr{[&](xpr& x) -> xpr {return op(fc_if64, <);}};
  ev["<="] = xpr{[&](xpr& x) -> xpr {return op(fc_if64, <=);}};
  ev[">"] = xpr{[&](xpr& x) -> xpr {return op(fc_if64, >);}};
  ev[">="] = xpr{[&](xpr& x) -> xpr {return op(fc_if64, >=);}};
  ev["*"] = xpr{[&](xpr& x) -> xpr {return op(fa_if64, *);}};
  ev["/"] = xpr{[&](xpr& x) -> xpr {return op(fa_if64, /);}};
  ev["-"] = xpr{[&](xpr& x) -> xpr {return op(fa_if64, -);}};
  ev["+"] = xpr{[&](xpr& x) -> xpr {return op(fa_if64, +);}};
  ev["%"] = xpr{[&](xpr& x) -> xpr {return op(fa_i64, %);}};
  ev["&"] = xpr{[&](xpr& x) -> xpr {return op(fa_i64, &);}};
  ev["|"] = xpr{[&](xpr& x) -> xpr {return op(fa_i64, |);}};
  ev["^"] = xpr{[&](xpr& x) -> xpr {return op(fa_i64, ^);}};
  ev["<<"] = xpr{[&](xpr& x) -> xpr {return op(fa_i64, <<);}};
  ev[">>"] = xpr{[&](xpr& x) -> xpr {return op(fa_i64, >>);}};
#undef op
}

Tokens str_tokens(std::string const& str) {
  return split(replace(str, {{"(", " ( "}, {")", " ) "}}), " ");
}

atm token_atm(std::string const& token) {
  try {
    if (token.find(".") != std::string::npos) {
      return atm{num{static_cast<f64>(std::stod(token))}};
    }
    return atm{num{static_cast<i64>(std::stol(token))}};
  }
  catch (...) {return sym_atm(token);}
}

xpr tokens_xpr(Tokens& tokens) {
  if (tokens.empty()) {throw std::runtime_error("unexpected EOF");}
  auto token = tokens.front(); tokens.pop_front();
  if (token == "(") {
    xpr x;
    auto l {xpr_lst(x)};
    while (tokens.front() != ")") {l->emplace_back(tokens_xpr(tokens));}
    tokens.pop_front();
    return x;
  }
  else if (token == ")") {throw std::runtime_error("unexpected ')'");}
  else {return xpr{token_atm(token)};}
}

xpr read(std::string const& str) {
  auto tokens = str_tokens(str);
  return tokens_xpr(tokens);
}

xpr eval(xpr& xr, env& ev) {
  if (auto const a = std::get_if<atm>(&xr)) {
    if (auto const s = std::get_if<sym>(a)) {if (ev.find(*s) != ev.end()) {return ev[*s];} throw std::runtime_error("unbound symbol");}
    if (auto const n = std::get_if<num>(a)) {
      if (auto const i = std::get_if<i64>(n)) {return i64_xpr(*i);}
      if (auto const f = std::get_if<f64>(n)) {return f64_xpr(*f);}
    }
    throw std::runtime_error("unknown atom");
  }
  if (auto const l = std::get_if<lst>(&xr)) {
    if (l->empty()) {return sym_xpr("nil");}
    xpr& fn = l->front();
    if (auto const a = std::get_if<atm>(&fn)) {
      if (auto const s = std::get_if<sym>(a)) {
        if (*s == "if") {
          xpr& cond {*std::next(l->begin(), 1)};
          xpr res {eval(cond, ev)};
          if (auto const s = std::get_if<sym>(std::get_if<atm>(&res))) {
            if (*s == "#f") {return eval(*std::next(l->begin(), 3), ev);}
          }
          else if (auto const a = std::get_if<lst>(&res)) {
            if (l->empty()) {return eval(*std::next(l->begin(), 3), ev);}
          }
          return eval(*std::next(l->begin(), 2), ev);
        }
        if (*s == "define") {
          xpr& symbol {*std::next(l->begin(), 1)};
          if (auto const s = std::get_if<sym>(std::get_if<atm>(&symbol))) {
            ev[*s] = eval(*std::next(l->begin(), 2), ev);
            return symbol;
          }
          throw std::runtime_error("expected symbol");
        }
        if (*s == "quote") {
          return l->back();
        }
        if (*s == "set!") {
          xpr& symbol {*std::next(l->begin(), 1)};
          if (auto const s = std::get_if<sym>(std::get_if<atm>(&symbol))) {
            if (ev.find(*s) != ev.end()) {ev[*s] = eval(*std::next(l->begin(), 2), ev);}
            return symbol;
          }
          return {};
        }
        if (*s == "lambda") {
          return xpr{[args = *std::next(l->begin(), 1), body = *std::next(l->begin(), 2), ep = ev, &ev](xpr xr) mutable -> xpr {
            auto p = std::get<lst>(args).begin();
            if (std::get<lst>(args).size() != std::get<lst>(xr).size()) {throw std::runtime_error("unbalanced lambda arguments");}
            env en {ev};
            for (auto& [k, v] : ep) {en[k] = v;}
            for (auto& e : std::get<lst>(xr)) {
              en[std::get<sym>(std::get<atm>(*p))] = e;
              std::advance(p, 1);
            }
            return eval(body, en);
          }};
        }
        xpr v {sym_xpr(*s)};
        xpr func {eval(v, ev)};
        if (auto const f = std::get_if<fun>(&func)) {
          xpr args;
          auto& argp = std::get<lst>(args);
          for (auto i = std::next(l->begin(), 1); i != l->end(); std::advance(i, 1)) {
            argp.emplace_back(eval(*i, ev));
          }
          return (*f)(args);
        }
      }
    }
    throw std::runtime_error("unknown function");
  }
  throw std::runtime_error("invalid eval");
}

template<typename T> std::string print(T const& t) {
  std::ostringstream os;
  os << "[";
  for_each(t,
    [&](auto const& e) {os << "'" << e << "', ";},
    [&](auto const& e) {os << "'" << e << "'";});
  os << "]\n";
  return os.str();
}

std::string print(xpr const& x) {
  std::ostringstream os;
  std::function<void(xpr const&)> const to_string_impl = [&](xpr const& x) {
    if (auto const a = std::get_if<atm>(&x)) {
      if (auto const s = std::get_if<sym>(a)) {os << *s;}
      else if (auto const n = std::get_if<num>(a)) {
        if (auto const i = std::get_if<i64>(n)) {os << *i;}
        else if (auto const f = std::get_if<f64>(n)) {os << *f;}
      }
      return;
    }
    lst const& l = std::get<lst>(x);
    os << "(";
    for_each(l,
      [&](auto const& e) {to_string_impl(e); os << " ";},
      [&](auto const& e) {to_string_impl(e);});
    os << ")";
  };
  to_string_impl(x);
  return os.str();
}

void repl() {
  env ev;
  env_init(ev);
  std::string input;
  std::cout << "Welcome to lispp\n> ";
  while (std::getline(std::cin, input)) {
    if (input.size()) {
      try {
        xpr x = read(input);
        xpr v = eval(x, ev);
        std::cout << print(v) << "\n";
      }
      catch (std::exception const& e) {std::cout << "Error: " << e.what() << "\n";}
    }
    std::cout << "> ";
  }
  std::cout << "\nttfn\n";
}

int main(int argc, char** argv) {
  repl();
  return 0;
}
Back to Top