Conditional assembly
-
I've written a 6502 assembler (for Commodore machines) and one of its features is conditional assembly, like this;
ifdef var1
else
elseif
So if var1 is defined then is assembled, else . However I want to expand this by allowing nested conditional assembly, like this:
ifdef var1
ifdef var2else
endif
elseendif
Basically the way my assembler works at the moment is if the 'ifdef' condition fails (the variable is not defined) then all of the code following it (until the corresponding 'else' directive) is ignored. It's the 'corresponding else' part which is the problem here. How do I determine which 'else' corresponds with which 'ifdef'? Please note that I can't use the code indentation to determine which 'level' the directives are on. In real life, all of the code will be in the first column (no indentation at all) as my assembler needs directives to be in the first column. I don't need specific code as such, I'm more interested in an algorithm for this. In case you're interested, here's my assembler: www.ajordison.co.uk
-
I've written a 6502 assembler (for Commodore machines) and one of its features is conditional assembly, like this;
ifdef var1
else
elseif
So if var1 is defined then is assembled, else . However I want to expand this by allowing nested conditional assembly, like this:
ifdef var1
ifdef var2else
endif
elseendif
Basically the way my assembler works at the moment is if the 'ifdef' condition fails (the variable is not defined) then all of the code following it (until the corresponding 'else' directive) is ignored. It's the 'corresponding else' part which is the problem here. How do I determine which 'else' corresponds with which 'ifdef'? Please note that I can't use the code indentation to determine which 'level' the directives are on. In real life, all of the code will be in the first column (no indentation at all) as my assembler needs directives to be in the first column. I don't need specific code as such, I'm more interested in an algorithm for this. In case you're interested, here's my assembler: www.ajordison.co.uk
The common method for processing such nested commands is using a stack. If you encounter a condition, you push it onto the stack which holds the required information like type (
if
) and evaluated result. Upon anelse
, you modify the current stack value accordingly (e.g. by setting anelse
flag), and upon anendif
you pop (discard). -
The common method for processing such nested commands is using a stack. If you encounter a condition, you push it onto the stack which holds the required information like type (
if
) and evaluated result. Upon anelse
, you modify the current stack value accordingly (e.g. by setting anelse
flag), and upon anendif
you pop (discard).Yes this is the approach I tried first, but I still had the same problem when I hit an 'else', i.e. which 'else' is it?
-
Yes this is the approach I tried first, but I still had the same problem when I hit an 'else', i.e. which 'else' is it?
It is the
else
for the current (top of stack, recent)if
. -
I've written a 6502 assembler (for Commodore machines) and one of its features is conditional assembly, like this;
ifdef var1
else
elseif
So if var1 is defined then is assembled, else . However I want to expand this by allowing nested conditional assembly, like this:
ifdef var1
ifdef var2else
endif
elseendif
Basically the way my assembler works at the moment is if the 'ifdef' condition fails (the variable is not defined) then all of the code following it (until the corresponding 'else' directive) is ignored. It's the 'corresponding else' part which is the problem here. How do I determine which 'else' corresponds with which 'ifdef'? Please note that I can't use the code indentation to determine which 'level' the directives are on. In real life, all of the code will be in the first column (no indentation at all) as my assembler needs directives to be in the first column. I don't need specific code as such, I'm more interested in an algorithm for this. In case you're interested, here's my assembler: www.ajordison.co.uk
It's called "lexical analysis"; and includes "token matching" (which is what you're trying to do): [Lexical analysis - Wikipedia](https://en.wikipedia.org/wiki/Lexical\_analysis) [Compiler Construction/Lexical analysis - Wikibooks, open books for an open world](https://en.wikibooks.org/wiki/Compiler\_Construction/Lexical\_analysis)
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal