mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Pass the flag instead of using a global. (#893)
Arguably missed in #769 Note, this is reminding me we have more class members to rename for `_`, but I felt it's best to use the new naming instead of adding more to clean up.
This commit is contained in:
@@ -98,7 +98,6 @@ cc_library(
|
||||
"//executable_semantics/ast:declaration",
|
||||
"//executable_semantics/ast:expression",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:tracing_flag",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
@@ -121,7 +120,6 @@ cc_library(
|
||||
"//executable_semantics/ast:expression",
|
||||
"//executable_semantics/ast:statement",
|
||||
"//executable_semantics/common:arena",
|
||||
"//executable_semantics/common:tracing_flag",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "common/check.h"
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
#include "executable_semantics/interpreter/type_checker.h"
|
||||
|
||||
@@ -34,23 +33,23 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
|
||||
declarations->insert(declarations->begin(), print);
|
||||
}
|
||||
|
||||
void ExecProgram(Nonnull<Arena*> arena, AST ast) {
|
||||
void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace) {
|
||||
AddIntrinsics(arena, &ast.declarations);
|
||||
if (tracing_output) {
|
||||
if (trace) {
|
||||
llvm::outs() << "********** source program **********\n";
|
||||
for (const auto decl : ast.declarations) {
|
||||
llvm::outs() << *decl;
|
||||
}
|
||||
llvm::outs() << "********** type checking **********\n";
|
||||
}
|
||||
TypeChecker type_checker(arena);
|
||||
TypeChecker type_checker(arena, trace);
|
||||
TypeChecker::TypeCheckContext p = type_checker.TopLevel(&ast.declarations);
|
||||
TypeEnv top = p.types;
|
||||
Env ct_top = p.values;
|
||||
for (const auto decl : ast.declarations) {
|
||||
type_checker.TypeCheck(decl, top, ct_top);
|
||||
}
|
||||
if (tracing_output) {
|
||||
if (trace) {
|
||||
llvm::outs() << "\n";
|
||||
llvm::outs() << "********** type checking complete **********\n";
|
||||
for (const auto decl : ast.declarations) {
|
||||
@@ -63,7 +62,8 @@ void ExecProgram(Nonnull<Arena*> arena, AST ast) {
|
||||
Nonnull<Expression*> call_main = arena->New<CallExpression>(
|
||||
source_loc, arena->New<IdentifierExpression>(source_loc, "main"),
|
||||
arena->New<TupleLiteral>(source_loc));
|
||||
int result = Interpreter(arena).InterpProgram(ast.declarations, call_main);
|
||||
int result =
|
||||
Interpreter(arena, trace).InterpProgram(ast.declarations, call_main);
|
||||
llvm::outs() << "result: " << result << "\n";
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
namespace Carbon {
|
||||
|
||||
// Runs the top-level declaration list.
|
||||
void ExecProgram(Nonnull<Arena*> arena, AST ast);
|
||||
void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
#include "executable_semantics/interpreter/action.h"
|
||||
#include "executable_semantics/interpreter/frame.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
@@ -377,7 +376,7 @@ void Interpreter::PatternAssignment(Nonnull<const Value*> pat,
|
||||
auto Interpreter::StepLvalue() -> Transition {
|
||||
Nonnull<Action*> act = stack.Top()->todo.Top();
|
||||
const Expression& exp = cast<LValAction>(*act).expression();
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "--- step lvalue " << exp << " (" << exp.source_loc()
|
||||
<< ") --->\n";
|
||||
}
|
||||
@@ -460,7 +459,7 @@ auto Interpreter::StepLvalue() -> Transition {
|
||||
auto Interpreter::StepExp() -> Transition {
|
||||
Nonnull<Action*> act = stack.Top()->todo.Top();
|
||||
const Expression& exp = cast<ExpressionAction>(*act).expression();
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "--- step exp " << exp << " (" << exp.source_loc()
|
||||
<< ") --->\n";
|
||||
}
|
||||
@@ -658,7 +657,7 @@ auto Interpreter::StepExp() -> Transition {
|
||||
auto Interpreter::StepPattern() -> Transition {
|
||||
Nonnull<Action*> act = stack.Top()->todo.Top();
|
||||
const Pattern& pattern = cast<PatternAction>(*act).pattern();
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "--- step pattern " << pattern << " ("
|
||||
<< pattern.source_loc() << ") --->\n";
|
||||
}
|
||||
@@ -741,7 +740,7 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
Nonnull<Frame*> frame = stack.Top();
|
||||
Nonnull<Action*> act = frame->todo.Top();
|
||||
const Statement& stmt = cast<StatementAction>(*act).statement();
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "--- step stmt ";
|
||||
stmt.PrintDepth(1, llvm::outs());
|
||||
llvm::outs() << " (" << stmt.source_loc() << ") --->\n";
|
||||
@@ -1136,7 +1135,7 @@ auto Interpreter::InterpProgram(llvm::ArrayRef<Nonnull<Declaration*>> fs,
|
||||
CHECK(stack.IsEmpty());
|
||||
CHECK(program_value == std::nullopt);
|
||||
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "********** initializing globals **********\n";
|
||||
}
|
||||
InitGlobals(fs);
|
||||
@@ -1145,14 +1144,14 @@ auto Interpreter::InterpProgram(llvm::ArrayRef<Nonnull<Declaration*>> fs,
|
||||
auto scopes = Stack<Nonnull<Scope*>>(arena->New<Scope>(globals));
|
||||
stack = Stack<Nonnull<Frame*>>(arena->New<Frame>("top", scopes, todo));
|
||||
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "********** calling main function **********\n";
|
||||
PrintState(llvm::outs());
|
||||
}
|
||||
|
||||
while (stack.Count() > 1 || !stack.Top()->todo.IsEmpty()) {
|
||||
Step();
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
PrintState(llvm::outs());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ using Env = Dictionary<std::string, Address>;
|
||||
|
||||
class Interpreter {
|
||||
public:
|
||||
explicit Interpreter(Nonnull<Arena*> arena)
|
||||
: arena(arena), globals(arena), heap(arena) {}
|
||||
explicit Interpreter(Nonnull<Arena*> arena, bool trace)
|
||||
: arena(arena), globals(arena), heap(arena), trace_(trace) {}
|
||||
|
||||
// Interpret the whole program.
|
||||
auto InterpProgram(llvm::ArrayRef<Nonnull<Declaration*>> fs,
|
||||
@@ -160,6 +160,8 @@ class Interpreter {
|
||||
Stack<Nonnull<Frame*>> stack;
|
||||
Heap heap;
|
||||
std::optional<Nonnull<const Value*>> program_value;
|
||||
|
||||
bool trace_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "executable_semantics/common/tracing_flag.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
#include "executable_semantics/interpreter/value.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
@@ -418,7 +417,7 @@ auto TypeChecker::Substitute(TypeEnv dict, Nonnull<const Value*> type)
|
||||
|
||||
auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
Env values) -> TCResult {
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "checking expression " << *e << "\ntypes: ";
|
||||
PrintTypeEnv(types, llvm::outs());
|
||||
llvm::outs() << "\nvalues: ";
|
||||
@@ -708,7 +707,7 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
auto TypeChecker::TypeCheckPattern(
|
||||
Nonnull<Pattern*> p, TypeEnv types, Env values,
|
||||
std::optional<Nonnull<const Value*>> expected) -> TCResult {
|
||||
if (tracing_output) {
|
||||
if (trace_) {
|
||||
llvm::outs() << "checking pattern " << *p;
|
||||
if (expected) {
|
||||
llvm::outs() << ", expecting " << **expected;
|
||||
|
||||
@@ -20,8 +20,8 @@ using TypeEnv = Dictionary<std::string, Nonnull<const Value*>>;
|
||||
|
||||
class TypeChecker {
|
||||
public:
|
||||
explicit TypeChecker(Nonnull<Arena*> arena)
|
||||
: arena(arena), interpreter(arena) {}
|
||||
explicit TypeChecker(Nonnull<Arena*> arena, bool trace)
|
||||
: arena(arena), interpreter(arena, trace), trace_(trace) {}
|
||||
|
||||
struct TypeCheckContext {
|
||||
explicit TypeCheckContext(Nonnull<Arena*> arena)
|
||||
@@ -139,6 +139,8 @@ class TypeChecker {
|
||||
|
||||
Nonnull<Arena*> arena;
|
||||
Interpreter interpreter;
|
||||
|
||||
bool trace_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
Reference in New Issue
Block a user