lispToMmix/js/execute.js

65 lines
No EOL
2.1 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()}){
if(target > 255) error("To much registers are required to run this. Support for this case will be build in later!",...data.pos);
let [type, d] = createType(data);
if(type == "code"){
try {
let { type, code } = nativefunc[data[0]]({
execute: ({ data, target = 0, ctx:contx = ctx }) => execute({ data, target, ctx:contx }),
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, name } = ctx.find(d,"V",data.pos);
if (size <= 8 && amount == 1){
if(type == COMPUTE_TYPES.FLOAT){
return {
type: 2,
code: ` ${size > 4 ? "LDOU" :"LDSF"} $${target},${name}`
}
}else{
return {
type: type,
code: ` LD${(["B", "W", "T", "T", "O", "O", "O", "O"])[size-1]}${type==0?"U":""} $${target},${name}`
}
}
}else{
return {
type: 0,
code: ` LDA $${target},${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
}
}else if (type == "str"){
return {
code: ` SET $${target},"${d}"`,
type: COMPUTE_TYPES.UINT
}
}
throw new Error("nothing found");
}