Skip to content

Commit

Permalink
Remove nullptr usage from parse processor
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Mar 9, 2024
1 parent 03a11c6 commit 04dfd13
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 67 deletions.
2 changes: 1 addition & 1 deletion include/cpp/jank/read/lex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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
11 changes: 9 additions & 2 deletions include/cpp/jank/read/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ namespace jank::read::parse
{
struct processor
{
/* TODO: none instead of nullptr. */
using object_result = result<runtime::object_ptr, error>;
struct object_source_info
{
native_bool operator==(object_source_info const &rhs) const;
native_bool operator!=(object_source_info const &rhs) const;

runtime::object_ptr ptr{};
};

using object_result = result<option<object_source_info>, error>;

struct iterator
{
Expand Down
9 changes: 6 additions & 3 deletions src/cpp/jank/analyze/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace jank::analyze
{
return err(parse_current->expect_err_move());
}
fn.push_back(parse_current->expect_ok());
fn.push_back(parse_current->expect_ok().unwrap().ptr);
}
auto fn_list(make_box<runtime::obj::persistent_list>(fn.rbegin(), fn.rend()));
return analyze(fn_list, expression_type::expression);
Expand Down Expand Up @@ -986,8 +986,11 @@ namespace jank::analyze
{
return parsed_it->expect_err_move();
}
auto result(
analyze(parsed_it->expect_ok(), current_frame, expression_type::expression, fn_ctx, true));
auto result(analyze(parsed_it->expect_ok().unwrap().ptr,
current_frame,
expression_type::expression,
fn_ctx,
true));
if(result.is_err())
{
return result.expect_err_move();
Expand Down
58 changes: 35 additions & 23 deletions src/cpp/jank/read/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ namespace jank::read::parse
}
}

native_bool
processor::object_source_info::operator==(processor::object_source_info const &rhs) const
{
return !(*this != rhs);
}

native_bool
processor::object_source_info::operator!=(processor::object_source_info const &rhs) const
{
return ptr != rhs.ptr;
}

processor::iterator::value_type processor::iterator::operator*() const
{
return latest.unwrap();
Expand Down Expand Up @@ -109,10 +121,9 @@ namespace jank::read::parse

processor::object_result processor::next()
{
/* TODO: Replace nullptr with none. */
if(token_current == token_end)
{
return ok(nullptr);
return ok(none);
}

while(true)
Expand Down Expand Up @@ -146,7 +157,7 @@ namespace jank::read::parse
}
++token_current;
expected_closer = none;
return ok(nullptr);
return ok(none);
case lex::token_kind::single_quote:
return parse_quote();
case lex::token_kind::nil:
Expand All @@ -166,7 +177,7 @@ namespace jank::read::parse
case lex::token_kind::escaped_string:
return parse_escaped_string();
case lex::token_kind::eof:
return ok(nullptr);
return ok(none);
default:
{
native_persistent_string msg{ fmt::format("unexpected token kind: {}",
Expand All @@ -191,15 +202,15 @@ namespace jank::read::parse
{
return err(it.latest.unwrap().expect_err());
}
ret.push_back(it.latest.unwrap().expect_ok());
ret.push_back(it.latest.unwrap().expect_ok().unwrap().ptr);
}
if(expected_closer.is_some())
{
return err(error{ start_token.pos, "Unterminated list" });
}

expected_closer = prev_expected_closer;
return make_box<runtime::obj::persistent_list>(ret.rbegin(), ret.rend());
return object_source_info{ make_box<runtime::obj::persistent_list>(ret.rbegin(), ret.rend()) };
}

processor::object_result processor::parse_vector()
Expand All @@ -216,15 +227,15 @@ namespace jank::read::parse
{
return err(it.latest.unwrap().expect_err());
}
ret.push_back(it.latest.unwrap().expect_ok());
ret.push_back(it.latest.unwrap().expect_ok().unwrap().ptr);
}
if(expected_closer.is_some())
{
return err(error{ start_token.pos, "Unterminated vector" });
}

expected_closer = prev_expected_closer;
return make_box<runtime::obj::persistent_vector>(ret.persistent());
return object_source_info{ make_box<runtime::obj::persistent_vector>(ret.persistent()) };
}

/* TODO: Uniqueness check. */
Expand Down Expand Up @@ -255,15 +266,15 @@ namespace jank::read::parse
}
auto const value(it.latest.unwrap().expect_ok());

ret.insert_or_assign(key, value);
ret.insert_or_assign(key.unwrap().ptr, value.unwrap().ptr);
}
if(expected_closer.is_some())
{
return err(error{ start_token.pos, "Unterminated map" });
}

