lispToMmix/js/ctx.js
2022-10-15 11:15:20 +02:00

66 lines
No EOL
1.9 KiB
JavaScript

import { link } from "fs";
import { error } from "./lexer.js";
export class context{
#list = {};
#types = {
u8: { size: 1,type: 0, content: false },
u16: { size: 2,type: 0, content: false },
u32: { size: 4,type: 0, content: false },
u64: { size: 8,type: 0, content: false },
i8: { size: 1,type: 1, content: false },
i16: { size: 2,type: 1, content: false },
i32: { size: 4,type: 1, content: false },
i64: { size: 8,type: 1, content: false },
f32: { size: 4,type: 2, content: false },
f64: { size: 8,type: 2, content: false },
char: {link:"u8"},
bool: {link:"u8"},
b: {link:"bool"},
};
constructor(){
console.log("create");
}
add({name,size,amount=1,type = 0, content = 0}){
console.log("add",name+"");
this.#list[name+""] = {size,amount,type,content};
console.log(this.#list);
}
find(name,pos){
let elem = this.#list[name+""];
console.log(this.#list);
if(!elem) error("Can not find '"+name+"' in context!");
return elem;
}
getType(name){
let type;
do{
if (type) type = this.#types[type.link]
else type = this.#types[name];
}while(type.link);
return type;
}
build(){
let out = `
LOC Data_Segment
GREG @
`;
for(let Name in this.#list){
let { size, amount, type, content } = this.#list[Name];
if(size<=8&&amount==1){
out += `${Name} ${(["BYTE", "WYDE", "TETRA", "TETRA", "OCTA", "OCTA", "OCTA", "OCTA"])[size-1]} ${content}\n`;
}else{
out += `
${name} BYTE ${content}
LOC ${name}+${size+amount+1}\n`;
}
}
out+=" LOC #100\nMain";
return out;
}
}