mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-22 03:02:06 +00:00
@@ -219,16 +219,18 @@ bool yywrap(void)
|
||||
contextStack->fileInfo = (struct FileStackNode *)fileInfo;
|
||||
}
|
||||
|
||||
fileInfo->iters[0]++;
|
||||
/* If this is a FOR, update the symbol value */
|
||||
if (contextStack->forName) {
|
||||
if (contextStack->forName && fileInfo->iters[0] <= contextStack->nbReptIters) {
|
||||
contextStack->forValue += contextStack->forStep;
|
||||
struct Symbol *sym = sym_AddSet(contextStack->forName,
|
||||
contextStack->forValue);
|
||||
|
||||
/* This error message will refer to the current iteration */
|
||||
if (sym->type != SYM_SET)
|
||||
fatalerror("Failed to update FOR symbol value\n");
|
||||
}
|
||||
/* Advance to the next iteration */
|
||||
fileInfo->iters[0]++;
|
||||
/* If this wasn't the last iteration, wrap instead of popping */
|
||||
if (fileInfo->iters[0] <= contextStack->nbReptIters) {
|
||||
lexer_RestartRept(contextStack->fileInfo->lineNo);
|
||||
@@ -479,6 +481,20 @@ void fstk_RunFor(char const *symName, int32_t start, int32_t stop, int32_t step,
|
||||
fatalerror("Not enough memory for FOR symbol name: %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
bool fstk_Break(void)
|
||||
{
|
||||
dbgPrint("Breaking out of REPT/FOR\n");
|
||||
|
||||
if (contextStack->fileInfo->type != NODE_REPT) {
|
||||
error("BREAK can only be used inside a REPT/FOR block\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Prevent more iterations */
|
||||
contextStack->nbReptIters = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void fstk_Init(char const *mainPath, size_t maxRecursionDepth)
|
||||
{
|
||||
struct LexerState *state = lexer_OpenFile(mainPath);
|
||||
|
||||
@@ -251,6 +251,7 @@ static struct KeywordMapping {
|
||||
{"REPT", T_POP_REPT},
|
||||
{"FOR", T_POP_FOR},
|
||||
{"ENDR", T_POP_ENDR},
|
||||
{"BREAK", T_POP_BREAK},
|
||||
|
||||
{"LOAD", T_POP_LOAD},
|
||||
{"ENDL", T_POP_ENDL},
|
||||
@@ -498,7 +499,7 @@ struct KeywordDictNode {
|
||||
uint16_t children[0x60 - ' '];
|
||||
struct KeywordMapping const *keyword;
|
||||
/* Since the keyword structure is invariant, the min number of nodes is known at compile time */
|
||||
} keywordDict[355] = {0}; /* Make sure to keep this correct when adding keywords! */
|
||||
} keywordDict[359] = {0}; /* Make sure to keep this correct when adding keywords! */
|
||||
|
||||
/* Convert a char into its index into the dict */
|
||||
static inline uint8_t dictIndex(char c)
|
||||
@@ -2049,6 +2050,85 @@ static int yylex_SKIP_TO_ENDC(void)
|
||||
return skipIfBlock(true);
|
||||
}
|
||||
|
||||
static int yylex_SKIP_TO_ENDR(void)
|
||||
{
|
||||
dbgPrint("Skipping remainder of REPT/FOR block\n");
|
||||
lexer_SetMode(LEXER_NORMAL);
|
||||
int depth = 1;
|
||||
bool atLineStart = lexerState->atLineStart;
|
||||
|
||||
/* Prevent expanding macro args and symbol interpolation in this state */
|
||||
lexerState->disableMacroArgs = true;
|
||||
lexerState->disableInterpolation = true;
|
||||
|
||||
for (;;) {
|
||||
if (atLineStart) {
|
||||
int c;
|
||||
|
||||
for (;;) {
|
||||
c = peek(0);
|
||||
if (!isWhitespace(c))
|
||||
break;
|
||||
shiftChars(1);
|
||||
}
|
||||
|
||||
if (startsIdentifier(c)) {
|
||||
shiftChars(1);
|
||||
switch (readIdentifier(c)) {
|
||||
case T_POP_FOR:
|
||||
case T_POP_REPT:
|
||||
depth++;
|
||||
break;
|
||||
|
||||
case T_POP_ENDR:
|
||||
depth--;
|
||||
if (!depth)
|
||||
goto finish;
|
||||
break;
|
||||
|
||||
case T_POP_IF:
|
||||
nIFDepth++;
|
||||
break;
|
||||
|
||||
case T_POP_ENDC:
|
||||
nIFDepth--;
|
||||
}
|
||||
}
|
||||
atLineStart = false;
|
||||
}
|
||||
|
||||
/* Read chars until EOL */
|
||||
do {
|
||||
int c = nextChar();
|
||||
|
||||
if (c == EOF) {
|
||||
goto finish;
|
||||
} else if (c == '\\') {
|
||||
/* Unconditionally skip the next char, including line conts */
|
||||
c = nextChar();
|
||||
} else if (c == '\r' || c == '\n') {
|
||||
atLineStart = true;
|
||||
}
|
||||
|
||||
if (c == '\r' || c == '\n') {
|
||||
/* Handle CRLF before nextLine() since shiftChars updates colNo */
|
||||
if (c == '\r' && peek(0) == '\n')
|
||||
shiftChars(1);
|
||||
/* Do this both on line continuations and plain EOLs */
|
||||
nextLine();
|
||||
}
|
||||
} while (!atLineStart);
|
||||
}
|
||||
finish:
|
||||
|
||||
lexerState->disableMacroArgs = false;
|
||||
lexerState->disableInterpolation = false;
|
||||
lexerState->atLineStart = false;
|
||||
|
||||
/* yywrap() will finish the REPT/FOR loop */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int yylex(void)
|
||||
{
|
||||
restart:
|
||||
@@ -2066,7 +2146,8 @@ restart:
|
||||
[LEXER_NORMAL] = yylex_NORMAL,
|
||||
[LEXER_RAW] = yylex_RAW,
|
||||
[LEXER_SKIP_TO_ELIF] = yylex_SKIP_TO_ELIF,
|
||||
[LEXER_SKIP_TO_ENDC] = yylex_SKIP_TO_ENDC
|
||||
[LEXER_SKIP_TO_ENDC] = yylex_SKIP_TO_ENDC,
|
||||
[LEXER_SKIP_TO_ENDR] = yylex_SKIP_TO_ENDR
|
||||
};
|
||||
int token = lexerModeFuncs[lexerState->mode]();
|
||||
|
||||
|
||||
@@ -463,6 +463,7 @@ static inline void failAssertMsg(enum AssertionType type, char const *msg)
|
||||
%token T_POP_POPC
|
||||
%token T_POP_SHIFT
|
||||
%token T_POP_ENDR
|
||||
%token T_POP_BREAK
|
||||
%token T_POP_LOAD T_POP_ENDL
|
||||
%token T_POP_FAIL
|
||||
%token T_POP_WARN
|
||||
@@ -684,6 +685,7 @@ simple_pseudoop : include
|
||||
| rept
|
||||
| for
|
||||
| shift
|
||||
| break
|
||||
| fail
|
||||
| warn
|
||||
| assert
|
||||
@@ -832,6 +834,12 @@ for_args : const {
|
||||
}
|
||||
;
|
||||
|
||||
break : T_POP_BREAK {
|
||||
if (fstk_Break())
|
||||
lexer_SetMode(LEXER_SKIP_TO_ENDR);
|
||||
}
|
||||
;
|
||||
|
||||
macrodef : T_LABEL T_COLON T_POP_MACRO {
|
||||
int32_t nDefinitionLineNo = lexer_GetLineNo();
|
||||
char *body;
|
||||
|
||||
@@ -1616,6 +1616,35 @@ blocks, you can use the escape sequence
|
||||
inside of
|
||||
.Ic FOR
|
||||
blocks, and they can be nested.
|
||||
.Pp
|
||||
You can stop a repeating block with the
|
||||
.Ic BREAK
|
||||
command.
|
||||
A
|
||||
.Ic BREAK
|
||||
inside of a
|
||||
.Ic REPT
|
||||
or
|
||||
.Ic FOR
|
||||
block will interrupt the current iteration and not repeat any more.
|
||||
It will continue running code after the block's
|
||||
.Ic ENDR .
|
||||
For example:
|
||||
.Bd -literal -offset indent
|
||||
FOR V, 1, 100
|
||||
PRINT "{d:V}"
|
||||
IF V == 5
|
||||
PRINT " stop! "
|
||||
BREAK
|
||||
ENDC
|
||||
PRINT ", "
|
||||
ENDR
|
||||
PRINTLN "done {d:V}"
|
||||
.Ed
|
||||
This will print:
|
||||
.Bd -literal -offset indent
|
||||
1, 2, 3, 4, 5 stop! done 5
|
||||
.Ed
|
||||
.Ss Aborting the assembly process
|
||||
.Ic FAIL
|
||||
and
|
||||
|
||||
Reference in New Issue
Block a user