Skip to content

Commit

Permalink
De-smelling code recomended by sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Feb 10, 2024
1 parent 00268c5 commit a03032b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 33 deletions.
14 changes: 6 additions & 8 deletions src/assembler/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,17 +521,15 @@ export default function assembler (assemblyCode: string): MACHINE_OBJECT {
let cspages = 0; let uspages = 0
if (AsmObj.PCodeStackPages > 0) {
cspages = AsmObj.PCodeStackPages
} else {
if (assemblyCode.indexOf('JSR ') !== -1 || assemblyCode.indexOf('RET') !== -1) {
cspages = 1
}
}
if (cspages === 0 && (assemblyCode.indexOf('JSR ') !== -1 || assemblyCode.indexOf('RET') !== -1)) {
cspages = 1
}
if (AsmObj.PUserStackPages > 0) {
uspages = AsmObj.PUserStackPages
} else {
if (assemblyCode.indexOf('POP ') !== -1 || assemblyCode.indexOf('PSH ') !== -1) {
uspages = 1
}
}
if (uspages === 0 && (assemblyCode.indexOf('POP ') !== -1 || assemblyCode.indexOf('PSH ') !== -1)) {
uspages = 1
}
const datapages = Math.ceil(AsmObj.memory.length / 32)
const codepages = Math.ceil(AsmObj.bytecode.length / (32 * 16))
Expand Down
10 changes: 5 additions & 5 deletions src/codeGenerator/assemblyProcessor/optimizerVM/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function metaGetSuperregister (ContractState: CONTRACT, RetVar: MemoryObj, reg:
}

export class API_MICROCODE {
static EXT_FUN: T_EXT_FUN[] = [
static readonly EXT_FUN: T_EXT_FUN[] = [
{
funName: 'clear_A',
execute (ContractState) {
Expand Down Expand Up @@ -281,7 +281,7 @@ export class API_MICROCODE {
}
]

static EXT_FUN_DAT: T_EXT_FUN_DAT[] = [
static readonly EXT_FUN_DAT: T_EXT_FUN_DAT[] = [
{
funName: 'set_A1',
execute (ContractState, ContentVar) {
Expand Down Expand Up @@ -350,7 +350,7 @@ export class API_MICROCODE {
}
]

static EXT_FUN_DAT_2: T_EXT_FUN_DAT_2[] = [
static readonly EXT_FUN_DAT_2: T_EXT_FUN_DAT_2[] = [
{
funName: 'set_A1_A2',
execute (ContractState, ContentVar1, ContentVar2) {
Expand All @@ -377,7 +377,7 @@ export class API_MICROCODE {
}
]

static EXT_FUN_RET: T_EXT_FUN_RET[] = [
static readonly EXT_FUN_RET: T_EXT_FUN_RET[] = [
{
funName: 'get_A1',
execute (ContractState, RetVar) {
Expand Down Expand Up @@ -520,7 +520,7 @@ export class API_MICROCODE {
}
]

static EXT_FUN_RET_DAT_2: T_EXT_FUN_RET_DAT_2[] = [
static readonly EXT_FUN_RET_DAT_2: T_EXT_FUN_RET_DAT_2[] = [
{
funName: 'add_Minutes_to_Timestamp',
execute: metaUnknowAndRevokeVariable
Expand Down
4 changes: 2 additions & 2 deletions src/codeGenerator/assemblyProcessor/optimizerVM/cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function metaReset (ContractState: CONTRACT) : boolean|null {
}

export class CPU {
static cpuMicrocode: CPU_MICROCODE[] = [
static readonly cpuMicrocode: CPU_MICROCODE[] = [
{
name: 'blank',
regex: /^\s*$/,
Expand Down Expand Up @@ -62,7 +62,7 @@ export class CPU {
},
{
name: 'program',
regex: /^\s*\^program\s+(\w+)\s+([\s\S]+)$/,
regex: /^\s*\^program\s(.+)$/,
execute: metaDoNothing
},
{
Expand Down
8 changes: 4 additions & 4 deletions src/codeGenerator/assemblyProcessor/optimizerVM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ export class CONTRACT {

unknownSuperRegisterA () : void {
this.Memory.forEach((Mem) => {
if (/^[A][1234]$/.test(Mem.varName)) {
if (/^A[1234]$/.test(Mem.varName)) {
Mem.value = unknownValue
Mem.shadow = ''
return
}
if (/^[A][1234]$/.test(Mem.shadow)) {
if (/^A[1234]$/.test(Mem.shadow)) {
Mem.shadow = ''
}
})
}

unknownSuperRegisterB () : void {
this.Memory.forEach((Mem) => {
if (/^[B][1234]$/.test(Mem.varName)) {
if (/^B[1234]$/.test(Mem.varName)) {
Mem.value = unknownValue
Mem.shadow = ''
return
}
if (/^[B][1234]$/.test(Mem.shadow)) {
if (/^B[1234]$/.test(Mem.shadow)) {
Mem.shadow = ''
}
})
Expand Down
14 changes: 7 additions & 7 deletions src/preprocessor/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ export default function preprocessor (sourcecode: string) : string {
retArr[retArr.length - 1] += line.slice(0, -1)
escapedLines++
}
return
}
if (escapedLines !== 0) {
retArr[retArr.length - 1] += line
retArr.push(...Array(escapedLines).fill(''))
escapedLines = 0
} else {
if (escapedLines !== 0) {
retArr[retArr.length - 1] += line
retArr.push(...Array(escapedLines).fill(''))
escapedLines = 0
} else {
retArr.push(line)
}
retArr.push(line)
}
})
if (escapedLines > 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/smartc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class SmartC {
/**
* Triggers compilation process
* @throws {Error} if compilation is not sucessfull */
compile () : SmartC {
compile () : this {
let preprocessed : string
let tokenized: PRE_TOKEN[]
let parsed: TOKEN[]
Expand Down
12 changes: 6 additions & 6 deletions src/tokenizer/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default function tokenizer (inputSourceCode: string): PRE_TOKEN[] {
throw new Error(`At line: ${AuxVars.currentLine}. ${RuleDouble.errorMsg}`)
}
if (RuleDouble.pretokenType === 'NONE') {
AuxVars.currentLine += (endParts[1].match(/\n/g) || '').length
AuxVars.currentLine += (endParts[1].match(/\n/g) ?? '').length
AuxVars.current += endParts[1].length
return true// breaks find function
}
Expand All @@ -180,7 +180,7 @@ export default function tokenizer (inputSourceCode: string): PRE_TOKEN[] {
value: endParts[1].slice(0, -RuleDouble.removeTrailing),
line: AuxVars.currentLine
})
AuxVars.currentLine += (endParts[1].match(/\n/g) || '').length
AuxVars.currentLine += (endParts[1].match(/\n/g) ?? '').length
AuxVars.current += endParts[1].length
return true// breaks find function
}
Expand All @@ -192,7 +192,7 @@ export default function tokenizer (inputSourceCode: string): PRE_TOKEN[] {
}
switch (RuleSingle.pretokenType) {
case 'NONE':
AuxVars.currentLine += (startParts[1].match(/\n/g) || '').length
AuxVars.currentLine += (startParts[1].match(/\n/g) ?? '').length
AuxVars.current += startParts[1].length + RuleSingle.addLength
return true
case 'ASM': {
Expand All @@ -208,7 +208,7 @@ export default function tokenizer (inputSourceCode: string): PRE_TOKEN[] {
const asmText = asmParts[2].slice(0, endLocation)
const asmCode = asmParts[1] + asmText + '}'
AuxVars.preTokens.push({ type: 'keyword', value: 'asm', line: AuxVars.currentLine, extValue: asmText })
AuxVars.currentLine += (asmCode.match(/\n/g) || '').length
AuxVars.currentLine += (asmCode.match(/\n/g) ?? '').length
AuxVars.current += asmCode.length
return true
}
Expand All @@ -224,13 +224,13 @@ export default function tokenizer (inputSourceCode: string): PRE_TOKEN[] {
line: AuxVars.currentLine,
extValue: structParts[2]
})
AuxVars.currentLine += (structParts[1].match(/\n/g) || '').length
AuxVars.currentLine += (structParts[1].match(/\n/g) ?? '').length
AuxVars.current += structParts[1].length
return true
}
default:
AuxVars.preTokens.push({ type: RuleSingle.pretokenType, value: startParts[1], line: AuxVars.currentLine })
AuxVars.currentLine += (startParts[1].match(/\n/g) || '').length
AuxVars.currentLine += (startParts[1].match(/\n/g) ?? '').length
AuxVars.current += startParts[1].length + RuleSingle.addLength
return true
}
Expand Down

0 comments on commit a03032b

Please sign in to comment.