nk 1.0.0
C++ Numerical Computing Runtime
Loading...
Searching...
No Matches
types.hpp
1// include/types.hpp
2#pragma once
3
4#include <nk/core/ast.hpp>
5#include <nk/core/environment.hpp>
6#include <nk/value/error.hpp> // nk::Error (L0) — re-exported here for existing users
7#include <nk/value/span.hpp>
8#include <nk/value/value.hpp>
9
10#include <chrono>
11#include <functional>
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include <unordered_set>
16#include <vector>
17
18namespace nk {
19
20// ============================================================
21// Timer types
22// ============================================================
23using Clock = std::chrono::high_resolution_clock;
24using TimePoint = Clock::time_point;
25
26// ============================================================
27// Function types
28// ============================================================
29using BinaryOpFunc = std::function<Value(const Value &, const Value &)>;
30using UnaryOpFunc = std::function<Value(const Value &)>;
31
32// Forward declaration for CallContext
33class Engine;
34
35// ============================================================
36// CallContext — passed to all external functions
37//
38// Provides controlled access to the interpreter state:
39// engine — allocator, outputText, figureManager, hasFunction, etc.
40// env — current scope (global for scripts, local for functions)
41// ============================================================
43{
44 Engine *engine;
45 Environment *env;
46};
47
48using ExternalFunc = std::function<
49 void(Span<const Value> args, size_t nargout, Span<Value> outs, CallContext &ctx)>;
50
51// ============================================================
52// Control flow signals
53// ============================================================
55{};
57{};
59{};
60
61enum class FlowSignal : uint8_t { NONE = 0, BREAK, CONTINUE, RETURN };
62
63// ============================================================
64// Reserved names — classified for precise use at call sites.
65// ============================================================
66//
67// Numeric/logical constants served by `constantsEnv_` (parent of the
68// base workspace). Always defined at runtime. Assigning to one creates
69// a shadow local; `clear name` removes the shadow.
70extern const std::unordered_set<std::string> kBuiltinConstants;
71
72// Runtime pseudo-variables set by the VM / display pipeline, not by
73// user code. `nargin` / `nargout` are loaded on function entry; `ans`
74// is set by DISPLAY for bare expressions; `end` is the magic keyword
75// used in indexing. None of them live in any environment.
76extern const std::unordered_set<std::string> kPseudoVars;
77
78// Union of the above — "any name the compiler / debugger treats
79// specially and must not mistake for a plain user variable". Most
80// filter sites want this broad set; the narrower ones are for places
81// where we specifically need "true constant" semantics (e.g. deciding
82// whether to LOAD_CONST from `constantsEnv_`).
83extern const std::unordered_set<std::string> kBuiltinNames;
84
85// nk::Error (runtime error with source location) moved to an L0 header:
86// <nk/value/error.hpp>, included above. Kept out of this heavy core header
87// so value/ops/toolboxes can throw it without pulling in the engine.
88
89// ============================================================
90// User-defined function descriptor
91// ============================================================
93{
94 std::string name;
95 std::vector<std::string> params;
96 std::vector<std::string> returns;
97 std::shared_ptr<const ASTNode> body;
98 std::shared_ptr<Environment> closureEnv;
99 mutable int8_t usesNarginNargout = -1;
100 // classdef methods/constructors only: the class that declares this body.
101 // Drives the member-access execution context (pushed while the body runs).
102 // Empty for ordinary functions.
103 std::string ownerClass;
104};
105
106} // namespace nk
Core MATLAB-compatible multidimensional array and scalar value type.
Definition value.hpp:61