import { createType, error, LISPcmd } from "./lexer.js"; import { convertType, getOutType } from "./types.js" export default { "+": ({ execute, data, target, nid, ctx }) => { let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i ? 0 : 1) })); let outType = getOutType(...params.map(d => d.type)); return { type: outType, code: params.map((d, i) => { if (i == 0) return [ d.code, convertType(d.type, outType, target) ]; return [ d.code, convertType(d.type, outType, target + 1), ` ${(["ADDU", "ADD", "FADD"])[outType]} $${target},$${target},$${target + 1}` ]; }).flat(Infinity).join("\n") } }, "-": ({ execute, data, target, nid, ctx }) => { let params = data.array.slice(1).map((d, i) => execute({ data: d, target: target + (i ? 0 : 1) })); let outType = getOutType(...params.map(d => d.type)); return { type: outType, code: params.map((d, i) => { if (i == 0) return [ d.code, convertType(d.type, outType, target) ]; return [ d.code, convertType(d.type, outType, target + 1), ` ${(["SUBU", "SUB", "FSUB"])[outType]} $${target},$${target},$${target + 1}` ]; }).flat(Infinity).join("\n") } }, defvar: ({ execute, data, target, nid, ctx }) => { let param = data[3]; let [type, d] = createType(param); let varType = ctx.getType(data[2]); if (varType.content) error("A variable only can be created with a primitive Type.", ...data[2].pos); if (type == "var" || type == "code") { error("devfar with input is not implemented yet.", ...param.pos) } else { ctx.add({ name: data[1] + "V", size: varType.size, amount: 1, type: varType.type, content: param }); return { code: "", type: 0 } } }, defarr: ({ execute, data, target, nid, ctx }) => { let param = data.slice(3).map(d => Number(d)); let varType = ctx.getType(data[2]); let amount = param.reduce((v, c) => v * c, 1); ctx.add({ name: data[1] + "V", size: varType.size, amount: amount, type: varType.type, content: param }); return { code: "", type: 0 }; } }