mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-28 18:57:48 +01:00
better error messages
This commit is contained in:
@@ -44,23 +44,33 @@ public:
|
|||||||
|
|
||||||
const Handle car() const {
|
const Handle car() const {
|
||||||
if (!_target) return Handle(nullptr);
|
if (!_target) return Handle(nullptr);
|
||||||
|
if (_target->_type != CellType::CONS) throw std::invalid_argument("Expected cons cell, got something else");
|
||||||
return dynamic_cast<ConsCell &>(*_target)._car.load();
|
return dynamic_cast<ConsCell &>(*_target)._car.load();
|
||||||
}
|
}
|
||||||
const Handle cdr() const {
|
const Handle cdr() const {
|
||||||
if (!_target) return Handle(nullptr);
|
if (!_target) return Handle(nullptr);
|
||||||
|
if (_target->_type != CellType::CONS) throw std::invalid_argument("Expected cons cell, got something else");
|
||||||
return dynamic_cast<ConsCell &>(*_target)._cdr.load();
|
return dynamic_cast<ConsCell &>(*_target)._cdr.load();
|
||||||
}
|
}
|
||||||
Handle car() {
|
Handle car() {
|
||||||
if (!_target) return Handle(nullptr);
|
if (!_target) return Handle(nullptr);
|
||||||
|
if (_target->_type != CellType::CONS) throw std::invalid_argument("Expected cons cell, got something else");
|
||||||
return dynamic_cast<ConsCell &>(*_target)._car.load();
|
return dynamic_cast<ConsCell &>(*_target)._car.load();
|
||||||
}
|
}
|
||||||
Handle cdr() {
|
Handle cdr() {
|
||||||
if (!_target) return Handle(nullptr);
|
if (!_target) return Handle(nullptr);
|
||||||
|
if (_target->_type != CellType::CONS) throw std::invalid_argument("Expected cons cell, got something else");
|
||||||
return dynamic_cast<ConsCell &>(*_target)._cdr.load();
|
return dynamic_cast<ConsCell &>(*_target)._cdr.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
CellValType val() const { return dynamic_cast<NumAtomCell &>(*_target)._val; }
|
CellValType val() const {
|
||||||
std::string_view strval() const { return dynamic_cast<StrAtomCell &>(*_target)._val; }
|
if (_target->_type != CellType::NUMATOM) throw std::invalid_argument("Expected number cell, got something else");
|
||||||
|
return dynamic_cast<NumAtomCell &>(*_target)._val;
|
||||||
|
}
|
||||||
|
std::string_view strval() const {
|
||||||
|
if (_target->_type != CellType::STRATOM) throw std::invalid_argument("Expected string cell, got something else");
|
||||||
|
return dynamic_cast<StrAtomCell &>(*_target)._val;
|
||||||
|
}
|
||||||
|
|
||||||
CellType type() const {
|
CellType type() const {
|
||||||
if (!_target) return CellType::CONS;
|
if (!_target) return CellType::CONS;
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ Handle Handle::makeNumCell(int64_t val) { return MemoryContext::get().create_cel
|
|||||||
Handle Handle::makeStrCell(std::string val) { return MemoryContext::get().create_cell<StrAtomCell>(std::move(val)); }
|
Handle Handle::makeStrCell(std::string val) { return MemoryContext::get().create_cell<StrAtomCell>(std::move(val)); }
|
||||||
|
|
||||||
void Handle::setcar(const Handle &car) {
|
void Handle::setcar(const Handle &car) {
|
||||||
|
if (_target->_type != CellType::CONS) throw std::invalid_argument("Expected cons cell, got something else");
|
||||||
MemoryContext::get().run_dirty<void>([&](std::function<void(Cell *)> lost) -> void {
|
MemoryContext::get().run_dirty<void>([&](std::function<void(Cell *)> lost) -> void {
|
||||||
lost(dynamic_cast<ConsCell &>(*_target)._car);
|
lost(dynamic_cast<ConsCell &>(*_target)._car);
|
||||||
dynamic_cast<ConsCell &>(*_target)._car = car.get();
|
dynamic_cast<ConsCell &>(*_target)._car = car.get();
|
||||||
@@ -75,6 +76,7 @@ void Handle::setcar(const Handle &car) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Handle::setcdr(const Handle &cdr) {
|
void Handle::setcdr(const Handle &cdr) {
|
||||||
|
if (_target->_type != CellType::CONS) throw std::invalid_argument("Expected cons cell, got something else");
|
||||||
MemoryContext::get().run_dirty<void>([&](std::function<void(Cell *)> lost) -> void {
|
MemoryContext::get().run_dirty<void>([&](std::function<void(Cell *)> lost) -> void {
|
||||||
lost(dynamic_cast<ConsCell &>(*_target)._cdr);
|
lost(dynamic_cast<ConsCell &>(*_target)._cdr);
|
||||||
dynamic_cast<ConsCell &>(*_target)._cdr = cdr.get();
|
dynamic_cast<ConsCell &>(*_target)._cdr = cdr.get();
|
||||||
|
|||||||
@@ -64,7 +64,10 @@ std::string Parser::Tokenizer::getNext() {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view Parser::Tokenizer::peek() const { return _tokens.front(); }
|
std::string_view Parser::Tokenizer::peek() const {
|
||||||
|
if (_tokens.empty()) throw std::invalid_argument("Unexpected end of input (or maybe missing \")\"?");
|
||||||
|
return _tokens.front();
|
||||||
|
}
|
||||||
|
|
||||||
void Parser::Tokenizer::load(std::string_view input) {
|
void Parser::Tokenizer::load(std::string_view input) {
|
||||||
std::string_view::size_type curpos = input.find_first_not_of(std::string{' ', '\n', '\r'});
|
std::string_view::size_type curpos = input.find_first_not_of(std::string{' ', '\n', '\r'});
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ void VM::step() {
|
|||||||
std::getline(_instream, read);
|
std::getline(_instream, read);
|
||||||
_s.push(Parser::parse_str(read));
|
_s.push(Parser::parse_str(read));
|
||||||
} else {
|
} else {
|
||||||
assert(false);
|
throw std::invalid_argument("Unexpected end of program");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user