import type { NBTObject } from 'sandstone/arguments' import type { DataPointPickClass, MCFunctionNode, SandstoneCore } from 'sandstone/core' import { ContainerNode } from 'sandstone/core' import type { DataPointClass } from 'sandstone/variables' import { Score } from 'sandstone/variables' import type { Condition } from './Flow' /** A condition callback that receives the switch value and returns a condition */ export type ConditionCallback = (value: ValueType) => Condition /** Widen literal types to their base types for better inference */ type Widen = T extends number ? number : T /** Static value case */ type StaticCaseEntry = { type: 'static'; value: Widen; callback: () => any } /** Condition callback case */ type ConditionCaseEntry = { type: 'condition'; condition: ConditionCallback; callback: () => any } /** Either a static or condition case entry */ type CaseEntry = StaticCaseEntry | ConditionCaseEntry /** Static value case tuple */ export type StaticCase = readonly ['case', Widen, () => any] /** Condition callback case tuple */ export type ConditionCase = readonly ['case', ConditionCallback, () => any] /** Either a static or condition case */ export type SwitchCase = StaticCase | ConditionCase export type DefaultType = { entries: CaseEntry[] default: () => any } export class CaseStatement { constructor( readonly entries: CaseEntry[] = [], ) {} /** @internal */ getStaticCases(): StaticCaseEntry[] { return this.entries.filter((e): e is StaticCaseEntry => e.type === 'static') } /** @internal */ getConditionCases(): ConditionCaseEntry[] { return this.entries.filter((e): e is ConditionCaseEntry => e.type === 'condition') } /** Add a static value case */ case(value: ValueType, callback: () => any): CaseStatement /** Add a condition case - checked after static cases fail. Use generic to specify switch value type on first condition: `.case(v => ...)` */ case(condition: ConditionCallback, callback: () => any): CaseStatement case( valueOrCondition: ValueType | ConditionCallback, callback: () => any, ) { if (typeof valueOrCondition === 'function') { return new CaseStatement([ ...this.entries as CaseEntry[], { type: 'condition', condition: valueOrCondition, callback }, ]) } return new CaseStatement([ ...this.entries, { type: 'static', value: valueOrCondition as Widen, callback }, ]) } default(callback: () => any): DefaultType { return { entries: this.entries, default: callback, } } } /** * AST node representing a switch statement. * Transformed into MCFunctions by SwitchTransformationVisitor during save(). */ export class SwitchNode extends ContainerNode { parentMCFunction: MCFunctionNode constructor( sandstoneCore: SandstoneCore, public value: DataPointClass | DataPointPickClass | Score, public staticCases: StaticCaseNode[], public conditionCases: ConditionCaseNode[], public defaultCase: DefaultCaseNode | undefined, ) { super(sandstoneCore) this.parentMCFunction = sandstoneCore.getCurrentMCFunctionOrThrow() // Generate case bodies within switch context. Pop the stack fully (not // Each case uses `balanceContext` so awaits inside a case callback // don't leak into the switch's (or sibling cases') context. this.parentMCFunction.balanceContext(this, () => { for (const caseNode of [...staticCases, ...conditionCases]) { this.parentMCFunction.balanceContext(caseNode, caseNode.callback.bind(caseNode)) } if (defaultCase) { this.parentMCFunction.balanceContext(defaultCase, defaultCase.callback.bind(defaultCase)) } }) } getValue = () => { throw new Error('SwitchNode must be transformed by SwitchTransformationVisitor') } } /** * AST node representing a static value case in a switch statement. */ export class StaticCaseNode extends ContainerNode { constructor( sandstoneCore: SandstoneCore, public value: number | NBTObject, public callback: () => any, ) { super(sandstoneCore) } getValue = () => null } /** * AST node representing a condition-based case in a switch statement. */ export class ConditionCaseNode extends ContainerNode { constructor( sandstoneCore: SandstoneCore, public condition: ConditionCallback, public callback: () => any, ) { super(sandstoneCore) } getValue = () => null } /** * AST node representing the default case in a switch statement. */ export class DefaultCaseNode extends ContainerNode { constructor( sandstoneCore: SandstoneCore, public callback: () => any, ) { super(sandstoneCore) } getValue = () => null } function isConditionCase( c: ConditionCallback | NBTObject, ): c is ConditionCallback { return typeof c === 'function' } export function executeSwitch< ValueType extends DataPointClass | DataPointPickClass | Score, CheckType extends ValueType extends Score ? number : NBTObject, >( sandstoneCore: SandstoneCore, value: ValueType, _cases: SwitchCase[] | [...SwitchCase[], ['default', () => any]] | CaseStatement | DefaultType, ) { let staticCaseEntries: StaticCaseEntry[] = [] let conditionCaseEntries: ConditionCaseEntry[] = [] let _default: ['default', () => {}] | undefined // Parse cases from the various input formats if (_cases instanceof CaseStatement) { staticCaseEntries = _cases.getStaticCases() conditionCaseEntries = _cases.getConditionCases() as ConditionCaseEntry[] } else if (Array.isArray(_cases)) { for (const _case of _cases) { if (_case[0] === 'default') { _default = _case continue } const [, condition, callback] = _case if (isConditionCase(condition)) { conditionCaseEntries.push({ type: 'condition' as const, condition, callback }) } else { staticCaseEntries.push({ type: 'static' as const, value: condition, callback }) } } } else { for (const _case of _cases.entries) { if (_case.type === 'condition') { conditionCaseEntries.push(_case) } else { staticCaseEntries.push(_case) } } if (_cases.default) { _default = ['default', _cases.default] } } // Create case nodes const staticCaseNodes = staticCaseEntries.map( (c) => new StaticCaseNode(sandstoneCore, c.value, c.callback), ) const conditionCaseNodes = conditionCaseEntries.map( (c) => new ConditionCaseNode(sandstoneCore, c.condition, c.callback), ) const defaultCaseNode = _default ? new DefaultCaseNode(sandstoneCore, _default[1]) : undefined // Create SwitchNode (commits to current MCFunction context) // The node constructor executes callbacks to populate case body nodes // SwitchTransformationVisitor transforms this into MCFunctions during save() new SwitchNode( sandstoneCore, value, staticCaseNodes, conditionCaseNodes, defaultCaseNode, ) }