lispToMmix/js/nativefuncs/math.js

81 lines
3.3 KiB
JavaScript
Raw Normal View History

import { argsCount } from "../errors.js";
import { convertType, getOutType } from "../types.js"
export default {
"+": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("+",2,data.pos);
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 }) => {
if (data.length < 3) argsCount("-", 2, data.pos);
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")
}
},
"*": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("*", 2, data.pos);
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),
` ${(["MULU", "MUL", "FMUL"])[outType]} $${target},$${target},$${target + 1}`
];
}).flat(Infinity).join("\n")
}
},
"/": ({ execute, data, target, nid, ctx }) => {
if (data.length < 3) argsCount("/", 2, data.pos);
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),
` ${(["DIVU", "DIV", "FDIV"])[outType]} $${target},$${target},$${target + 1}`
];
}).flat(Infinity).join("\n")
}
},
}