lispToMmix/js/nativefuncs/vars.js

56 lines
No EOL
1.9 KiB
JavaScript

import { createType, error, LISPcmd } from "../lexer.js";
import { COMPUTE_TYPES, convertType } from "../types.js";
export default {
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], vType: "V", size: varType.size, amount: 1, type: varType.type, content: param });
return {
code: "",
type: 0
}
}
},
set: ({ execute, data, target, nid, ctx }) => {
let toSet = ctx.find(data[1],"V");
let { code, type } = execute({ data: data[2], target });
if(toSet.type == COMPUTE_TYPES.FLOAT){
return {
code: `${code}
${convertType(type, toSet.type, target)}
${toSet.size > 4 ? "STOU" : "STSF"} $${target},${toSet.name}`,
type: toSet.type
};
}else{
return {
code: `${code}
${convertType(type, toSet.type, target)}
ST${(["B", "W", "T", "T", "O", "O", "O", "O"])[toSet.size - 1]}${type == 0 ? "U" : ""} $${target},${toSet.name}`,
type: toSet.type
};
}
},
defarr: ({ execute, data, target, nid, ctx }) => {
let param = data.array.slice(3, -1).map(d => Number(d));
let varType = ctx.getType(data[2]);
let amount = param.reduce((v, c) => v * c, 1);
ctx.add({ name: data[1], vType:"V", size: varType.size, amount: amount, type: varType.type, config: param, content: data.array.slice(-1)[0] });
return {
code: "",
type: 0
};
},
}