@mojir/lits
Version:
Lits is a pure functional programming language implemented in TypeScript
126 lines (125 loc) • 6.37 kB
TypeScript
import type { JsFunction } from '../Lits/Lits';
import type { SpecialExpressionType } from '../builtin';
import type { Arity } from '../builtin/interface';
import type { specialExpressionTypes } from '../builtin/specialExpressionTypes';
import type { FunctionType, NodeType, NodeTypes } from '../constants/constants';
import type { Context } from '../evaluator/interface';
import type { Any, Arr, Coll } from '../interface';
import type { ReservedSymbol } from '../tokenizer/reservedNames';
import type { SourceCodeInfo } from '../tokenizer/token';
import type { FUNCTION_SYMBOL, REGEXP_SYMBOL } from '../utils/symbols';
export type EvaluatedFunction = [BindingTarget[], AstNode[], Context];
interface GenericLitsFunction {
[FUNCTION_SYMBOL]: true;
sourceCodeInfo?: SourceCodeInfo;
functionType: FunctionType;
arity: Arity;
}
export interface RegularExpression {
[REGEXP_SYMBOL]: true;
sourceCodeInfo?: SourceCodeInfo;
s: string;
f: string;
}
export interface NativeJsFunction extends GenericLitsFunction {
functionType: 'NativeJsFunction';
name: string | undefined;
nativeFn: JsFunction;
docString: string;
}
export interface UserDefinedFunction extends GenericLitsFunction {
functionType: 'UserDefined';
name: string | undefined;
evaluatedfunction: EvaluatedFunction;
docString: string;
}
export interface PartialFunction extends GenericLitsFunction {
functionType: 'Partial';
function: FunctionLike;
params: Arr;
placeholders: number[];
}
export interface CompFunction extends GenericLitsFunction {
functionType: 'Comp';
params: Arr;
}
export interface ConstantlyFunction extends GenericLitsFunction {
functionType: 'Constantly';
value: Any;
}
export interface JuxtFunction extends GenericLitsFunction {
functionType: 'Juxt';
params: Arr;
}
export interface ComplementFunction extends GenericLitsFunction {
functionType: 'Complement';
function: FunctionLike;
}
export interface EveryPredFunction extends GenericLitsFunction {
functionType: 'EveryPred';
params: Arr;
}
export interface SomePredFunction extends GenericLitsFunction {
functionType: 'SomePred';
params: Arr;
}
export interface FNullFunction extends GenericLitsFunction {
functionType: 'Fnull';
function: FunctionLike;
params: Arr;
}
export interface NormalBuiltinFunction extends GenericLitsFunction {
functionType: 'Builtin';
normalBuiltinSymbolType: number;
name: string;
}
export interface SpecialBuiltinFunction extends GenericLitsFunction {
functionType: 'SpecialBuiltin';
specialBuiltinSymbolType: typeof specialExpressionTypes['&&'] | typeof specialExpressionTypes['||'] | typeof specialExpressionTypes['array'] | typeof specialExpressionTypes['object'] | typeof specialExpressionTypes['defined?'] | typeof specialExpressionTypes['recur'] | typeof specialExpressionTypes['throw'] | typeof specialExpressionTypes['??'];
}
export interface ModuleFunction extends GenericLitsFunction {
functionType: 'Module';
moduleName: string;
functionName: string;
}
export type LitsFunction = NativeJsFunction | UserDefinedFunction | NormalBuiltinFunction | SpecialBuiltinFunction | ModuleFunction | PartialFunction | CompFunction | ConstantlyFunction | JuxtFunction | ComplementFunction | EveryPredFunction | SomePredFunction | FNullFunction;
export type LitsFunctionType = LitsFunction['functionType'];
export type FunctionLike = LitsFunction | Coll | number;
export type AstNode<T extends NodeType = NodeType, Payload = unknown> = [T, Payload] | [T, Payload, SourceCodeInfo];
export type ExpressionNode = NormalExpressionNode | SpecialExpressionNode | NumberNode | StringNode;
export type SpreadNode = AstNode<typeof NodeTypes.Spread, AstNode>;
export type NumberNode = AstNode<typeof NodeTypes.Number, number>;
export type StringNode = AstNode<typeof NodeTypes.String, string>;
export type UserDefinedSymbolNode = AstNode<typeof NodeTypes.UserDefinedSymbol, string>;
export type NormalBuiltinSymbolNode = AstNode<typeof NodeTypes.NormalBuiltinSymbol, number>;
export type SpecialBuiltinSymbolNode = AstNode<typeof NodeTypes.SpecialBuiltinSymbol, SpecialExpressionType>;
export type SymbolNode = UserDefinedSymbolNode | NormalBuiltinSymbolNode | SpecialBuiltinSymbolNode;
export type ReservedSymbolNode = AstNode<typeof NodeTypes.ReservedSymbol, ReservedSymbol>;
export type SpecialExpressionNode<T extends [SpecialExpressionType, ...unknown[]] = [SpecialExpressionType, ...unknown[]]> = AstNode<typeof NodeTypes.SpecialExpression, T>;
export type NormalExpressionNodeWithName = AstNode<typeof NodeTypes.NormalExpression, [NormalBuiltinSymbolNode | UserDefinedSymbolNode, AstNode[]]>;
export type NormalExpressionNodeExpression = AstNode<typeof NodeTypes.NormalExpression, [AstNode, AstNode[]]>;
export type NormalExpressionNode = NormalExpressionNodeWithName | NormalExpressionNodeExpression;
export declare const bindingTargetTypes: {
readonly symbol: 11;
readonly rest: 12;
readonly object: 13;
readonly array: 14;
readonly literal: 15;
readonly wildcard: 16;
};
export type BindingTargetType = typeof bindingTargetTypes[keyof typeof bindingTargetTypes];
type GenericTarget<T extends BindingTargetType, Payload extends unknown[]> = [T, Payload] | [T, Payload, SourceCodeInfo];
export type SymbolBindingTarget = GenericTarget<typeof bindingTargetTypes.symbol, [SymbolNode, AstNode | undefined]>;
export type RestBindingTarget = GenericTarget<typeof bindingTargetTypes.rest, [string, AstNode | undefined]>;
export type ObjectBindingTarget = GenericTarget<typeof bindingTargetTypes.object, [Record<string, BindingTarget>, AstNode | undefined]>;
export type ArrayBindingTarget = GenericTarget<typeof bindingTargetTypes.array, [(BindingTarget | null)[], AstNode | undefined]>;
export type LiteralBindingTarget = GenericTarget<typeof bindingTargetTypes.literal, [AstNode]>;
export type WildcardBindingTarget = GenericTarget<typeof bindingTargetTypes.wildcard, []>;
export type BindingTarget = SymbolBindingTarget | RestBindingTarget | ObjectBindingTarget | ArrayBindingTarget | LiteralBindingTarget | WildcardBindingTarget;
export type BindingNode = AstNode<typeof NodeTypes.Binding, [BindingTarget, AstNode]>;
type AstBody = AstNode[];
export interface Ast {
body: AstBody;
hasDebugData: boolean;
}
export {};