Be more explicit about AST types (#921)

This was born out of wanting FunctionDeclaration to explicitly have a Block for a body, and became a bit more of specifying types around. Note this forces exec_program to generate a Block for print()'s body, which is probably more correct as now we can expect a standard FunctionDeclaration AST structure, even for the built-ins.

There is a syntactic change here: a continuation's body is now a Block, not just a Statement. I've added an example disallowed test case. I think this is more reasonable syntax.

Other than that, note that optional_else now generates a valid Block. This has me thinking about whether we can eliminate Sequence, but that seemed well out of scope for this.
This commit is contained in:
Jon Meow
2021-10-28 13:54:45 -07:00
committed by GitHub
parent dea277d8e1
commit bbd4940e6d
11 changed files with 143 additions and 122 deletions
+60 -63
View File
@@ -60,6 +60,48 @@ class Statement {
SourceLocation source_loc_;
};
class Sequence : public Statement {
public:
Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
std::optional<Nonnull<Statement*>> next)
: Statement(Kind::Sequence, source_loc),
statement_(statement),
next_(next) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Sequence;
}
auto statement() const -> const Statement& { return *statement_; }
auto statement() -> Statement& { return *statement_; }
auto next() const -> std::optional<Nonnull<const Statement*>> {
return next_;
}
auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
private:
Nonnull<Statement*> statement_;
std::optional<Nonnull<Statement*>> next_;
};
class Block : public Statement {
public:
Block(SourceLocation source_loc, std::optional<Nonnull<Sequence*>> sequence)
: Statement(Kind::Block, source_loc), sequence_(sequence) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Block;
}
auto sequence() const -> std::optional<Nonnull<const Sequence*>> {
return sequence_;
}
auto sequence() -> std::optional<Nonnull<Sequence*>> { return sequence_; }
private:
std::optional<Nonnull<Sequence*>> sequence_;
};
class ExpressionStatement : public Statement {
public:
ExpressionStatement(SourceLocation source_loc,
@@ -123,12 +165,11 @@ class VariableDefinition : public Statement {
class If : public Statement {
public:
If(SourceLocation source_loc, Nonnull<Expression*> condition,
Nonnull<Statement*> then_statement,
std::optional<Nonnull<Statement*>> else_statement)
Nonnull<Block*> then_block, std::optional<Nonnull<Block*>> else_block)
: Statement(Kind::If, source_loc),
condition_(condition),
then_statement_(then_statement),
else_statement_(else_statement) {}
then_block_(then_block),
else_block_(else_block) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::If;
@@ -136,19 +177,17 @@ class If : public Statement {
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto then_statement() const -> const Statement& { return *then_statement_; }
auto then_statement() -> Statement& { return *then_statement_; }
auto else_statement() const -> std::optional<Nonnull<const Statement*>> {
return else_statement_;
}
auto else_statement() -> std::optional<Nonnull<Statement*>> {
return else_statement_;
auto then_block() const -> const Block& { return *then_block_; }
auto then_block() -> Block& { return *then_block_; }
auto else_block() const -> std::optional<Nonnull<const Block*>> {
return else_block_;
}
auto else_block() -> std::optional<Nonnull<Block*>> { return else_block_; }
private:
Nonnull<Expression*> condition_;
Nonnull<Statement*> then_statement_;
std::optional<Nonnull<Statement*>> else_statement_;
Nonnull<Block*> then_block_;
std::optional<Nonnull<Block*>> else_block_;
};
class Return : public Statement {
@@ -189,52 +228,10 @@ class Return : public Statement {
std::optional<Nonnull<const FunctionDeclaration*>> function_;
};
class Sequence : public Statement {
public:
Sequence(SourceLocation source_loc, Nonnull<Statement*> statement,
std::optional<Nonnull<Statement*>> next)
: Statement(Kind::Sequence, source_loc),
statement_(statement),
next_(next) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Sequence;
}
auto statement() const -> const Statement& { return *statement_; }
auto statement() -> Statement& { return *statement_; }
auto next() const -> std::optional<Nonnull<const Statement*>> {
return next_;
}
auto next() -> std::optional<Nonnull<Statement*>> { return next_; }
private:
Nonnull<Statement*> statement_;
std::optional<Nonnull<Statement*>> next_;
};
class Block : public Statement {
public:
Block(SourceLocation source_loc, std::optional<Nonnull<Statement*>> statement)
: Statement(Kind::Block, source_loc), statement_(statement) {}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Block;
}
auto statement() const -> std::optional<Nonnull<const Statement*>> {
return statement_;
}
auto statement() -> std::optional<Nonnull<Statement*>> { return statement_; }
private:
std::optional<Nonnull<Statement*>> statement_;
};
class While : public Statement {
public:
While(SourceLocation source_loc, Nonnull<Expression*> condition,
Nonnull<Statement*> body)
Nonnull<Block*> body)
: Statement(Kind::While, source_loc),
condition_(condition),
body_(body) {}
@@ -245,12 +242,12 @@ class While : public Statement {
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto body() const -> const Statement& { return *body_; }
auto body() -> Statement& { return *body_; }
auto body() const -> const Block& { return *body_; }
auto body() -> Block& { return *body_; }
private:
Nonnull<Expression*> condition_;
Nonnull<Statement*> body_;
Nonnull<Block*> body_;
};
class Break : public Statement {
@@ -352,7 +349,7 @@ class Match : public Statement {
class Continuation : public Statement {
public:
Continuation(SourceLocation source_loc, std::string continuation_variable,
Nonnull<Statement*> body)
Nonnull<Block*> body)
: Statement(Kind::Continuation, source_loc),
continuation_variable_(std::move(continuation_variable)),
body_(body) {}
@@ -364,12 +361,12 @@ class Continuation : public Statement {
auto continuation_variable() const -> const std::string& {
return continuation_variable_;
}
auto body() const -> const Statement& { return *body_; }
auto body() -> Statement& { return *body_; }
auto body() const -> const Block& { return *body_; }
auto body() -> Block& { return *body_; }
private:
std::string continuation_variable_;
Nonnull<Statement*> body_;
Nonnull<Block*> body_;
};
// A run statement.