Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested macro definitions #74

Open
Rhialto opened this issue Aug 22, 2020 · 1 comment
Open

Nested macro definitions #74

Rhialto opened this issue Aug 22, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@Rhialto
Copy link

Rhialto commented Aug 22, 2020

The docs say:

GENERAL:
    Most everything is recursive.  You cannot have a macro DEFINITION
    within a macro definition, but can nest macro calls, repeat loops,
    and include files.

Actually to have such a facility is useful sometimes. Digital's Macro-11 assembler allows it. One way this is commonly used (well, common for a specific assembler for an obsolete CPU architecture) is use-once macros. A macro, if it is used, re-defines itself to be empty. Something like this: (see also https://gitlab.com/Rhialto/macro11/-/blob/master/tests/test-endm.lst.ok for a listing output)

;;;;;
;
; Test nested macros and name on .ENDM
;

    .macro      M1
    .word       1
    .endm       M1      ; ok

    M1

    .macro      M2
    .word       2
    .macro      M3
    .word       3
    .endm       M3      ; ok
    .endm       M2      ; ok

    M2
    M3

    .macro      M4
    .word       4
    .macro      M4
    .endm       M4      ; ok
    .endm       M4      ; ok

    M4
    M4                  ; should be empty now

    .macro      M5
    .word       5
    .macro      M5
    .endm       notM5   ; wrong; detected when M5 is expanded
    .endm       M5      ; ok

    M5
    M5

    .macro      M6
    .endm       notM6   ; wrong

    .endm               ; end without macro
    .endr               ; end without repetition
    .endc               ; end without condition

@andrew-davie
Copy link
Member

andrew-davie commented Aug 23, 2020

This particular use-case can be easily replicated by defining a global inside the macro.
Then the macro simply checks if the global is defined (via IFCONST) just before that line.

  MAC onceonly
    IFNCONST onceonly_used_already
onceonly_used_already = 1
 ; body of macro
    ENDIF
  ENDM
; code....

 onceonly. ; body will be inserted
 onceonly ; body will not be inserted, because onceonly_used_already has been defined

My point here is that the solution is very easy with the existing code.
I don't think this is a good reason to try a rewrite.

@thrust26 thrust26 added the enhancement New feature or request label Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants