Skip to content

Commit

Permalink
Merge branch 'reader' into transient
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Mar 22, 2024
2 parents 79e9275 + 06dc166 commit d51223d
Show file tree
Hide file tree
Showing 96 changed files with 2,877 additions and 583 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ add_library(
src/cpp/jank/util/cli.cpp
src/cpp/jank/util/mapped_file.cpp
src/cpp/jank/util/scope_exit.cpp
src/cpp/jank/util/escape.cpp
src/cpp/jank/profile/time.cpp
src/cpp/jank/read/lex.cpp
src/cpp/jank/read/parse.cpp
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
* similar to http://eta-lang.org/
* also emacs site
* rizin https://rizin.re/
* nice docs: https://github.com/aidenybai/million

* compile-time type for string literals
* "foo" => static-string : (3)
Expand Down
10 changes: 5 additions & 5 deletions include/cpp/jank/analyze/expr/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace jank::analyze::expr
{
struct function_context : gc
{
static constexpr bool pointer_free{ true };
static constexpr native_bool pointer_free{ true };

size_t param_count{};
bool is_variadic{};
bool is_tail_recursive{};
native_bool is_variadic{};
native_bool is_tail_recursive{};
/* TODO: is_pure */
};

Expand Down Expand Up @@ -54,13 +54,13 @@ namespace jank::analyze::expr

struct arity_key
{
bool operator==(arity_key const &rhs) const
native_bool operator==(arity_key const &rhs) const
{
return param_count == rhs.param_count && is_variadic == rhs.is_variadic;
}

size_t param_count{};
bool is_variadic{};
native_bool is_variadic{};
};

template <typename E>
Expand Down
3 changes: 2 additions & 1 deletion include/cpp/jank/analyze/expr/let.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace jank::analyze::expr
{
using pair_type = std::pair<runtime::obj::symbol_ptr, native_box<E>>;

let(expression_type const type, bool const needs_box, local_frame_ptr const f)
let(expression_type const type, native_bool const needs_box, local_frame_ptr const f)
: expression_base{ gc{}, type, f, needs_box }
{
}
Expand All @@ -30,6 +30,7 @@ namespace jank::analyze::expr
pair_maps = runtime::conj(
pair_maps,
make_box<runtime::obj::persistent_vector>(
std::in_place,
detail::to_runtime_data(frame->find_local_or_capture(e.first).unwrap().binding),
e.second->to_runtime_data()));
}
Expand Down
8 changes: 6 additions & 2 deletions include/cpp/jank/analyze/expr/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace jank::analyze::expr
struct map : expression_base
{
native_vector<std::pair<native_box<E>, native_box<E>>> data_exprs;
option<runtime::object_ptr> meta;

runtime::object_ptr to_runtime_data() const
{
Expand All @@ -17,7 +18,8 @@ namespace jank::analyze::expr
{
pair_maps
= runtime::conj(pair_maps,
make_box<runtime::obj::persistent_vector>(e.first->to_runtime_data(),
make_box<runtime::obj::persistent_vector>(std::in_place,
e.first->to_runtime_data(),
e.second->to_runtime_data()));
}

Expand All @@ -26,7 +28,9 @@ namespace jank::analyze::expr
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::map"),
make_box("data_exprs"),
pair_maps));
pair_maps,
make_box("meta"),
detail::to_runtime_data(meta)));
}
};
}
32 changes: 32 additions & 0 deletions include/cpp/jank/analyze/expr/set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <jank/analyze/expression_base.hpp>
#include <jank/runtime/seq.hpp>

