symbol-processor/src/property.ts
2024-07-14 22:10:04 -04:00

61 lines
1.6 KiB
TypeScript

import { INTERNAL, formatType } from './common';
export class Property {
readonly offset: number;
readonly #type: string;
readonly #name: string;
readonly #self: string;
// Constructor
constructor(offset: number, type: string, name: string, self: string) {
this.offset = offset;
this.#type = type;
this.#name = name;
this.#self = self;
}
// Getters
type() {
return `${INTERNAL}type_${this.fullName()}`;
}
typedef() {
let arrayInfo = '';
const arrayInfoIndex = this.#name.indexOf('[');
if (arrayInfoIndex !== -1) {
arrayInfo = this.#name.substring(arrayInfoIndex);
}
return `typedef ${formatType(this.#type)}${this.type()}${arrayInfo};\n`;
}
name() {
let name = this.#name;
const arrayInfoIndex = this.#name.indexOf('[');
if (arrayInfoIndex !== -1) {
name = name.substring(0, arrayInfoIndex);
}
return name;
}
#fullName(separator: string) {
return this.#self + separator + this.name();
}
fullName() {
return this.#fullName('_');
}
prettyName() {
return this.#fullName('::');
}
rawType() {
return this.#type;
}
}
export class StaticProperty extends Property {
// Constructor
constructor(address: number, type: string, name: string, self: string) {
super(address, type, name, self);
}
// Reference
referenceDefinition(addSelf: boolean) {
return `${this.type()} &${addSelf ? this.prettyName() : this.name()}`;
}
}