lispToMmix/js/ctx.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-10-15 11:15:20 +02:00
import { link } from "fs";
2022-10-14 19:30:47 +02:00
import { error } from "./lexer.js";
export class context{
#list = {};
2022-10-15 11:15:20 +02:00
#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"},
};
2022-10-14 19:30:47 +02:00
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;
}
2022-10-15 11:15:20 +02:00
getType(name){
let type;
do{
if (type) type = this.#types[type.link]
else type = this.#types[name];
}while(type.link);
return type;
}
2022-10-14 19:30:47 +02:00
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;
}
}