Skip to content

Commit

Permalink
Update main.js
Browse files Browse the repository at this point in the history
Dynamic input handling
  • Loading branch information
najmiter authored Dec 11, 2023
1 parent a021be2 commit ced655e
Showing 1 changed file with 68 additions and 137 deletions.
205 changes: 68 additions & 137 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,3 @@
const code = `
include irvine32.inc
.data
first dd 1011
second dd 80
third dd 25
ye_akhri_hai db 0
the_gorgeous_number dd 0
pretty_msg byte "The pretty number is >>> ", 0
.code
main proc
mov eax, first
mov ebx, second
cmp eax, ebx
jg move_first_operand
jmp move_second_operand
check_:
cmp ye_akhri_hai, 1
je ouwt
mov ye_akhri_hai, 1
mov eax, the_gorgeous_number
mov ebx, third
cmp eax, ebx
jg move_first_operand
jmp move_second_operand
move_first_operand:
mov the_gorgeous_number, eax
jmp check_
move_second_operand:
mov the_gorgeous_number, ebx
jmp check_
ouwt:
mov edx, offset pretty_msg
call WriteString
mov eax, the_gorgeous_number
call WriteDec
call CRLF
call ReadChar
invoke ExitProcess, 0
main endp
end main
`.split('\n');


const print = console.log;
const DOM = []

const Chitter = {
asm: {
arithmetics: new Set([
Expand Down Expand Up @@ -89,83 +27,76 @@ const Chitter = {
}
}


for (const line of code) {
const tokens = line.split(' ');
const line_ = [];

print(tokens);
let spaces = 0;
while (tokens[++spaces] === '');

for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
let klass = 'plain';
let is_jump = false;

let space_not_added = true;
// let count = 0;

if (token.length === 0 && space_not_added) {
line_.push('');
space_not_added = false;
continue;
}

// in_middle = true;

let comma = '';
if (token.includes(',')) {
token = token.substring(0, token.indexOf(','));
comma = ',';
}

if (!isNaN(token)) {
klass = 'constant'
} else if (token.endsWith(':')) {
klass = 'function-label';
} else {
for (const key of Object.keys(Chitter.asm)) {
if (Chitter.asm[key].has(token.toUpperCase())) {
klass = key;

if (key === 'jumps') {
is_jump = true;
print(key)

// print(tokens)
while (tokens[++i] === '');

const t = tokens[i];
// print(t)
line_.push(`<span class="function-label">${t}</span>`);
}
break;
const hightlight_btn = document.getElementById("btn-highlight");
hightlight_btn.addEventListener("click", () => {
// const print = console.log;
const DOM = []

const code = document.getElementById("input-text").value.split("\n");
const print = console.log;
// print(code);

const DOM = [];

for (const line of code) {
const tokens = line.split(" ");
const line_ = [];
// print(tokens);

let spaces = 0;
while (tokens[spaces++] === "");

for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
let klass = "plain";
let is_jump = false;

if (!isNaN(token)) {
klass = "constant";
} else if (token.endsWith(":")) {
klass = "function-label";
} else {
for (const key of Object.keys(Chitter.asm)) {
if (Chitter.asm[key].has(token.toUpperCase())) {
klass = key;

if (key === "jumps") {
is_jump = true;

const t = tokens[i];
// print(t)
line_.push(
`<span class="function-label">${t}</span>`
);
}
break;
}
}
}

let empty = "";
while (--spaces >= 0) empty += "&nbsp";
// print(empty);

if (is_jump) {
line_.unshift(`<span class="${klass}">${token}</span>`);
} else {
line_.push(`<span class="${klass}">${token}</span>`);
}

// if (comma.length !== 0) {
// line_.push(`<span class="plain">,</span>`);
// }
}
}
}

let empty = '';
while (--spaces >= 0) empty += ' ';

if (is_jump) {
line_.unshift(`${empty}<span class="${klass}">${token}</span>`);
} else {
line_.push(`<span class="${klass}">${token}</span>`);
}
const newbie = document.createElement("code");
newbie.innerHTML = line_.join(" ") + "<br>";

if (comma.length !== 0) {
line_.push(`<span class="plain">,</span>`);
DOM.push(newbie);
}
}

const newbie = document.createElement('code');
newbie.innerHTML = line_.join(' ') + '<br>';

DOM.push(newbie);
}
const output = document.getElementById("output-text");
output.innerHTML = "";
// print(DOM);
DOM.forEach((e) => output.appendChild(e));
});

const pre = document.createElement('pre')
const body = document.querySelector('body');
body.appendChild(pre);
DOM.forEach(e => pre.appendChild(e));

0 comments on commit ced655e

Please sign in to comment.