shithub: rgbds

Download patch

ref: 953f79c0d96d8181ca4b854f97ad4bdc5ef79ef3
parent: 3c5e1caa7c29d1a559d1144d3d00d9d981a7ac9a
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Tue Feb 23 10:57:21 EST 2021

Support 'MACRO mac' as well as 'mac: MACRO' for defining macros

The new syntax is used in documentation, but
the old syntax is not yet deprecated.

--- a/src/asm/parser.y
+++ b/src/asm/parser.y
@@ -934,7 +934,12 @@
 		}
 ;
 
-macrodef	: T_LABEL T_COLON T_POP_MACRO T_NEWLINE {
+macrodef	: T_POP_MACRO T_ID T_NEWLINE {
+			lexer_CaptureMacroBody(&captureBody);
+		} T_NEWLINE {
+			sym_AddMacro($2, captureBody.lineNo, captureBody.body, captureBody.size);
+		}
+		| T_LABEL T_COLON T_POP_MACRO T_NEWLINE {
 			lexer_CaptureMacroBody(&captureBody);
 		} T_NEWLINE {
 			sym_AddMacro($1, captureBody.lineNo, captureBody.body, captureBody.size);
--- a/src/asm/rgbasm.5
+++ b/src/asm/rgbasm.5
@@ -1076,33 +1076,40 @@
 .Ic IF
 constructs.
 .Bd -literal -offset indent
-MyMacro: MACRO
-         ld   a,80
+MACRO MyMacro
+         ld a, 80
          call MyFunc
-         ENDM
+ENDM
 .Ed
 .Pp
-Note that a single colon
+The example above defines
+.Ql MyMacro
+as a new macro.
+You may use the older syntax
+.Ql MyMacro: MACRO
+instead of
+.Ql MACRO MyMacro ,
+with a single colon
 .Ql \&:
-following the macro's name is required.
+following the macro's name.
 Macros can't be exported or imported.
 .Pp
 Plainly nesting macro definitions is not allowed, but this can be worked around using
 .Ic EQUS .
-This won't work:
+So this won't work:
 .Bd -literal -offset indent
-outer: MACRO
-inner: MACRO
-    PRINTLN "Hello!"
+MACRO outer
+    MACRO inner
+        PRINTLN "Hello!"
+    ENDM
 ENDM
-ENDM
 .Ed
 .Pp
 But this will:
 .Bd -literal -offset indent
-outer: MACRO
-definition equs "inner: MACRO\[rs]nPRINTLN \[rs]"Hello!\[rs]"\[rs]nENDM"
-definition
+MACRO outer
+definition EQUS "MACRO inner\[rs]nPRINTLN \[rs]"Hello!\[rs]"\[rs]nENDM"
+    definition
     PURGE definition
 ENDM
 .Ed
@@ -1393,7 +1400,7 @@
 .Pp
 Suppose your macro contains a loop.
 .Bd -literal -offset indent
-LoopyMacro: MACRO
+MACRO LoopyMacro
             xor  a,a
 \&.loop       ld   [hl+],a
             dec  c
@@ -1411,7 +1418,7 @@
 .Ic REPT
 blocks.
 .Bd -literal -offset indent
-LoopyMacro: MACRO
+MACRO LoopyMacro
             xor  a,a
 \&.loop\[rs]@     ld   [hl+],a
             dec  c
@@ -1440,7 +1447,7 @@
 .Ic \[rs]9 , \[rs]1
 being the first argument specified on the macro invocation.
 .Bd -literal -offset indent
-LoopyMacro: MACRO
+MACRO LoopyMacro
             ld   hl,\[rs]1
             ld   c,\[rs]2
             xor  a,a
@@ -1465,7 +1472,7 @@
 if you perform further calculations on them.
 For instance, consider the following:
 .Bd -literal -offset indent
-print_double: MACRO
+MACRO print_double
     PRINTLN \[rs]1 * 2
 ENDM
     print_double 1 + 2
@@ -1480,7 +1487,7 @@
 Line continuations work as usual inside macros or lists of macro arguments.
 However, some characters need to be escaped, as in the following example:
 .Bd -literal -offset indent
-PrintMacro: MACRO
+MACRO PrintMacro
     PRINT \[rs]1
 ENDM
 
--- /dev/null
+++ b/test/asm/macro-syntax.asm
@@ -1,0 +1,15 @@
+
+	old: MACRO ; comment
+		println "out with the ", \1
+	ENDM ; comment
+
+	MACRO new ; comment
+		println "in with the ", \1
+	ENDM ; comment
+
+	old 1
+	new 2
+
+	bad1: MACRO bad2 ; comment
+		println "which?"
+	ENDM ; comment
--- /dev/null
+++ b/test/asm/macro-syntax.err
@@ -1,0 +1,5 @@
+ERROR: macro-syntax.asm(13):
+    syntax error, unexpected identifier, expecting newline
+ERROR: macro-syntax.asm(15):
+    syntax error, unexpected ENDM
+error: Assembly aborted (2 errors)!
--- /dev/null
+++ b/test/asm/macro-syntax.out
@@ -1,0 +1,3 @@
+out with the $1
+in with the $2
+which?
--- /dev/null
+++ b/test/asm/macro-syntax.simple.err
@@ -1,0 +1,5 @@
+ERROR: macro-syntax.asm(13):
+    syntax error
+ERROR: macro-syntax.asm(15):
+    syntax error
+error: Assembly aborted (2 errors)!