import { ASTNode, NodeType } from "./common"; import { Lex } from "./lexer"; import { Parse } from "./parser"; export type FnError = `Function execution error: ${T}`; export type ToStringInner = T extends | string | number ? `${T}` : T extends readonly any[] ? T extends readonly [infer Head, ...infer Tail] ? `${ToStringInner< Tail, `${Carry extends "" ? "" : `${Carry}, `}${ToStringInner}` >}` : `[${Carry}]` : FnError<`Can't stringify`>; export type UnarrayIfOnlyHead = T extends [ infer Head, infer Next ] ? T : T extends [infer Head] ? Head : T; export type BUILTIN_ARR = { [Idx in keyof Args]: Args[Idx] extends ASTNode ? Evaluate : never; }; export type BUILTIN_ToString = ToStringInner< UnarrayIfOnlyHead<{ [Idx in keyof Args]: Args[Idx] extends ASTNode ? ToStringInner> : never; }> >; // export type BUILTIN_Print export type SENTINEL_NO_BUILTIN = "__NO_BUILTIN__"; export type MapBuiltins = Node["name"] extends "tostring" ? BUILTIN_ToString : Node["name"] extends "arr" ? BUILTIN_ARR : SENTINEL_NO_BUILTIN; export type EvalError = `Eval error: ${T}`; export type Evaluate = Node["type"] extends NodeType.INT ? Node["value"] : Node["type"] extends NodeType.EXT ? MapBuiltins : EvalError<`Unhandled node type ${Node["type"]}`>; const input = `5, arr(5000)` as const; const lex_result = null as unknown as Lex; const parse_result = null as unknown as Parse; const eval_result = null as unknown as Evaluate;