nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
environment.hpp
1// include/environment.hpp
2#pragma once
3
4#include <nk/value/value.hpp>
5#include <functional>
6#include <memory>
7#include <string>
8#include <unordered_map>
9#include <unordered_set>
10#include <vector>
11
12namespace nk {
13
14// A single `import` declaration active in the current scope. See
15// namespace_design.md Sections 3-4 for resolution semantics.
16struct Import
17{
18 std::vector<std::string> path; // ["signal", "transforms"] for `import signal.transforms.*`
19 bool wildcard = false; // true for `.*` form
20 std::string alias; // for `import x.y as alias`; "" otherwise
21 // For non-wildcard, non-alias single-symbol form (`import a.b.c`),
22 // path = [a, b, c] and the imported leaf name is path.back().
23};
24
25class Environment
26{
27public:
28 // For normal execution: raw parent pointer (caller guarantees lifetime)
29 explicit Environment(Environment *parent = nullptr, Environment *globalsEnv = nullptr);
30
31 // For snapshots/closures: owning parent
32 explicit Environment(std::shared_ptr<Environment> owningParent, Environment *globalsEnv);
33
34 void set(const std::string &name, Value val);
35 Value *get(const std::string &name);
36 bool has(const std::string &name) const;
37
38 void setLocal(const std::string &name, Value val);
39 Value *getLocal(const std::string &name);
40
41 // Fast path: get pointer for an existing variable, assign in-place.
42 // Returns pointer to the Value if found locally (single hash lookup).
43 // Returns nullptr if not found locally.
44 Value *getLocalFast(const std::string &name);
45
46 // Fast path: set an existing local variable without hash lookup for globals.
47 // Caller must ensure the variable already exists locally.
48 void setLocalFast(const std::string &name, Value val);
49
50 void declareGlobal(const std::string &name);
51 bool isGlobal(const std::string &name) const;
52
53 // Names this environment has declared `global`. The VALUE of each lives in
54 // globalsEnv_ (get/set delegate there), never in this env's local storage —
55 // so localNames() and globalNames() are disjoint. This set IS the base
56 // workspace's global membership when called on the engine's workspaceEnv.
57 const std::unordered_set<std::string> &globalNames() const { return globals_; }
58
59 Environment *globalsEnv() const { return globalsEnv_; }
60
61 // Iterate over local variables only
62 void forEachLocal(const std::function<void(const std::string &, const Value &)> &fn) const;
63
64 // Create a deep snapshot of this environment and its parent chain
65 std::shared_ptr<Environment> snapshot(std::shared_ptr<Environment> newParent,
66 Environment *gs) const;
67
68 void remove(const std::string &name);
69
70 void clearAll(bool keepImports = false);
71
72 // Reset for reuse — clears all data and sets new parent/globalsEnv
73 void reset(Environment *parent, Environment *gs);
74
75 std::vector<std::string> localNames() const;
76
77 // ── Active imports (scope-local) ──────────────────────────
78 // Imports added via `import` statements at this scope. Lookups
79 // check this scope's imports first, then walk parent_ chain.
80 void pushImport(Import imp) { activeImports_.push_back(std::move(imp)); }
81 const std::vector<Import> &activeImports() const { return activeImports_; }
82 void clearImports() { activeImports_.clear(); }
83 Environment *parentForImports() const { return parent_; }
84
85private:
86 // Small buffer: inline storage for first N variables (avoids unordered_map for small scopes)
87 static constexpr size_t SBO_SLOTS = 8;
88 struct Slot
89 {
90 std::string name;
91 Value value;
92 };
93 Slot sbo_[SBO_SLOTS];
94 size_t sboCount_ = 0;
95
96 // Overflow map for large scopes
97 std::unordered_map<std::string, Value> vars_;
98
99 std::unordered_set<std::string> globals_;
100 std::vector<Import> activeImports_;
101 Environment *parent_ = nullptr; // non-owning, for lookup
102 std::shared_ptr<Environment> owningParent_; // owning, for snapshots only
103 Environment *globalsEnv_ = nullptr;
104 bool hasGlobals_ = false;
105
106 // Internal helpers
107 Value *sboFind(const std::string &name);
108 const Value *sboFind(const std::string &name) const;
109 void sboSet(const std::string &name, Value val);
110};
111
112} // namespace nk
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61