UNPKG

mathjslab

Version:

MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.

88 lines (87 loc) 3.14 kB
import type { NodeBuiltInFunction, NodeFunctionDefinition } from './AST'; import type { FunctionHandle } from './FunctionHandle'; /** * Runtime representation of a built-in function after name/handle resolution. * * Built-ins are stored as AST/runtime nodes in the interpreter function table, * but the call engine works with this wrapper so all callable forms share the * same discriminated-union shape. */ interface BuiltinCallable { type: 'BUILTIN'; node: NodeBuiltInFunction; } /** * Runtime representation of an anonymous function handle. * * MATLAB/Octave anonymous functions do not have a function-table name. The * narrowed `id: undefined` type documents that this callable is backed by the * handle expression itself, including its parameter list, expression body, and * captured closure. */ interface LambdaCallable { type: 'LAMBDA'; node: FunctionHandle & { id: undefined; }; } /** * Runtime representation of a user-defined function. * * This covers ordinary function definitions as well as nested/local functions; * the function node carries attributes such as `nested` and persistent storage. */ interface FunctionDefinitionCallable { type: 'FCNDEF'; node: NodeFunctionDefinition; } /** * Unified callable abstraction used by call dispatch, `nargin`, `nargout`, * stack frames, function handles, and introspection. * * Keeping this union outside `Interpreter.ts` avoids parallel structural * definitions drifting apart as the MATLAB/Octave-like function engine grows. */ type Callable = BuiltinCallable | LambdaCallable | FunctionDefinitionCallable; /** * Factory and type-guard helpers for the `Callable` union. * * The helpers intentionally return tiny immutable wrapper objects instead of * mutating AST/function-handle nodes. This keeps function definitions, built-in * nodes, and handles usable in more than one call context. */ declare const Callables: { /** * Wrap a built-in runtime node for uniform dispatch. */ builtin(node: NodeBuiltInFunction): BuiltinCallable; /** * Wrap an anonymous function handle. */ lambda(node: FunctionHandle & { id: undefined; }): LambdaCallable; /** * Wrap a user-defined function definition. */ functionDefinition(node: NodeFunctionDefinition): FunctionDefinitionCallable; /** * Wrap a function-table node while preserving its concrete kind. */ fromFunctionNode(node: NodeBuiltInFunction | NodeFunctionDefinition): BuiltinCallable | FunctionDefinitionCallable; /** * Narrow a callable to the built-in variant. */ isBuiltin(callable: Callable): callable is BuiltinCallable; /** * Narrow a callable to the anonymous-function variant. */ isLambda(callable: Callable): callable is LambdaCallable; /** * Narrow a callable to the user-defined-function variant. */ isFunctionDefinition(callable: Callable): callable is FunctionDefinitionCallable; }; export type { BuiltinCallable, LambdaCallable, FunctionDefinitionCallable, Callable }; export { Callables }; export default Callables;