rgbds icon indicating copy to clipboard operation
rgbds copied to clipboard

[Feature request] Allow multiple instructions on one line with backslashes

Open Rangi42 opened this issue 3 years ago • 1 comments

In comments people often use "/" or "\" to separate multiple instructions, like "ld a, [hli] \ ld h, [hl] \ ld l, a". It could be possible to do this in rgbasm as well: outside of string literals, backslash is only used for macro arguments and line continuations, and line continuations must be followed only by whitespace or comments before the newline.

beginLineContinuation() could change to return a T_NEWLINE (or some T_FAKE_NEWLINE if necessary) if it runs into a non-whitespace character, instead of printing an error. (Edit: the T_EOL introduced by #716 might be appropriate here.)

Rangi42 avatar Mar 26 '21 19:03 Rangi42

In PR #901 @ISSOtm was "strongly against" \ as a statement separator because its meaning then changes between "line continuation" and "statement separator" depending on context. Another available possibility was ., but that doesn't stand out as much as / or \.

Rangi42 avatar Jul 04 '21 20:07 Rangi42

If we just want to allow multiple instructions on one line, not labels, directives, etc, then it's a pretty simple change to the parser (using . as the separator here):

 plain_directive        : label
-               | label cpu_command
+               | label cpu_commands

 [...]

+cpu_commands   : cpu_command
+               | cpu_command T_PERIOD cpu_commands
+;

Rangi42 avatar Nov 02 '23 00:11 Rangi42

: was also suggested as a separator, which wouldn't be ambiguous with an anon label decl. :: and .. are also possibilities.

ISSOtm avatar Nov 02 '23 06:11 ISSOtm

I would avoid : because putting anonymous labels or references to them on the same line would look odd:

: ldh a, [rSTAT] : and %11 : cp STATF_VBL : jr nz, :-

For comparison:

; two colons
: ldh a, [rSTAT] :: and %11 :: cp STATF_VBL :: jr nz, :-

; two periods
: ldh a, [rSTAT] .. and %11 .. cp STATF_VBL .. jr nz, :-

; one period
: ldh a, [rSTAT] . and %11 . cp STATF_VBL . jr nz, :-

On the other hand, periods might look weird with local labels:

; two periods
.wait_vbl: ldh a, [rSTAT] .. and %11 .. cp STATF_VBL .. jr nz, .wait_vbl

; one period
.wait_vbl: ldh a, [rSTAT] . and %11 . cp STATF_VBL . jr nz, .wait_vbl

What are others' preferences?

Rangi42 avatar Nov 02 '23 17:11 Rangi42

Hmm, periods should probably be avoided, as there was a suggested "current label scope" syntax using multiple periods.

ISSOtm avatar Nov 02 '23 19:11 ISSOtm

Then it's between : or ::. Here's a real-world comparison:

; using single colon
RestoreAllPP:
	ld a, MON_PP : call GetPartyParamLocation
	push hl
	ld a, MON_MOVES : call GetPartyParamLocation
	pop de
	xor a : ld [wMenuCursorY], a : ld [wMonType], a
	ld c, NUM_MOVES
.loop
	ld a, [hli]
	and a : ret z
	push hl
	push de : push bc
	call GetMaxPPOfMove
	pop bc : pop de
	ld a, [de] : and PP_UP_MASK : ld b, a
	ld a, [wTempPP] : add b : ld [de], a
	inc de
	ld hl, wMenuCursorY : inc [hl]
	pop hl
	dec c : jr nz, .loop
	ret
; using double colon
RestoreAllPP:
	ld a, MON_PP :: call GetPartyParamLocation
	push hl
	ld a, MON_MOVES :: call GetPartyParamLocation
	pop de
	xor a :: ld [wMenuCursorY], a :: ld [wMonType], a
	ld c, NUM_MOVES
.loop
	ld a, [hli]
	and a :: ret z
	push hl
	push de :: push bc
	call GetMaxPPOfMove
	pop bc :: pop de
	ld a, [de] :: and PP_UP_MASK :: ld b, a
	ld a, [wTempPP] :: add b :: ld [de], a
	inc de
	ld hl, wMenuCursorY :: inc [hl]
	pop hl
	dec c :: jr nz, .loop
	ret

Rangi42 avatar Nov 02 '23 19:11 Rangi42

I just realized $ means end-of-line in regex; maybe that would justify it?

; using dollar sign
RestoreAllPP:
	ld a, MON_PP $ call GetPartyParamLocation
	push hl
	ld a, MON_MOVES $ call GetPartyParamLocation
	pop de
	xor a $ ld [wMenuCursorY], a $ ld [wMonType], a
	ld c, NUM_MOVES
.loop
	ld a, [hli]
	and a $ ret z
	push hl
	push de $ push bc
	call GetMaxPPOfMove
	pop bc $ pop de
	ld a, [de] $ and PP_UP_MASK $ ld b, a
	ld a, [wTempPP] $ add b $ ld [de], a
	inc de
	ld hl, wMenuCursorY $ inc [hl]
	pop hl
	dec c $ jr nz, .loop
	ret

And for completeness, here's #, the other unused punctuation:

; using pound sign
RestoreAllPP:
	ld a, MON_PP # call GetPartyParamLocation
	push hl
	ld a, MON_MOVES # call GetPartyParamLocation
	pop de
	xor a # ld [wMenuCursorY], a # ld [wMonType], a
	ld c, NUM_MOVES
.loop
	ld a, [hli]
	and a # ret z
	push hl
	push de # push bc
	call GetMaxPPOfMove
	pop bc # pop de
	ld a, [de] # and PP_UP_MASK # ld b, a
	ld a, [wTempPP] # add b # ld [de], a
	inc de
	ld hl, wMenuCursorY # inc [hl]
	pop hl
	dec c # jr nz, .loop
	ret

Rangi42 avatar Nov 02 '23 19:11 Rangi42

So, : might conflict with a ternary operator. Although note the discussion in #621 about what syntax to use for that anyway.

Rangi42 avatar Nov 02 '23 19:11 Rangi42

are you trying to make assembly code even less readable than it is?

untoxa avatar Nov 02 '23 21:11 untoxa

@ISSOtm The examples posted by sylvie (zlago) were selling me on $ specifically. I quickly got used to how it looks. And it doesn't conflict with later feature plans (./.. for nested scopes, : for ternary operator, # for raw strings/tokens, ' for character literals).

Rangi42 avatar Nov 03 '23 16:11 Rangi42