mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 14:40:11 +01:00
We will eventually need the ability to materialize temporaries eventually, but we don't really seem to need it yet, and when we do, I think we should do it in a way that gives uniform treatment to temporaries and local variables.
114 lines
2.7 KiB
C++
114 lines
2.7 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 {
|
|
|
|
namespace {
|
|
|
|
struct TagVisitor {
|
|
template <typename Alternative>
|
|
auto operator()(const Alternative&) -> ActionKind {
|
|
return Alternative::Kind;
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
auto Action::tag() const -> ActionKind {
|
|
return std::visit(TagVisitor(), value);
|
|
}
|
|
|
|
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(std::ostream& out) {
|
|
switch (tag()) {
|
|
case ActionKind::LValAction:
|
|
PrintExp(GetLValAction().exp);
|
|
break;
|
|
case ActionKind::ExpressionAction:
|
|
PrintExp(GetExpressionAction().exp);
|
|
break;
|
|
case ActionKind::StatementAction:
|
|
PrintStatement(GetStatementAction().stmt, 1);
|
|
break;
|
|
case ActionKind::ValAction:
|
|
PrintValue(GetValAction().val, out);
|
|
break;
|
|
}
|
|
out << "<" << pos << ">";
|
|
if (results.size() > 0) {
|
|
out << "(";
|
|
for (auto& result : results) {
|
|
if (result) {
|
|
PrintValue(result, out);
|
|
}
|
|
out << ",";
|
|
}
|
|
out << ")";
|
|
}
|
|
}
|
|
|
|
void Action::PrintList(Stack<Action*> ls, std::ostream& out) {
|
|
if (!ls.IsEmpty()) {
|
|
PrintList(ls.Pop(), out);
|
|
if (!ls.IsEmpty()) {
|
|
out << " :: ";
|
|
PrintList(ls, out);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|