symbol-processor/src/property.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-05-03 04:46:21 +00:00
import { formatType } from './common';
2024-01-04 20:27:02 +00:00
2024-05-03 04:46:21 +00:00
export class Property {
readonly offset: number;
2024-01-04 20:27:02 +00:00
readonly #type: string;
readonly #name: string;
2024-05-03 04:46:21 +00:00
readonly #self: string;
2024-01-04 20:27:02 +00:00
// Constructor
2024-05-03 04:46:21 +00:00
constructor(offset: number, type: string, name: string, self: string) {
this.offset = offset;
2024-01-04 20:27:02 +00:00
this.#type = type;
this.#name = name;
2024-05-03 04:46:21 +00:00
this.#self = self;
2024-01-04 20:27:02 +00:00
}
// Getters
2024-05-03 04:46:21 +00:00
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`;
2024-01-04 20:27:02 +00:00
}
2024-05-03 04:46:21 +00:00
name() {
let name = this.#name;
const arrayInfoIndex = this.#name.indexOf('[');
if (arrayInfoIndex !== -1) {
name = name.substring(0, arrayInfoIndex);
2024-01-04 20:27:02 +00:00
}
2024-05-03 04:46:21 +00:00
return name;
2024-01-04 20:27:02 +00:00
}
2024-05-03 04:46:21 +00:00
fullName() {
return `${this.#self}_${this.name()}`;
2024-02-01 03:12:12 +00:00
}
2024-05-03 04:46:21 +00:00
rawType() {
return this.#type;
2024-01-04 20:27:02 +00:00
}
}
2024-05-03 04:46:21 +00:00
export class StaticProperty extends Property {
2024-01-04 20:27:02 +00:00
// Constructor
2024-05-03 04:46:21 +00:00
constructor(address: number, type: string, name: string, self: string) {
super(address, type, name, self);
2024-01-04 20:27:02 +00:00
}
// Generate Variable Definition
2024-05-03 04:46:21 +00:00
globalPointerDefinition() {
return `${this.type()} *${this.fullName()}_pointer;\n`;
2024-01-07 07:41:08 +00:00
}
// Generate Macro
2024-05-03 04:46:21 +00:00
macro() {
return `#define ${this.fullName()} (*${this.fullName()}_pointer)\n`;
2024-01-04 20:27:02 +00:00
}
}