add command-line flag to enable/disable tracing output (#325)

* add command-line flag to enable/disable tracing output

* adding missing exit for pattern variable in wrong context and a test case for it (#324)

* Update executable_semantics/interpreter/interpreter.cpp

comment on separate line as code

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>

* fix interpreter's handling of optional else of if statement (#323)

* Update pattern_variable_fail.golden due to error (#334)

* Use llvm's CommandLine for parsing (#332)

* GitHub testing action (#331)

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>

* add copyright

* Create a Dictionary abstraction over the raw Cons list. (#327)

* Create a Dictionary abstraction over the raw Cons list.

* renamed Cons and some methods of Dictionary, various other cleanup

* Update executable_semantics/tracing_flag.cpp

added namespace comment

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>

* added a comment to cpp file

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
Co-authored-by: Dave Abrahams <dabrahams@google.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Jeremy G. Siek
2021-03-03 16:01:59 -05:00
committed by GitHub
co-authored by Jon Meow Chandler Carruth Dave Abrahams
parent cd18e24176
commit fd96e0e630
7 changed files with 95 additions and 35 deletions
@@ -16,6 +16,7 @@
#include "executable_semantics/ast/function_definition.h"
#include "executable_semantics/interpreter/stack.h"
#include "executable_semantics/interpreter/typecheck.h"
#include "executable_semantics/tracing_flag.h"
namespace Carbon {
@@ -397,11 +398,13 @@ auto ToValue(Expression* value) -> Value* {
// Returns 0 if the value doesn't match the pattern.
auto PatternMatch(Value* p, Value* v, Env* env, std::list<std::string>* vars,
int line_num) -> Env* {
std::cout << "pattern_match(";
PrintValue(p, std::cout);
std::cout << ", ";
PrintValue(v, std::cout);
std::cout << ")" << std::endl;
if (tracing_output) {
std::cout << "pattern_match(";
PrintValue(p, std::cout);
std::cout << ", ";
PrintValue(v, std::cout);
std::cout << ")" << std::endl;
}
switch (p->tag) {
case ValKind::VarPatV: {
Address a = AllocateValue(CopyVal(v, line_num));
@@ -545,9 +548,11 @@ void StepLvalue() {
Frame* frame = state->stack.Top();
Action* act = frame->todo.Top();
Expression* exp = act->u.exp;
std::cout << "--- step lvalue ";
PrintExp(exp);
std::cout << " --->" << std::endl;
if (tracing_output) {
std::cout << "--- step lvalue ";
PrintExp(exp);
std::cout << " --->" << std::endl;
}
switch (exp->tag) {
case ExpressionKind::Variable: {
// { {x :: C, E, F} :: S, H}
@@ -605,9 +610,11 @@ void StepExp() {
Frame* frame = state->stack.Top();
Action* act = frame->todo.Top();
Expression* exp = act->u.exp;
std::cout << "--- step exp ";
PrintExp(exp);
std::cout << " --->" << std::endl;
if (tracing_output) {
std::cout << "--- step exp ";
PrintExp(exp);
std::cout << " --->" << std::endl;
}
switch (exp->tag) {
case ExpressionKind::PatternVariable: {
frame->todo.Push(MakeExpAct(exp->u.pattern_variable.type));
@@ -747,9 +754,11 @@ void StepStmt() {
Action* act = frame->todo.Top();
Statement* const stmt = act->u.stmt;
assert(stmt != nullptr && "null statement!");
std::cout << "--- step stmt ";
PrintStatement(stmt, 1);
std::cout << " --->" << std::endl;
if (tracing_output) {
std::cout << "--- step stmt ";
PrintStatement(stmt, 1);
std::cout << " --->" << std::endl;
}
switch (stmt->tag) {
case StatementKind::Match:
// { { (match (e) ...) :: C, E, F} :: S, H}
@@ -918,12 +927,13 @@ void HandleValue() {
act->results.push_back(val_act->u.val);
act->pos++;
std::cout << "--- handle value ";
PrintValue(val_act->u.val, std::cout);
std::cout << " with ";
PrintAct(act, std::cout);
std::cout << " --->" << std::endl;
if (tracing_output) {
std::cout << "--- handle value ";
PrintValue(val_act->u.val, std::cout);
std::cout << " with ";
PrintAct(act, std::cout);
std::cout << " --->" << std::endl;
}
switch (act->tag) {
case ActionKind::DeleteTmpAction: {
KillValue(state->heap[act->u.delete_tmp]);
@@ -1325,7 +1335,9 @@ void Step() {
// Interpret the whole porogram.
auto InterpProgram(std::list<Declaration>* fs) -> int {
state = new State(); // Runtime state.
std::cout << "********** initializing globals **********" << std::endl;
if (tracing_output) {
std::cout << "********** initializing globals **********" << std::endl;
}
InitGlobals(fs);
Expression* arg =
@@ -1336,14 +1348,18 @@ auto InterpProgram(std::list<Declaration>* fs) -> int {
auto* frame = new Frame("top", Stack(scope), todo);
state->stack = Stack(frame);
std::cout << "********** calling main function **********" << std::endl;
PrintState(std::cout);
if (tracing_output) {
std::cout << "********** calling main function **********" << std::endl;
PrintState(std::cout);
}
while (state->stack.CountExceeds(1) ||
state->stack.Top()->todo.CountExceeds(1) ||
state->stack.Top()->todo.Top()->tag != ActionKind::ValAction) {
Step();
PrintState(std::cout);
if (tracing_output) {
PrintState(std::cout);
}
}
Value* v = state->stack.Top()->todo.Top()->u.val;
return ValToInt(v, 0);