mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Sorry about the big change, this is hard to split. ParenContents is used by both, templated, and expects the same pointer type. While I could duplicate ParenContents with some ExpressionParenContents or PatternParenContents, that seems a little kludgy versus a single large change handling both. The worst of it is that Expression is already pretty sweeping, Pattern is really just incrementally adding. That said, I believe this includes a couple fixes I found with incorrect use of dyn_cast in typecheck.cpp (checked nullptr at the wrong step in 2 code locations). There's also a missing `*` in member.cpp this caught. I adjust passing of expressions for Return due to nullness (I felt adding another constructor was the best solution). I add a `.Release()` to BisonWrap due to things like `$3.first` needing some way to work through BIsonWrap. I felt this was better than `operator->`, but feel free to comment if you prefer the other path (`.Release()` conveniently lets me do pair unwrapping, so it felt a better solution). I do add a TODO to think about better Ptr-to-Ptr cast<> support too, though, as that doesn't work cleanly with LLVM's infra. But so far it seems to only come up in one spot, so I'm not prioritizing it.
52 lines
1.6 KiB
C++
52 lines
1.6 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
|
|
|
|
#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
|
|
#define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
|
|
|
|
#include <list>
|
|
#include <optional>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "common/ostream.h"
|
|
#include "executable_semantics/ast/declaration.h"
|
|
#include "executable_semantics/ast/expression.h"
|
|
#include "executable_semantics/ast/pattern.h"
|
|
#include "executable_semantics/interpreter/frame.h"
|
|
#include "executable_semantics/interpreter/heap.h"
|
|
#include "executable_semantics/interpreter/stack.h"
|
|
#include "executable_semantics/interpreter/value.h"
|
|
|
|
namespace Carbon {
|
|
|
|
using Env = Dictionary<std::string, Address>;
|
|
|
|
struct State {
|
|
Stack<Ptr<Frame>> stack;
|
|
Heap heap;
|
|
std::optional<const Value*> program_value;
|
|
};
|
|
|
|
extern State* state;
|
|
|
|
void InitEnv(const Declaration& d, Env* env);
|
|
void PrintStack(const Stack<Frame*>& ls, llvm::raw_ostream& out);
|
|
void PrintEnv(Env values, llvm::raw_ostream& out);
|
|
|
|
/***** Interpreters *****/
|
|
|
|
// Attempts to match `v` against the pattern `p`. If matching succeeds, returns
|
|
// the bindings of pattern variables to their matched values.
|
|
auto PatternMatch(const Value* p, const Value* v, SourceLocation loc)
|
|
-> std::optional<Env>;
|
|
|
|
auto InterpProgram(const std::list<Ptr<const Declaration>>& fs) -> int;
|
|
auto InterpExp(Env values, Ptr<const Expression> e) -> const Value*;
|
|
auto InterpPattern(Env values, Ptr<const Pattern> p) -> const Value*;
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
|