UNPKG

mathjslab

Version:

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

141 lines (140 loc) 7.64 kB
import type { NameEntry, NodeBuiltInFunction, NodeFunctionDefinition, NodeInput } from './AST'; import { CharString } from './CharString'; import { Structure } from './Structure'; import type { SymbolResolution } from './Context'; import { FunctionHandle } from './FunctionHandle'; /** Function-like AST node accepted by lookup helpers. */ type LookupFunction = NodeBuiltInFunction | NodeFunctionDefinition; /** Callback that resolves a function name in the current interpreter context. */ type ResolveFunction = (name: string) => LookupFunction | undefined; /** Callback that maps aliases such as operators or built-in shorthands to canonical names. */ type AliasNameFunction = (name: string) => string; /** Callback that renders a function handle with interpreter-specific unparsing rules. */ type UnparseHandle = (handle: FunctionHandle) => string; /** Interpreter evaluation-error callback used by pure lookup helpers. */ type ThrowEvalError = (message: string) => never; /** Callback used to parse/evaluate anonymous handles supplied to `str2func`. */ type EvaluateAnonymousHandle = (source: string) => NodeInput; /** Callback that builds the `functions(handle).workspace` cell array. */ type WorkspaceInfo = (handle: FunctionHandle) => NodeInput; /** Minimal metadata for an imported static class method. */ type StaticMethodInfo = { className: string; methodName: string; sourceName?: string; }; /** Callback that resolves a named handle as an imported static class method. */ type ResolveStaticMethod = (name: string, handle?: FunctionHandle) => StaticMethodInfo | undefined; /** * Implements function/handle lookup and introspection helpers. * * Browser-hosted MathJSLab cannot fully mirror MATLAB/Octave file-system * lookup, so this module focuses on symbols the runtime can know * synchronously: variables, user-defined functions, built-ins, function * handles, runtime class names, and host-provided script/class sources exposed * through the virtual `.m` resolver. */ declare class FunctionLookup { private static readonly runtimeClassNames; /** * Compute the numeric result of `exist(name, kind)`. * * @param name Queried identifier. * @param kind Optional MATLAB/Octave `exist` kind selector. * @param variable Resolved variable entry, when present. * @param func Resolved function entry, when present. * @param classDefined Whether an external/source-provider class with this name is known. * @param scriptDefined Whether a host-provided script source with this name is known. * @returns MATLAB-like `exist` code for in-memory symbols supported by the runtime. */ static existCode(name: string, kind: string | undefined, variable: NameEntry | undefined, func: LookupFunction | undefined, classDefined?: boolean, scriptDefined?: boolean): number; /** * Compute `exist` from a structured symbol-resolution result. * * @param name Queried identifier. * @param kind Optional MATLAB/Octave `exist` kind selector. * @param resolved Structured symbol result, when present. * @returns MATLAB-like `exist` code. */ static existCodeFromResolution(name: string, kind: string | undefined, resolved: SymbolResolution | undefined): number; /** * Produce the user-facing result for `which`. * * @param name Queried identifier. * @param variable Resolved variable entry, when present. * @param func Resolved function entry, when present. * @param handle Resolved function-handle variable, when present. * @param unparseHandle Callback used to render anonymous handles. * @param classDefined Whether an external/source-provider class with this name is known. * @param scriptDefined Whether a host-provided script source with this name is known. * @returns Text value describing what the name resolves to. */ static whichResult(name: string, variable: NameEntry | undefined, func: LookupFunction | undefined, handle: FunctionHandle | undefined, unparseHandle: UnparseHandle, classDefined?: boolean, scriptDefined?: boolean, staticMethod?: StaticMethodInfo): CharString; /** * Produce `which` text from a structured symbol-resolution result. * * @param name Queried identifier. * @param resolved Structured symbol result, when present. * @param handle Resolved function-handle variable, when present. * @param unparseHandle Callback used to render anonymous handles. * @returns Text value describing what the name resolves to. */ static whichResultFromResolution(name: string, resolved: SymbolResolution | undefined, handle: FunctionHandle | undefined, unparseHandle: UnparseHandle, staticMethod?: StaticMethodInfo): CharString; /** * Convert a function handle to its string representation. * * @param handle Function handle to render. * @param unparseHandle Callback used for anonymous handles. * @returns Named handle id or anonymous handle source text. */ static func2str(handle: FunctionHandle, unparseHandle: UnparseHandle): CharString; /** * Convert text to a named or anonymous function handle. * * Anonymous handles are parsed/evaluated through the supplied callback so * the interpreter can reuse its normal parser and closure creation logic. * * @param sourceText Text supplied to `str2func`. * @param evaluateAnonymousHandle Callback used to evaluate anonymous handle text. * @param throwEvalError Error callback for invalid handle text. * @returns Runtime function handle. */ static str2func(sourceText: string, evaluateAnonymousHandle: EvaluateAnonymousHandle, throwEvalError: ThrowEvalError): FunctionHandle; /** * Build the structure returned by `functions(handle)`. * * @param handle Function handle to inspect. * @param aliasNameFunction Callback that maps aliases to canonical names. * @param resolveFunction Callback that resolves functions in the current context. * @param unparseHandle Callback used for anonymous handles. * @param workspaceInfo Callback that returns captured workspace metadata. * @returns MATLAB-like introspection structure. */ static functionsInfo(handle: FunctionHandle, aliasNameFunction: AliasNameFunction, resolveFunction: ResolveFunction, unparseHandle: UnparseHandle, workspaceInfo?: WorkspaceInfo, resolveStaticMethod?: ResolveStaticMethod): Structure; /** * Resolve the function targeted by a named handle. * * Handle closures are consulted first so local/nested handles remain bound * to their lexical function environment. * * @param handle Function handle being invoked or inspected. * @param name Target name stored in the handle. * @param aliasNameFunction Callback that maps aliases to canonical names. * @param resolveFunction Callback that resolves functions in the active context. * @returns Resolved built-in/user function, if present. */ static resolveHandleFunction(handle: FunctionHandle, name: string, aliasNameFunction: AliasNameFunction, resolveFunction: ResolveFunction): LookupFunction | undefined; /** * Runtime class names currently recognized by `exist(name, 'class')`. * * @param name Class name to test. * @returns `true` for built-in runtime class names. */ static isRuntimeClassName(name: string): boolean; } export type { LookupFunction, StaticMethodInfo }; export { FunctionLookup }; declare const _default: { FunctionLookup: typeof FunctionLookup; }; export default _default;