Files
carbon-lang/executable_semantics/interpreter/action.cpp
T
Jon Meow 8fccecadeb Refactor output to be more streaming-focused. (#666)
- Switch code to llvm::raw_ostream as part of standardizing output forms.
    - Preferring llvm::raw_ostream over std::ostream because other tooling code should be expected to rely on llvm more closely, and an overall preference towards library consistency.
    - There are a couple spots in syntax/ that still use std streams, but I'd prefer to take a separate PR to see how best to address those.
    - std::boolalpha doesn't work with llvm, so I've implemented equivalent in a couple places (not enough that it felt like worth making a helper function).
- Implement Print(ostream) as consistently as we can, as an instance member.
    - This facilitates the use of the common/ostream.h template to provide operators.
    - Preferring this approach so that Print is easily accessible via gdb, per suggestion on #executable-semantics.
- Switch code currently calling `type->Print(ostream)` to instead do `ostream << *type`.
- Remove the unused `PrintTypeEnv`, nothing used it and the declaration didn't match the definition.
2021-07-20 13:16:48 -07:00

98 lines
2.3 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 "executable_semantics/interpreter/action.h"
#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/stack.h"
namespace Carbon {
auto Action::MakeLValAction(const Expression* e) -> Action* {
auto* act = new Action();
act->value = LValAction({.exp = e});
return act;
}
auto Action::MakeExpressionAction(const Expression* e) -> Action* {
auto* act = new Action();
act->value = ExpressionAction({.exp = e});
return act;
}
auto Action::MakeStatementAction(const Statement* s) -> Action* {
auto* act = new Action();
act->value = StatementAction({.stmt = s});
return act;
}
auto Action::MakeValAction(const Value* v) -> Action* {
auto* act = new Action();
act->value = ValAction({.val = v});
return act;
}
auto Action::GetLValAction() const -> const LValAction& {
return std::get<LValAction>(value);
}
auto Action::GetExpressionAction() const -> const ExpressionAction& {
return std::get<ExpressionAction>(value);
}
auto Action::GetStatementAction() const -> const StatementAction& {
return std::get<StatementAction>(value);
}
auto Action::GetValAction() const -> const ValAction& {
return std::get<ValAction>(value);
}
void Action::Print(llvm::raw_ostream& out) const {
switch (tag()) {
case ActionKind::LValAction:
out << *GetLValAction().exp;
break;
case ActionKind::ExpressionAction:
out << *GetExpressionAction().exp;
break;
case ActionKind::StatementAction:
GetStatementAction().stmt->PrintDepth(1, out);
break;
case ActionKind::ValAction:
out << *GetValAction().val;
break;
}
out << "<" << pos << ">";
if (results.size() > 0) {
out << "(";
for (auto& result : results) {
if (result) {
out << *result;
}
out << ",";
}
out << ")";
}
}
void Action::PrintList(Stack<Action*> ls, llvm::raw_ostream& out) {
if (!ls.IsEmpty()) {
out << *ls.Pop();
if (!ls.IsEmpty()) {
out << " :: ";
PrintList(ls, out);
}
}
}
} // namespace Carbon