diff --git a/executable_semantics/BUILD b/executable_semantics/BUILD index c69935958890..dc6e9a1351d9 100644 --- a/executable_semantics/BUILD +++ b/executable_semantics/BUILD @@ -8,7 +8,6 @@ cc_binary( name = "executable_semantics", srcs = ["main.cpp"], deps = [ - "//executable_semantics/common:tracing_flag", "//executable_semantics/interpreter:exec_program", "//executable_semantics/syntax", "@llvm-project//llvm:Support", diff --git a/executable_semantics/common/BUILD b/executable_semantics/common/BUILD index be405b9e2508..e17d835e8634 100644 --- a/executable_semantics/common/BUILD +++ b/executable_semantics/common/BUILD @@ -38,10 +38,3 @@ cc_library( "@llvm-project//llvm:Support", ], ) - -cc_library( - name = "tracing_flag", - srcs = ["tracing_flag.cpp"], - hdrs = ["tracing_flag.h"], - visibility = ["//executable_semantics:__subpackages__"], -) diff --git a/executable_semantics/common/tracing_flag.cpp b/executable_semantics/common/tracing_flag.cpp deleted file mode 100644 index 11200771ba75..000000000000 --- a/executable_semantics/common/tracing_flag.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Part of the Carbon Language project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include "executable_semantics/common/tracing_flag.h" - -namespace Carbon { - -bool tracing_output = false; - -} // namespace Carbon diff --git a/executable_semantics/common/tracing_flag.h b/executable_semantics/common/tracing_flag.h deleted file mode 100644 index a22ae9f91c25..000000000000 --- a/executable_semantics/common/tracing_flag.h +++ /dev/null @@ -1,15 +0,0 @@ -// Part of the Carbon Language project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#ifndef EXECUTABLE_SEMANTICS_COMMON_TRACING_FLAG_H_ -#define EXECUTABLE_SEMANTICS_COMMON_TRACING_FLAG_H_ - -namespace Carbon { - -// Program option to enable/disable tracing. -extern bool tracing_output; - -} // namespace Carbon - -#endif // EXECUTABLE_SEMANTICS_COMMON_TRACING_FLAG_H_ diff --git a/executable_semantics/interpreter/BUILD b/executable_semantics/interpreter/BUILD index 022df3bddd03..651bd049a676 100644 --- a/executable_semantics/interpreter/BUILD +++ b/executable_semantics/interpreter/BUILD @@ -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", ], ) diff --git a/executable_semantics/interpreter/exec_program.cpp b/executable_semantics/interpreter/exec_program.cpp index ffa9815a94c7..7d9d2f0decce 100644 --- a/executable_semantics/interpreter/exec_program.cpp +++ b/executable_semantics/interpreter/exec_program.cpp @@ -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, declarations->insert(declarations->begin(), print); } -void ExecProgram(Nonnull arena, AST ast) { +void ExecProgram(Nonnull 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, AST ast) { Nonnull call_main = arena->New( source_loc, arena->New(source_loc, "main"), arena->New(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"; } diff --git a/executable_semantics/interpreter/exec_program.h b/executable_semantics/interpreter/exec_program.h index 227f438aae8f..1731b9a89389 100644 --- a/executable_semantics/interpreter/exec_program.h +++ b/executable_semantics/interpreter/exec_program.h @@ -14,7 +14,7 @@ namespace Carbon { // Runs the top-level declaration list. -void ExecProgram(Nonnull arena, AST ast); +void ExecProgram(Nonnull arena, AST ast, bool trace); } // namespace Carbon diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 9cb5ebdb1390..e14a055d64b9 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -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 pat, auto Interpreter::StepLvalue() -> Transition { Nonnull act = stack.Top()->todo.Top(); const Expression& exp = cast(*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 act = stack.Top()->todo.Top(); const Expression& exp = cast(*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 act = stack.Top()->todo.Top(); const Pattern& pattern = cast(*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 = stack.Top(); Nonnull act = frame->todo.Top(); const Statement& stmt = cast(*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> 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> fs, auto scopes = Stack>(arena->New(globals)); stack = Stack>(arena->New("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()); } } diff --git a/executable_semantics/interpreter/interpreter.h b/executable_semantics/interpreter/interpreter.h index eb1b8fd1e1c3..8a4bd13b4ba5 100644 --- a/executable_semantics/interpreter/interpreter.h +++ b/executable_semantics/interpreter/interpreter.h @@ -25,8 +25,8 @@ using Env = Dictionary; class Interpreter { public: - explicit Interpreter(Nonnull arena) - : arena(arena), globals(arena), heap(arena) {} + explicit Interpreter(Nonnull arena, bool trace) + : arena(arena), globals(arena), heap(arena), trace_(trace) {} // Interpret the whole program. auto InterpProgram(llvm::ArrayRef> fs, @@ -160,6 +160,8 @@ class Interpreter { Stack> stack; Heap heap; std::optional> program_value; + + bool trace_; }; } // namespace Carbon diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index 99bc72f4f4e6..75db6d393f96 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -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 type) auto TypeChecker::TypeCheckExp(Nonnull 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 e, TypeEnv types, auto TypeChecker::TypeCheckPattern( Nonnull p, TypeEnv types, Env values, std::optional> expected) -> TCResult { - if (tracing_output) { + if (trace_) { llvm::outs() << "checking pattern " << *p; if (expected) { llvm::outs() << ", expecting " << **expected; diff --git a/executable_semantics/interpreter/type_checker.h b/executable_semantics/interpreter/type_checker.h index 4dc95f7f7419..4a8f69e1dacf 100644 --- a/executable_semantics/interpreter/type_checker.h +++ b/executable_semantics/interpreter/type_checker.h @@ -20,8 +20,8 @@ using TypeEnv = Dictionary>; class TypeChecker { public: - explicit TypeChecker(Nonnull arena) - : arena(arena), interpreter(arena) {} + explicit TypeChecker(Nonnull arena, bool trace) + : arena(arena), interpreter(arena, trace), trace_(trace) {} struct TypeCheckContext { explicit TypeCheckContext(Nonnull arena) @@ -139,6 +139,8 @@ class TypeChecker { Nonnull arena; Interpreter interpreter; + + bool trace_; }; } // namespace Carbon diff --git a/executable_semantics/main.cpp b/executable_semantics/main.cpp index 4810ceddadc7..de77b5044dda 100644 --- a/executable_semantics/main.cpp +++ b/executable_semantics/main.cpp @@ -6,7 +6,6 @@ #include #include -#include "executable_semantics/common/tracing_flag.h" #include "executable_semantics/interpreter/exec_program.h" #include "executable_semantics/syntax/parse.h" #include "llvm/Support/CommandLine.h" @@ -30,13 +29,10 @@ int main(int argc, char* argv[]) { llvm::cl::Required); llvm::cl::ParseCommandLineOptions(argc, argv); - if (trace_option) { - Carbon::tracing_output = true; - } Carbon::Arena arena; std::variant ast_or_error = - Carbon::Parse(&arena, input_file_name); + Carbon::Parse(&arena, input_file_name, trace_option); if (auto* error = std::get_if(&ast_or_error)) { // Diagnostic already reported to std::cerr; this is just a return code. @@ -44,5 +40,6 @@ int main(int argc, char* argv[]) { } // Typecheck and run the parsed program. - Carbon::ExecProgram(&arena, std::get(ast_or_error)); + Carbon::ExecProgram(&arena, std::get(ast_or_error), + trace_option); } diff --git a/executable_semantics/syntax/BUILD b/executable_semantics/syntax/BUILD index adb7a63af58d..e0f654c0eac5 100644 --- a/executable_semantics/syntax/BUILD +++ b/executable_semantics/syntax/BUILD @@ -43,7 +43,6 @@ cc_library( "//executable_semantics/ast:paren_contents", "//executable_semantics/common:arena", "//executable_semantics/common:error", - "//executable_semantics/common:tracing_flag", ], ) diff --git a/executable_semantics/syntax/lexer.lpp b/executable_semantics/syntax/lexer.lpp index cf4f34665ce7..6fc9e7bfd142 100644 --- a/executable_semantics/syntax/lexer.lpp +++ b/executable_semantics/syntax/lexer.lpp @@ -9,7 +9,6 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include "common/check.h" #include "common/string_helpers.h" - #include "executable_semantics/common/tracing_flag.h" #include "executable_semantics/syntax/parse_and_lex_context.h" #include "executable_semantics/syntax/parser.h" #include "llvm/ADT/StringExtras.h" @@ -246,8 +245,8 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" CHECK(str.consume_front("\"") && str.consume_back("\"")); std::optional unescaped = Carbon::UnescapeStringLiteral(str); if (unescaped == std::nullopt) { - if (Carbon::tracing_output) { - // Print a newline because tracing prints an incomplete line + if (context.trace()) { + // Print a newline because trace prints an incomplete line // "Reading a token: ". llvm::errs() << "\n"; } @@ -279,8 +278,8 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" } . { - if (Carbon::tracing_output) { - // Print a newline because tracing prints an incomplete line + if (context.trace()) { + // Print a newline because trace prints an incomplete line // "Reading a token: ". llvm::errs() << "\n"; } diff --git a/executable_semantics/syntax/parse.cpp b/executable_semantics/syntax/parse.cpp index bc53ab819e4d..6d6a4a8e67de 100644 --- a/executable_semantics/syntax/parse.cpp +++ b/executable_semantics/syntax/parse.cpp @@ -6,15 +6,14 @@ #include "common/check.h" #include "executable_semantics/common/error.h" -#include "executable_semantics/common/tracing_flag.h" #include "executable_semantics/syntax/lexer.h" #include "executable_semantics/syntax/parse_and_lex_context.h" #include "executable_semantics/syntax/parser.h" namespace Carbon { -auto Parse(Nonnull arena, const std::string& input_file_name) - -> std::variant { +auto Parse(Nonnull arena, const std::string& input_file_name, + bool trace) -> std::variant { FILE* input_file = fopen(input_file_name.c_str(), "r"); if (input_file == nullptr) { FATAL_PROGRAM_ERROR_NO_LINE() << "Error opening '" << input_file_name @@ -28,11 +27,11 @@ auto Parse(Nonnull arena, const std::string& input_file_name) // Prepare other parser arguments. std::optional ast = std::nullopt; - ParseAndLexContext context(arena->New(input_file_name)); + ParseAndLexContext context(arena->New(input_file_name), trace); // Do the parse. auto parser = Parser(arena, scanner, context, &ast); - if (tracing_output) { + if (trace) { parser.set_debug_level(1); } auto syntax_error_code = parser(); diff --git a/executable_semantics/syntax/parse.h b/executable_semantics/syntax/parse.h index 2e4ebc27226d..4669f6043859 100644 --- a/executable_semantics/syntax/parse.h +++ b/executable_semantics/syntax/parse.h @@ -18,8 +18,8 @@ using SyntaxErrorCode = int; // Returns the AST representing the contents of the named file, or an error code // if parsing fails. Allocations go into the provided arena. -auto Parse(Nonnull arena, const std::string& input_file_name) - -> std::variant; +auto Parse(Nonnull arena, const std::string& input_file_name, + bool trace) -> std::variant; } // namespace Carbon diff --git a/executable_semantics/syntax/parse_and_lex_context.h b/executable_semantics/syntax/parse_and_lex_context.h index 7df72a2bd579..397d09a46d58 100644 --- a/executable_semantics/syntax/parse_and_lex_context.h +++ b/executable_semantics/syntax/parse_and_lex_context.h @@ -17,8 +17,8 @@ namespace Carbon { class ParseAndLexContext { public: // Creates an instance analyzing the given input file. - ParseAndLexContext(Nonnull input_file_name) - : input_file_name(input_file_name) {} + ParseAndLexContext(Nonnull input_file_name, bool trace) + : input_file_name(input_file_name), trace_(trace) {} // Writes a syntax error diagnostic containing message to standard error. auto PrintDiagnostic(const std::string& message) -> void; @@ -28,6 +28,8 @@ class ParseAndLexContext { static_cast(current_token_position.begin.line)); } + auto trace() -> bool { return trace_; } + // The source range of the token being (or just) lex'd. location current_token_position; @@ -35,6 +37,8 @@ class ParseAndLexContext { // A path to the file processed, relative to the current working directory // when *this is called. Nonnull input_file_name; + + bool trace_; }; } // namespace Carbon