Skip to content

Commit

Permalink
Update dynamic_environment::run to throw only error type object
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Jul 25, 2024
1 parent 00714e2 commit df05da2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 67 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.209_amd64.deb
sudo apt install build/meevax_0.5.210_amd64.deb
```

or
Expand Down Expand Up @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.209.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.210.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.209_amd64.deb`
| `package` | Generate debian package `meevax_0.5.210_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.209
0.5.210
2 changes: 2 additions & 0 deletions configure/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ HTML_DYNAMIC_SECTIONS = YES
HTML_INDEX_NUM_ENTRIES = 1000
SEARCHENGINE = NO

GENERATE_LATEX = NO

MACRO_EXPANSION = YES
PREDEFINED = NDEBUG

Expand Down
56 changes: 43 additions & 13 deletions include/meevax/kernel/dynamic_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ inline namespace kernel
return run();
}

auto raise(object const& x) -> decltype(auto)
{
return exception_handler.is<null>() ? throw x : apply(exception_handler, x);
}

auto run() -> object
{
assert(last(c).template is<instruction>());
Expand All @@ -119,9 +114,17 @@ inline namespace kernel
*
* ----------------------------------------------------------------- */
assert(cadr(c).template is<absolute>());
s = cons(cdadr(c), s);
c = cddr(c);
goto fetch;

if (let const& x = cdadr(c); x == undefined)
{
throw error(make<string>("undefined variable"), caadr(c));
}
else
{
s = cons(cdadr(c), s);
c = cddr(c);
goto fetch;
}

case instruction::load_relative: /* ------------------------------------
*
Expand Down Expand Up @@ -399,7 +402,6 @@ inline namespace kernel
* ----------------------------------------------------------------- */
assert(cdr(s).template is<null>());
assert(cdr(c).template is<null>());

s = cons(car(s), car(d));
e = cadr(d);
c = caddr(d);
Expand Down Expand Up @@ -517,13 +519,41 @@ inline namespace kernel
}();
}
}
catch (error const& error)
catch (object const& thrown) // by the primitive procedure `throw`.
{
return raise(make(error));
if (thrown.is_also<error>())
{
thrown.as<error>().detail(error::in::running, cons(s, e, c, d)).raise();
return unspecified;
}
else
{
throw error(make<string>("uncaught exception"), thrown);
}
}
catch (error & thrown) // by any primitive procedure other than `throw`.
{
if (exception_handler)
{
return apply(exception_handler, thrown.make());
}
else // In most cases, this clause will never be called.
{
thrown.detail(error::in::running, cons(s, e, c, d)).raise();
return unspecified;
}
}
catch (std::exception const& exception)
catch (std::exception const& exception) // by the system.
{
return raise(make<error>(make<string>(exception.what())));
if (auto thrown = error(make<string>(exception.what())); exception_handler)
{
return apply(exception_handler, thrown.make());
}
else // In most cases, this clause will never be called.
{
thrown.detail(error::in::running, cons(s, e, c, d));
throw thrown;
}
}
}
};
Expand Down
33 changes: 23 additions & 10 deletions include/meevax/kernel/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,35 @@ inline namespace kernel
struct error : public virtual pair // (<message> . <irritants>)
, public std::exception
{
struct detail
enum class in
{
let expression;

explicit detail(let const& expression)
: expression { expression }
{}
evaluating,
compiling,
expanding,
generating,
optimizing,
running,
};

std::vector<detail> details {};
std::vector<std::pair<in, object>> contexts {};

mutable std::string explanation {};
mutable std::string cache {};

using pair::pair;

~error() override = default;

template <typename... Ts>
auto append(Ts&&... xs) -> auto &
auto detail(Ts&&... xs) -> auto &
{
details.emplace_back(std::forward<decltype(xs)>(xs)...);
contexts.emplace_back(std::forward<decltype(xs)>(xs)...);
return *this;
}

auto irritants() const noexcept -> object const&;

virtual auto make() const -> object;

auto message() const noexcept -> object const&;

/*
Expand All @@ -73,6 +76,11 @@ inline namespace kernel
{
using error::error;

auto make() const -> object override
{
return meevax::make(*this);
}

auto raise() const -> void override
{
throw *this;
Expand All @@ -83,6 +91,11 @@ inline namespace kernel
{
using error::error;

auto make() const -> object override
{
return meevax::make(*this);
}

auto raise() const -> void override
{
throw *this;
Expand Down
2 changes: 0 additions & 2 deletions include/meevax/kernel/port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#ifndef INCLUDED_MEEVAX_KERNEL_PORT_HPP
#define INCLUDED_MEEVAX_KERNEL_PORT_HPP

#include <filesystem>

namespace meevax
{
inline namespace kernel
Expand Down
14 changes: 12 additions & 2 deletions include/meevax/kernel/syntactic_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ inline namespace kernel

inline auto expand(object const& expression,
object const& bound_variables = nullptr, // list of <formals>
object const& free_variables = nullptr) const -> object
object const& free_variables = nullptr) const -> object try
{
if (not expression.is<pair>())
{
Expand Down Expand Up @@ -817,12 +817,17 @@ inline namespace kernel

return expander::call(*this, expression, bound_variables, free_variables);
}
catch (error & e)
{
e.detail(error::in::expanding, expression).raise();
return unspecified;
}

inline auto generate(object const& expression,
object const& bound_variables = nullptr, // list of <formals>
object const& free_variables = nullptr,
object const& continuation = list(make(instruction::stop)),
bool tail = false) -> object
bool tail = false) -> object try
{
if (expression.is<null>())
{
Expand Down Expand Up @@ -885,6 +890,11 @@ inline namespace kernel
return generator::call(*this, expression, bound_variables, free_variables, continuation, tail);
}
}
catch (error & e)
{
e.detail(error::in::generating, expression).raise();
return unspecified;
}

inline auto identify(object const& variable,
object const& bound_variables,
Expand Down
2 changes: 1 addition & 1 deletion include/meevax/kernel/textual_input_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef INCLUDED_MEEVAX_KERNEL_TEXTUAL_INPUT_PORT_HPP
#define INCLUDED_MEEVAX_KERNEL_TEXTUAL_INPUT_PORT_HPP

#include <forward_list>
#include <filesystem>
#include <istream>

#include <meevax/kernel/eof.hpp>
Expand Down
37 changes: 15 additions & 22 deletions src/kernel/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ inline namespace kernel
*/
struct dump
{
environment & registers;
environment * context;

let s, e, c, d;

explicit dump(environment & registers)
: registers { registers }
, s { std::exchange(registers.s, nullptr) }
, e { std::exchange(registers.e, nullptr) }
, c { std::exchange(registers.c, nullptr) }
, d { std::exchange(registers.d, nullptr) }
explicit dump(environment * context)
: context { context }
, s { std::exchange(context->s, nullptr) }
, e { std::exchange(context->e, nullptr) }
, c { std::exchange(context->c, nullptr) }
, d { std::exchange(context->d, nullptr) }
{}

~dump()
Expand All @@ -99,28 +99,21 @@ inline namespace kernel

auto undump() -> void
{
registers.s = s;
registers.e = e;
registers.c = c;
registers.d = d;
context->s = s;
context->e = e;
context->c = c;
context->d = d;
}
};

auto undump = dump(*this);
auto undump = dump(this);

return execute(optimize(compile(expression)));
}
catch (object const& x)
catch (error & e)
{
if (x.is_also<error>())
{
x.as<error>().append(expression).raise();
return unspecified;
}
else
{
throw error(make<string>("uncaught exception"), x);
}
e.detail(error::in::evaluating, expression).raise();
return unspecified;
}

auto resolve(object const& form) -> object
Expand Down
Loading

0 comments on commit df05da2

Please sign in to comment.