pokered icon indicating copy to clipboard operation
pokered copied to clipboard

Suggestion: update macro `li` to check entry length

Open Narishma-gb opened this issue 1 month ago • 6 comments

With the new constants in #543, similar to what dname does:

ASSERT CHARLEN(\1) <= n, "Name longer than {d:n} characters: \1"

We could pass constants such as ITEM_NAME_LENGTH and MOVE_NAME_LENGTH as arguments to list_start.

Usage:

  • ItemNames
  • MoveNames
  • TrainerNames
  • StatModTextStrings
  • VitaminStats
  • TrainerNamePointers

Narishma-gb avatar Nov 27 '25 15:11 Narishma-gb

I like this. It would get done for the Gen 2 repos as well.

list_start already takes an optional pointer argument, so:

MACRO? list_start
	DEF list_index = 0
	DEF list_item_length = 0
	IF _NARG > 1
		DEF list_item_length = \1
	ENDC
	_redef_current_label CURRENT_LIST_START, "._list_start\@", 2, \#
ENDM

MACRO? li
	ASSERT STRFIND(\1, "@") == -1, "String terminator \"@\" in list entry: \1"
	IF list_item_length
		ASSERT CHARLEN(\1) <= list_item_length, \
			"List entry longer than {d:list_item_length} characters: \1"
	ENDC
	db \1, "@"
	DEF list_index += 1
ENDM

Rangi42 avatar Nov 27 '25 16:11 Rangi42

Can you pass two optional arguments to a macro? How can we find out what is \1?

Narishma-gb avatar Nov 28 '25 16:11 Narishma-gb

As written above, you would need to pass 0 explicitly as \1 in order to pass a label as \2.

However, if it also checked for STRLEN("\1") > 0, then you could leave \1 empty, e.g. list_start, .label. But I think that could be too confusing/easy to miss, so better to just allow list_start, list_start maxlen, or list_start maxlen, label.

Rangi42 avatar Nov 28 '25 21:11 Rangi42

I see, thank you!

On a side note, I would like to propose a simplification of _redef_current_label. Currently it computes the total number of arguments, to fetch the last one as label, if present. Instead each macro that calls it could SHIFT whatever data is before, and pass \# where label is immediately next, if present. It's used by table_width and list_start, where this change seems to work.

MACRO? _redef_current_label
	IF DEF(\1)
		PURGE \1
	ENDC
-	IF _NARG == 3 + (\3)
-		DEF \1 EQUS "\<_NARG>"
+	IF _NARG > 2
+		DEF \1 EQUS "\3"
	ELIF STRLEN(#__SCOPE__)
		IF {{__SCOPE__}} - @ == 0
			DEF \1 EQUS #{__SCOPE__}
		ENDC
	ENDC
	IF !DEF(\1)
		DEF \1 EQUS \2
		{\1}:
	ENDC
ENDM

MACRO? table_width
	DEF CURRENT_TABLE_WIDTH = \1
+	SHIFT
-	_redef_current_label CURRENT_TABLE_START, "._table_width\@", 2, \#
+	_redef_current_label CURRENT_TABLE_START, "._table_width\@", \#
ENDM

Narishma-gb avatar Nov 29 '25 10:11 Narishma-gb

@Narishma-gb I went ahead and implemented this in pokecrystal before pokered, if you want to review: https://github.com/pret/pokecrystal/pull/1210

Rangi42 avatar Dec 01 '25 17:12 Rangi42

Thanks, it looks great!

Narishma-gb avatar Dec 01 '25 17:12 Narishma-gb