import type { Expression } from '@cortex-js/compute-engine' ; import { v4 as uuidv4 } from 'uuid' ; import { usePyodide } from './pyodide-cas' ; export interface UseCas { doneLoading : Promise < void > ; executeCommand ( command ) : void ; cancelCommand ( command ) : void ; } /**
 *
 *
 * Equal: Numerical evaluation
 *
 * Evaluate: Symbolical evaluation
 *
 * TODO: Apply: | apply *3 | apply +2 and things like that
 */ export type CasExpression = [ 'Equal' | 'Evaluate' , ... Expression [ ] ] ; /**
 * A command has one expression. It is expected that all expressions/commands are submitted and executed in order.
 * We'll only parse the topmost "Equal"/"Evaluate". If an expression has multiple instances of those, the user is expected to create multiple CAS commands.
 *
 * The expression can have some direct getters (e.g. 3x+5+1 has `x` as a getter)
 * Those getters can have a value with some *undefined* symbols. For example `x = 3`, `x = 7cm` and `x = undefined` are all valid values.
 * Notably, a getter with another getter is not okay. We do not recursively evaluate getters, instead it's expected that it has already been evaluated.
 */ export class CasCommand { readonly id : string ; readonly gettersData : Map < string , any > ; readonly expression : CasExpression ; readonly callback : ( result : any | Error ) => void ; constructor ( gettersData , expression , callback ) { this . id = uuidv4 ( ) ; this . gettersData = gettersData ; this . expression = expression ; this . callback = callback ; } } export function useCas ( ) { const cas = usePyodide ( ) ; function executeCommand ( command ) { console . log ( 'Executing' , command ) ; cas . executeCommand ( command ) ; } function cancelCommand ( command ) { cas . cancelCommand ( command ) ; } return { doneLoading : cas . doneLoading , executeCommand , cancelCommand , } ; }