mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-27 07:10:09 +01:00
Private unions (#492)
* changed union of Statement to be private * changed the union in Expression to be private * changed union in Value to be private * changed AST constructors to be static methods * updates to syntax unit tests
This commit is contained in:
@@ -4,11 +4,68 @@
|
||||
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto MakeExpStmt(int line_num, const Expression* exp) -> const Statement* {
|
||||
const Expression* Statement::GetExpression() const {
|
||||
assert(tag == StatementKind::ExpressionStatement);
|
||||
return u.exp;
|
||||
}
|
||||
|
||||
Assignment Statement::GetAssign() const {
|
||||
assert(tag == StatementKind::Assign);
|
||||
return u.assign;
|
||||
}
|
||||
|
||||
VariableDefinition Statement::GetVariableDefinition() const {
|
||||
assert(tag == StatementKind::VariableDefinition);
|
||||
return u.variable_definition;
|
||||
}
|
||||
|
||||
IfStatement Statement::GetIf() const {
|
||||
assert(tag == StatementKind::If);
|
||||
return u.if_stmt;
|
||||
}
|
||||
|
||||
const Expression* Statement::GetReturn() const {
|
||||
assert(tag == StatementKind::Return);
|
||||
return u.return_stmt;
|
||||
}
|
||||
|
||||
Sequence Statement::GetSequence() const {
|
||||
assert(tag == StatementKind::Sequence);
|
||||
return u.sequence;
|
||||
}
|
||||
|
||||
Block Statement::GetBlock() const {
|
||||
assert(tag == StatementKind::Block);
|
||||
return u.block;
|
||||
}
|
||||
|
||||
While Statement::GetWhile() const {
|
||||
assert(tag == StatementKind::While);
|
||||
return u.while_stmt;
|
||||
}
|
||||
|
||||
Match Statement::GetMatch() const {
|
||||
assert(tag == StatementKind::Match);
|
||||
return u.match_stmt;
|
||||
}
|
||||
|
||||
Continuation Statement::GetContinuation() const {
|
||||
assert(tag == StatementKind::Continuation);
|
||||
return u.continuation;
|
||||
}
|
||||
|
||||
Run Statement::GetRun() const {
|
||||
assert(tag == StatementKind::Run);
|
||||
return u.run;
|
||||
}
|
||||
|
||||
auto Statement::MakeExpStmt(int line_num, const Expression* exp)
|
||||
-> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::ExpressionStatement;
|
||||
@@ -16,8 +73,8 @@ auto MakeExpStmt(int line_num, const Expression* exp) -> const Statement* {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeAssign(int line_num, const Expression* lhs, const Expression* rhs)
|
||||
-> const Statement* {
|
||||
auto Statement::MakeAssign(int line_num, const Expression* lhs,
|
||||
const Expression* rhs) -> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Assign;
|
||||
@@ -26,8 +83,8 @@ auto MakeAssign(int line_num, const Expression* lhs, const Expression* rhs)
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeVarDef(int line_num, const Expression* pat, const Expression* init)
|
||||
-> const Statement* {
|
||||
auto Statement::MakeVarDef(int line_num, const Expression* pat,
|
||||
const Expression* init) -> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::VariableDefinition;
|
||||
@@ -36,8 +93,9 @@ auto MakeVarDef(int line_num, const Expression* pat, const Expression* init)
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeIf(int line_num, const Expression* cond, const Statement* then_stmt,
|
||||
const Statement* else_stmt) -> const Statement* {
|
||||
auto Statement::MakeIf(int line_num, const Expression* cond,
|
||||
const Statement* then_stmt, const Statement* else_stmt)
|
||||
-> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::If;
|
||||
@@ -47,8 +105,8 @@ auto MakeIf(int line_num, const Expression* cond, const Statement* then_stmt,
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeWhile(int line_num, const Expression* cond, const Statement* body)
|
||||
-> const Statement* {
|
||||
auto Statement::MakeWhile(int line_num, const Expression* cond,
|
||||
const Statement* body) -> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::While;
|
||||
@@ -57,21 +115,22 @@ auto MakeWhile(int line_num, const Expression* cond, const Statement* body)
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeBreak(int line_num) -> const Statement* {
|
||||
auto Statement::MakeBreak(int line_num) -> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Break;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeContinue(int line_num) -> const Statement* {
|
||||
auto Statement::MakeContinue(int line_num) -> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Continue;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeReturn(int line_num, const Expression* e) -> const Statement* {
|
||||
auto Statement::MakeReturn(int line_num, const Expression* e)
|
||||
-> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Return;
|
||||
@@ -79,7 +138,7 @@ auto MakeReturn(int line_num, const Expression* e) -> const Statement* {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeSeq(int line_num, const Statement* s1, const Statement* s2)
|
||||
auto Statement::MakeSeq(int line_num, const Statement* s1, const Statement* s2)
|
||||
-> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
@@ -89,7 +148,8 @@ auto MakeSeq(int line_num, const Statement* s1, const Statement* s2)
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeBlock(int line_num, const Statement* stmt) -> const Statement* {
|
||||
auto Statement::MakeBlock(int line_num, const Statement* stmt)
|
||||
-> const Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Block;
|
||||
@@ -97,7 +157,7 @@ auto MakeBlock(int line_num, const Statement* stmt) -> const Statement* {
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeMatch(
|
||||
auto Statement::MakeMatch(
|
||||
int line_num, const Expression* exp,
|
||||
std::list<std::pair<const Expression*, const Statement*>>* clauses)
|
||||
-> const Statement* {
|
||||
@@ -111,8 +171,9 @@ auto MakeMatch(
|
||||
|
||||
// Returns an AST node for a continuation statement give its line number and
|
||||
// parts.
|
||||
auto MakeContinuationStatement(int line_num, std::string continuation_variable,
|
||||
const Statement* body) -> const Statement* {
|
||||
auto Statement::MakeContinuation(int line_num,
|
||||
std::string continuation_variable,
|
||||
const Statement* body) -> const Statement* {
|
||||
auto* continuation = new Statement();
|
||||
continuation->line_num = line_num;
|
||||
continuation->tag = StatementKind::Continuation;
|
||||
@@ -123,7 +184,8 @@ auto MakeContinuationStatement(int line_num, std::string continuation_variable,
|
||||
}
|
||||
|
||||
// Returns an AST node for a run statement give its line number and argument.
|
||||
auto MakeRun(int line_num, const Expression* argument) -> const Statement* {
|
||||
auto Statement::MakeRun(int line_num, const Expression* argument)
|
||||
-> const Statement* {
|
||||
auto* run = new Statement();
|
||||
run->line_num = line_num;
|
||||
run->tag = StatementKind::Run;
|
||||
@@ -132,7 +194,7 @@ auto MakeRun(int line_num, const Expression* argument) -> const Statement* {
|
||||
}
|
||||
|
||||
// Returns an AST node for an await statement give its line number.
|
||||
auto MakeAwait(int line_num) -> const Statement* {
|
||||
auto Statement::MakeAwait(int line_num) -> const Statement* {
|
||||
auto* await = new Statement();
|
||||
await->line_num = line_num;
|
||||
await->tag = StatementKind::Await;
|
||||
@@ -150,11 +212,11 @@ void PrintStatement(const Statement* s, int depth) {
|
||||
switch (s->tag) {
|
||||
case StatementKind::Match:
|
||||
std::cout << "match (";
|
||||
PrintExp(s->u.match_stmt.exp);
|
||||
PrintExp(s->GetMatch().exp);
|
||||
std::cout << ") {";
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
for (auto& clause : *s->u.match_stmt.clauses) {
|
||||
for (auto& clause : *s->GetMatch().clauses) {
|
||||
std::cout << "case ";
|
||||
PrintExp(clause.first);
|
||||
std::cout << " =>" << std::endl;
|
||||
@@ -168,9 +230,9 @@ void PrintStatement(const Statement* s, int depth) {
|
||||
break;
|
||||
case StatementKind::While:
|
||||
std::cout << "while (";
|
||||
PrintExp(s->u.while_stmt.cond);
|
||||
PrintExp(s->GetWhile().cond);
|
||||
std::cout << ")" << std::endl;
|
||||
PrintStatement(s->u.while_stmt.body, depth - 1);
|
||||
PrintStatement(s->GetWhile().body, depth - 1);
|
||||
break;
|
||||
case StatementKind::Break:
|
||||
std::cout << "break;";
|
||||
@@ -180,49 +242,49 @@ void PrintStatement(const Statement* s, int depth) {
|
||||
break;
|
||||
case StatementKind::VariableDefinition:
|
||||
std::cout << "var ";
|
||||
PrintExp(s->u.variable_definition.pat);
|
||||
PrintExp(s->GetVariableDefinition().pat);
|
||||
std::cout << " = ";
|
||||
PrintExp(s->u.variable_definition.init);
|
||||
PrintExp(s->GetVariableDefinition().init);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::ExpressionStatement:
|
||||
PrintExp(s->u.exp);
|
||||
PrintExp(s->GetExpression());
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::Assign:
|
||||
PrintExp(s->u.assign.lhs);
|
||||
PrintExp(s->GetAssign().lhs);
|
||||
std::cout << " = ";
|
||||
PrintExp(s->u.assign.rhs);
|
||||
PrintExp(s->GetAssign().rhs);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::If:
|
||||
std::cout << "if (";
|
||||
PrintExp(s->u.if_stmt.cond);
|
||||
PrintExp(s->GetIf().cond);
|
||||
std::cout << ")" << std::endl;
|
||||
PrintStatement(s->u.if_stmt.then_stmt, depth - 1);
|
||||
PrintStatement(s->GetIf().then_stmt, depth - 1);
|
||||
std::cout << std::endl << "else" << std::endl;
|
||||
PrintStatement(s->u.if_stmt.else_stmt, depth - 1);
|
||||
PrintStatement(s->GetIf().else_stmt, depth - 1);
|
||||
break;
|
||||
case StatementKind::Return:
|
||||
std::cout << "return ";
|
||||
PrintExp(s->u.return_stmt);
|
||||
PrintExp(s->GetReturn());
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::Sequence:
|
||||
PrintStatement(s->u.sequence.stmt, depth);
|
||||
PrintStatement(s->GetSequence().stmt, depth);
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
} else {
|
||||
std::cout << " ";
|
||||
}
|
||||
PrintStatement(s->u.sequence.next, depth - 1);
|
||||
PrintStatement(s->GetSequence().next, depth - 1);
|
||||
break;
|
||||
case StatementKind::Block:
|
||||
std::cout << "{";
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
PrintStatement(s->u.block.stmt, depth);
|
||||
PrintStatement(s->GetBlock().stmt, depth);
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
@@ -232,19 +294,19 @@ void PrintStatement(const Statement* s, int depth) {
|
||||
}
|
||||
break;
|
||||
case StatementKind::Continuation:
|
||||
std::cout << "continuation " << *s->u.continuation.continuation_variable
|
||||
<< " ";
|
||||
std::cout << "continuation "
|
||||
<< *s->GetContinuation().continuation_variable << " ";
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
PrintStatement(s->u.continuation.body, depth - 1);
|
||||
PrintStatement(s->GetContinuation().body, depth - 1);
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
break;
|
||||
case StatementKind::Run:
|
||||
std::cout << "run ";
|
||||
PrintExp(s->u.run.argument);
|
||||
PrintExp(s->GetRun().argument);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::Await:
|
||||
|
||||
Reference in New Issue
Block a user