expected_closer = prev_expected_closer;
return make_box<runtime::obj::persistent_array_map>(ret);
return object_source_info{ make_box<runtime::obj::persistent_array_map>(ret) };
}

processor::object_result processor::parse_quote()
Expand All @@ -278,28 +289,28 @@ namespace jank::read::parse
{
return val_result;
}
else if(val_result.expect_ok() == nullptr)
else if(val_result.expect_ok().is_none())
{
return err(error{ start_token.pos, native_persistent_string{ "invalid value after quote" } });
}

return runtime::erase(
return object_source_info{ runtime::erase(
make_box<runtime::obj::persistent_list>(make_box<runtime::obj::symbol>("quote"),
val_result.expect_ok_move()));
val_result.expect_ok().unwrap().ptr)) };
}

processor::object_result processor::parse_nil()
{
++token_current;
return ok(runtime::obj::nil::nil_const());
return object_source_info{ runtime::obj::nil::nil_const() };
}

processor::object_result processor::parse_boolean()
{
auto const token((*token_current).expect_ok());
++token_current;
auto const b(boost::get<native_bool>(token.data));
return ok(make_box<runtime::obj::boolean>(b));
return object_source_info{ make_box<runtime::obj::boolean>(b) };
}

processor::object_result processor::parse_symbol()
Expand Down Expand Up @@ -341,7 +352,7 @@ namespace jank::read::parse
{
name = sv;
}
return ok(make_box<runtime::obj::symbol>(ns, name));
return object_source_info{ make_box<runtime::obj::symbol>(ns, name) };
}

processor::object_result processor::parse_keyword()
Expand Down Expand Up @@ -377,30 +388,31 @@ namespace jank::read::parse
{
return err(intern_res.expect_err());
}
return ok(intern_res.expect_ok());
return object_source_info{ intern_res.expect_ok() };
}

processor::object_result processor::parse_integer()
{
auto const token(token_current->expect_ok());
++token_current;
return ok(make_box<runtime::obj::integer>(boost::get<native_integer>(token.data)));
return object_source_info{ make_box<runtime::obj::integer>(
boost::get<native_integer>(token.data)) };
}

processor::object_result processor::parse_real()
{
auto const token(token_current->expect_ok());
++token_current;
return ok(make_box<runtime::obj::real>(boost::get<native_real>(token.data)));
return object_source_info{ make_box<runtime::obj::real>(boost::get<native_real>(token.data)) };
}

processor::object_result processor::parse_string()
{
auto const token(token_current->expect_ok());
++token_current;
auto const sv(boost::get<native_persistent_string_view>(token.data));
return ok(
make_box<runtime::obj::persistent_string>(native_persistent_string{ sv.data(), sv.size() }));
return object_source_info{ make_box<runtime::obj::persistent_string>(
native_persistent_string{ sv.data(), sv.size() }) };
}

processor::object_result processor::parse_escaped_string()
Expand All @@ -413,7 +425,7 @@ namespace jank::read::parse
{
return err(error{ token.pos, res.expect_err_move() });
}
return ok(make_box<runtime::obj::persistent_string>(res.expect_ok_move()));
return object_source_info{ make_box<runtime::obj::persistent_string>(res.expect_ok_move()) };
}

processor::iterator processor::begin()
Expand All @@ -423,6 +435,6 @@ namespace jank::read::parse

processor::iterator processor::end()
{
return { some(ok(nullptr)), *this };
return { some(ok(none)), *this };
}
}
6 changes: 4 additions & 2 deletions src/cpp/jank/runtime/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ namespace jank::runtime
native_vector<analyze::expression_ptr> exprs{};
for(auto const &form : p_prc)
{
auto const expr(an_prc.analyze(form.expect_ok(), analyze::expression_type::statement));
auto const expr(
an_prc.analyze(form.expect_ok().unwrap().ptr, analyze::expression_type::statement));
ret = evaluate::eval(*this, jit_prc, expr.expect_ok());
exprs.emplace_back(expr.expect_ok());
}
Expand Down Expand Up @@ -230,7 +231,8 @@ namespace jank::runtime
native_vector<analyze::expression_ptr> ret{};
for(auto const &form : p_prc)
{
auto const expr(an_prc.analyze(form.expect_ok(), analyze::expression_type::statement));
auto const expr(
an_prc.analyze(form.expect_ok().unwrap().ptr, analyze::expression_type::statement));
if(eval)
{
evaluate::eval(*this, jit_prc, expr.expect_ok());
Expand Down
Loading

0 comments on commit 04dfd13

Please sign in to comment.