namespace jank::analyze::expr
{
template <typename E>
struct set : expression_base
{
native_vector<native_box<E>> data_exprs;
option<runtime::object_ptr> meta;

runtime::object_ptr to_runtime_data() const
{
runtime::object_ptr pair_maps(make_box<runtime::obj::persistent_vector>());
for(auto const &e : data_exprs)
{
pair_maps = runtime::conj(pair_maps, e->to_runtime_data());
}

return runtime::merge(
static_cast<expression_base const *>(this)->to_runtime_data(),
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::set"),
make_box("data_exprs"),
pair_maps,
make_box("meta"),
detail::to_runtime_data(meta)));
}
};
}
5 changes: 4 additions & 1 deletion include/cpp/jank/analyze/expr/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace jank::analyze::expr
struct vector : expression_base
{
native_vector<native_box<E>> data_exprs;
option<runtime::object_ptr> meta;

runtime::object_ptr to_runtime_data() const
{
Expand All @@ -23,7 +24,9 @@ namespace jank::analyze::expr
runtime::obj::persistent_array_map::create_unique(make_box("__type"),
make_box("expr::vector"),
make_box("data_exprs"),
pair_maps));
pair_maps,
make_box("meta"),
detail::to_runtime_data(meta)));
}
};
}
4 changes: 3 additions & 1 deletion include/cpp/jank/analyze/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <jank/analyze/expr/primitive_literal.hpp>
#include <jank/analyze/expr/vector.hpp>
#include <jank/analyze/expr/map.hpp>
#include <jank/analyze/expr/set.hpp>
#include <jank/analyze/expr/function.hpp>
#include <jank/analyze/expr/recur.hpp>
#include <jank/analyze/expr/local_reference.hpp>
Expand All @@ -33,6 +34,7 @@ namespace jank::analyze
expr::primitive_literal<E>,
expr::vector<E>,
expr::map<E>,
expr::set<E>,
expr::function<E>,
expr::recur<E>,
expr::local_reference,
Expand All @@ -43,7 +45,7 @@ namespace jank::analyze
expr::try_<E>,
expr::native_raw<E>>;

static constexpr bool pointer_free{ false };
static constexpr native_bool pointer_free{ false };

expression() = default;
expression(expression const &) = default;
Expand Down
2 changes: 1 addition & 1 deletion include/cpp/jank/analyze/expression_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace jank::analyze
return_statement
};

inline bool is_statement(expression_type const expr_type)
inline native_bool is_statement(expression_type const expr_type)
{
return expr_type != expression_type::expression;
}
Expand Down
2 changes: 1 addition & 1 deletion include/cpp/jank/analyze/local_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace jank::analyze
catch_
};

static constexpr bool pointer_free{ false };
static constexpr native_bool pointer_free{ false };

local_frame() = delete;
local_frame(local_frame const &) = default;
Expand Down
8 changes: 8 additions & 0 deletions include/cpp/jank/analyze/processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ namespace jank::analyze
expression_type,
option<expr::function_context_ptr> const &,
native_bool needs_box);
expression_result analyze_set(runtime::obj::persistent_set_ptr const &,
local_frame_ptr &,
expression_type,
option<expr::function_context_ptr> const &,
native_bool needs_box);

/* Returns whether or not the form is a special symbol. */
native_bool is_special(runtime::object_ptr form);

