import { CallExpr } from "../Expr" ; import { ExprEvaluatorContext , OperatorDescriptorMap } from "../ExprEvaluator" ; function compare ( context , call , strict = false ) { const left = context . evaluate ( call . args [ 0 ] ) as any ; const right = context . evaluate ( call . args [ 1 ] ) as any ; if ( ! ( ( typeof left === "number" && typeof right === "number" ) || ( typeof left === "string" && typeof right === "string" ) ) ) { if ( strict ) { throw new Error ( `invalid operands '${ left }' and '${ right }' for operator '${ call . op }'` ) ; } } switch ( call . op ) { case "<" : return left < right ; case ">" : return left > right ; case "<=" : return left <= right ; case ">=" : return left >= right ; default : throw new Error ( `invalid comparison operator '${ call . op }'` ) ; } } const operators = { "!" : { call : ( context , call ) => { return ! context . evaluate ( call . args [ 0 ] ) ; } } , "==" : { call : ( context , call ) => { const left = context . evaluate ( call . args [ 0 ] ) ; const right = context . evaluate ( call . args [ 1 ] ) ; return left === right ; } } , "!=" : { call : ( context , call ) => { const left = context . evaluate ( call . args [ 0 ] ) ; const right = context . evaluate ( call . args [ 1 ] ) ; return left !== right ; } } , "<" : { call : ( context , call ) => compare ( context , call ) } , ">" : { call : ( context , call ) => compare ( context , call ) } , "<=" : { call : ( context , call ) => compare ( context , call ) } , ">=" : { call : ( context , call ) => compare ( context , call ) } } ; export const ComparisonOperators = operators ; export type ComparisonOperatorNames = keyof typeof operators ;