From 04dfd13f37139e592f32c88c06d85548a5239c72 Mon Sep 17 00:00:00 2001 From: jeaye Date: Sat, 9 Mar 2024 06:53:08 +0000 Subject: [PATCH] Remove nullptr usage from parse processor --- include/cpp/jank/read/lex.hpp | 2 +- include/cpp/jank/read/parse.hpp | 11 ++++- src/cpp/jank/analyze/processor.cpp | 9 ++-- src/cpp/jank/read/parse.cpp | 58 ++++++++++++++---------- src/cpp/jank/runtime/context.cpp | 6 ++- test/cpp/jank/read/parse.cpp | 73 +++++++++++++++--------------- 6 files changed, 92 insertions(+), 67 deletions(-) diff --git a/include/cpp/jank/read/lex.hpp b/include/cpp/jank/read/lex.hpp index 9a4b5fa06..dd3294ee6 100644 --- a/include/cpp/jank/read/lex.hpp +++ b/include/cpp/jank/read/lex.hpp @@ -67,7 +67,7 @@ namespace jank::read::lex static constexpr size_t ignore_pos{ std::numeric_limits::max() }; size_t pos{ ignore_pos }; size_t size{ 1 }; - token_kind kind; + token_kind kind{ token_kind::eof }; boost::variant data; }; diff --git a/include/cpp/jank/read/parse.hpp b/include/cpp/jank/read/parse.hpp index 07b12dd46..eb724ea19 100644 --- a/include/cpp/jank/read/parse.hpp +++ b/include/cpp/jank/read/parse.hpp @@ -14,8 +14,15 @@ namespace jank::read::parse { struct processor { - /* TODO: none instead of nullptr. */ - using object_result = result; + 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, error>; struct iterator { diff --git a/src/cpp/jank/analyze/processor.cpp b/src/cpp/jank/analyze/processor.cpp index b919ab2bd..bc8d0d554 100644 --- a/src/cpp/jank/analyze/processor.cpp +++ b/src/cpp/jank/analyze/processor.cpp @@ -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(fn.rbegin(), fn.rend())); return analyze(fn_list, expression_type::expression); @@ -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(); diff --git a/src/cpp/jank/read/parse.cpp b/src/cpp/jank/read/parse.cpp index c25fc76c4..9f176a6fb 100644 --- a/src/cpp/jank/read/parse.cpp +++ b/src/cpp/jank/read/parse.cpp @@ -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(); @@ -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) @@ -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: @@ -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: {}", @@ -191,7 +202,7 @@ 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()) { @@ -199,7 +210,7 @@ namespace jank::read::parse } expected_closer = prev_expected_closer; - return make_box(ret.rbegin(), ret.rend()); + return object_source_info{ make_box(ret.rbegin(), ret.rend()) }; } processor::object_result processor::parse_vector() @@ -216,7 +227,7 @@ 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()) { @@ -224,7 +235,7 @@ namespace jank::read::parse } expected_closer = prev_expected_closer; - return make_box(ret.persistent()); + return object_source_info{ make_box(ret.persistent()) }; } /* TODO: Uniqueness check. */ @@ -255,7 +266,7 @@ 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()) { @@ -263,7 +274,7 @@ namespace jank::read::parse } expected_closer = prev_expected_closer; - return make_box(ret); + return object_source_info{ make_box(ret) }; } processor::object_result processor::parse_quote() @@ -278,20 +289,20 @@ 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(make_box("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() @@ -299,7 +310,7 @@ namespace jank::read::parse auto const token((*token_current).expect_ok()); ++token_current; auto const b(boost::get(token.data)); - return ok(make_box(b)); + return object_source_info{ make_box(b) }; } processor::object_result processor::parse_symbol() @@ -341,7 +352,7 @@ namespace jank::read::parse { name = sv; } - return ok(make_box(ns, name)); + return object_source_info{ make_box(ns, name) }; } processor::object_result processor::parse_keyword() @@ -377,21 +388,22 @@ 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(boost::get(token.data))); + return object_source_info{ make_box( + boost::get(token.data)) }; } processor::object_result processor::parse_real() { auto const token(token_current->expect_ok()); ++token_current; - return ok(make_box(boost::get(token.data))); + return object_source_info{ make_box(boost::get(token.data)) }; } processor::object_result processor::parse_string() @@ -399,8 +411,8 @@ namespace jank::read::parse auto const token(token_current->expect_ok()); ++token_current; auto const sv(boost::get(token.data)); - return ok( - make_box(native_persistent_string{ sv.data(), sv.size() })); + return object_source_info{ make_box( + native_persistent_string{ sv.data(), sv.size() }) }; } processor::object_result processor::parse_escaped_string() @@ -413,7 +425,7 @@ namespace jank::read::parse { return err(error{ token.pos, res.expect_err_move() }); } - return ok(make_box(res.expect_ok_move())); + return object_source_info{ make_box(res.expect_ok_move()) }; } processor::iterator processor::begin() @@ -423,6 +435,6 @@ namespace jank::read::parse processor::iterator processor::end() { - return { some(ok(nullptr)), *this }; + return { some(ok(none)), *this }; } } diff --git a/src/cpp/jank/runtime/context.cpp b/src/cpp/jank/runtime/context.cpp index 98332af13..e4de502fc 100644 --- a/src/cpp/jank/runtime/context.cpp +++ b/src/cpp/jank/runtime/context.cpp @@ -197,7 +197,8 @@ namespace jank::runtime native_vector 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()); } @@ -230,7 +231,8 @@ namespace jank::runtime native_vector 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()); diff --git a/test/cpp/jank/read/parse.cpp b/test/cpp/jank/read/parse.cpp index 7e8a0128d..136aad1a8 100644 --- a/test/cpp/jank/read/parse.cpp +++ b/test/cpp/jank/read/parse.cpp @@ -29,7 +29,7 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r(p.next()); CHECK(r.is_ok()); - CHECK(r.expect_ok() == nullptr); + CHECK(r.expect_ok().is_none()); } TEST_CASE("Nil") @@ -39,7 +39,7 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box(nullptr))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, runtime::obj::nil::nil_const())); } TEST_CASE("Boolean") @@ -49,9 +49,9 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const t(p.next()); CHECK(t.is_ok()); - CHECK(runtime::detail::equal(t.expect_ok(), make_box(true))); + CHECK(runtime::detail::equal(t.expect_ok().unwrap().ptr, make_box(true))); auto const f(p.next()); - CHECK(runtime::detail::equal(f.expect_ok(), make_box(false))); + CHECK(runtime::detail::equal(f.expect_ok().unwrap().ptr, make_box(false))); } TEST_CASE("Integer") @@ -61,7 +61,7 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box(1234))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box(1234))); } TEST_CASE("Comments") @@ -71,11 +71,11 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const i(p.next()); CHECK(i.is_ok()); - CHECK(runtime::detail::equal(i.expect_ok(), make_box(1234))); + CHECK(runtime::detail::equal(i.expect_ok().unwrap().ptr, make_box(1234))); auto const eof(p.next()); CHECK(eof.is_ok()); - CHECK(eof.expect_ok() == nullptr); + CHECK(eof.expect_ok().is_none()); } TEST_CASE("Real") @@ -85,7 +85,7 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box(12.34l))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box(12.34l))); } TEST_CASE("String") @@ -99,7 +99,7 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box(s))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box(s))); } } @@ -112,7 +112,7 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box(s))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box(s))); } } } @@ -128,7 +128,8 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box("", s))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, + make_box("", s))); } } @@ -139,7 +140,8 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), make_box("", "/"))); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, + make_box("", "/"))); } SUBCASE("Qualified") @@ -156,7 +158,7 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box(s.first, s.second))); } } @@ -172,7 +174,7 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box(s.first, s.second))); } } @@ -197,7 +199,7 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(runtime::detail::equal(r.expect_ok(), + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, make_box( make_box("quote"), make_box(s.first, s.second)))); @@ -217,7 +219,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, rt_ctx.intern_keyword(runtime::obj::symbol{ "", s }, true).expect_ok())); } } @@ -234,7 +236,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, rt_ctx.intern_keyword(runtime::obj::symbol{ s.first, s.second }, true).expect_ok())); } } @@ -249,7 +251,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, rt_ctx.intern_keyword(runtime::obj::symbol{ "", native_persistent_string{ s } }, false) .expect_ok())); } @@ -275,7 +277,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, rt_ctx.intern_keyword(runtime::obj::symbol{ "foo.bar.spam", "foo" }, true).expect_ok())); } } @@ -291,8 +293,8 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(r.expect_ok() != nullptr); - CHECK(runtime::detail::equal(r.expect_ok(), make_box())); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, + make_box())); } } @@ -306,7 +308,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, make_box(make_box(1 * i), make_box(2 * i), make_box(3 * i), @@ -322,13 +324,14 @@ namespace jank::read::parse auto const r1(p.next()); CHECK(r1.is_ok()); CHECK(runtime::detail::equal( - r1.expect_ok(), + r1.expect_ok().unwrap().ptr, make_box(make_box("def"), make_box("foo-bar"), make_box(1)))); auto const r2(p.next()); CHECK(r2.is_ok()); - CHECK(runtime::detail::equal(r2.expect_ok(), make_box("foo-bar"))); + CHECK(runtime::detail::equal(r2.expect_ok().unwrap().ptr, + make_box("foo-bar"))); } SUBCASE("Extra close") @@ -338,7 +341,7 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r1(p.next()); CHECK(r1.is_ok()); - CHECK(runtime::detail::equal(r1.expect_ok(), make_box(1))); + CHECK(runtime::detail::equal(r1.expect_ok().unwrap().ptr, make_box(1))); auto const r2(p.next()); CHECK(r2.is_err()); } @@ -364,8 +367,8 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(r.expect_ok() != nullptr); - CHECK(runtime::detail::equal(r.expect_ok(), make_box())); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, + make_box())); } } @@ -379,7 +382,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, make_box(runtime::detail::native_persistent_vector{ make_box(1 * i), make_box(2 * i), @@ -396,7 +399,7 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r1(p.next()); CHECK(r1.is_ok()); - CHECK(runtime::detail::equal(r1.expect_ok(), make_box(1))); + CHECK(runtime::detail::equal(r1.expect_ok().unwrap().ptr, make_box(1))); auto const r2(p.next()); CHECK(r2.is_err()); } @@ -422,9 +425,8 @@ namespace jank::read::parse { auto const r(p.next()); CHECK(r.is_ok()); - CHECK(r.expect_ok() != nullptr); - CHECK( - runtime::detail::equal(r.expect_ok(), make_box())); + CHECK(runtime::detail::equal(r.expect_ok().unwrap().ptr, + make_box())); } } @@ -438,7 +440,7 @@ namespace jank::read::parse auto const r(p.next()); CHECK(r.is_ok()); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, make_box( runtime::detail::in_place_unique{}, make_array_box(make_box(1 * i), @@ -456,9 +458,8 @@ namespace jank::read::parse processor p{ rt_ctx, lp.begin(), lp.end() }; auto const r(p.next()); CHECK(r.is_ok()); - CHECK(r.expect_ok() != nullptr); CHECK(runtime::detail::equal( - r.expect_ok(), + r.expect_ok().unwrap().ptr, make_box( runtime::detail::in_place_unique{}, make_array_box( @@ -488,7 +489,7 @@ namespace jank::read::parse auto const r1(p.next()); CHECK(r1.is_ok()); CHECK(runtime::detail::equal( - r1.expect_ok(), + r1.expect_ok().unwrap().ptr, rt_ctx.intern_keyword(runtime::obj::symbol{ "foo" }, true).expect_ok())); auto const r2(p.next()); CHECK(r2.is_err());