Files
carbon-lang/executable_semantics/interpreter/action.cpp
T
Jeremy G. SiekandGeoff Romer b60ef90d62 Merge HandleValue into the Step functions (#598)
* moved most of logic from HandleValue into the Step functions

* removed the HandleValue function

* added some comments

* responses to Geoffrey

* move increment of act->pos out of the step logic for value actions

* changed act->pos to start at 0 and not have any gaps

* Update executable_semantics/interpreter/action.h

Co-authored-by: Geoff Romer <gromer@google.com>

* update comment based on Geoffrey's suggestion

Co-authored-by: Geoff Romer <gromer@google.com>
2021-06-29 09:44:40 -04:00

110 lines
2.5 KiB
C++

// 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 <iostream>
#include <iterator>
#include <map>
#include <optional>
#include <utility>
#include <vector>
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/function_definition.h"
#include "executable_semantics/interpreter/interpreter.h"
#include "executable_semantics/interpreter/stack.h"
#include "executable_semantics/interpreter/typecheck.h"
namespace Carbon {
void PrintAct(Action* act, std::ostream& out) {
switch (act->tag) {
case ActionKind::DeleteTmpAction:
std::cout << "delete_tmp(" << act->u.delete_tmp << ")";
break;
case ActionKind::ExpToLValAction:
out << "exp=>lval";
break;
case ActionKind::LValAction:
case ActionKind::ExpressionAction:
PrintExp(act->u.exp);
break;
case ActionKind::StatementAction:
PrintStatement(act->u.stmt, 1);
break;
case ActionKind::ValAction:
PrintValue(act->u.val, out);
break;
}
out << "<" << act->pos << ">";
if (act->results.size() > 0) {
out << "(";
for (auto& result : act->results) {
if (result) {
PrintValue(result, out);
}
out << ",";
}
out << ")";
}
}
void PrintActList(Stack<Action*> ls, std::ostream& out) {
if (!ls.IsEmpty()) {
PrintAct(ls.Pop(), out);
if (!ls.IsEmpty()) {
out << " :: ";
PrintActList(ls, out);
}
}
}
auto MakeExpAct(const Expression* e) -> Action* {
auto* act = new Action();
act->tag = ActionKind::ExpressionAction;
act->u.exp = e;
act->pos = 0;
return act;
}
auto MakeLvalAct(const Expression* e) -> Action* {
auto* act = new Action();
act->tag = ActionKind::LValAction;
act->u.exp = e;
act->pos = 0;
return act;
}
auto MakeStmtAct(const Statement* s) -> Action* {
auto* act = new Action();
act->tag = ActionKind::StatementAction;
act->u.stmt = s;
act->pos = 0;
return act;
}
auto MakeValAct(const Value* v) -> Action* {
auto* act = new Action();
act->tag = ActionKind::ValAction;
act->u.val = v;
act->pos = 0;
return act;
}
auto MakeExpToLvalAct() -> Action* {
auto* act = new Action();
act->tag = ActionKind::ExpToLValAction;
act->pos = 0;
return act;
}
auto MakeDeleteAct(Address a) -> Action* {
auto* act = new Action();
act->tag = ActionKind::DeleteTmpAction;
act->pos = 0;
act->u.delete_tmp = a;
return act;
}
} // namespace Carbon