@bitloops/bl-transpiler
Version:
The one and only Bitloops Language transpiler
176 lines • 5.41 kB
JavaScript
import { BitloopsTypesMapping } from '../../../../helpers/mappings.js';
import { ValidationError } from '../../types.js';
export const ROOT_TYPE = 'Root';
export class IntermediateASTNodeValidationError extends ValidationError {
}
export class IntermediateASTNode {
nodeType;
children;
nextSibling;
parent;
metaData;
value;
classNodeName;
constructor(nodeType, metadata, classNodeName) {
this.nodeType = nodeType;
this.metaData = metadata;
this.children = [];
this.classNodeName = classNodeName;
}
getValue() {
return this.value;
}
setValue(value) {
this.value = value;
}
getNodeType() {
return this.nodeType;
}
setNodeType(nodeType) {
this.nodeType = nodeType;
}
setClassNodeName(classNodeName) {
this.classNodeName = classNodeName;
}
getClassNodeName() {
return this.classNodeName;
}
getParent() {
return this.parent;
}
getChildren() {
return this.children;
}
isLeaf() {
return this.children.length == 0;
}
isRoot() {
return this.nodeType === ROOT_TYPE;
}
getMetadata() {
return this.metaData;
}
setMetadata(metaData) {
this.metaData = metaData;
}
setParent(parent) {
this.parent = parent;
}
addChild(childNode) {
childNode.setParent(this);
const numOfChildren = this.children.length;
if (numOfChildren > 0) {
const previousSibling = this.children[numOfChildren - 1];
previousSibling.addSibling(childNode);
}
this.children.push(childNode);
}
removeChild(childNode) {
const index = this.children.indexOf(childNode);
if (index === -1)
throw new Error('Could not find child');
if (index > 0) {
const previousSibling = this.children[index - 1];
previousSibling.addSibling(null);
}
this.children.splice(index, 1);
}
replaceChild(childNodeTobeReplaced, newChildNode) {
newChildNode.setParent(this);
const index = this.children.indexOf(childNodeTobeReplaced);
if (index === -1)
throw new Error('Could not find child');
if (index > 0) {
const previousSibling = this.children[index - 1];
previousSibling.addSibling(newChildNode);
}
this.children.splice(index, 1, newChildNode);
}
addSibling(siblingNode) {
this.nextSibling = siblingNode;
}
addSiblingBetween(siblingNode) {
const parent = this.getParent();
siblingNode.setParent(parent);
const nextSibling = this.nextSibling;
this.addSibling(siblingNode);
siblingNode.addSibling(nextSibling);
const allSiblings = parent.children;
const index = allSiblings.indexOf(this);
if (index === -1)
throw new Error('Could not find child');
allSiblings.splice(index + 1, 0, siblingNode);
}
getFirstChild() {
return this.children[0];
}
getNextSibling() {
return this.nextSibling ?? null;
}
hasChildren() {
return this.children.length > 0;
}
buildArrayValue() {
const children = this.getChildren();
this.value = { [this.classNodeName]: [] };
children.forEach((child) => {
this.value[this.classNodeName].push(child.value);
});
}
buildLeafValue(value) {
this.value = {
[this.classNodeName]: value,
};
}
buildObjectValue() {
const children = this.getChildren();
this.value = { [this.classNodeName]: {} };
children.forEach((child) => {
this.value[this.classNodeName] = { ...this.value[this.classNodeName], ...child.value };
});
}
validate(_symbolTable) {
return;
}
addToSymbolTable(_symbolTableManager) {
throw new Error('Method not implemented.');
}
static isIntermediateASTNodeValidationError(value) {
if (value instanceof IntermediateASTNodeValidationError) {
return true;
}
return false;
}
getStatementListNode() {
return this.getChildNodeByType(BitloopsTypesMapping.TStatements);
}
IsStatementListNode() {
return this.nodeType === BitloopsTypesMapping.TStatements;
}
IsEntityIdentifierNode() {
return this.nodeType === BitloopsTypesMapping.TEntityIdentifier;
}
getChildNodeByType(nodeType) {
const children = this.getChildren();
return children.find((child) => child.getNodeType() === nodeType) ?? null;
}
getChildrenNodesByType(nodeType) {
const children = this.getChildren();
return children.filter((child) => child.getNodeType() === nodeType) ?? null;
}
getChildNodeByClassNodeName(classNodeName) {
const children = this.getChildren();
return children.find((child) => child.getClassNodeName() === classNodeName) ?? null;
}
getFirstParentNodeByType(nodeType) {
let parent = this.getParent();
while (!parent.isRoot()) {
if (parent.getNodeType() === nodeType) {
return parent;
}
parent = parent.getParent();
}
return null;
}
}
//# sourceMappingURL=IntermediateASTNode.js.map