Skip to content

Commit

Permalink
Fixed lines numbers in assembler highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Apr 24, 2022
1 parent 61b6ac7 commit e55798d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
22 changes: 14 additions & 8 deletions dev/src/__tests__/asmHighlight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,55 @@ describe('Assembly compilation:', () => {
it('should highlight: regular opCodes', () => {
const code = 'SET @a #0000000000000100\nSET @b $a\nCLR @b\nINC @b\nDEC @a\nADD @a $b\nSUB @a $b\nMUL @a $b\nDIV @a $b\nBOR @a $b\nAND @a $b\nXOR @a $b\nSET @a $b\nNOT @a\nSET @a $($b)\nSET @a $c\nADD @a $b\nSET @a $($b + $c)\nPSH $b\nJSR :__fn_teste\nPOP @a\nSET @($a) $b\nSET @($a + $b) $c\nMOD @a $b\nSHL @a $b\nSHR @a $b\nSLP $a\nJMP :__fn_main\n\n__fn_teste:\nPOP @teste_d\nSET @r0 $teste_d\nINC @r0\nPSH $r0\nRET\n\n__fn_main:\nPCS\nINC @a\nFIN'
const highlighted = "<span class='asmInstruction'>SET </span><span class='asmVariable'>@a </span><span class='asmNumber'>#0000000000000100</span><br><span class='asmInstruction'>SET </span><span class='asmVariable'>@b </span><span class='asmVariable'>$a</span><br><span class='asmInstruction'>CLR </span><span class='asmVariable'>@b</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmInstruction'>DEC </span><span class='asmVariable'>@a</span><br><span class='asmInstruction'>ADD </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>SUB </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>MUL </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>DIV </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>BOR </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>AND </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>XOR </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>SET </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>NOT </span><span class='asmVariable'>@a</span><br><span class='asmInstruction'>SET </span><span class='asmVariable'>@a</span> $(<span class='asmVariable'>$b</span>)<br><span class='asmInstruction'>SET </span><span class='asmVariable'>@a </span><span class='asmVariable'>$c</span><br><span class='asmInstruction'>ADD </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>SET </span><span class='asmVariable'>@a </span>$(<span class='asmVariable'>$b</span> + <span class='asmVariable'>$c</span>)<br><span class='asmInstruction'>PSH </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>JSR </span><span class='asmLabel'>:__fn_teste</span><br><span class='asmInstruction'>POP </span><span class='asmVariable'>@a</span><br><span class='asmInstruction'>SET </span>@(<span class='asmVariable'>$a</span>) <span class='asmVariable'>$b</span><br><span class='asmInstruction'>SET </span>@(<span class='asmVariable'>$a</span> + <span class='asmVariable'>$b</span>) <span class='asmVariable'>$c</span><br><span class='asmInstruction'>MOD </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>SHL </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>SHR </span><span class='asmVariable'>@a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>SLP </span><span class='asmVariable'>$a</span><br><span class='asmInstruction'>JMP </span><span class='asmLabel'>:__fn_main</span><br><br><span class='asmLabel'>__fn_teste:</span><br><span class='asmInstruction'>POP </span><span class='asmVariable'>@teste_d</span><br><span class='asmInstruction'>SET </span><span class='asmVariable'>@r0 </span><span class='asmVariable'>$teste_d</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@r0</span><br><span class='asmInstruction'>PSH </span><span class='asmVariable'>$r0</span><br><span class='asmInstruction'>RET</span><br><br><span class='asmLabel'>__fn_main:</span><br><span class='asmInstruction'>PCS</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@a</span><br><span class='asmInstruction'>FIN</span><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight: opCodes for api functions', () => {
const code = 'FUN clear_A_B\nFUN set_A1 $a\nFUN set_A1_A2 $a $b\nFUN @a check_A_equals_B\nFUN @a add_Minutes_to_Timestamp $b $c\n'
const highlighted = "<span class='asmInstruction'>FUN clear_A_B</span><br><span class='asmInstruction'>FUN set_A1 </span><span class='asmVariable'>$a</span><br><span class='asmInstruction'>FUN set_A1_A2 </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>FUN </span><span class='asmVariable'>@a </span><span class='asmInstruction'>check_A_equals_B</span><br><span class='asmInstruction'>FUN </span><span class='asmVariable'>@a </span><span class='asmInstruction'>add_Minutes_to_Timestamp </span><span class='asmVariable'>$b </span><span class='asmVariable'>$c</span><br><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight: rare opCodes ', () => {
const code = 'FIZ $a\nSTZ $a\nERR :__error\nINC @a\nNOP\nNOP\n__error:\nDEC @a'
const highlighted = "<span class='asmInstruction'>FIZ </span><span class='asmVariable'>$a</span><br><span class='asmInstruction'>STZ </span><span class='asmVariable'>$a</span><br><span class='asmInstruction'>ERR </span><span class='asmLabel'>:__error</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@a</span><br><span class='asmInstruction'>NOP</span><br><span class='asmInstruction'>NOP</span><br><span class='asmLabel'>__error:</span><br><span class='asmInstruction'>DEC </span><span class='asmVariable'>@a</span><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight: all branches opCodes with positive offset (no overflow)', () => {
const code = 'BZR $a :__if1_endif\nINC @b\n__if1_endif:\nBNZ $a :__if2_endif\nINC @b\n__if2_endif:\nBLE $a $b :__if3_endif\nINC @b\n__if3_endif:\nBGE $a $b :__if4_endif\nINC @b\n__if4_endif:\nBLT $a $b :__if5_endif\nINC @b\n__if5_endif:\nBGT $a $b :__if6_endif\nINC @b\n__if6_endif:\nBNE $a $b :__if7_endif\nINC @b\n__if7_endif:\nBEQ $a $b :__if8_endif\nINC @b\n__if8_endif:\nFIN\n'
const highlighted = "<span class='asmInstruction'>BZR </span><span class='asmVariable'>$a </span><span class='asmLabel'>:__if1_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if1_endif:</span><br><span class='asmInstruction'>BNZ </span><span class='asmVariable'>$a </span><span class='asmLabel'>:__if2_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if2_endif:</span><br><span class='asmInstruction'>BLE </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if3_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if3_endif:</span><br><span class='asmInstruction'>BGE </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if4_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if4_endif:</span><br><span class='asmInstruction'>BLT </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if5_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if5_endif:</span><br><span class='asmInstruction'>BGT </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if6_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if6_endif:</span><br><span class='asmInstruction'>BNE </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if7_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if7_endif:</span><br><span class='asmInstruction'>BEQ </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if8_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if8_endif:</span><br><span class='asmInstruction'>FIN</span><br><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight: all branches opCodes with positive offset (no overflow)', () => {
const code = 'BZR $a :__if1_endif\nINC @b\n__if1_endif:\nBNZ $a :__if2_endif\nINC @b\n__if2_endif:\nBLE $a $b :__if3_endif\nINC @b\n__if3_endif:\nBGE $a $b :__if4_endif\nINC @b\n__if4_endif:\nBLT $a $b :__if5_endif\nINC @b\n__if5_endif:\nBGT $a $b :__if6_endif\nINC @b\n__if6_endif:\nBNE $a $b :__if7_endif\nINC @b\n__if7_endif:\nBEQ $a $b :__if8_endif\nINC @b\n__if8_endif:\nFIN\n'
const highlighted = "<span class='asmInstruction'>BZR </span><span class='asmVariable'>$a </span><span class='asmLabel'>:__if1_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if1_endif:</span><br><span class='asmInstruction'>BNZ </span><span class='asmVariable'>$a </span><span class='asmLabel'>:__if2_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if2_endif:</span><br><span class='asmInstruction'>BLE </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if3_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if3_endif:</span><br><span class='asmInstruction'>BGE </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if4_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if4_endif:</span><br><span class='asmInstruction'>BLT </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if5_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if5_endif:</span><br><span class='asmInstruction'>BGT </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if6_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if6_endif:</span><br><span class='asmInstruction'>BNE </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if7_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if7_endif:</span><br><span class='asmInstruction'>BEQ </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b </span><span class='asmLabel'>:__if8_endif</span><br><span class='asmInstruction'>INC </span><span class='asmVariable'>@b</span><br><span class='asmLabel'>__if8_endif:</span><br><span class='asmInstruction'>FIN</span><br><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight: ^declare, ^const, ^comment, ^program and multi spaces', () => {
const code = '^declare r0\n^const SET @c #9887766554433221\n^comment This is a comment\n SET @a #0000000000000005\n ^program something something something\n'
const highlighted = "<span class='asmDirective'>^declare</span><span class='asmVariable'> r0</span><br><span class='asmDirective'>^const</span><span class='asmInstruction'> SET </span><span class='asmVariable'>@c </span><span class='asmNumber'>#9887766554433221</span><br><span class='asmDirective'>^comment</span><span class='asmComment'> This is a comment</span><br><span class='asmInstruction'> SET </span><span class='asmVariable'>@a </span><span class='asmNumber'>#0000000000000005</span><br><span class='asmDirective'> ^program something</span> something something<br><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight with line numbers', () => {
const code = '^declare r0\n^declare r1\n^declare r2\n^declare a\n\nSET @r0 #0000000000000001\nSLP $r0\nFUN @a get_B1\nFIN\n'
const highlighted = "<div class='table'><div class='div_row'><div class='div_cell_a'>1</div><div class='div_cell_b'><span class='asmDirective'>^declare</span><span class='asmVariable'> r0</span></div></div><div class='div_row'><div class='div_cell_a'>2</div><div class='div_cell_b'><span class='asmDirective'>^declare</span><span class='asmVariable'> r1</span></div></div><div class='div_row'><div class='div_cell_a'>3</div><div class='div_cell_b'><span class='asmDirective'>^declare</span><span class='asmVariable'> r2</span></div></div><div class='div_row'><div class='div_cell_a'>4</div><div class='div_cell_b'><span class='asmDirective'>^declare</span><span class='asmVariable'> a</span></div></div><div class='div_row'><div class='div_cell_a'>5</div><div class='div_cell_b'></div></div><div class='div_row'><div class='div_cell_a'>6</div><div class='div_cell_b'><span class='asmInstruction'>SET </span><span class='asmVariable'>@r0 </span><span class='asmNumber'>#0000000000000001</span></div></div><div class='div_row'><div class='div_cell_a'>7</div><div class='div_cell_b'><span class='asmInstruction'>SLP </span><span class='asmVariable'>$r0</span></div></div><div class='div_row'><div class='div_cell_a'>8</div><div class='div_cell_b'><span class='asmInstruction'>FUN </span><span class='asmVariable'>@a </span><span class='asmInstruction'>get_B1</span></div></div><div class='div_row'><div class='div_cell_a'>9</div><div class='div_cell_b'><span class='asmInstruction'>FIN</span></div></div><div class='div_row'><div class='div_cell_a'>10</div><div class='div_cell_b'></div></div></div>"
const result = asmHighlight(code, true)
expect(result).toBe(highlighted)
})
it('should highlight error: ^comment and multi spaces', () => {
const code = 'SET @a #0000000000000100\nC @a\n\nFUN Xclear_A_B\nFUN Xset_A1 $a\nFUN Xset_A1_A2 $a $b\nFUN @a Xcheck_A_equals_B\nFUN @a Xadd_Minutes_to_Timestamp $b $c'
const highlighted = "<span class='asmInstruction'>SET </span><span class='asmVariable'>@a </span><span class='asmNumber'>#0000000000000100</span><br><span class='asmError'>C @a</span><br><br><span class='asmInstruction'>FUN </span><span class='asmError'>Xclear_A_B</span><br><span class='asmInstruction'>FUN </span><span class='asmError'>Xset_A1 </span><span class='asmVariable'>$a</span><br><span class='asmInstruction'>FUN </span><span class='asmError'>Xset_A1_A2 </span><span class='asmVariable'>$a </span><span class='asmVariable'>$b</span><br><span class='asmInstruction'>FUN </span><span class='asmVariable'>@a </span><span class='asmError'>Xcheck_A_equals_B</span><br><span class='asmInstruction'>FUN </span><span class='asmVariable'>@a </span><span class='asmError'>Xadd_Minutes_to_Timestamp </span><span class='asmVariable'>$b </span><span class='asmVariable'>$c</span><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
it('should highlight error: Rule not found and Non existent 0x36 api function', () => {
const code = 'long day here\nFUN @A 0x36APIFunction $B'
const highlighted = "<span class='asmError'>long day here</span><br><span class='asmError'>FUN @A 0x36APIFunction $B</span><br>"
const result = asmHighlight(code)
const result = asmHighlight(code, false)
expect(result).toBe(highlighted)
})
})
11 changes: 7 additions & 4 deletions dev/src/asmHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @returns same text with syntax highlight in html
*/
// eslint-disable-next-line no-unused-vars
export function asmHighlight (asmSourceCode: string) {
export function asmHighlight (asmSourceCode: string, addLineNumber: boolean) {
const Config = {
divId: 'asmCodeline',
divClass: 'asmLine',
Expand Down Expand Up @@ -166,14 +166,17 @@ export function asmHighlight (asmSourceCode: string) {
// loop thru all lines
lines.forEach((line, idx) => {
if (addDivLine === true) {
ret += `<div id='${Config.divId}${idx}' class='${Config.divClass}'>`
ret += `<div class='div_row'><div class='div_cell_a'>${idx + 1}</div><div class='div_cell_b'>`
}
// Find a rule for instruction
const FoundRule = allowedCodes.find(Rule => Rule.regex.exec(line) !== null)
ret += colorThisLine(line, FoundRule)
if (addDivLine === true) ret += '</div>'
if (addDivLine === true) ret += '</div></div>'
else ret += '<br>'
})
if (addDivLine) {
return "<div class='table'>" + ret + '</div>'
}
return ret
}

Expand Down Expand Up @@ -350,5 +353,5 @@ export function asmHighlight (asmSourceCode: string) {
}
}

return toHTML(asmSourceCode, false)
return toHTML(asmSourceCode, addLineNumber)
}
4 changes: 2 additions & 2 deletions dev/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function compileCode () {
const asmCode = compiler.getAssemblyCode()
const bcode = compiler.getMachineCode()

document.getElementById('assembly_output').innerHTML = asmHighlight(asmCode)
document.getElementById('assembly_output').innerHTML = asmHighlight(asmCode, false)

const t1 = new Date()
let compileMessage = `<span class='msg_success'>Compile sucessfull!!!</span> <small>Done at ${t1.getHours()}:${t1.getMinutes()}:${t1.getSeconds()} in ${t1 - t0} ms.`
Expand Down Expand Up @@ -200,7 +200,7 @@ function SetColorCode () {
if (document.getElementById('source_is_c').checked) {
dest.innerHTML = hljs.highlight(source.value, { language: 'c' }).value
} else {
dest.innerHTML = asmHighlight(source.value)
dest.innerHTML = asmHighlight(source.value, true)
}
source.className = 'transp'
}
Expand Down

1 comment on commit e55798d

@vercel
Copy link

@vercel vercel bot commented on e55798d Apr 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

smart-c – ./

smart-c-git-main-deleterium.vercel.app
smart-c.vercel.app
smart-c-deleterium.vercel.app

Please sign in to comment.