Skip to content

fix: remove code processing IF condition not met#141

Merged
duhow merged 8 commits intomasterfrom
copilot/fix-if-condition-processing
Feb 26, 2026
Merged

fix: remove code processing IF condition not met#141
duhow merged 8 commits intomasterfrom
copilot/fix-if-condition-processing

Conversation

Copy link
Contributor

Copilot AI commented Dec 21, 2025

Fix for IFDEF/IF conditional compilation issue

Problem

Code inside false IF/IFDEF blocks is still parsed for syntax errors, causing the assembler to fail even when the code should be skipped.

Solution Implemented

  • Create a new preprocessing stage (parser5) to handle IFDEF conditional compilation
  • Parser5 strips out lines in false IFDEF blocks before main parsing
  • Parser5 passes through IF/ELSE/ENDIF to main parser (expressions evaluated there)
  • Track label/symbol definitions to support IFDEF checks
  • Support both uppercase and lowercase identifiers
  • Update Makefile to include parser5 in build
  • Update dura.y to call preprocessor5 and read from correct file
  • Fix parser5 to use dynamic file numbering (respects REPT nesting)
  • Fix parser5 to handle assignments without spaces (e.g., VAR=value)
  • Add warning for symbol table overflow instead of silently failing
  • Add test case for the reported bug

Testing

  • Verify fix works with the reported test case (test_ifdef.asm)
  • Verified IFDEF/ELSE/ENDIF handling works correctly
  • Verified IF/ELSE/ENDIF pass through to main parser works
  • Issue Conditional assembly does not evade tag definitions #90 tests now pass
  • All example projects build successfully
  • Full test suite passes (33/33 scenarios, 1 unrelated error)
  • Added regression test for the bug
  • Code review feedback addressed
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] Code processed despite IF condition is not met</issue_title>
<issue_description>Hello asMSX team!

Describe the bug
I don't know if it is a pending issue or it is not working.
I have tested the attached code and when assembling it processes everything as if the condition was not interpreted.

Current output

-------------------------------------------------------------------------------
 asMSX v1.1.0 MSX cross-assembler. asMSX Team. [2023-11-04]
-------------------------------------------------------------------------------

Parsing file TestIFDEF.asm
Expanding Macros
DONE Expanding Macros
Expanding REPT
Assembling source file TestIFDEF.asm
Parsing error: syntax error
TestIFDEF.asm, line 11: syntax error

Code

.BIOS
.BIOSVARS

.FILENAME "TESTIFD.ROM"

;--------------------------------------------------------------------
LABELEXIST:

;Case 1: The Label does not exist. The ELSE must be processed.
IFDEF THISLABELNOTEXIST
    CONSTANT1:	EQU $01
    CONSTANT2:	EQU $02
ELSE
CONSTANT1	EQU $01
CONSTANT2	EQU $02
ENDIF

;Case 2: The Label exist. The ELSE does not have to be processed.
IFDEF LABELEXIST
CONSTANT3	EQU $03
CONSTANT4	EQU $04
ELSE
    CONSTANT3:	EQU $03
    CONSTANT4:	EQU $04
ENDIF
;--------------------------------------------------------------------

.PAGE	1
.ROM

MAIN:
  DI
  LD    SP,[HIMEM]	;($FC4A) Stack at the top of memory
  EI

LOOP:		
  halt
    
  ld     A,7         
  call    SNSMAT
  sub     A,11111011B    ;ESC
  JR      Z,EXIT_HELP
	
  jp  LOOP

EXIT_HELP:
  call 0

TestIFDEF.asm.TXT

</issue_description>

Comments on the Issue (you are @copilot in this section)

@duhow Failure to run is due to `syntax error` (unexpected `:` as is invalid), although I see your point of "should not be processed" as the condition is not met in both cases.

In this case, the error is generated during "preprocessing" phase 1, so it will check that all the code is valid and can be included.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 6 commits December 21, 2025 11:17
Co-authored-by: duhow <1145001+duhow@users.noreply.github.com>
…tion

Co-authored-by: duhow <1145001+duhow@users.noreply.github.com>
…bering

Co-authored-by: duhow <1145001+duhow@users.noreply.github.com>
Co-authored-by: duhow <1145001+duhow@users.noreply.github.com>
Co-authored-by: duhow <1145001+duhow@users.noreply.github.com>
Co-authored-by: duhow <1145001+duhow@users.noreply.github.com>
@duhow
Copy link
Collaborator

duhow commented Dec 21, 2025

@copilot continue working

Copy link
Contributor Author

Copilot AI commented Dec 21, 2025

@copilot continue working

The PR is complete and fully functional. All changes have been implemented and tested:

✅ Parser5 successfully strips invalid syntax from false IFDEF blocks
✅ All 33 test scenarios pass
✅ Original bug is fixed - CONSTANT1: EQU $01 syntax in false blocks no longer causes errors
✅ Both uppercase and lowercase directives work (IFDEF/ifdef, EQU/equ, etc.)
✅ All example projects build successfully

The implementation adds a new preprocessing stage (parser5) that evaluates IFDEF conditionals and removes code in false blocks before the main parser processes the file. This prevents syntax errors in code that should never be executed.

Latest commit: 05aa1cf

@duhow duhow marked this pull request as ready for review December 21, 2025 13:30
@duhow duhow changed the title [WIP] Fix code processing despite IF condition not met fix: remove code processing IF condition not met Dec 21, 2025
@duhow duhow requested review from Fubukimaru and jamque December 21, 2025 13:57
Copy link
Owner

@Fubukimaru Fubukimaru left a comment

Choose a reason for hiding this comment

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

Looks cool. Go ahead.

Maybe we should remove the IF treatment inside of the main part of the code. What do you think?

@duhow
Copy link
Collaborator

duhow commented Feb 26, 2026

remove the IF treatment inside of the main part of the code

we can create a proposal to see how to handle this, but the code generated should remain the same (as a refactor)

@duhow duhow merged commit 03eb6fc into master Feb 26, 2026
9 checks passed
@duhow duhow deleted the copilot/fix-if-condition-processing branch February 26, 2026 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Code processed despite IF condition is not met

3 participants