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 31, 2021
1 parent 39c38f9 commit 916a397
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/asm/rgbasm.5
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ The following symbols are defined by the assembler:
.It Dv _NARG Ta Ic EQU Ta Number of arguments passed to macro, updated by Ic SHIFT
.It Dv __LINE__ Ta Ic EQU Ta The current line number
.It Dv __FILE__ Ta Ic EQUS Ta The current filename
.It Dv __SCOPE__ Ta Ic EQU Ta The current scoped label (empty if none is scoped)
.It Dv __DATE__ Ta Ic EQUS Ta Today's date
.It Dv __TIME__ Ta Ic EQUS Ta The current time
.It Dv __ISO_8601_LOCAL__ Ta Ic EQUS 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 @@ -717,6 +733,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 @@ -727,6 +744,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
28 changes: 28 additions & 0 deletions test/asm/scope.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SECTION "test", ROM0

assert !STRLEN("{__SCOPE__}")

SomeFunction:
println __FILE__, ":{d:__LINE__}:{__SCOPE__}: 2 + 2 = ", 2+2

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.
1 change: 1 addition & 0 deletions test/asm/scope.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scope.asm:6:SomeFunction: 2 + 2 = $4

0 comments on commit 916a397

Please sign in to comment.