Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor : For ts integration : src/simulator/src/verilogHelpers.ts #443

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions src/simulator/src/verilogHelpers.js

This file was deleted.

85 changes: 85 additions & 0 deletions src/simulator/src/verilogHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
interface Node {
verilogLabel?: string;
parent: {
verilogLabel: string;
};
label?: string;
}

/**
* Replaces spaces in the string with underscores, except for the last space.
* @param str - The input string.
* @returns The string with spaces replaced by underscores.
*/
function replaceSpaces(str: string): string {
if (str.search(/ /g) < str.length - 1 && str.search(/ /g) >= 0) {
return str.replace(/ Inverse/g, '_inv').replace(/ /g, '_');
}
return str;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Escapes the string if it starts with a number or contains non-alphanumeric characters.
* @param str - The input string.
* @returns The escaped string.
*/
function escapeString(str: string): string {
if (str.substring(0, 1).search(/\\/g) < 0) {
if (str.search(/[\W]/g) > -1 || str.substring(0, 1).search(/[0-9]/g) > -1) {
return `\\${str} `;
}
}
return str;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Sanitizes a label by replacing spaces and escaping special characters.
* @param name - The label to sanitize.
* @returns The sanitized label.
*/
export function sanitizeLabel(name: string): string {
let temp = replaceSpaces(name);
return escapeString(temp);
}

/**
* Generates the base node name based on the node's label or a default value.
* @param node - The node object.
* @param currentCount - The current count of nodes.
* @param totalCount - The total count of nodes.
* @returns The base node name.
*/
function getBaseNodeName(node: Node, currentCount: number, totalCount: number): string {
return node.label ? sanitizeLabel(node.label) : totalCount > 1 ? `out_${currentCount}` : 'out';
}

/**
* Constructs the final node name based on the parent's label and the base node name.
* @param parentVerilogLabel - The parent's label.
* @param nodeName - The base node name.
* @returns The final node name.
*/
function constructFinalNodeName(parentVerilogLabel: string, nodeName: string): string {
if (parentVerilogLabel.substring(0, 1).search(/\\/g) < 0) {
return `${parentVerilogLabel}_${nodeName}`;
} else {
return `${parentVerilogLabel.substring(0, parentVerilogLabel.length - 1)}_${nodeName} `;
}
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Generates a node name based on the node's properties and context.
* @param node - The node object.
* @param currentCount - The current count of nodes.
* @param totalCount - The total count of nodes.
* @returns The generated node name.
*/
export function generateNodeName(node: Node, currentCount: number, totalCount: number): string {
if (node.verilogLabel) {
return node.verilogLabel;
}

const parentVerilogLabel = node.parent.verilogLabel;
const nodeName = getBaseNodeName(node, currentCount, totalCount);
return constructFinalNodeName(parentVerilogLabel, nodeName);
}