mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-30 22:02:41 +01:00
This reverts commit bf6bb800c4.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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:
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user