Browse Source

fmt code

Signed-off-by: Rain Mark <rain.by.zhou@gmail.com>
main
Rain Mark 4 years ago
parent
commit
964f1a1741
  1. 2
      build.sh
  2. 39
      src/core.h
  3. 5
      src/lispoo.cpp
  4. 39
      src/lispoo.h

2
build.sh

@ -1,3 +1,5 @@
#!/bin/bash
# git-clang-format --style=Google HEAD^
g++ -Werror -std=c++20 -Isrc src/lispoo.cpp -o lispoo

39
src/core.h

@ -6,13 +6,12 @@ namespace lispoo {
inline std::shared_ptr<Expr> sum(const std::shared_ptr<Expr>& a,
const std::shared_ptr<Expr>& b) {
if (a->type() == Type::Float
&& b->type() == Type::Float) {
if (a->type() == Type::Float && b->type() == Type::Float) {
return std::make_shared<Float>(get_value<Float>(a) + get_value<Float>(b));
}
if (a->type() == Type::Integer
&& b->type() == Type::Integer) {
return std::make_shared<Integer>(get_value<Integer>(a) + get_value<Integer>(b));
if (a->type() == Type::Integer && b->type() == Type::Integer) {
return std::make_shared<Integer>(get_value<Integer>(a) +
get_value<Integer>(b));
}
if (a->type() == Type::Float) {
return std::make_shared<Float>(get_value<Float>(a) + get_value<Integer>(b));
@ -65,14 +64,12 @@ inline std::shared_ptr<Expr> message(const std::shared_ptr<Expr>& expr) {
inline void init() {
// syntax
putenv("quote",
[](auto expr, auto env) {
putenv("quote", [](auto expr, auto env) {
assert_len(expr, 2);
auto value = get_value<List>(expr);
return value[1];
});
putenv("define",
[](auto expr, auto env) {
putenv("define", [](auto expr, auto env) {
assert_len(expr, 3);
auto value = get_value<List>(expr);
assert_type<Type::Symbol>(value[1]);
@ -83,8 +80,7 @@ inline void init() {
env->put(symbol, eval(value[2], env));
return nil;
});
putenv("set!",
[](auto expr, auto env) {
putenv("set!", [](auto expr, auto env) {
assert_len(expr, 3);
auto value = get_value<List>(expr);
assert_type<Type::Symbol>(value[1]);
@ -92,8 +88,7 @@ inline void init() {
env->put(symbol, eval(value[2], env));
return nil;
});
putenv("progn",
[](auto expr, auto env) {
putenv("progn", [](auto expr, auto env) {
auto val = nil;
auto value = get_value<List>(expr);
for (auto i = 1; i < value.size(); ++i) {
@ -101,8 +96,7 @@ inline void init() {
}
return val;
});
putenv("if",
[](auto expr, auto env) {
putenv("if", [](auto expr, auto env) {
// (if (cond) (then body) (else body))
assert_len(expr, 4);
auto value = get_value<List>(expr);
@ -112,8 +106,7 @@ inline void init() {
return eval(value[3], env);
}
});
putenv("while",
[](auto expr, auto env) {
putenv("while", [](auto expr, auto env) {
// (while (cond) (loop body))
assert_len(expr, 3);
auto value = get_value<List>(expr);
@ -122,12 +115,10 @@ inline void init() {
}
return nil;
});
putenv("lambda",
[](auto expr, auto env) {
putenv("lambda", [](auto expr, auto env) {
// (lambda (args) (body))
assert_len(expr, 3);
auto lambda =
[expr, parent = env](auto args, auto) {
auto lambda = [expr, parent = env](auto args, auto) {
auto value = get_value<List>(expr);
auto symbols = get_value<List>(value[1]);
// arguments bind
@ -147,14 +138,12 @@ inline void init() {
});
// normal builtin function
putenv("+",
[](auto expr, auto env) {
putenv("+", [](auto expr, auto env) {
assert_len(expr, 3);
auto value = get_value<List>(expr);
return sum(eval(value[1], env), eval(value[2], env));
});
putenv("message",
[](auto expr, auto env) {
putenv("message", [](auto expr, auto env) {
auto value = get_value<List>(expr);
for (auto i = 1; i < value.size(); ++i) {
message(eval(value[i], env));

5
src/lispoo.cpp

@ -1,7 +1,8 @@
#include <lispoo.h>
#include <core.h>
#include <sstream>
#include <lispoo.h>
#include <fstream>
#include <sstream>
int main(int argc, char* argv[]) {
if (argc != 2) {

39
src/lispoo.h

@ -1,13 +1,12 @@
#pragma once
#include <iostream>
#include <exception>
#include <string>
#include <vector>
#include <memory>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace lispoo {
@ -80,14 +79,12 @@ private:
class Env;
class Callable : public Expr {
public:
using Fn = std::function<std::shared_ptr<Expr>(const std::shared_ptr<Expr>&,
const std::shared_ptr<Env>& env)>;
using Fn = std::function<std::shared_ptr<Expr>(
const std::shared_ptr<Expr>&, const std::shared_ptr<Env>& env)>;
explicit Callable(Fn&& lambda) : lambda_(lambda) {}
Type type() override { return Type::Callable; }
const Fn& value() const {
return lambda_;
}
const Fn& value() const { return lambda_; }
private:
Fn lambda_;
@ -117,16 +114,16 @@ private:
std::unordered_map<std::string, std::shared_ptr<Expr>> expr_map_;
std::shared_ptr<Env> env_;
};
static std::shared_ptr<Env> global = std::make_shared<Env>(std::shared_ptr<Env>());
static std::shared_ptr<Env> global =
std::make_shared<Env>(std::shared_ptr<Env>());
inline void putenv(const std::string& symbol, Callable::Fn&& lambda) {
global->put(symbol, std::make_shared<Callable>(std::forward<Callable::Fn>(lambda)));
global->put(symbol,
std::make_shared<Callable>(std::forward<Callable::Fn>(lambda)));
}
// type utils
inline bool is_par(char ch) {
return ch == '(' || ch == ')';
}
inline bool is_par(char ch) { return ch == '(' || ch == ')'; }
template <typename T>
inline decltype(auto) get_value(const std::shared_ptr<Expr>& expr) {
return std::static_pointer_cast<T>(expr)->value();
@ -184,13 +181,15 @@ inline void tokenize(const std::string& str, std::vector<std::string>& tokens) {
continue;
}
auto s = i;
for (; !(std::isspace(str[i]) || is_par(str[i])); ++i);
for (; !(std::isspace(str[i]) || is_par(str[i])); ++i)
;
tokens.emplace_back(str.c_str() + s, i - s);
i--;
}
}
inline std::shared_ptr<Expr> parse_atom(const std::vector<std::string>& tokens, long& cursor) {
inline std::shared_ptr<Expr> parse_atom(const std::vector<std::string>& tokens,
long& cursor) {
auto token = tokens[cursor];
auto type = Type::Symbol;
if (std::isdigit(token[0]) || token[0] == '-') {
@ -221,7 +220,8 @@ inline std::shared_ptr<Expr> parse_atom(const std::vector<std::string>& tokens,
return nil;
}
inline std::shared_ptr<Expr> parse(const std::vector<std::string>& tokens, long& cursor) {
inline std::shared_ptr<Expr> parse(const std::vector<std::string>& tokens,
long& cursor) {
if (cursor >= tokens.size()) {
oops("parse error");
}
@ -235,7 +235,8 @@ inline std::shared_ptr<Expr> parse(const std::vector<std::string>& tokens, long&
return parse_atom(tokens, cursor);
}
inline std::shared_ptr<Expr> eval(const std::shared_ptr<Expr>& expr, const std::shared_ptr<Env>& env) {
inline std::shared_ptr<Expr> eval(const std::shared_ptr<Expr>& expr,
const std::shared_ptr<Env>& env) {
if (!expr) {
oops("syntax error");
return nil;

Loading…
Cancel
Save