Skip to content

Commit

Permalink
Revert "improve abstraction for AssocList, fix bug in optional else (#…
Browse files Browse the repository at this point in the history
…315)" (#320)

This reverts commit e77dfa6.
  • Loading branch information
Dave Abrahams authored Mar 1, 2021
1 parent 171448d commit edf389d
Show file tree
Hide file tree
Showing 55 changed files with 7,430 additions and 292 deletions.
6 changes: 5 additions & 1 deletion executable_semantics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ EXAMPLES = [
"fun6_fail_type",
"funptr1",
"if1",
"if2",
# (Temporarily disabled pending
# https://github.com/carbon-language/carbon-lang/issues/311)
# "if2",
"if3",
"match_int_default",
"match_int",
Expand All @@ -96,6 +98,8 @@ EXAMPLES = [
"tuple_match",
"tuple1",
"tuple2",
"undef1",
"undef2",
"while1",
"zero",
]
Expand Down
5 changes: 3 additions & 2 deletions executable_semantics/ast/declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ void StructDeclaration::Print() const {
void ChoiceDeclaration::Print() const {
std::cout << "choice " << name << " {" << std::endl;
for (auto& alternative : alternatives) {
std::cout << "alt " << alternative.first << " " << *alternative.second
<< ";" << std::endl;
std::cout << "alt " << alternative.first << " ";
PrintExp(alternative.second);
std::cout << ";" << std::endl;
}
std::cout << "}" << std::endl;
}
Expand Down
30 changes: 14 additions & 16 deletions executable_semantics/ast/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
namespace Carbon {

struct Value;

template <class K, class V>
class AssocList;

struct 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 {
Expand All @@ -40,10 +38,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);
}
Expand All @@ -62,9 +60,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;
};

Expand All @@ -77,10 +75,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 {
Expand All @@ -100,8 +98,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;
};

Expand All @@ -112,8 +110,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;
};

Expand All @@ -128,8 +126,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;
};

Expand Down
72 changes: 40 additions & 32 deletions executable_semantics/ast/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,85 +192,93 @@ static void PrintFields(
if (i != 0) {
std::cout << ", ";
}
std::cout << iter->first << " = " << *iter->second;
std::cout << iter->first << " = ";
PrintExp(iter->second);
}
}

void PrintExp(const Expression* e, std::ostream& out) {
void PrintExp(const Expression* e) {
switch (e->tag) {
case ExpressionKind::Index:
out << *e->u.index.aggregate << "[" << *e->u.index.offset << "]";
PrintExp(e->u.index.aggregate);
std::cout << "[";
PrintExp(e->u.index.offset);
std::cout << "]";
break;
case ExpressionKind::GetField:
out << *e->u.get_field.aggregate << "." << *e->u.get_field.field;
PrintExp(e->u.get_field.aggregate);
std::cout << ".";
std::cout << *e->u.get_field.field;
break;
case ExpressionKind::Tuple:
out << "(";
std::cout << "(";
PrintFields(e->u.tuple.fields);
out << ")";
std::cout << ")";
break;
case ExpressionKind::Integer:
out << e->u.integer;
std::cout << e->u.integer;
break;
case ExpressionKind::Boolean:
out << std::boolalpha;
out << e->u.boolean;
std::cout << std::boolalpha;
std::cout << e->u.boolean;
break;
case ExpressionKind::PrimitiveOp:
out << "(";
std::cout << "(";
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);
out << " ";
std::cout << " ";
auto iter = e->u.primitive_op.arguments->begin();
PrintExp(*iter, out);
PrintExp(*iter);
} else if (e->u.primitive_op.arguments->size() == 2) {
auto iter = e->u.primitive_op.arguments->begin();
out << **iter << " ";
PrintExp(*iter);
std::cout << " ";
PrintOp(e->u.primitive_op.op);
out << " ";
std::cout << " ";
++iter;
out << **iter;
PrintExp(*iter);
}
out << ")";
std::cout << ")";
break;
case ExpressionKind::Variable:
out << *e->u.variable.name;
std::cout << *e->u.variable.name;
break;
case ExpressionKind::PatternVariable:
out << *e->u.pattern_variable.type << ": " << *e->u.pattern_variable.name;
PrintExp(e->u.pattern_variable.type);
std::cout << ": ";
std::cout << *e->u.pattern_variable.name;
break;
case ExpressionKind::Call:
out << *e->u.call.function;
PrintExp(e->u.call.function);
if (e->u.call.argument->tag == ExpressionKind::Tuple) {
out << *e->u.call.argument;
PrintExp(e->u.call.argument);
} else {
out << "(" << *e->u.call.argument << ")";
std::cout << "(";
PrintExp(e->u.call.argument);
std::cout << ")";
}
break;
case ExpressionKind::BoolT:
out << "Bool";
std::cout << "Bool";
break;
case ExpressionKind::IntT:
out << "Int";
std::cout << "Int";
break;
case ExpressionKind::TypeT:
out << "Type";
std::cout << "Type";
break;
case ExpressionKind::AutoT:
out << "auto";
std::cout << "auto";
break;
case ExpressionKind::FunctionT:
out << "fn " << *e->u.function_type.parameter << " -> "
<< *e->u.function_type.return_type;
std::cout << "fn ";
PrintExp(e->u.function_type.parameter);
std::cout << " -> ";
PrintExp(e->u.function_type.return_type);
break;
}
}

