nisp
Version:
A language that for easily build cross-language language
47 lines (46 loc) • 1.4 kB
TypeScript
export interface Sandbox {
[name: string]: Fn;
}
export interface Fn {
(...args: any[]): any;
macro?: boolean;
}
export interface Nisp {
(ctx: Context): any;
}
export declare function macro(fn: Nisp): any;
export interface Context {
ast: any[];
sandbox: Sandbox;
env?: any;
parent?: Context;
index?: number;
}
/**
* Get nth argument value
*/
export declare function arg(ctx: Context, index: number): any;
export declare class NispError {
name: string;
stack: any[];
message: string;
constructor(msg: string, stack: any[]);
toString(): string;
}
/**
* Throw error with stack info
*/
export declare let error: (ctx: Context, msg: string) => never;
/**
* Eval an nisp ast
* @param {any} ast The abstract syntax tree of nisp.
* It's a common flaw that array cannot carry plain data,
* such as `['foo', [1,2]]`, The `1` will be treat as a function name.
* So it's recommended to use object to carry plain data,
* such as translate the example to `['foo', { data: [1, 2] }]`.
* @param {Sandbox} sandbox The interface to the real world.
* It defined functions to reduce the data of each expression.
* @param {any} env The system space of the vm.
* @param {any} parent Parent context, it is used to back trace the execution stack.
*/
export default function (ast: any, sandbox: Sandbox, env?: any, parent?: Context, index?: number): any;