tstosc
Version:
A transpiler that convert TypeScript to SuperCollider's SCLang.
118 lines (115 loc) • 3.74 kB
text/typescript
import ts from 'typescript';
import { GeneratorContext } from '../context.cjs';
import '../../../cli/args.cjs';
/**
* ### Generate Context Relationship
*
* * `statement_label`: Handled and wrapped here, if there is a label.
* * `is_generating_constructor`: Whether add trailing `^tstosc__built_instance` for constructor.
*
* ### Warning
*
* This function does **not** add wrapping-brace.
*
* @param block A code block that could be part of function, or just a stand-alone scope.
*/
declare function convertTSCodeBlockToSC(block: ts.BlockLike, generator_context?: GeneratorContext): string;
/**
* ### Warning
*
* As a compiler, this function will even consider **dead-code-causing** single `return` as early-return.
*
* ### Description
*
* Check if giving statements contains a early-return such as:
* ```ts
* function f()
* {
* if (some_cond) { return 1 } // <--- Like this.
*
* if (some_other_cond_0) { doSomething() }
* else if (some_other_cond_1) { return 2 } // <--- Also this.
* else
* {
* if (some_other_cond_3) { return 3 } // <--- And this.
* }
*
* doSomething()
* return 0
* }
* ```
*
* ### Logic:
*
* 1. Go through the statements and find out if there is flow-control statement.
* * If so, recursively check if there is a return statement inside `then` and `else` part.
* * If not, then no early-return found.
*
* @param at_outest_level If the statement checking is at the outest level of function.
* This will affect the final logic on checking if it is a early-return:
* * If outest level, the `return` should not be the last statement.
* * If already checking the last statement, the nested block's `return` is not early return.
*/
declare function hasEarlyReturnIn(stmts: ReadonlyArray<ts.Statement>, at_outest_level?: boolean): boolean;
/**
* ### Warning
*
* This function assume that all nested statements are wrapped in outer `block`,
* and their `return x` are also converted to `return_with.value(x)`.
*
* ### Description
*
* Convert TypeScript's `if` statement to the form like this:
* ```ts
* function f()
* {
* if (some_cond) { return 1 } // <--- Like this.
*
* if (some_other_cond_0) { doSomething() }
* else if (some_other_cond_1) { return 2 } // <--- Also this.
* else
* {
* if (some_other_cond_3) { return 3 } // <--- And this.
* }
*
* doSomething()
* return 0
* }
* ```
*
* to:
*
* ```sclang
* var f = {
* block { |return_with|
* var some_cond=true, some_other_cond_0=false, some_other_cond_1=false, some_other_cond_3=false;
* if(some_cond,
* { return_with.value(1) },
* { }
* );
*
* if(some_other_cond_0,
* { doSomething(); },
* {
* if (some_other_cond_1,
* { return_with.value(2) },
* { if(some_other_cond_3, { return_with.value(3) }, { }); }
* );
* }
* );
*
* doSomething();
* return_with.value(0)
* }
* }
* ```
*
* ### Generate Context Relationship
*
* * `indent_level`:
* By default, each statement handle the indentation.
* As an exception here, since the return value contains
*/
declare function convertTSCodeBlockWithEarlyReturnToSC(stmts: ReadonlyArray<ts.Statement>, generator_context?: GeneratorContext): string;
declare function isTSFlowControlStatement(stmt: ts.Statement): stmt is ts.LabeledStatement | ts.IterationStatement | ts.TryStatement | ts.IfStatement | ts.SwitchStatement;
export { convertTSCodeBlockToSC, convertTSCodeBlockWithEarlyReturnToSC, hasEarlyReturnIn, isTSFlowControlStatement };