lispToMmix/js/execute.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-10-14 19:30:47 +02:00
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);
2022-10-14 19:30:47 +02:00
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
});
2022-10-14 19:30:47 +02:00
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);
2022-10-14 19:30:47 +02:00
if (size <= 8 && amount == 1){
if(type == COMPUTE_TYPES.FLOAT){
return {
type: 2,
code: ` ${size > 4 ? "LDOU" :"LDSF"} $${target},${name}`
2022-10-14 19:30:47 +02:00
}
}else{
return {
type: type,
code: ` LD${(["B", "W", "T", "T", "O", "O", "O", "O"])[size-1]}${type==0?"U":""} $${target},${name}`
2022-10-14 19:30:47 +02:00
}
}
}else{
return {
type: 0,
code: ` LDA $${target},${name}`
2022-10-14 19:30:47 +02:00
}
}
}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
}
2022-10-14 19:30:47 +02:00
}
throw new Error("nothing found");
}