auto operator<<(std::ostream& out, const Expression& e) -> std::ostream& {
PrintExp(&e, out);
return out;
}

} // namespace Carbon
3 changes: 1 addition & 2 deletions executable_semantics/ast/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ auto MakeFunType(int line_num, Expression* param, Expression* ret)
-> Expression*;
auto MakeAutoType(int line_num) -> Expression*;

void PrintExp(const Expression* exp, std::ostream& out);
auto operator<<(std::ostream& os, const Expression& v) -> std::ostream&;
void PrintExp(const Expression* exp);

} // namespace Carbon

Expand Down
6 changes: 4 additions & 2 deletions executable_semantics/ast/function_definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
}

void PrintFunDefDepth(const FunctionDefinition* f, int depth) {
std::cout << "fn " << f->name << " " << *f->param_pattern << " -> "
<< *f->return_type;
std::cout << "fn " << f->name << " ";
PrintExp(f->param_pattern);
std::cout << " -> ";
PrintExp(f->return_type);
if (f->body) {
std::cout << " {" << std::endl;
PrintStatement(f->body, depth);
Expand Down
5 changes: 3 additions & 2 deletions executable_semantics/ast/member.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ 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 << " : " << *m->u.field.type
<< ";" << std::endl;
std::cout << "var " << *m->u.field.name << " : ";
PrintExp(m->u.field.type);
std::cout << ";" << std::endl;
break;
}
}
Expand Down
35 changes: 26 additions & 9 deletions executable_semantics/ast/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,15 @@ void PrintStatement(Statement* s, int depth) {
}
switch (s->tag) {
case StatementKind::Match:
std::cout << "match (" << *s->u.match_stmt.exp << ") {";
std::cout << "match (";
PrintExp(s->u.match_stmt.exp);
std::cout << ") {";
if (depth < 0 || depth > 1) {
std::cout << std::endl;
for (auto& clause : *s->u.match_stmt.clauses) {
std::cout << "case " << *clause.first << " =>" << std::endl;
std::cout << "case ";
PrintExp(clause.first);
std::cout << " =>" << std::endl;
PrintStatement(clause.second, depth - 1);
std::cout << std::endl;
}
Expand All @@ -129,7 +133,9 @@ void PrintStatement(Statement* s, int depth) {
std::cout << "}";
break;
case StatementKind::While:
std::cout << "while (" << *s->u.while_stmt.cond << ")" << std::endl;
std::cout << "while (";
PrintExp(s->u.while_stmt.cond);
std::cout << ")" << std::endl;
PrintStatement(s->u.while_stmt.body, depth - 1);
break;
case StatementKind::Break:
Expand All @@ -139,23 +145,34 @@ void PrintStatement(Statement* s, int depth) {
std::cout << "continue;";
break;
case StatementKind::VariableDefinition:
std::cout << "var " << *s->u.variable_definition.pat << " = "
<< *s->u.variable_definition.init << ";";
std::cout << "var ";
PrintExp(s->u.variable_definition.pat);
std::cout << " = ";
PrintExp(s->u.variable_definition.init);
std::cout << ";";
break;
case StatementKind::ExpressionStatement:
std::cout << *s->u.exp << ";";
PrintExp(s->u.exp);
std::cout << ";";
break;
case StatementKind::Assign:
std::cout << *s->u.assign.lhs << " = " << *s->u.assign.rhs << ";";
PrintExp(s->u.assign.lhs);
std::cout << " = ";
PrintExp(s->u.assign.rhs);
std::cout << ";";
break;
case StatementKind::If:
std::cout << "if (" << *s->u.if_stmt.cond << ")" << std::endl;
std::cout << "if (";
PrintExp(s->u.if_stmt.cond);
std::cout << ")" << 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 " << *s->u.return_stmt << ";";
std::cout << "return ";
PrintExp(s->u.return_stmt);
std::cout << ";";
break;
case StatementKind::Sequence:
PrintStatement(s->u.sequence.stmt, depth);
Expand Down
4 changes: 2 additions & 2 deletions executable_semantics/interpreter/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ namespace Carbon {
void PrintAct(Action* act, std::ostream& out) {
switch (act->tag) {
case ActionKind::DeleteTmpAction:
out << "delete_tmp(" << act->u.delete_tmp << ")";
std::cout << "delete_tmp(" << act->u.delete_tmp << ")";
break;
case ActionKind::ExpToLValAction:
out << "exp=>lval";
break;
case ActionKind::LValAction:
case ActionKind::ExpressionAction:
out << *act->u.exp;
PrintExp(act->u.exp);
break;
case ActionKind::StatementAction:
PrintStatement(act->u.stmt, 1);
Expand Down
2 changes: 1 addition & 1 deletion executable_semantics/interpreter/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/statement.h"
#include "executable_semantics/interpreter/stack.h"
#include "executable_semantics/interpreter/cons_list.h"
#include "executable_semantics/interpreter/value.h"

namespace Carbon {
Expand Down
Loading

0 comments on commit edf389d

Please sign in to comment.