lispToMmix/js/execute.js
2022-10-14 19:30:47 +02:00

53 lines
No EOL
1.6 KiB
JavaScript

import { context } from "./ctx.js";
import { createType, error } from "./lexer.js";
import nativefunc from "./nativefunc.js";
import { COMPUTE_TYPES } from "./types.js";
let nid = 0;
export function execute({ data, target = 0, ctx = new context()}){
let [type, d] = createType(data);
if(type == "code"){
try {
let { type, code } = nativefunc[data[0]]({ execute: ({data, target=0}) => execute({data, target,ctx}),data,target,nid:()=>nid++,ctx});
return {code,type};
} catch (e) {
console.log(e);
error(`'${data[0]}' is not a function`,...data.pos);
}
}else if (type == "var"){
let { size, amount, type } = ctx.find(d+"V");
if (size <= 8 && amount == 1){
if(type == COMPUTE_TYPES.FLOAT){
return {
type: 2,
code: ` ${size > 4 ? "LDOU" :"SFLOT"} $${target},${d}V`
}
}else{
return {
type: type,
code: ` LD${(["B", "W", "T", "T", "O", "O", "O", "O"])[size-1]}${type==0?"U":""} $${target},${d}V`
}
}
}else{
return {
type: 0,
code: ` LDA $${target},${elem.name}`
}
}
}else if (type == "bool"){
return {
code: ` SET $${target},${d}`,
type: COMPUTE_TYPES.UINT
}
}else if (type == "num"){
return {
code: ` SET $${target},${d}`,
type: COMPUTE_TYPES.UINT
}
}
throw new Error("nothing found");
}