|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <functional> |
| 4 | +#include <optional> |
| 5 | +#include <string> |
| 6 | +#include <lbd/fe/ast.h> |
| 7 | +#include <lbd/fe/parser.h> |
| 8 | + |
| 9 | +namespace intp::interp { |
| 10 | + struct NativeFunction; |
| 11 | + struct Thunk; |
| 12 | + struct Env; |
| 13 | + |
| 14 | + /// Runtime representation of Lambda Expression |
| 15 | + struct Closure { |
| 16 | + std::string param; |
| 17 | + const fe::ast::Expression *body; /// Non-owning, read-only AST pointer |
| 18 | + std::shared_ptr<Env> env; /// Environment at the time of Lambda Expression creation |
| 19 | + |
| 20 | + friend std::ostream &operator<<(std::ostream &os, const Closure &closure); |
| 21 | + }; |
| 22 | + |
| 23 | + using Value = std::variant<double, std::string, Closure, NativeFunction>; |
| 24 | + |
| 25 | + struct NativeFunction { |
| 26 | + using Impl = std::function<Value( |
| 27 | + const std::vector<std::shared_ptr<Thunk> > &, const std::shared_ptr<Env> &)>; |
| 28 | + |
| 29 | + std::size_t arity; |
| 30 | + Impl impl; |
| 31 | + |
| 32 | + friend std::ostream &operator<<(std::ostream &os, const NativeFunction &native_fn); |
| 33 | + }; |
| 34 | + |
| 35 | + /// Pretty print a runtime value for REPL/diagnostics |
| 36 | + std::ostream &operator<<(std::ostream &os, const Value &value); |
| 37 | + |
| 38 | + /// Lazy Thunk (call-by-need) |
| 39 | + struct Thunk : std::enable_shared_from_this<Thunk> { |
| 40 | + mutable std::optional<Value> cached; |
| 41 | + const fe::ast::Expression *expr = nullptr; /// Non-owning, read-only AST pointer |
| 42 | + std::shared_ptr<Env> env; /// Environment for evaluating Expression |
| 43 | + |
| 44 | + Thunk() = default; |
| 45 | + |
| 46 | + Thunk(const fe::ast::Expression *expr, std::shared_ptr<Env> env); |
| 47 | + |
| 48 | + /// Force computation on Thunk and return a const reference to Value |
| 49 | + const Value &force() const; /// Marked const as cached is mutable |
| 50 | + |
| 51 | + /// Sets Thunk's fields after construction |
| 52 | + /// Allows for recursive reference |
| 53 | + void set(const fe::ast::Expression *expr_, std::shared_ptr<Env> env_); |
| 54 | + }; |
| 55 | + |
| 56 | + struct Env : std::enable_shared_from_this<Env> { |
| 57 | + std::unordered_map<std::string, std::shared_ptr<Thunk> > table; |
| 58 | + std::shared_ptr<Env> parent; |
| 59 | + |
| 60 | + explicit Env(std::shared_ptr<Env> parent = nullptr); |
| 61 | + |
| 62 | + std::shared_ptr<Thunk> lookup(const std::string &name) const; |
| 63 | + |
| 64 | + void bind(const std::string &name, std::shared_ptr<Thunk> thunk); |
| 65 | + }; |
| 66 | + |
| 67 | + Value eval_expr(const fe::ast::Expression &expr, std::shared_ptr<Env> env); |
| 68 | + |
| 69 | + Value apply_fn_apl(Value fn_value, const std::vector<std::shared_ptr<Thunk> > &args, |
| 70 | + const std::shared_ptr<Env> &call_site_env); |
| 71 | + |
| 72 | + /// Program Driver |
| 73 | + struct Result { |
| 74 | + std::shared_ptr<Env> global_env; |
| 75 | + }; |
| 76 | + |
| 77 | + Result interpret(const fe::ast::Program &program); |
| 78 | + |
| 79 | + /// Add builtins Native Functions into Environment |
| 80 | + void install_builtins(const std::shared_ptr<Env> &env); |
| 81 | +} |
0 commit comments