symbol-processor/src/property.ts

60 lines
1.5 KiB
TypeScript

import { 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 `__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() {
return `${this.#self}_${this.name()}`;
}
rawType() {
return this.#type;
}
}
export class StaticProperty extends Property {
// Constructor
constructor(address: number, type: string, name: string, self: string) {
super(address, type, name, self);
}
// Generate Variable Definition
globalPointerDefinition() {
return `${this.type()} *${this.fullName()}_pointer;\n`;
}
// Generate Macro
macro() {
return `#define ${this.fullName()} (*${this.fullName()}_pointer)\n`;
}
}