{"version":3,"file":"semiliteral.mjs","names":[],"sources":["../../../src/expression/definitions/semiliteral.ts"],"sourcesContent":["import {ValueType, array} from '../types';\n\nimport type {Expression} from '../expression';\nimport type {ParsingContext} from '../parsing_context';\nimport type {EvaluationContext} from '../evaluation_context';\nimport type {Type} from '../types';\nimport {typeOf, Value, isValue} from '../values';\nimport {Literal} from './literal';\n\nexport class Semiliteral implements Expression {\n    type: Type;\n    arr: Array<Expression>;\n\n    constructor(arr: Array<Expression>) {\n        let elementType: Type | null = null;\n        for (const expr of arr) {\n            if (!elementType) {\n                elementType = expr.type;\n            } else if (elementType === expr.type) {\n                continue;\n            } else {\n                elementType = ValueType;\n                break;\n            }\n        }\n        this.type = array(elementType ?? ValueType, arr.length);\n        this.arr = arr;\n    }\n\n    static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression {\n        if (args.length !== 2)\n            return context.error(\n                `'semiliteral' expression requires exactly one argument, but found ${args.length - 1} instead.`\n            ) as null;\n\n        if (!isValue(args[1]))\n            return context.error(`invalid value of type \"${typeof args[1]}\"`) as null;\n\n        const value = args[1] as Value;\n        const type = typeOf(value);\n\n        if (type.kind === 'array') {\n            const arr = value as Array<unknown>;\n            const arrayContext = context.concat(1);\n            const parsed: Expression[] = [];\n            for (let i = 0; i < arr.length; i++) {\n                const item = arrayContext.parse(arr[i], i, ValueType);\n                if (!item) return null;\n                parsed.push(item);\n            }\n            return new Semiliteral(parsed);\n        } else {\n            return new Literal(type, value);\n        }\n    }\n\n    evaluate(ctx: EvaluationContext): Array<Value> {\n        return this.arr.map((arg) => arg.evaluate(ctx));\n    }\n\n    eachChild(fn: (_: Expression) => void) {\n        this.arr.forEach(fn);\n    }\n\n    outputDefined() {\n        return this.arr.every((arg) => arg.outputDefined());\n    }\n}\n"],"mappings":";;;;AASA,IAAa,cAAb,MAAa,YAAkC;CAI3C,YAAY,KAAwB;EAChC,IAAI,cAA2B;EAC/B,KAAK,MAAM,QAAQ,KACf,IAAI,CAAC,aACD,cAAc,KAAK;OAChB,IAAI,gBAAgB,KAAK,MAC5B;OACG;GACH,cAAc;GACd;EACJ;EAEJ,KAAK,OAAO,MAAM,eAAe,WAAW,IAAI,MAAM;EACtD,KAAK,MAAM;CACf;CAEA,OAAO,MAAM,MAA8B,SAAqC;EAC5E,IAAI,KAAK,WAAW,GAChB,OAAO,QAAQ,MACX,qEAAqE,KAAK,SAAS,EAAE,UACzF;EAEJ,IAAI,CAAC,QAAQ,KAAK,EAAE,GAChB,OAAO,QAAQ,MAAM,0BAA0B,OAAO,KAAK,GAAG,EAAE;EAEpE,MAAM,QAAQ,KAAK;EACnB,MAAM,OAAO,OAAO,KAAK;EAEzB,IAAI,KAAK,SAAS,SAAS;GACvB,MAAM,MAAM;GACZ,MAAM,eAAe,QAAQ,OAAO,CAAC;GACrC,MAAM,SAAuB,CAAC;GAC9B,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;IACjC,MAAM,OAAO,aAAa,MAAM,IAAI,IAAI,GAAG,SAAS;IACpD,IAAI,CAAC,MAAM,OAAO;IAClB,OAAO,KAAK,IAAI;GACpB;GACA,OAAO,IAAI,YAAY,MAAM;EACjC,OACI,OAAO,IAAI,QAAQ,MAAM,KAAK;CAEtC;CAEA,SAAS,KAAsC;EAC3C,OAAO,KAAK,IAAI,KAAK,QAAQ,IAAI,SAAS,GAAG,CAAC;CAClD;CAEA,UAAU,IAA6B;EACnC,KAAK,IAAI,QAAQ,EAAE;CACvB;CAEA,gBAAgB;EACZ,OAAO,KAAK,IAAI,OAAO,QAAQ,IAAI,cAAc,CAAC;CACtD;AACJ"}