Skip to content

Commit

Permalink
Implement __SCOPE__
Browse files Browse the repository at this point in the history
__SCOPE__ expands to the currently scoped label.
It is empty if inside the main scope.

Fixes gbdev#775
  • Loading branch information
Rangi42 committed Mar 5, 2021
1 parent 88048cd commit 1ee5343
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/asm/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,8 @@ The following symbols are defined by the assembler:
.It Ic SET Ta Dv _RS Ta _RS Counter
.It Ic EQU Ta Dv _NARG Ta Number of arguments passed to macro, updated by Ic SHIFT
.It Ic EQU Ta Dv __LINE__ Ta The current line number
.It Ic EQUS Ta Dv __FILE__ Ta The current filename
.It Ic EQUS Ta Dv __FILE__ Ta The current filename (quoted as a string literal)
.It Ic EQU Ta Dv __SCOPE__ Ta The current scoped label (empty if none is scoped)
.It Ic EQUS Ta Dv __DATE__ Ta Today's date
.It Ic EQUS Ta Dv __TIME__ Ta The current time
.It Ic EQUS Ta Dv __ISO_8601_LOCAL__ Ta ISO 8601 timestamp (local)
Expand Down
21 changes: 20 additions & 1 deletion src/asm/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static int32_t Callback__LINE__(void)
return lexer_GetLineNo();
}

static char const *Callback__FILE__(void)
static const char *Callback__FILE__(void)
{
/*
* FIXME: this is dangerous, and here's why this is CURRENTLY okay. It's still bad, fix it.
Expand Down Expand Up @@ -129,6 +129,22 @@ static char const *Callback__FILE__(void)
return buf;
}

static const char *Callback__SCOPE__(void)
{
static char buf[MAXSYMLEN + 1];

if (!labelScope) {
// __SCOPE__ is empty inside the main scope (no global label)
buf[0] = '\0';
} else {
// labelScope is a valid label, so it will fit within MAXSYMLEN characters
assert(strlen(labelScope) < sizeof(buf));
strcpy(buf, labelScope);
}

return buf;
}

static int32_t CallbackPC(void)
{
struct Section const *section = sect_GetSymbolSection();
Expand Down Expand Up @@ -712,6 +728,7 @@ void sym_Init(time_t now)
struct Symbol *_NARGSymbol = createBuiltinSymbol("_NARG");
struct Symbol *__LINE__Symbol = createBuiltinSymbol("__LINE__");
struct Symbol *__FILE__Symbol = createBuiltinSymbol("__FILE__");
struct Symbol *__SCOPE__Symbol = createBuiltinSymbol("__SCOPE__");

PCSymbol->type = SYM_LABEL;
PCSymbol->section = NULL;
Expand All @@ -722,6 +739,8 @@ void sym_Init(time_t now)
__LINE__Symbol->numCallback = Callback__LINE__;
__FILE__Symbol->type = SYM_EQUS;
__FILE__Symbol->strCallback = Callback__FILE__;
__SCOPE__Symbol->type = SYM_EQUS;
__SCOPE__Symbol->strCallback = Callback__SCOPE__;
sym_AddSet("_RS", 0)->isBuiltin = true;

sym_AddEqu("__RGBDS_MAJOR__", PACKAGE_VERSION_MAJOR)->isBuiltin = true;
Expand Down
1 change: 1 addition & 0 deletions test/asm/scope-noscope.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PRINTLN "inside {__SCOPE__}"
Empty file added test/asm/scope-noscope.err
Empty file.
1 change: 1 addition & 0 deletions test/asm/scope-noscope.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inside
25 changes: 25 additions & 0 deletions test/asm/scope.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SECTION "test", ROM0

assert !STRLEN("{__SCOPE__}")

verify: MACRO
assert !STRCMP("{__SCOPE__}", "\1")
assert __SCOPE__ == \1
ENDM

Alpha:
verify Alpha
.local
verify Alpha

Beta::
.local::
verify Beta

S EQUS """
Gamma:
verify Gamma
"""
S
PURGE S
verify Gamma
Empty file added test/asm/scope.err
Empty file.
Empty file added test/asm/scope.out
Empty file.

0 comments on commit 1ee5343

Please sign in to comment.