Skip to content

Commit

Permalink
Feat warning for variables not initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Feb 9, 2024
1 parent 9992463 commit 881de59
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/codeGenerator/astProcessor/binaryAsnProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ export default function binaryAsnProcessor (

function assignmentProc () : GENCODE_SOLVED_OBJECT {
assignmentStartErrorTests()
AuxVars.isLeftSideOfAssignment = true
if (CurrentNode.Operation.type === 'Assignment') {
AuxVars.isLeftSideOfAssignment = true
}
AuxVars.hasVoidArray = false
const LGenObj = genCode(Program, AuxVars, {
RemAST: CurrentNode.Left,
Expand Down
18 changes: 18 additions & 0 deletions src/codeGenerator/astProcessor/setupGenCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export default function setupGenCode (
if (MemFound === undefined) {
throw new Error(`At line: ${line}. Using variable '${varName}' before declaration.`)
}
if (!MemFound.isSet) {
detectAndSetNotInitialized(MemFound, line, varDeclaration !== '')
}
if (varDeclaration !== '') { // we are in declarations sentence
MemFound.isDeclared = true
return deepCopy(MemFound)
Expand All @@ -144,9 +147,24 @@ export default function setupGenCode (
if (FoundMemory === undefined) {
throw new Error(`At line: ${line}. No variable found at address '${addr}'.`)
}
if (!FoundMemory.isSet) {
detectAndSetNotInitialized(FoundMemory, line, false)
}
return deepCopy(FoundMemory)
}

function detectAndSetNotInitialized (Memory: MEMORY_SLOT, line: number, isInitialization: boolean) {
if (AuxVars.isLeftSideOfAssignment || Memory.hexContent) {
Memory.isSet = true
return
}
if (isInitialization) {
return
}
AuxVars.warnings.push(`Warning: at line ${line}. Variable '${Memory.name}' is used but not initialized.`)
Memory.isSet = true // No more warning for same variable
}

function auxvarsGetNewJumpID () : string {
// This code shall be equal GlobalCodeVars.getNewJumpID()
AuxVars.jumpId++
Expand Down
4 changes: 4 additions & 0 deletions src/shaper/memoryProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default function memoryProcessor (
}
}
header.isDeclared = AuxVars.isFunctionArgument
header.isSet = AuxVars.isFunctionArgument
// If is not an array, just send the header
if (dimensions.length === 0) {
return [header]
Expand Down Expand Up @@ -133,6 +134,7 @@ export default function memoryProcessor (
Mem2.asmName = `${header.asmName}_${i - 1}`
Mem2.scope = AuxVars.currentScopeName
Mem2.declaration = header.ArrayItem.declaration
Mem2.isSet = true // No way to track array items for using before initialized
retArrMem.push(Mem2)
}
// create array type definition
Expand Down Expand Up @@ -277,6 +279,7 @@ export default function memoryProcessor (
StructMemHeader.asmName = AuxVars.currentPrefix + phraseCode[startingTokenCounter].value
StructMemHeader.scope = AuxVars.currentScopeName
StructMemHeader.isDeclared = AuxVars.isFunctionArgument
StructMemHeader.isSet = AuxVars.isFunctionArgument
return [StructMemHeader]
}
// It IS array of structs
Expand All @@ -293,6 +296,7 @@ export default function memoryProcessor (
StructMemHeader.asmName = AuxVars.currentPrefix + phraseCode[startingTokenCounter].value
StructMemHeader.scope = AuxVars.currentScopeName
StructMemHeader.isDeclared = AuxVars.isFunctionArgument
StructMemHeader.isSet = AuxVars.isFunctionArgument
StructMemHeader.type = 'array'
StructMemHeader.typeDefinition = StructMemHeader.asmName
StructMemHeader.ArrayItem = {
Expand Down
9 changes: 9 additions & 0 deletions src/shaper/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function getTypeDefinitionTemplate<T extends 'register'|'long'|'fixed'|'s
RetObj.MemoryTemplate.declaration = 'long'
RetObj.MemoryTemplate.size = 1
RetObj.MemoryTemplate.isDeclared = true
RetObj.MemoryTemplate.isSet = true
break
case 'long':
case 'void':
Expand Down Expand Up @@ -67,6 +68,7 @@ const longArg : MEMORY_SLOT = {
type: 'long',
scope: 'dummy',
declaration: 'long',
isSet: false,
size: 1,
isDeclared: true
}
Expand All @@ -78,6 +80,7 @@ const longPtrArg : MEMORY_SLOT = {
type: 'long',
scope: 'dummy',
declaration: 'long_ptr',
isSet: false,
size: 1,
isDeclared: true
}
Expand All @@ -89,6 +92,7 @@ const fixedArg : MEMORY_SLOT = {
type: 'fixed',
scope: 'dummy',
declaration: 'fixed',
isSet: false,
size: 1,
isDeclared: true
}
Expand All @@ -98,6 +102,7 @@ export const autoCounterTemplate : MEMORY_SLOT = {
asmName: '_counterTimestamp',
declaration: 'long',
isDeclared: true,
isSet: false,
name: '_counterTimestamp',
scope: '',
size: 1,
Expand Down Expand Up @@ -139,6 +144,7 @@ export const BuiltInTemplate: SC_FUNCTION[] = [
type: 'long',
scope: 'memcopy',
declaration: 'void_ptr',
isSet: false,
size: 1,
isDeclared: true
},
Expand All @@ -149,6 +155,7 @@ export const BuiltInTemplate: SC_FUNCTION[] = [
type: 'long',
scope: 'memcopy',
declaration: 'void_ptr',
isSet: false,
size: 1,
isDeclared: true
}
Expand Down Expand Up @@ -537,6 +544,7 @@ export function getMemoryTemplate (memType: MEMORY_BASE_TYPES) : MEMORY_SLOT {
asmName: '',
isDeclared: false,
declaration: '',
isSet: false,
address: -1,
name: '',
scope: '',
Expand All @@ -549,6 +557,7 @@ export const fixedBaseTemplate : MEMORY_SLOT = {
asmName: 'f100000000',
declaration: 'fixed',
isDeclared: false,
isSet: true,
name: 'f100000000',
hexContent: '0000000005f5e100',
scope: '',
Expand Down
2 changes: 2 additions & 0 deletions src/typings/syntaxTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export type MEMORY_SLOT = {
isDeclared: boolean
/** Variable type during declaration */
declaration: DECLARATION_TYPES
/** Control warning if using variables before setting it. */
isSet: boolean
/** Offset in memory. -1 if this slot is not in memory */
address: number
/** Variable name */
Expand Down

0 comments on commit 881de59

Please sign in to comment.