gbdev / gbdev/rgbds

[Feature request] Inline assertions whose success/failure result can be used in expressions

Open
#1,885 4 comments 0 reactions 0 assignees View on GitHub
enhancement rgbasm
Dominant language
C++
Stars
1.6k
Forks
193
Avg merge
22h 17m
Merged PRs (30d)
26

Description

Spinoff from the comments of #201. I've encountered a use case for this twice recently:

- [In pokered](https://github.com/pret/pokered/pull/557#discussion_r2656596307), we twice want to do `assert COND, "message"` but then use `COND` in some further logic. Having to define a temp variable would be more trouble than it's worth vs just repeating the `COND` expression.
- In pokecrystal, we repeat this pattern for getting an unlabeled SRAM location from its labeled WRAM counterpart:
```asm
ld hl, sPlayerData + (wEventFlags - wPlayerData)
ld hl, sPlayerData + (wHallOfFameCount - wPlayerData)
ld hl, sPlayerData + (wPlayerID - wPlayerData)
; etc
```
What I'd rather do:
```asm
def Saved(label) = sPlayerData + (label - wPlayerData) ; user-defined function
ld hl, Saved(wEventFlags)
ld hl, Saved(wHallOfFameCount)
ld hl, Saved(wPlayerID)
; etc
```
However, that would be unsafe to use without a precondition of `label >= wPlayerData && label < wPlayerDataEnd`. User-defined functions are single expressions, so we would need an expression `assert`:
```asm
def Saved(label) = \ ; user-defined function
assert(label >= wPlayerData && label < wPlayerDataEnd, "Cannot save label") * \
sPlayerData + (label - wPlayerData)
ld hl, Saved(wEventFlags)
ld hl, Saved(wHallOfFameCount)
ld hl, Saved(wPlayerID)
; etc
```

We could probably also handle three-arg `assert(warn/fail/fatal, cond, msg)`.

Contributor guide

Open the contributing guide

Research direction

No implementation files, entry points, or tests are named in the issue. Start by reviewing the existing assert syntax and user-defined function handling, then define how one- and three-argument expression assertions should behave; done means the assertion result can be used in an expression with coverage for success and failure cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.