Skip to content

Commit

Permalink
Enforce error method
Browse files Browse the repository at this point in the history
Also rename and enforce check for accessing while potentially growing array
  • Loading branch information
JohnDTill committed Aug 4, 2023
1 parent 2278414 commit 84dccf1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/forscape_parse_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ std::string ParseTree::str(ParseNode pn) const alloc_except {
}

ParseNode ParseTree::addError(const Typeset::Selection& sel) noexcept {
assert(notAccessingDataWhileModifying(sel));

ParseNode pn = data.size();
#ifndef NDEBUG
created.insert(pn);
Expand All @@ -220,8 +222,9 @@ ParseNode ParseTree::addError(const Typeset::Selection& sel) noexcept {
}

template<typename T> ParseNode ParseTree::addNode(Op type, const Selection& sel, const T& children) alloc_except {
//assert(notInTree(sel)); //EVENTUALLY: I don't think this belongs
assert(notInTree(children));
assert(type != OP_ERROR); //There is a specific method to create errors, not the general ones
assert(notAccessingDataWhileModifying(sel));
assert(notAccessingDataWhileModifying(children));

ParseNode pn = data.size();
#ifndef NDEBUG
Expand Down Expand Up @@ -378,7 +381,7 @@ ParseNode ParseTree::popNaryChild() noexcept{

ParseNode ParseTree::finishNary(Op type, const Selection& sel) alloc_except {
assert(!nary_start.empty());
assert(notInTree(sel));
assert(notAccessingDataWhileModifying(sel));
size_t N = nary_construction_stack.size()-nary_start.back();

ParseNode pn = data.size();
Expand Down Expand Up @@ -500,10 +503,10 @@ bool ParseTree::inFinalState() const noexcept {
return nary_construction_stack.empty() && nary_start.empty();
}

template<typename T> bool ParseTree::notInTree(const T& obj) const noexcept {
template<typename T> bool ParseTree::notAccessingDataWhileModifying(const T& obj) const noexcept {
if(data.empty()) return true;

auto potential_index = reinterpret_cast<const size_t*>(&obj) - &data[0];
const auto potential_index = reinterpret_cast<const size_t*>(&obj) - &data[0];
return potential_index < 0 || static_cast<size_t>(potential_index) >= data.size();
}

Expand Down
2 changes: 1 addition & 1 deletion src/forscape_parse_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ParseTree {
bool isNode(ParseNode pn) const noexcept;
bool isLastAllocatedNode(ParseNode pn) const noexcept;
bool inFinalState() const noexcept;
template<typename T> bool notInTree(const T& obj) const noexcept;
template<typename T> bool notAccessingDataWhileModifying(const T& obj) const noexcept;
std::string toGraphviz() const;
std::string toGraphviz(ParseNode pn) const;
#endif
Expand Down
10 changes: 6 additions & 4 deletions src/forscape_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ ParseNode Parser::switchStatement() alloc_except {
consume(COLON);
match(NEWLINE);
if(peek(CASE) || peek(DEFAULT) || peek(RIGHTBRACKET)){
ParseNode case_node = parse_tree.addNode<2>(OP_CASE, parse_tree.getSelection(case_key), {case_key, NONE});
Typeset::Selection sel(parse_tree.getSelection(case_key));
ParseNode case_node = parse_tree.addNode<2>(OP_CASE, sel, {case_key, NONE});
parse_tree.addNaryChild(case_node);
}else{
ParseNode case_codepath = blockStatement();
Expand All @@ -276,7 +277,8 @@ ParseNode Parser::switchStatement() alloc_except {
consume(COLON);
match(NEWLINE);
if(peek(CASE) || peek(DEFAULT) || peek(RIGHTBRACKET)){
ParseNode case_node = parse_tree.addNode<2>(OP_DEFAULT, parse_tree.getSelection(default_label), {default_label, NONE});
Typeset::Selection sel(parse_tree.getSelection(default_label));
ParseNode case_node = parse_tree.addNode<2>(OP_DEFAULT, sel, {default_label, NONE});
parse_tree.addNaryChild(case_node);
}else{
ParseNode default_codepath = blockStatement();
Expand Down Expand Up @@ -952,7 +954,7 @@ ParseNode Parser::rightUnary(ParseNode n) alloc_except {
case PERIOD: advance();
if(!peek(IDENTIFIER)){
const Typeset::Marker& m = rMarkPrev();
ParseNode blank = parse_tree.addTerminal(OP_ERROR, Typeset::Selection(m, m));
ParseNode blank = parse_tree.addError(Typeset::Selection(m, m));
n = parse_tree.addNode<2>(OP_SCOPE_ACCESS, {n, blank}); break;
}
n = parse_tree.addNode<2>(OP_SCOPE_ACCESS, {n, primary()}); break;
Expand Down Expand Up @@ -2001,7 +2003,7 @@ ParseNode Parser::error(ErrorCode code) alloc_except {
return error(code, selection());
}

ParseNode Parser::error(ErrorCode code, const Typeset::Selection& c) alloc_except {
ParseNode Parser::error(ErrorCode code, Typeset::Selection c) alloc_except {
error_stream.fail(c, code);

return parse_tree.addError(c);
Expand Down
2 changes: 1 addition & 1 deletion src/forscape_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Parser {
ParseNode big(Op type) alloc_except;
ParseNode oneArgConstruct(Op type) alloc_except;
ParseNode error(ErrorCode code) alloc_except;
ParseNode error(ErrorCode code, const Typeset::Selection& c) alloc_except;
ParseNode error(ErrorCode code, Typeset::Selection c) alloc_except;
void advance() noexcept;
bool match(ForscapeTokenType type) noexcept;
bool peek(ForscapeTokenType type) const noexcept;
Expand Down
6 changes: 4 additions & 2 deletions src/forscape_static_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ ParseNode StaticPass::resolveStmt(ParseNode pn) noexcept{
assert(pn != NONE);

switch (parse_tree.getOp(pn)) {
case OP_SETTINGS_UPDATE:
case OP_SETTINGS_UPDATE:{
//EVENTUALLY: settings should only be applied to the lexical scope
// not a concern now since the static pass and interpreter will undergo a total rewrite
settings().enact( parse_tree.getFlag(pn) );
return parse_tree.addTerminal(OP_DO_NOTHING, parse_tree.getSelection(pn));
Typeset::Selection sel(parse_tree.getSelection(pn));
return parse_tree.addTerminal(OP_DO_NOTHING, sel);
}

case OP_DO_NOTHING:
return pn;
Expand Down

0 comments on commit 84dccf1

Please sign in to comment.