using special_function_type
= std::function<expression_result(runtime::obj::persistent_list_ptr const &,
Expand Down
3 changes: 3 additions & 0 deletions include/cpp/jank/codegen/processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ namespace jank::codegen
option<handle> gen(analyze::expr::map<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &,
native_bool box_needed);
option<handle> gen(analyze::expr::set<analyze::expression> const &,
analyze::expr::function_arity<analyze::expression> const &,
native_bool box_needed);
option<handle> gen(analyze::expr::local_reference const &,
analyze::expr::function_arity<analyze::expression> const &,
native_bool box_needed);
Expand Down
2 changes: 2 additions & 0 deletions include/cpp/jank/evaluate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace jank::evaluate
runtime::object_ptr
eval(runtime::context &, jit::processor const &, analyze::expr::map<analyze::expression> const &);
runtime::object_ptr
eval(runtime::context &, jit::processor const &, analyze::expr::set<analyze::expression> const &);
runtime::object_ptr
eval(runtime::context &, jit::processor const &, analyze::expr::local_reference const &);
runtime::object_ptr eval(runtime::context &,
jit::processor const &,
Expand Down
22 changes: 22 additions & 0 deletions include/cpp/jank/native_box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ namespace jank
return nullptr;
}

template <typename T, size_t N>
constexpr native_box<T> make_array_box()
{
auto const ret(new(GC) T[N]{});
if(!ret)
{
throw std::runtime_error{ "unable to allocate array box" };
}
return ret;
}

template <typename T>
constexpr native_box<T> make_array_box(size_t const length)
{
auto const ret(new(GC) T[length]{});
if(!ret)
{
throw std::runtime_error{ "unable to allocate array box" };
}
return ret;
}

template <typename T, typename... Args>
native_box<T> make_array_box(Args &&...args)
{
Expand Down
37 changes: 37 additions & 0 deletions include/cpp/jank/native_persistent_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,43 @@ namespace jank
return npos;
}

[[gnu::const]]
constexpr native_bool starts_with(value_type const c) const noexcept
{
return size() > 0 && data()[0] == c;
}

[[gnu::const]]
constexpr native_bool starts_with(const_pointer_type const s) const noexcept
{
auto const this_sz(size());
auto const s_sz(traits_type::length(s));
if(this_sz < s_sz)
{
return false;
}
return traits_type::compare(data(), s, s_sz) == 0;
}

[[gnu::const]]
constexpr native_bool ends_with(value_type const c) const noexcept
{
auto const s(size());
return s > 0 && data()[s - 1] == c;
}

[[gnu::const]]
constexpr native_bool ends_with(const_pointer_type const s) const noexcept
{
auto const this_sz(size());
auto const s_sz(traits_type::length(s));
if(this_sz < s_sz)
{
return false;
}
return traits_type::compare(data() + this_sz - s_sz, s, s_sz) == 0;
}

/*** Immutable modifications. ***/
constexpr native_persistent_string
substr(size_type const pos = 0, size_type const count = npos) const
Expand Down
2 changes: 1 addition & 1 deletion include/cpp/jank/option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ namespace jank
return *reinterpret_cast<T const *>(data) != *reinterpret_cast<T const *>(rhs.data);
}

return true;
return false;
}

constexpr native_bool operator==(option<T> const &rhs) const
Expand Down
13 changes: 12 additions & 1 deletion include/cpp/jank/read/lex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ namespace jank::read::lex
open_curly_bracket,
close_curly_bracket,
single_quote,
meta_hint,
reader_macro,
reader_macro_comment,
reader_macro_conditional,
reader_macro_conditional_splice,
syntax_quote,
unquote,
unquote_splice,
deref,
/* Has string data. */
comment,
nil,
Expand All @@ -40,6 +49,7 @@ namespace jank::read::lex

struct token
{
token() = default;
token(token_kind const k);
token(size_t const p, token_kind const k);
token(size_t const p, token_kind const k, native_integer const);
Expand All @@ -51,6 +61,7 @@ namespace jank::read::lex
token(size_t const p, size_t const s, token_kind const k, native_integer const);
token(size_t const p, size_t const s, token_kind const k, native_real const);
token(size_t const p, size_t const s, token_kind const k, native_persistent_string_view const);
token(size_t const p, size_t const s, token_kind const k, char const * const);
token(size_t const p, size_t const s, token_kind const k, native_bool const);

native_bool operator==(token const &rhs) const;
Expand All @@ -67,7 +78,7 @@ namespace jank::read::lex
static constexpr size_t ignore_pos{ std::numeric_limits<size_t>::max() };
size_t pos{ ignore_pos };
size_t size{ 1 };
token_kind kind;
token_kind kind{ token_kind::eof };
boost::variant<no_data, native_integer, native_real, native_persistent_string_view, native_bool>
data;
};
Expand Down
Loading

0 comments on commit d51223d

Please sign in to comment.