Switch Clause to Ptr (#800)

This is consistent with other lists, doesn't seem to benefit from being a Ptr.
This commit is contained in:
Jon Meow
2021-09-01 15:19:58 -07:00
committed by GitHub
parent 9f67c4b9a8
commit 5f8b231322
5 changed files with 24 additions and 33 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
out << "match (" << *match.Exp() << ") {";
if (depth < 0 || depth > 1) {
out << "\n";
for (auto& clause : *match.Clauses()) {
for (auto& clause : match.Clauses()) {
out << "case " << *clause.first << " =>\n";
clause.second->PrintDepth(depth - 1, out);
out << "\n";
+4 -4
View File
@@ -226,8 +226,8 @@ class Continue : public Statement {
class Match : public Statement {
public:
Match(SourceLocation loc, Ptr<const Expression> exp,
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>* clauses)
: Statement(Kind::Match, loc), exp(exp), clauses(clauses) {}
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>> clauses)
: Statement(Kind::Match, loc), exp(exp), clauses(std::move(clauses)) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->Tag() == Kind::Match;
@@ -235,13 +235,13 @@ class Match : public Statement {
auto Exp() const -> Ptr<const Expression> { return exp; }
auto Clauses() const
-> const std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>* {
-> const std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>& {
return clauses;
}
private:
Ptr<const Expression> exp;
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>* clauses;
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>> clauses;
};
// A continuation statement.
@@ -765,12 +765,12 @@ auto Interpreter::StepStmt() -> Transition {
// * 2: the pattern for clause 1
// * ...
auto clause_num = (act->Pos() - 1) / 2;
if (clause_num >= static_cast<int>(match_stmt.Clauses()->size())) {
if (clause_num >= static_cast<int>(match_stmt.Clauses().size())) {
DeallocateScope(frame->scopes.Top());
frame->scopes.Pop();
return Done{};
}
auto c = match_stmt.Clauses()->begin();
auto c = match_stmt.Clauses().begin();
std::advance(c, clause_num);
if (act->Pos() % 2 == 1) {
@@ -784,7 +784,7 @@ auto Interpreter::StepStmt() -> Transition {
std::optional<Env> matches = PatternMatch(pat, v, stmt->SourceLoc());
if (matches) { // we have a match, start the body
// Ensure we don't process any more clauses.
act->SetPos(2 * match_stmt.Clauses()->size() + 1);
act->SetPos(2 * match_stmt.Clauses().size() + 1);
for (const auto& [name, value] : *matches) {
frame->scopes.Top()->values.Set(name, value);
@@ -667,12 +667,12 @@ auto TypeChecker::TypeCheckStmt(Ptr<const Statement> s, TypeEnv types,
const auto& match = cast<Match>(*s);
auto res = TypeCheckExp(match.Exp(), types, values);
auto res_type = res.type;
auto new_clauses = global_arena->RawNew<
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>>();
for (auto& clause : *match.Clauses()) {
new_clauses->push_back(TypeCheckCase(res_type, clause.first,
clause.second, types, values,
ret_type, is_omitted_ret_type));
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>
new_clauses;
for (auto& clause : match.Clauses()) {
new_clauses.push_back(TypeCheckCase(res_type, clause.first,
clause.second, types, values,
ret_type, is_omitted_ret_type));
}
auto new_s =
global_arena->New<Match>(s->SourceLoc(), res.exp, new_clauses);
@@ -826,12 +826,12 @@ static auto CheckOrEnsureReturn(std::optional<Ptr<const Statement>> opt_stmt,
switch (stmt->Tag()) {
case Statement::Kind::Match: {
const auto& match = cast<Match>(*stmt);
auto new_clauses = global_arena->RawNew<
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>>();
for (const auto& clause : *match.Clauses()) {
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>
new_clauses;
for (const auto& clause : match.Clauses()) {
auto s = CheckOrEnsureReturn(clause.second, omitted_ret_type,
stmt->SourceLoc());
new_clauses->push_back(std::make_pair(clause.first, s));
new_clauses.push_back(std::make_pair(clause.first, s));
}
return global_arena->New<Match>(stmt->SourceLoc(), match.Exp(),
new_clauses);
+6 -15
View File
@@ -128,8 +128,8 @@
%type <ParenContents<Pattern>> paren_pattern_contents
%type <BisonWrap<std::pair<std::string, Ptr<const Expression>>>> alternative
%type <std::list<std::pair<std::string, Ptr<const Expression>>>> alternative_list
%type <std::pair<Ptr<const Pattern>, Ptr<const Statement>>*> clause
%type <std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>*> clause_list
%type <BisonWrap<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>> clause
%type <std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>> clause_list
%token
// Most tokens have their spelling defined in lexer.lpp.
@@ -460,31 +460,22 @@ maybe_empty_tuple_pattern:
;
clause:
CASE pattern DOUBLE_ARROW statement
{
$$ = global_arena
->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(
$2, $4);
}
{ $$ = std::pair<Ptr<const Pattern>, Ptr<const Statement>>($2, $4); }
| DEFAULT DOUBLE_ARROW statement
{
auto vp = global_arena -> New<BindingPattern>(
context.SourceLoc(), std::nullopt,
global_arena->New<AutoPattern>(context.SourceLoc()));
$$ = global_arena
->RawNew<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(
vp, $3);
$$ = std::pair<Ptr<const Pattern>, Ptr<const Statement>>(vp, $3);
}
;
clause_list:
// Empty
{
$$ = global_arena->RawNew<
std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>>();
}
{ $$ = std::list<std::pair<Ptr<const Pattern>, Ptr<const Statement>>>(); }
| clause clause_list
{
$$ = $2;
$$->push_front(*$1);
$$.push_front($1);
}
;
statement: