improve abstraction for AssocList, fix bug in optional else (#315)

* improve abstraction for AssocList, fix bug in optional else

* add flag for tracing output, clean up code for output

* turned off tracing by default, updated goldens, removed two examples that use pointers, shouldn't have been there yet
This commit is contained in:
Jeremy G. Siek
2021-03-01 10:00:02 -05:00
committed by GitHub
parent 102ea3ccaf
commit bf6bb800c4
55 changed files with 293 additions and 7431 deletions
+2 -3
View File
@@ -21,9 +21,8 @@ void StructDeclaration::Print() const {
void ChoiceDeclaration::Print() const {
std::cout << "choice " << name << " {" << std::endl;
for (auto& alternative : alternatives) {
std::cout << "alt " << alternative.first << " ";
PrintExp(alternative.second);
std::cout << ";" << std::endl;
std::cout << "alt " << alternative.first << " " << *alternative.second
<< ";" << std::endl;
}
std::cout << "}" << std::endl;
}
+16 -14
View File
@@ -15,14 +15,16 @@
namespace Carbon {
struct Value;
template <class K, class V>
struct AssocList;
class AssocList;
using Address = unsigned int;
using TypeEnv = AssocList<std::string, Value*>;
using Env = AssocList<std::string, Address>;
/// TODO:explain this. Also name it if necessary. Consult with jsiek.
using ExecutionEnvironment = std::pair<TypeEnv*, Env*>;
using ExecutionEnvironment = std::pair<TypeEnv, Env>;
/// An existential AST declaration satisfying the Declaration concept.
class Declaration {
@@ -38,10 +40,10 @@ class Declaration {
public: // Declaration concept API, in addition to ValueSemantic.
void Print() const { box->Print(); }
auto Name() const -> std::string { return box->Name(); }
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> Declaration {
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration {
return box->TypeChecked(env, ct_env);
}
void InitGlobals(Env*& globals) const { return box->InitGlobals(globals); }
void InitGlobals(Env& globals) const { return box->InitGlobals(globals); }
auto TopLevel(ExecutionEnvironment& e) const -> void {
return box->TopLevel(e);
}
@@ -60,9 +62,9 @@ class Declaration {
virtual ~Box() {}
virtual auto Print() const -> void = 0;
virtual auto Name() const -> std::string = 0;
virtual auto TypeChecked(TypeEnv* env, Env* ct_env) const
virtual auto TypeChecked(TypeEnv env, Env ct_env) const
-> Declaration = 0;
virtual auto InitGlobals(Env*& globals) const -> void = 0;
virtual auto InitGlobals(Env& globals) const -> void = 0;
virtual auto TopLevel(ExecutionEnvironment&) const -> void = 0;
};
@@ -75,10 +77,10 @@ class Declaration {
auto Print() const -> void override { return content.Print(); }
auto Name() const -> std::string override { return content.Name(); }
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> Declaration override {
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration override {
return content.TypeChecked(env, ct_env);
}
auto InitGlobals(Env*& globals) const -> void override {
auto InitGlobals(Env& globals) const -> void override {
content.InitGlobals(globals);
}
auto TopLevel(ExecutionEnvironment& e) const -> void override {
@@ -98,8 +100,8 @@ struct FunctionDeclaration {
auto Print() const -> void;
auto Name() const -> std::string;
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> Declaration;
auto InitGlobals(Env*& globals) const -> void;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
auto InitGlobals(Env& globals) const -> void;
auto TopLevel(ExecutionEnvironment&) const -> void;
};
@@ -110,8 +112,8 @@ struct StructDeclaration {
void Print() const;
auto Name() const -> std::string;
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> Declaration;
void InitGlobals(Env*& globals) const;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
void InitGlobals(Env& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
};
@@ -126,8 +128,8 @@ struct ChoiceDeclaration {
void Print() const;
auto Name() const -> std::string;
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> Declaration;
void InitGlobals(Env*& globals) const;
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
void InitGlobals(Env& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
};
+32 -40
View File
@@ -192,93 +192,85 @@ static void PrintFields(
if (i != 0) {
std::cout << ", ";
}
std::cout << iter->first << " = ";
PrintExp(iter->second);
std::cout << iter->first << " = " << *iter->second;
}
}
void PrintExp(const Expression* e) {
void PrintExp(const Expression* e, std::ostream& out) {
switch (e->tag) {
case ExpressionKind::Index:
PrintExp(e->u.index.aggregate);
std::cout << "[";
PrintExp(e->u.index.offset);
std::cout << "]";
out << *e->u.index.aggregate << "[" << *e->u.index.offset << "]";
break;
case ExpressionKind::GetField:
PrintExp(e->u.get_field.aggregate);
std::cout << ".";
std::cout << *e->u.get_field.field;
out << *e->u.get_field.aggregate << "." << *e->u.get_field.field;
break;
case ExpressionKind::Tuple:
std::cout << "(";
out << "(";
PrintFields(e->u.tuple.fields);
std::cout << ")";
out << ")";
break;
case ExpressionKind::Integer:
std::cout << e->u.integer;
out << e->u.integer;
break;
case ExpressionKind::Boolean:
std::cout << std::boolalpha;
std::cout << e->u.boolean;
out << std::boolalpha;
out << e->u.boolean;
break;
case ExpressionKind::PrimitiveOp:
std::cout << "(";
out << "(";
if (e->u.primitive_op.arguments->size() == 0) {
PrintOp(e->u.primitive_op.op);
} else if (e->u.primitive_op.arguments->size() == 1) {
PrintOp(e->u.primitive_op.op);
std::cout << " ";
out << " ";
auto iter = e->u.primitive_op.arguments->begin();
PrintExp(*iter);
PrintExp(*iter, out);
} else if (e->u.primitive_op.arguments->size() == 2) {
auto iter = e->u.primitive_op.arguments->begin();
PrintExp(*iter);
std::cout << " ";
out << **iter << " ";
PrintOp(e->u.primitive_op.op);
std::cout << " ";
out << " ";
++iter;
PrintExp(*iter);
out << **iter;
}
std::cout << ")";
out << ")";
break;
case ExpressionKind::Variable:
std::cout << *e->u.variable.name;
out << *e->u.variable.name;
break;
case ExpressionKind::PatternVariable:
PrintExp(e->u.pattern_variable.type);
std::cout << ": ";
std::cout << *e->u.pattern_variable.name;
out << *e->u.pattern_variable.type << ": " << *e->u.pattern_variable.name;
break;
case ExpressionKind::Call:
PrintExp(e->u.call.function);
out << *e->u.call.function;
if (e->u.call.argument->tag == ExpressionKind::Tuple) {
PrintExp(e->u.call.argument);
out << *e->u.call.argument;
} else {
std::cout << "(";
PrintExp(e->u.call.argument);
std::cout << ")";
out << "(" << *e->u.call.argument << ")";
}
break;
case ExpressionKind::BoolT:
std::cout << "Bool";
out << "Bool";
break;
case ExpressionKind::IntT:
std::cout << "Int";
out << "Int";
break;
case ExpressionKind::TypeT:
std::cout << "Type";
out << "Type";
break;
case ExpressionKind::AutoT:
std::cout << "auto";
out << "auto";
break;
case ExpressionKind::FunctionT:
std::cout << "fn ";
PrintExp(e->u.function_type.parameter);
std::cout << " -> ";
PrintExp(e->u.function_type.return_type);
out << "fn " << *e->u.function_type.parameter << " -> "
<< *e->u.function_type.return_type;
break;
}
}
auto operator<<(std::ostream& out, const Expression& e) -> std::ostream& {
PrintExp(&e, out);
return out;
}
} // namespace Carbon
+2 -1
View File
@@ -109,7 +109,8 @@ auto MakeFunType(int line_num, Expression* param, Expression* ret)
-> Expression*;
auto MakeAutoType(int line_num) -> Expression*;
void PrintExp(const Expression* exp);
void PrintExp(const Expression* exp, std::ostream& out);
auto operator<<(std::ostream& os, const Expression& v) -> std::ostream&;
} // namespace Carbon
@@ -21,10 +21,8 @@ auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
}
void PrintFunDefDepth(const FunctionDefinition* f, int depth) {
std::cout << "fn " << f->name << " ";
PrintExp(f->param_pattern);
std::cout << " -> ";
PrintExp(f->return_type);
std::cout << "fn " << f->name << " " << *f->param_pattern << " -> "
<< *f->return_type;
if (f->body) {
std::cout << " {" << std::endl;
PrintStatement(f->body, depth);
+2 -3
View File
@@ -20,9 +20,8 @@ auto MakeField(int line_num, std::string name, Expression* type) -> Member* {
void PrintMember(Member* m) {
switch (m->tag) {
case MemberKind::FieldMember:
std::cout << "var " << *m->u.field.name << " : ";
PrintExp(m->u.field.type);
std::cout << ";" << std::endl;
std::cout << "var " << *m->u.field.name << " : " << *m->u.field.type
<< ";" << std::endl;
break;
}
}
+9 -26
View File
@@ -115,15 +115,11 @@ void PrintStatement(Statement* s, int depth) {
}
switch (s->tag) {
case StatementKind::Match:
std::cout << "match (";
PrintExp(s->u.match_stmt.exp);
std::cout << ") {";
std::cout << "match (" << *s->u.match_stmt.exp << ") {";
if (depth < 0 || depth > 1) {
std::cout << std::endl;
for (auto& clause : *s->u.match_stmt.clauses) {
std::cout << "case ";
PrintExp(clause.first);
std::cout << " =>" << std::endl;
std::cout << "case " << *clause.first << " =>" << std::endl;
PrintStatement(clause.second, depth - 1);
std::cout << std::endl;
}
@@ -133,9 +129,7 @@ void PrintStatement(Statement* s, int depth) {
std::cout << "}";
break;
case StatementKind::While:
std::cout << "while (";
PrintExp(s->u.while_stmt.cond);
std::cout << ")" << std::endl;
std::cout << "while (" << *s->u.while_stmt.cond << ")" << std::endl;
PrintStatement(s->u.while_stmt.body, depth - 1);
break;
case StatementKind::Break:
@@ -145,34 +139,23 @@ void PrintStatement(Statement* s, int depth) {
std::cout << "continue;";
break;
case StatementKind::VariableDefinition:
std::cout << "var ";
PrintExp(s->u.variable_definition.pat);
std::cout << " = ";
PrintExp(s->u.variable_definition.init);
std::cout << ";";
std::cout << "var " << *s->u.variable_definition.pat << " = "
<< *s->u.variable_definition.init << ";";
break;
case StatementKind::ExpressionStatement:
PrintExp(s->u.exp);
std::cout << ";";
std::cout << *s->u.exp << ";";
break;
case StatementKind::Assign:
PrintExp(s->u.assign.lhs);
std::cout << " = ";
PrintExp(s->u.assign.rhs);
std::cout << ";";
std::cout << *s->u.assign.lhs << " = " << *s->u.assign.rhs << ";";
break;
case StatementKind::If:
std::cout << "if (";
PrintExp(s->u.if_stmt.cond);
std::cout << ")" << std::endl;
std::cout << "if (" << *s->u.if_stmt.cond << ")" << std::endl;
PrintStatement(s->u.if_stmt.then_stmt, depth - 1);
std::cout << std::endl << "else" << std::endl;
PrintStatement(s->u.if_stmt.else_stmt, depth - 1);
break;
case StatementKind::Return:
std::cout << "return ";
PrintExp(s->u.return_stmt);
std::cout << ";";
std::cout << "return " << *s->u.return_stmt << ";";
break;
case StatementKind::Sequence:
PrintStatement(s->u.sequence.stmt, depth);