nk
1.0.0
C++ Numerical Computing Runtime
Toggle main menu visibility
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
18
namespace
nk {
19
20
// ============================================================
21
// Timer types
22
// ============================================================
23
using
Clock = std::chrono::high_resolution_clock;
24
using
TimePoint = Clock::time_point;
25
26
// ============================================================
27
// Function types
28
// ============================================================
29
using
BinaryOpFunc = std::function<
Value
(
const
Value
&,
const
Value
&)>;
30
using
UnaryOpFunc = std::function<
Value
(
const
Value
&)>;
31
32
// Forward declaration for CallContext
33
class
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
// ============================================================
42
struct
CallContext
43
{
44
Engine
*engine;
45
Environment
*env;
46
};
47
48
using
ExternalFunc = std::function<
49
void(
Span<const Value>
args,
size_t
nargout,
Span<Value>
outs,
CallContext
&ctx)>;
50
51
// ============================================================
52
// Control flow signals
53
// ============================================================
54
struct
BreakSignal
55
{};
56
struct
ContinueSignal
57
{};
58
struct
ReturnSignal
59
{};
60
61
enum 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.
70
extern
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.
76
extern
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_`).
83
extern
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
// ============================================================
92
struct
UserFunction
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
nk::Engine
Definition
engine.hpp:32
nk::Environment
Definition
environment.hpp:26
nk::Span
Definition
span.hpp:11
nk::Value
Core MATLAB-compatible multidimensional array and scalar value type.
Definition
value.hpp:61
nk::BreakSignal
Definition
types.hpp:55
nk::CallContext
Definition
types.hpp:43
nk::ContinueSignal
Definition
types.hpp:57
nk::ReturnSignal
Definition
types.hpp:59
nk::UserFunction
Definition
types.hpp:93
src
core
include
nk
core
types.hpp
Generated by
1.17.0