From e55798d859755072ca9c356152ca8487d2ccd8c6 Mon Sep 17 00:00:00 2001 From: deleterium Date: Sat, 23 Apr 2022 21:33:04 -0300 Subject: [PATCH] Fixed lines numbers in assembler highlight --- dev/src/__tests__/asmHighlight.spec.ts | 22 ++++++++++++++-------- dev/src/asmHighlight.ts | 11 +++++++---- dev/src/index.js | 4 ++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/dev/src/__tests__/asmHighlight.spec.ts b/dev/src/__tests__/asmHighlight.spec.ts index f6fe4c9..26d0d29 100644 --- a/dev/src/__tests__/asmHighlight.spec.ts +++ b/dev/src/__tests__/asmHighlight.spec.ts @@ -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 = "SET @a #0000000000000100
SET @b $a
CLR @b
INC @b
DEC @a
ADD @a $b
SUB @a $b
MUL @a $b
DIV @a $b
BOR @a $b
AND @a $b
XOR @a $b
SET @a $b
NOT @a
SET @a $($b)
SET @a $c
ADD @a $b
SET @a $($b + $c)
PSH $b
JSR :__fn_teste
POP @a
SET @($a) $b
SET @($a + $b) $c
MOD @a $b
SHL @a $b
SHR @a $b
SLP $a
JMP :__fn_main

__fn_teste:
POP @teste_d
SET @r0 $teste_d
INC @r0
PSH $r0
RET

__fn_main:
PCS
INC @a
FIN
" - 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 = "FUN clear_A_B
FUN set_A1 $a
FUN set_A1_A2 $a $b
FUN @a check_A_equals_B
FUN @a add_Minutes_to_Timestamp $b $c

" - 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 = "FIZ $a
STZ $a
ERR :__error
INC @a
NOP
NOP
__error:
DEC @a
" - 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 = "BZR $a :__if1_endif
INC @b
__if1_endif:
BNZ $a :__if2_endif
INC @b
__if2_endif:
BLE $a $b :__if3_endif
INC @b
__if3_endif:
BGE $a $b :__if4_endif
INC @b
__if4_endif:
BLT $a $b :__if5_endif
INC @b
__if5_endif:
BGT $a $b :__if6_endif
INC @b
__if6_endif:
BNE $a $b :__if7_endif
INC @b
__if7_endif:
BEQ $a $b :__if8_endif
INC @b
__if8_endif:
FIN

" - 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 = "BZR $a :__if1_endif
INC @b
__if1_endif:
BNZ $a :__if2_endif
INC @b
__if2_endif:
BLE $a $b :__if3_endif
INC @b
__if3_endif:
BGE $a $b :__if4_endif
INC @b
__if4_endif:
BLT $a $b :__if5_endif
INC @b
__if5_endif:
BGT $a $b :__if6_endif
INC @b
__if6_endif:
BNE $a $b :__if7_endif
INC @b
__if7_endif:
BEQ $a $b :__if8_endif
INC @b
__if8_endif:
FIN

" - 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 = "^declare r0
^const SET @c #9887766554433221
^comment This is a comment
SET @a #0000000000000005
^program something something something

" - 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 = "
1
^declare r0
2
^declare r1
3
^declare r2
4
^declare a
5
6
SET @r0 #0000000000000001
7
SLP $r0
8
FUN @a get_B1
9
FIN
10
" + 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 = "SET @a #0000000000000100
C @a

FUN Xclear_A_B
FUN Xset_A1 $a
FUN Xset_A1_A2 $a $b
FUN @a Xcheck_A_equals_B
FUN @a Xadd_Minutes_to_Timestamp $b $c
" - 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 = "long day here
FUN @A 0x36APIFunction $B
" - const result = asmHighlight(code) + const result = asmHighlight(code, false) expect(result).toBe(highlighted) }) }) diff --git a/dev/src/asmHighlight.ts b/dev/src/asmHighlight.ts index 9032c8b..fb030b2 100644 --- a/dev/src/asmHighlight.ts +++ b/dev/src/asmHighlight.ts @@ -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', @@ -166,14 +166,17 @@ export function asmHighlight (asmSourceCode: string) { // loop thru all lines lines.forEach((line, idx) => { if (addDivLine === true) { - ret += `
` + ret += `
${idx + 1}
` } // Find a rule for instruction const FoundRule = allowedCodes.find(Rule => Rule.regex.exec(line) !== null) ret += colorThisLine(line, FoundRule) - if (addDivLine === true) ret += '
' + if (addDivLine === true) ret += '
' else ret += '
' }) + if (addDivLine) { + return "
" + ret + '
' + } return ret } @@ -350,5 +353,5 @@ export function asmHighlight (asmSourceCode: string) { } } - return toHTML(asmSourceCode, false) + return toHTML(asmSourceCode, addLineNumber) } diff --git a/dev/src/index.js b/dev/src/index.js index 8b4f6c1..3b680c6 100644 --- a/dev/src/index.js +++ b/dev/src/index.js @@ -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 = `Compile sucessfull!!! Done at ${t1.getHours()}:${t1.getMinutes()}:${t1.getSeconds()} in ${t1 - t0} ms.` @@ -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' }