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";
|
|
|
|
|
2022-10-15 23:25:07 +02:00
|
|
|
let count = 0;
|
|
|
|
|
2022-10-14 19:30:47 +02:00
|
|
|
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"},
|
2022-10-16 15:49:34 +02:00
|
|
|
c: {link:"u8"},
|
2022-10-15 11:15:20 +02:00
|
|
|
bool: {link:"u8"},
|
|
|
|
b: {link:"bool"},
|
|
|
|
};
|
2022-10-15 23:25:07 +02:00
|
|
|
#upper = [];
|
2022-10-16 15:49:34 +02:00
|
|
|
constructor(upper = null){
|
2022-10-15 23:25:07 +02:00
|
|
|
this.#upper = upper;
|
2022-10-14 19:30:47 +02:00
|
|
|
}
|
2022-10-15 23:25:07 +02:00
|
|
|
nextLevel(){
|
|
|
|
return new context(this);
|
|
|
|
}
|
|
|
|
add({name,vType,size,amount=1,type = 0, content = 0,config = [1]}){
|
|
|
|
if (!this.#list[vType]) this.#list[vType] = {};
|
|
|
|
this.#list[vType][name+""] = {name: vType+(count++),size,amount,type,content,config};
|
2022-10-14 19:30:47 +02:00
|
|
|
}
|
2022-10-15 23:25:07 +02:00
|
|
|
find(name, vType,pos=name.pos,quit=true){
|
|
|
|
let elem = (this.#list[vType]??{})[name+""]??null;
|
|
|
|
|
2022-10-16 15:49:34 +02:00
|
|
|
if (!elem && this.#upper){
|
|
|
|
elem = this.#upper.find(name+"",vType,pos,false);
|
2022-10-15 23:25:07 +02:00
|
|
|
}
|
|
|
|
if(!elem&&quit) error("Can not find '"+name+"' in context!",...pos);
|
2022-10-14 19:30:47 +02:00
|
|
|
return elem;
|
|
|
|
}
|
2022-10-15 11:15:20 +02:00
|
|
|
|
2022-10-15 23:25:07 +02:00
|
|
|
getType(name,pos=name.pos, quit = true){
|
|
|
|
let lastName = name;
|
2022-10-16 15:49:34 +02:00
|
|
|
let type = this.#types[name+""];
|
2022-10-15 23:25:07 +02:00
|
|
|
|
|
|
|
if(!type){
|
2022-10-16 15:49:34 +02:00
|
|
|
if(this.#upper) type = this.#upper.getType(name+"", pos, false);
|
2022-10-15 23:25:07 +02:00
|
|
|
if (!type) error("Can not find '" + name + "' in context", ...pos);
|
|
|
|
return type;
|
|
|
|
}else{
|
|
|
|
if(type.link){
|
|
|
|
type = this.getType(type.link,pos,false);
|
|
|
|
if (!type) error("Can not find '" + name + "' in context", ...pos);
|
|
|
|
return type;
|
|
|
|
}else{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*do{
|
|
|
|
|
2022-10-15 11:15:20 +02:00
|
|
|
if (type) type = this.#types[type.link]
|
|
|
|
else type = this.#types[name];
|
2022-10-15 23:25:07 +02:00
|
|
|
}while(type&&type.link);*/
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
addLinkType(name,link){
|
|
|
|
this.#types[name] = {link};
|
2022-10-15 11:15:20 +02:00
|
|
|
}
|
|
|
|
|
2022-10-14 19:30:47 +02:00
|
|
|
build(){
|
|
|
|
let out = `
|
|
|
|
LOC Data_Segment
|
|
|
|
GREG @
|
|
|
|
|
|
|
|
`;
|
2022-10-15 23:25:07 +02:00
|
|
|
|
|
|
|
for(let vType in this.#list){
|
|
|
|
for(let UName in this.#list[vType]){
|
|
|
|
let { size, amount, type, content, name } = this.#list[vType][UName];
|
|
|
|
if (!isNaN(content)) content = Number(content);
|
|
|
|
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`;
|
|
|
|
}
|
2022-10-14 19:30:47 +02:00
|
|
|
}
|
2022-10-15 23:25:07 +02:00
|
|
|
|
2022-10-14 19:30:47 +02:00
|
|
|
}
|
2022-10-15 23:25:07 +02:00
|
|
|
out +=" LOC #100\nMain SWYM\n";
|
2022-10-14 19:30:47 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|