type-unlambda/src/unlambda.ts

32 lines
871 B
TypeScript
Raw Normal View History

2020-11-06 09:07:33 +00:00
/**
2020-11-06 09:40:02 +00:00
* unlambda.ts - Unlambda interpreter entry.
2020-11-06 09:07:33 +00:00
*
* @author CismonX <admin@cismon.net>
* @license MIT
*/
import { Parse, ParseResult } from './parser';
import { Eval } from './runtime';
/**
* Get the parse result expression, or `never` if parse failed.
*/
2021-06-03 13:27:35 +00:00
type ParseResultValue<T extends ParseResult> =
Parse<T[1]> extends [never, ''] ? T[0] : never;
2020-11-06 09:07:33 +00:00
/**
2021-06-03 13:27:35 +00:00
* Get the output string of an evalalutation result, or `never`
* if evaluation failed.
2020-11-06 09:07:33 +00:00
*/
2021-06-03 13:27:35 +00:00
type EvalResultOutput<T> = T extends [any, [any, infer O, any]] ? O : never;
2020-11-06 09:07:33 +00:00
/**
* Given the source code of an Unlambda program, and input string.
*
* Returns the output of program execution, or `never` if something went wrong.
*/
2021-06-03 12:22:30 +00:00
type Unlambda<Code extends string, Input extends string = ''> =
2020-11-06 09:07:33 +00:00
EvalResultOutput<Eval<ParseResultValue<Parse<Code>>, [], [Input, '', '']>>;
2021-06-03 12:22:30 +00:00
export default Unlambda;