mirror of
https://github.com/dwolfe884/obsidian-x86-flow-graph.git
synced 2026-07-22 07:30:26 +00:00
Removed import, moved everything inside class, added more this. calls.
This commit is contained in:
parent
ded6a2bfc7
commit
28542b3492
2 changed files with 251 additions and 249 deletions
343
main.js
343
main.js
|
|
@ -28,213 +28,216 @@ __export(src_exports, {
|
|||
});
|
||||
module.exports = __toCommonJS(src_exports);
|
||||
var import_obsidian = require("obsidian");
|
||||
var nodeid = 1;
|
||||
var edgeid = 5555;
|
||||
var workingx = 0;
|
||||
var workingy = 0;
|
||||
var nodes = [];
|
||||
var edges = [];
|
||||
var lines = [];
|
||||
var fromnode = -1;
|
||||
var locations = {};
|
||||
var visitslocs = {};
|
||||
var colorGreen = "4";
|
||||
var x86_flow_graph = class extends import_obsidian.Plugin {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.nodeid = 1;
|
||||
this.edgeid = 5555;
|
||||
this.workingx = 0;
|
||||
this.workingy = 0;
|
||||
this.nodes = [];
|
||||
this.edges = [];
|
||||
this.lines = [];
|
||||
this.fromnode = -1;
|
||||
this.locations = {};
|
||||
this.visitslocs = {};
|
||||
this.colorGreen = "4";
|
||||
this.colorRed = "1";
|
||||
}
|
||||
async onload() {
|
||||
this.addCommand({
|
||||
id: "x86-create-flow-diagram",
|
||||
name: "Convert x86 assembly into a flow diagram on a canvas",
|
||||
editorCallback: (editor, view) => {
|
||||
lines = [];
|
||||
nodes = [];
|
||||
nodes = [];
|
||||
edges = [];
|
||||
visitslocs = {};
|
||||
locations = {};
|
||||
nodeid = 1;
|
||||
edgeid = 5555;
|
||||
workingx = 0;
|
||||
workingy = 0;
|
||||
this.lines = [];
|
||||
this.nodes = [];
|
||||
this.edges = [];
|
||||
this.visitslocs = {};
|
||||
this.locations = {};
|
||||
this.nodeid = 1;
|
||||
this.edgeid = 5555;
|
||||
this.workingx = 0;
|
||||
this.workingy = 0;
|
||||
const tmp = editor.getSelection().split("\n");
|
||||
tmp.forEach((element) => {
|
||||
if (element != "" && !element.contains("```")) {
|
||||
lines.push(element);
|
||||
this.lines.push(element);
|
||||
}
|
||||
});
|
||||
lines.forEach((line, linenum) => {
|
||||
this.lines.forEach((line, linenum) => {
|
||||
if (line[0] != " " && line[0] != " ") {
|
||||
let newkey = line.trim().split("#")[0].trim();
|
||||
if (newkey[newkey.length - 1] == ":") {
|
||||
newkey = newkey.slice(0, newkey.length - 1);
|
||||
}
|
||||
if (!(newkey in locations)) {
|
||||
locations[newkey] = linenum;
|
||||
visitslocs[newkey] = 0;
|
||||
if (!(newkey in this.locations)) {
|
||||
this.locations[newkey] = linenum;
|
||||
this.visitslocs[newkey] = 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
generatenodes(0, lines, fromnode, "");
|
||||
this.generatenodes(0, this.lines, this.fromnode, "");
|
||||
let outfile = "";
|
||||
const currfile = this.app.workspace.getActiveFile();
|
||||
const d = new Date();
|
||||
if (currfile) {
|
||||
outfile = currfile.parent.path + "/" + d.getTime() + ".canvas";
|
||||
}
|
||||
let finalCanvas = { nodes, edges };
|
||||
let finalCanvas = { nodes: this.nodes, edges: this.edges };
|
||||
this.app.vault.create(outfile, JSON.stringify(finalCanvas));
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
function generatenodes(linenum, text, fromnode2, edgelabel) {
|
||||
const retarray = MakeNodeFromLineToNextJump(linenum, text, fromnode2, edgelabel);
|
||||
let newnode = retarray[0];
|
||||
let whereto = retarray[1];
|
||||
if (newnode != null) {
|
||||
fromnode2 = newnode["id"];
|
||||
const wegood = nodeAlredyAdded(newnode);
|
||||
if (wegood) {
|
||||
return;
|
||||
} else {
|
||||
nodes.push(newnode);
|
||||
generatenodes(linenum, text, fromnode, edgelabel) {
|
||||
const retarray = this.MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel);
|
||||
let newnode = retarray[0];
|
||||
let whereto = retarray[1];
|
||||
if (newnode != null) {
|
||||
fromnode = newnode["id"];
|
||||
const wegood = this.nodeAlredyAdded(newnode);
|
||||
if (wegood) {
|
||||
return;
|
||||
} else {
|
||||
this.nodes.push(newnode);
|
||||
}
|
||||
}
|
||||
if (whereto == "fin") {
|
||||
return;
|
||||
}
|
||||
edgelabel = "";
|
||||
if (whereto.length != 1) {
|
||||
edgelabel = "true";
|
||||
}
|
||||
if (!(whereto[0] in this.locations)) {
|
||||
newnode = this.generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1);
|
||||
const newedge = { id: this.edgeid.toString(), fromNode: this.nodes[this.nodes.length - 1].id.toString().toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: "true", color: this.colorGreen };
|
||||
this.edges.push(newedge);
|
||||
this.nodes.push(newnode);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
} else {
|
||||
this.generatenodes(this.locations[whereto[0]], text, fromnode, edgelabel);
|
||||
}
|
||||
if (whereto.length == 2) {
|
||||
this.generatenodes(whereto[1], text, fromnode, "false");
|
||||
}
|
||||
}
|
||||
if (whereto == "fin") {
|
||||
return;
|
||||
}
|
||||
edgelabel = "";
|
||||
if (whereto.length != 1) {
|
||||
edgelabel = "true";
|
||||
nodeAlredyAdded(checknode) {
|
||||
let retval = false;
|
||||
this.nodes.forEach((node) => {
|
||||
if (checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]) {
|
||||
retval = true;
|
||||
}
|
||||
});
|
||||
return retval;
|
||||
}
|
||||
if (!(whereto[0] in locations)) {
|
||||
newnode = generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1);
|
||||
const newedge = { id: edgeid.toString(), fromNode: (nodeid - 2).toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: "true", color: colorGreen };
|
||||
edges.push(newedge);
|
||||
nodes.push(newnode);
|
||||
edgeid = edgeid + 1;
|
||||
} else {
|
||||
generatenodes(locations[whereto[0]], text, fromnode2, edgelabel);
|
||||
}
|
||||
if (whereto.length == 2) {
|
||||
generatenodes(whereto[1], text, fromnode2, "false");
|
||||
}
|
||||
return;
|
||||
}
|
||||
function nodeAlredyAdded(checknode) {
|
||||
let retval = false;
|
||||
nodes.forEach((node) => {
|
||||
if (checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]) {
|
||||
retval = true;
|
||||
MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel) {
|
||||
let currnode = "```\n";
|
||||
let i = linenum;
|
||||
let newnode;
|
||||
let jmploc;
|
||||
let newedge;
|
||||
let edgecolor = "";
|
||||
let side = 1;
|
||||
if (edgelabel == "false") {
|
||||
edgecolor = "1";
|
||||
side = -1;
|
||||
} else if (edgelabel == "true") {
|
||||
edgecolor = "4";
|
||||
side = 1;
|
||||
}
|
||||
});
|
||||
return retval;
|
||||
}
|
||||
function MakeNodeFromLineToNextJump(linenum, text, fromnode2, edgelabel) {
|
||||
let currnode = "```\n";
|
||||
let i = linenum;
|
||||
let newnode;
|
||||
let jmploc;
|
||||
let newedge;
|
||||
let edgecolor = "";
|
||||
let side = 1;
|
||||
if (edgelabel == "false") {
|
||||
edgecolor = "1";
|
||||
side = -1;
|
||||
} else if (edgelabel == "true") {
|
||||
edgecolor = "4";
|
||||
side = 1;
|
||||
}
|
||||
while (i < text.length) {
|
||||
let line = text[i];
|
||||
if (line[0] == " " || line[0] == " ") {
|
||||
if (line.trim()[0] == "j") {
|
||||
currnode = currnode + line + "\n```";
|
||||
newnode = generateNewNode(currnode, linenum, i, side);
|
||||
if (fromnode2 != -1) {
|
||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: edgelabel, color: edgecolor };
|
||||
edges.push(newedge);
|
||||
edgeid = edgeid + 1;
|
||||
}
|
||||
if (line.trim().slice(0, 3) == "jmp") {
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
||||
} else {
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim(), i + 1];
|
||||
}
|
||||
i = text.length + 20;
|
||||
} else {
|
||||
currnode = currnode + line + "\n";
|
||||
}
|
||||
} else {
|
||||
line = line.trim();
|
||||
if (line[line.length - 1] == ":") {
|
||||
line = line.slice(0, line.length - 1);
|
||||
}
|
||||
if (visitslocs[line.trim()] == 0 && i != linenum) {
|
||||
currnode = currnode + "```";
|
||||
newnode = generateNewNode(currnode, linenum, i, side);
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
||||
if (fromnode2 != -1) {
|
||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
edges.push(newedge);
|
||||
edgeid = edgeid + 1;
|
||||
}
|
||||
i = text.length + 20;
|
||||
} else if (visitslocs[line.trim()] == 0 && i == linenum) {
|
||||
currnode = currnode + line + "\n";
|
||||
visitslocs[line.trim()] = nodeid;
|
||||
} else {
|
||||
if (currnode == "```\n") {
|
||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: visitslocs[line.trim()].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
edges.push(newedge);
|
||||
edgeid = edgeid + 1;
|
||||
} else {
|
||||
currnode = currnode + "\n```";
|
||||
newnode = generateNewNode(currnode, linenum, i, side);
|
||||
if (fromnode2 != -1) {
|
||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
edges.push(newedge);
|
||||
edgeid = edgeid + 1;
|
||||
newedge = { id: edgeid.toString(), fromNode: newnode["id"].toString(), fromSide: "bottom", toNode: visitslocs[line.trim()].toString(), toSide: "top", label: "" };
|
||||
edges.push(newedge);
|
||||
edgeid = edgeid + 1;
|
||||
while (i < text.length) {
|
||||
let line = text[i];
|
||||
if (line[0] == " " || line[0] == " ") {
|
||||
if (line.trim()[0] == "j") {
|
||||
currnode = currnode + line + "\n```";
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||
if (fromnode != -1) {
|
||||
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"], toSide: "top", label: edgelabel, color: edgecolor };
|
||||
this.edges.push(newedge);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
}
|
||||
if (line.trim().slice(0, 3) == "jmp") {
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
||||
} else {
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim(), i + 1];
|
||||
}
|
||||
i = text.length + 20;
|
||||
} else {
|
||||
currnode = currnode + line + "\n";
|
||||
}
|
||||
i = text.length + 20;
|
||||
jmploc = "fin";
|
||||
} else {
|
||||
line = line.trim();
|
||||
if (line[line.length - 1] == ":") {
|
||||
line = line.slice(0, line.length - 1);
|
||||
}
|
||||
if (this.visitslocs[line.trim()] == 0 && i != linenum) {
|
||||
currnode = currnode + "```";
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
|
||||
if (fromnode != -1) {
|
||||
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
this.edges.push(newedge);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
}
|
||||
i = text.length + 20;
|
||||
} else if (this.visitslocs[line.trim()] == 0 && i == linenum) {
|
||||
currnode = currnode + line + "\n";
|
||||
this.visitslocs[line.trim()] = this.nodeid;
|
||||
} else {
|
||||
if (currnode == "```\n") {
|
||||
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: this.visitslocs[line.trim()].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
this.edges.push(newedge);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
} else {
|
||||
currnode = currnode + "\n```";
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||
if (fromnode != -1) {
|
||||
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
this.edges.push(newedge);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
newedge = { id: this.edgeid.toString(), fromNode: newnode["id"].toString(), fromSide: "bottom", toNode: this.visitslocs[line.trim()].toString(), toSide: "top", label: "" };
|
||||
this.edges.push(newedge);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
}
|
||||
}
|
||||
i = text.length + 20;
|
||||
jmploc = "fin";
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
if (i != text.length + 21) {
|
||||
if (currnode == "```\n") {
|
||||
currnode = currnode + "End of assembly\n```";
|
||||
} else {
|
||||
currnode = currnode + "```";
|
||||
}
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side);
|
||||
jmploc = "fin";
|
||||
if (fromnode != -1) {
|
||||
newedge = { id: this.edgeid.toString(), fromNode: fromnode.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
this.edges.push(newedge);
|
||||
this.edgeid = this.edgeid + 1;
|
||||
}
|
||||
}
|
||||
i = i + 1;
|
||||
return [newnode, jmploc];
|
||||
}
|
||||
if (i != text.length + 21) {
|
||||
if (currnode == "```\n") {
|
||||
currnode = currnode + "End of assembly\n```";
|
||||
} else {
|
||||
currnode = currnode + "```";
|
||||
}
|
||||
newnode = generateNewNode(currnode, linenum, i, side);
|
||||
jmploc = "fin";
|
||||
if (fromnode2 != -1) {
|
||||
newedge = { id: edgeid.toString(), fromNode: fromnode2.toString(), fromSide: "bottom", toNode: newnode["id"].toString(), toSide: "top", label: edgelabel, color: edgecolor };
|
||||
edges.push(newedge);
|
||||
edgeid = edgeid + 1;
|
||||
}
|
||||
generateNewNode(nodeText, startLineNum, endLineNum, side) {
|
||||
const newnode = {
|
||||
type: "text",
|
||||
id: this.nodeid.toString(),
|
||||
x: this.workingx * side,
|
||||
y: this.workingy,
|
||||
width: 550,
|
||||
height: 35 * nodeText.split("\n").length,
|
||||
text: nodeText,
|
||||
startline: startLineNum,
|
||||
endline: endLineNum
|
||||
};
|
||||
this.workingy = this.workingy + 300;
|
||||
this.workingx = this.workingx + 50 * this.nodeid;
|
||||
this.nodeid = this.nodeid + 1;
|
||||
return newnode;
|
||||
}
|
||||
return [newnode, jmploc];
|
||||
}
|
||||
function generateNewNode(nodeText, startLineNum, endLineNum, side) {
|
||||
const newnode = {
|
||||
type: "text",
|
||||
id: nodeid.toString(),
|
||||
x: workingx * side,
|
||||
y: workingy,
|
||||
width: 550,
|
||||
height: 35 * nodeText.split("\n").length,
|
||||
text: nodeText,
|
||||
startline: startLineNum,
|
||||
endline: endLineNum
|
||||
};
|
||||
workingy = workingy + 300;
|
||||
workingx = workingx + 50 * nodeid;
|
||||
nodeid = nodeid + 1;
|
||||
return newnode;
|
||||
}
|
||||
};
|
||||
|
|
|
|||
157
src/index.ts
157
src/index.ts
|
|
@ -1,22 +1,23 @@
|
|||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace } from 'obsidian';
|
||||
import { CanvasTextData, CanvasColor, NodeSide, CanvasEdgeData, CanvasData } from 'obsidian/canvas';
|
||||
import * as internal from 'stream';
|
||||
|
||||
let nodeid = 1;
|
||||
let edgeid = 5555
|
||||
let workingx = 0;
|
||||
let workingy = 0;
|
||||
let nodes: any[] = []
|
||||
let edges: CanvasEdgeData[] = [] //{"id":"d1e0d15da69178a9","fromNode":"4018052da21dde12","fromSide":"bottom","toNode":"0afaa4e14a75cfe1","toSide":"top","label":"false"}
|
||||
let lines: string[] = []
|
||||
let fromnode = -1
|
||||
let locations: any = {}
|
||||
let visitslocs: any = {}
|
||||
let colorGreen: CanvasColor = "4"
|
||||
let colorRed: CanvasColor = "1"
|
||||
|
||||
|
||||
export default class x86_flow_graph extends Plugin {
|
||||
|
||||
nodeid = 1;
|
||||
edgeid = 5555
|
||||
workingx = 0;
|
||||
workingy = 0;
|
||||
nodes: any[] = []
|
||||
edges: CanvasEdgeData[] = [] //{"id":"d1e0d15da69178a9","fromNode":"4018052da21dde12","fromSide":"bottom","toNode":"0afaa4e14a75cfe1","toSide":"top","label":"false"}
|
||||
lines: string[] = []
|
||||
fromnode = -1
|
||||
locations: any = {}
|
||||
visitslocs: any = {}
|
||||
colorGreen: CanvasColor = "4"
|
||||
colorRed: CanvasColor = "1"
|
||||
|
||||
async onload(){
|
||||
// This adds the command to take selected text and create code flow diagram
|
||||
this.addCommand({
|
||||
|
|
@ -24,28 +25,27 @@ export default class x86_flow_graph extends Plugin {
|
|||
name: 'Convert x86 assembly into a flow diagram on a canvas',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
// Array of splt
|
||||
lines = [];
|
||||
nodes = [];
|
||||
nodes = [];
|
||||
edges = [];
|
||||
this.lines = [];
|
||||
this.nodes = [];
|
||||
this.edges = [];
|
||||
// Dictionary where keys = memory locations and values = if they have been seen in a node (1) or not (0)
|
||||
visitslocs = {};
|
||||
this.visitslocs = {};
|
||||
// Dictionary where keys = memory locations and values = line number
|
||||
locations = {};
|
||||
this.locations = {};
|
||||
//NodeID's and EdgeID's must be unique for each node in a canvas
|
||||
nodeid = 1;
|
||||
edgeid = 5555;
|
||||
workingx = 0;
|
||||
workingy = 0;
|
||||
this.nodeid = 1;
|
||||
this.edgeid = 5555;
|
||||
this.workingx = 0;
|
||||
this.workingy = 0;
|
||||
const tmp = editor.getSelection().split("\n")
|
||||
//For loop to remove blank lines and lines containing codeblock characters
|
||||
tmp.forEach(element => {
|
||||
if(element != "" && !element.contains("```")){
|
||||
lines.push(element)
|
||||
this.lines.push(element)
|
||||
}
|
||||
});
|
||||
|
||||
lines.forEach((line,linenum) => {
|
||||
this.lines.forEach((line,linenum) => {
|
||||
//Only look at lines that could be locations
|
||||
if(line[0] != '\t' && line[0] != " "){
|
||||
//Cut out white space and comments (#)
|
||||
|
|
@ -55,15 +55,15 @@ export default class x86_flow_graph extends Plugin {
|
|||
newkey = newkey.slice(0,newkey.length-1)
|
||||
}
|
||||
//Populate locations and visits array with line numbers and all 0's
|
||||
if (!(newkey in locations)) {
|
||||
locations[newkey] = linenum;
|
||||
visitslocs[newkey] = 0
|
||||
if (!(newkey in this.locations)) {
|
||||
this.locations[newkey] = linenum;
|
||||
this.visitslocs[newkey] = 0
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//Enter recursive function
|
||||
generatenodes(0,lines,fromnode,"")
|
||||
this.generatenodes(0,this.lines,this.fromnode,"")
|
||||
|
||||
//Get current directory to produce canvas in
|
||||
let outfile = ""
|
||||
|
|
@ -72,29 +72,27 @@ export default class x86_flow_graph extends Plugin {
|
|||
if(currfile){
|
||||
outfile = currfile.parent.path + "/" + d.getTime() + ".canvas"
|
||||
}
|
||||
let finalCanvas: CanvasData = {nodes: nodes, edges: edges}
|
||||
let finalCanvas: CanvasData = {nodes: this.nodes, edges: this.edges}
|
||||
this.app.vault.create(outfile,JSON.stringify(finalCanvas))
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//Recursive function alert! Runs itself once for uncondontional jumps (jmps) and twice for conditional jumps (jz)
|
||||
//Returns when it reaches the end of the assembly section OR if it returns to a code block it's seen before
|
||||
function generatenodes(linenum: any, text: any, fromnode: any, edgelabel: string){
|
||||
const retarray = MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel)
|
||||
generatenodes(linenum: any, text: any, fromnode: any, edgelabel: string){
|
||||
const retarray = this.MakeNodeFromLineToNextJump(linenum, text, fromnode, edgelabel)
|
||||
let newnode: any = retarray[0]
|
||||
let whereto: any = retarray[1]
|
||||
if(newnode != null){
|
||||
fromnode = newnode['id']
|
||||
//Check if node is already in list
|
||||
const wegood = nodeAlredyAdded(newnode)
|
||||
const wegood = this.nodeAlredyAdded(newnode)
|
||||
if(wegood){
|
||||
return
|
||||
}
|
||||
else{
|
||||
nodes.push(newnode)
|
||||
this.nodes.push(newnode)
|
||||
}
|
||||
}
|
||||
//We are at the end of the file
|
||||
|
|
@ -106,28 +104,28 @@ function generatenodes(linenum: any, text: any, fromnode: any, edgelabel: string
|
|||
edgelabel = "true"
|
||||
}
|
||||
//Error Handling for trying to jump to a non-existant location
|
||||
if(!(whereto[0] in locations)){
|
||||
if(!(whereto[0] in this.locations)){
|
||||
//Make the error node
|
||||
newnode = generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1)
|
||||
newnode = this.generateNewNode("```\nERROR: Jumping to non-existant\nlocation " + whereto[0].trim() + "\n```", -1, -1, 1)
|
||||
//nodeid-2 refers to the ID of the node that generated the error
|
||||
const newedge: CanvasEdgeData = {id:edgeid.toString(),fromNode:(nodeid-2).toString(),fromSide:"bottom",toNode:newnode['id'],toSide:"top",label:"true", color:colorGreen}
|
||||
edges.push(newedge)
|
||||
nodes.push(newnode)
|
||||
edgeid = edgeid + 1
|
||||
const newedge: CanvasEdgeData = {id:this.edgeid.toString(),fromNode:(this.nodes[this.nodes.length-1].id.toString()/*this.nodeid-2*/).toString(),fromSide:"bottom",toNode:newnode['id'],toSide:"top",label:"true", color:this.colorGreen}
|
||||
this.edges.push(newedge)
|
||||
this.nodes.push(newnode)
|
||||
this.edgeid = this.edgeid + 1
|
||||
}
|
||||
else{
|
||||
generatenodes(locations[whereto[0]], text, fromnode, edgelabel)
|
||||
this.generatenodes(this.locations[whereto[0]], text, fromnode, edgelabel)
|
||||
}
|
||||
if(whereto.length == 2){
|
||||
generatenodes(whereto[1], text, fromnode, "false")
|
||||
this.generatenodes(whereto[1], text, fromnode, "false")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//This function returns true if the node has already been added to the nodes array
|
||||
function nodeAlredyAdded(checknode: any){
|
||||
nodeAlredyAdded(checknode: any){
|
||||
let retval = false
|
||||
nodes.forEach(node => {
|
||||
this.nodes.forEach((node: { [x: string]: any; }) => {
|
||||
if(checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]){
|
||||
retval = true
|
||||
}
|
||||
|
|
@ -141,7 +139,7 @@ function nodeAlredyAdded(checknode: any){
|
|||
//text : full assembly code block
|
||||
//fromnode : nodeID of the previous node that needs to be connected via an edge (-1 if there isn't one)
|
||||
//edgelabel : "true","false", or "" to color and label the edges
|
||||
function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edgelabel: string) {
|
||||
MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edgelabel: string) {
|
||||
let currnode = "```\n"
|
||||
let i = linenum
|
||||
let newnode
|
||||
|
|
@ -164,12 +162,12 @@ function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edge
|
|||
//If the current instruction is a jump
|
||||
if(line.trim()[0] == 'j'){
|
||||
currnode = currnode + line + "\n```"
|
||||
newnode = generateNewNode(currnode, linenum, i, side)
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||
//Only parse the first node
|
||||
if(fromnode != -1){
|
||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"],toSide:"top",label:edgelabel, color:edgecolor}
|
||||
edges.push(newedge)
|
||||
edgeid = edgeid + 1
|
||||
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"],toSide:"top",label:edgelabel, color:edgecolor}
|
||||
this.edges.push(newedge)
|
||||
this.edgeid = this.edgeid + 1
|
||||
}
|
||||
if(line.trim().slice(0,3) == 'jmp'){
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
|
||||
|
|
@ -191,46 +189,46 @@ function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edge
|
|||
line = line.slice(0,line.length-1)
|
||||
}
|
||||
//Have we visited this location before (0 == no) and is this not the first line of a node?
|
||||
if(visitslocs[line.trim()] == 0 && i != linenum){
|
||||
if(this.visitslocs[line.trim()] == 0 && i != linenum){
|
||||
//Close the node text and create a node object
|
||||
currnode = currnode + "```"
|
||||
newnode = generateNewNode(currnode, linenum, i, side)
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||
//Set the jump location to the location name strimming away the command and comments
|
||||
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
|
||||
//If there is a node before the current one
|
||||
if(fromnode != -1){
|
||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||
edges.push(newedge)
|
||||
edgeid = edgeid + 1
|
||||
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||
this.edges.push(newedge)
|
||||
this.edgeid = this.edgeid + 1
|
||||
}
|
||||
i = text.length+20
|
||||
}
|
||||
//Have we visited this location before and is this the first line of a node?
|
||||
else if(visitslocs[line.trim()] == 0 && i == linenum){
|
||||
else if(this.visitslocs[line.trim()] == 0 && i == linenum){
|
||||
currnode = currnode + line + "\n"
|
||||
visitslocs[line.trim()] = nodeid
|
||||
this.visitslocs[line.trim()] = this.nodeid
|
||||
}
|
||||
//We have visited this before
|
||||
else{
|
||||
//If this is an empty node, just draw a line from the previous node to the already visited one
|
||||
if(currnode == "```\n"){
|
||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:visitslocs[line.trim()].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||
edges.push(newedge)
|
||||
edgeid = edgeid + 1
|
||||
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:this.visitslocs[line.trim()].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||
this.edges.push(newedge)
|
||||
this.edgeid = this.edgeid + 1
|
||||
}
|
||||
//If it's not empty make the node and draw 2 edges
|
||||
else{
|
||||
currnode = currnode + "\n```"
|
||||
newnode = generateNewNode(currnode, linenum, i, side)
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||
if(fromnode != -1){
|
||||
//Draw edge from previous node to the new node
|
||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||
edges.push(newedge)
|
||||
edgeid = edgeid + 1
|
||||
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel,color:edgecolor}
|
||||
this.edges.push(newedge)
|
||||
this.edgeid = this.edgeid + 1
|
||||
//Draw edge from new node to previously seen node
|
||||
newedge = {id:edgeid.toString(),fromNode:newnode["id"].toString(),fromSide:"bottom",toNode:visitslocs[line.trim()].toString(),toSide:"top",label:""}
|
||||
edges.push(newedge)
|
||||
edgeid = edgeid + 1
|
||||
newedge = {id:this.edgeid.toString(),fromNode:newnode["id"].toString(),fromSide:"bottom",toNode:this.visitslocs[line.trim()].toString(),toSide:"top",label:""}
|
||||
this.edges.push(newedge)
|
||||
this.edgeid = this.edgeid + 1
|
||||
}
|
||||
}
|
||||
i = text.length+20
|
||||
|
|
@ -250,24 +248,24 @@ function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any, edge
|
|||
else{
|
||||
currnode = currnode + "```"
|
||||
}
|
||||
newnode = generateNewNode(currnode, linenum, i, side)
|
||||
newnode = this.generateNewNode(currnode, linenum, i, side)
|
||||
jmploc = "fin"
|
||||
if(fromnode != -1){
|
||||
newedge = {id:edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel, color:edgecolor}
|
||||
edges.push(newedge)
|
||||
edgeid = edgeid + 1
|
||||
newedge = {id:this.edgeid.toString(),fromNode:fromnode.toString(),fromSide:"bottom",toNode:newnode["id"].toString(),toSide:"top",label:edgelabel, color:edgecolor}
|
||||
this.edges.push(newedge)
|
||||
this.edgeid = this.edgeid + 1
|
||||
}
|
||||
}
|
||||
return [newnode, jmploc]
|
||||
}
|
||||
|
||||
function generateNewNode(nodeText: string, startLineNum: any, endLineNum: any, side: any){
|
||||
generateNewNode(nodeText: string, startLineNum: any, endLineNum: any, side: any){
|
||||
//const newnode = {"id":nodeid, "x": workingx*side, "y": workingy, "width": 550,"height": 25*nodeText.split("\n").length, "type": "text", "text": nodeText, "startline": startLineNum,"endline": endLineNum}
|
||||
const newnode: x86Node = {
|
||||
type: 'text',
|
||||
id: nodeid.toString(),
|
||||
x: workingx*side,
|
||||
y: workingy,
|
||||
id: this.nodeid.toString(),
|
||||
x: this.workingx*side,
|
||||
y: this.workingy,
|
||||
width: 550,
|
||||
height: 35*nodeText.split("\n").length,
|
||||
text: nodeText,
|
||||
|
|
@ -275,11 +273,12 @@ function generateNewNode(nodeText: string, startLineNum: any, endLineNum: any, s
|
|||
endline: endLineNum
|
||||
};
|
||||
|
||||
workingy = workingy + 300
|
||||
workingx = workingx + (50*nodeid)
|
||||
nodeid = nodeid + 1
|
||||
this.workingy = this.workingy + 300
|
||||
this.workingx = this.workingx + (50*this.nodeid)
|
||||
this.nodeid = this.nodeid + 1
|
||||
return newnode
|
||||
}
|
||||
}
|
||||
|
||||
//Extending CanvasNodeData to add attributes for detecting repeated blocks of code
|
||||
interface x86Node extends CanvasTextData{
|
||||
|
|
|
|||
Loading…
Reference in a new issue