shithub: rgbds

Download patch

ref: d37aa93a7d0a0f17da1fe127ae6ec3a3a092477e
parent: bba532193b80c8ccf2beff68a13d2786f0f31b72
author: Rangi <[email protected]>
date: Sun Apr 25 15:50:54 EDT 2021

Port some cleanup from the WIP 'strings' branch

This is mostly variable renaming

--- a/Makefile
+++ b/Makefile
@@ -61,9 +61,9 @@
 	src/asm/lexer.o \
 	src/asm/macro.o \
 	src/asm/main.o \
-	src/asm/parser.o \
 	src/asm/opt.o \
 	src/asm/output.o \
+	src/asm/parser.o \
 	src/asm/rpn.o \
 	src/asm/section.o \
 	src/asm/symbol.o \
@@ -204,7 +204,7 @@
 # compilation and make the continous integration infrastructure return failure.
 
 develop:
-	$Qenv $(MAKE) -j WARNFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-type-limits \
+	$Qenv $(MAKE) WARNFLAGS="-Werror -Wall -Wextra -Wpedantic -Wno-type-limits \
 		-Wno-sign-compare -Wvla -Wformat -Wformat-security -Wformat-overflow=2 \
 		-Wformat-truncation=1 -Wformat-y2k -Wswitch-enum -Wunused \
 		-Wuninitialized -Wunknown-pragmas -Wstrict-overflow=4 \
--- a/include/asm/format.h
+++ b/include/asm/format.h
@@ -9,8 +9,8 @@
 #ifndef RGBDS_FORMAT_SPEC_H
 #define RGBDS_FORMAT_SPEC_H
 
-#include <stdint.h>
 #include <stdbool.h>
+#include <stdint.h>
 
 enum FormatState {
 	FORMAT_SIGN,    // expects '+' or ' ' (optional)
--- a/include/asm/symbol.h
+++ b/include/asm/symbol.h
@@ -82,8 +82,7 @@
 
 static inline bool sym_IsNumeric(struct Symbol const *sym)
 {
-	return sym->type == SYM_LABEL || sym->type == SYM_EQU
-	    || sym->type == SYM_SET;
+	return sym->type == SYM_LABEL || sym->type == SYM_EQU || sym->type == SYM_SET;
 }
 
 static inline bool sym_IsLabel(struct Symbol const *sym)
@@ -124,19 +123,19 @@
 struct Symbol *sym_AddSet(char const *symName, int32_t value);
 uint32_t sym_GetPCValue(void);
 uint32_t sym_GetConstantSymValue(struct Symbol const *sym);
-uint32_t sym_GetConstantValue(char const *s);
+uint32_t sym_GetConstantValue(char const *symName);
 /*
  * Find a symbol by exact name, bypassing expansion checks
  */
-struct Symbol *sym_FindExactSymbol(char const *name);
+struct Symbol *sym_FindExactSymbol(char const *symName);
 /*
  * Find a symbol by exact name; may not be scoped, produces an error if it is
  */
-struct Symbol *sym_FindUnscopedSymbol(char const *name);
+struct Symbol *sym_FindUnscopedSymbol(char const *symName);
 /*
  * Find a symbol, possibly scoped, by name
  */
-struct Symbol *sym_FindScopedSymbol(char const *name);
+struct Symbol *sym_FindScopedSymbol(char const *symName);
 struct Symbol const *sym_GetPC(void);
 struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size);
 struct Symbol *sym_Ref(char const *symName);
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -943,10 +943,10 @@
 	} else if (c == '{' && !lexerState->disableInterpolation) {
 		/* If character is an open brace, do symbol interpolation */
 		shiftChar();
-		char const *ptr = readInterpolation(0);
+		char const *str = readInterpolation(0);
 
-		if (ptr && ptr[0])
-			beginExpansion(ptr, false, ptr);
+		if (str && str[0])
+			beginExpansion(str, false, str);
 		goto restart;
 	}
 
@@ -1381,10 +1381,10 @@
 
 		if (c == '{') { /* Nested interpolation */
 			shiftChar();
-			char const *ptr = readInterpolation(depth + 1);
+			char const *str = readInterpolation(depth + 1);
 
-			if (ptr && ptr[0])
-				beginExpansion(ptr, false, ptr);
+			if (str && str[0])
+				beginExpansion(str, false, str);
 			continue; /* Restart, reading from the new buffer */
 		} else if (c == EOF || c == '\r' || c == '\n' || c == '"') {
 			error("Missing }\n");
@@ -1490,6 +1490,7 @@
 
 	size_t i = 0;
 	bool multiline = false;
+	char const *str;
 
 	// We reach this function after reading a single quote, but we also support triple quotes
 	if (peek() == '"') {
@@ -1584,8 +1585,7 @@
 			case '9':
 			case '<':
 				shiftChar();
-				char const *str = readMacroArg(c);
-
+				str = readMacroArg(c);
 				if (str) {
 					while (*str)
 						append_yylval_string(*str++);
@@ -1608,11 +1608,10 @@
 			// We'll be exiting the string scope, so re-enable expansions
 			// (Not interpolations, since they're handled by the function itself...)
 			lexerState->disableMacroArgs = false;
-			char const *ptr = readInterpolation(0);
-
-			if (ptr) {
-				while (*ptr)
-					append_yylval_string(*ptr++);
+			str = readInterpolation(0);
+			if (str) {
+				while (*str)
+					append_yylval_string(*str++);
 			}
 			lexerState->disableMacroArgs = true;
 			continue; // Do not copy an additional character
@@ -1642,6 +1641,7 @@
 	lexerState->disableInterpolation = true;
 
 	bool multiline = false;
+	char const *str;
 
 	// We reach this function after reading a single quote, but we also support triple quotes
 	append_yylval_string('"');
@@ -1733,9 +1733,8 @@
 			case '9':
 			case '<':
 				shiftChar();
-				char const *str = readMacroArg(c);
-
-				if (str)
+				str = readMacroArg(c);
+				if (str && str[0])
 					i = appendEscapedSubstring(str, i);
 				continue; // Do not copy an additional character
 
@@ -1761,10 +1760,9 @@
 			// We'll be exiting the string scope, so re-enable expansions
 			// (Not interpolations, since they're handled by the function itself...)
 			lexerState->disableMacroArgs = false;
-			char const *ptr = readInterpolation(0);
-
-			if (ptr && ptr[0])
-				i = appendEscapedSubstring(ptr, i);
+			str = readInterpolation(0);
+			if (str && str[0])
+				i = appendEscapedSubstring(str, i);
 			lexerState->disableMacroArgs = true;
 			continue; // Do not copy an additional character
 
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -179,15 +179,15 @@
 /*
  * Create a new symbol by name
  */
-static struct Symbol *createsymbol(char const *s)
+static struct Symbol *createsymbol(char const *symName)
 {
 	struct Symbol *symbol = malloc(sizeof(*symbol));
 
 	if (!symbol)
-		fatalerror("Failed to create symbol '%s': %s\n", s, strerror(errno));
+		fatalerror("Failed to create symbol '%s': %s\n", symName, strerror(errno));
 
-	if (snprintf(symbol->name, MAXSYMLEN + 1, "%s", s) > MAXSYMLEN)
-		warning(WARNING_LONG_STR, "Symbol name is too long: '%s'\n", s);
+	if (snprintf(symbol->name, MAXSYMLEN + 1, "%s", symName) > MAXSYMLEN)
+		warning(WARNING_LONG_STR, "Symbol name is too long: '%s'\n", symName);
 
 	symbol->isExported = false;
 	symbol->isBuiltin = false;
@@ -228,37 +228,37 @@
 	sym->macroSize = strlen(string);
 }
 
-struct Symbol *sym_FindExactSymbol(char const *name)
+struct Symbol *sym_FindExactSymbol(char const *symName)
 {
-	return hash_GetElement(symbols, name);
+	return hash_GetElement(symbols, symName);
 }
 
-struct Symbol *sym_FindUnscopedSymbol(char const *name)
+struct Symbol *sym_FindUnscopedSymbol(char const *symName)
 {
-	if (strchr(name, '.')) {
-		error("Expected non-scoped symbol name, not \"%s\"\n", name);
+	if (strchr(symName, '.')) {
+		error("Expected non-scoped symbol name, not \"%s\"\n", symName);
 		return NULL;
 	}
-	return sym_FindExactSymbol(name);
+	return sym_FindExactSymbol(symName);
 }
 
-struct Symbol *sym_FindScopedSymbol(char const *name)
+struct Symbol *sym_FindScopedSymbol(char const *symName)
 {
-	char const *dotPtr = strchr(name, '.');
+	char const *dotPtr = strchr(symName, '.');
 
 	if (dotPtr) {
 		if (strchr(dotPtr + 1, '.'))
 			fatalerror("'%s' is a nonsensical reference to a nested local symbol\n",
-				   name);
+				   symName);
 		/* If auto-scoped local label, expand the name */
-		if (dotPtr == name) { /* Meaning, the name begins with the dot */
+		if (dotPtr == symName) { /* Meaning, the name begins with the dot */
 			char fullname[MAXSYMLEN + 1];
 
-			fullSymbolName(fullname, sizeof(fullname), name, labelScope);
+			fullSymbolName(fullname, sizeof(fullname), symName, labelScope);
 			return sym_FindExactSymbol(fullname);
 		}
 	}
-	return sym_FindExactSymbol(name);
+	return sym_FindExactSymbol(symName);
 }
 
 struct Symbol const *sym_GetPC(void)
@@ -287,7 +287,7 @@
 	} else {
 		/* Do not keep a reference to the label's name after purging it */
 		if (symbol->name == labelScope)
-			labelScope = NULL;
+			sym_SetCurrentSymbolScope(NULL);
 
 		/*
 		 * FIXME: this leaks symbol->macro for SYM_EQUS and SYM_MACRO, but this can't
@@ -331,12 +331,12 @@
 /*
  * Return a constant symbol's value
  */
-uint32_t sym_GetConstantValue(char const *s)
+uint32_t sym_GetConstantValue(char const *symName)
 {
-	struct Symbol const *sym = sym_FindScopedSymbol(s);
+	struct Symbol const *sym = sym_FindScopedSymbol(symName);
 
 	if (sym == NULL)
-		error("'%s' not defined\n", s);
+		error("'%s' not defined\n", symName);
 	else
 		return sym_GetConstantSymValue(sym);
 
@@ -357,23 +357,23 @@
  * Create a symbol that will be non-relocatable and ensure that it
  * hasn't already been defined or referenced in a context that would
  * require that it be relocatable
- * @param symbolName The name of the symbol to create
+ * @param symName The name of the symbol to create
  * @param numeric If false, the symbol may not have been referenced earlier
  */
-static struct Symbol *createNonrelocSymbol(char const *symbolName, bool numeric)
+static struct Symbol *createNonrelocSymbol(char const *symName, bool numeric)
 {
-	struct Symbol *symbol = sym_FindExactSymbol(symbolName);
+	struct Symbol *symbol = sym_FindExactSymbol(symName);
 
 	if (!symbol) {
-		symbol = createsymbol(symbolName);
+		symbol = createsymbol(symName);
 	} else if (sym_IsDefined(symbol)) {
-		error("'%s' already defined at ", symbolName);
+		error("'%s' already defined at ", symName);
 		dumpFilename(symbol);
 		putc('\n', stderr);
 		return NULL; // Don't allow overriding the symbol, that'd be bad!
 	} else if (!numeric) {
 		// The symbol has already been referenced, but it's not allowed
-		error("'%s' already referenced at ", symbolName);
+		error("'%s' already referenced at ", symName);
 		dumpFilename(symbol);
 		putc('\n', stderr);
 		return NULL; // Don't allow overriding the symbol, that'd be bad!
@@ -448,7 +448,7 @@
 }
 
 /*
- * Alter a SET symbols value
+ * Alter a SET symbol's value
  */
 struct Symbol *sym_AddSet(char const *symName, int32_t value)
 {
@@ -474,18 +474,18 @@
 
 /*
  * Add a label (aka "relocatable symbol")
- * @param name The label's full name (so `.name` is invalid)
+ * @param symName The label's full name (so `.name` is invalid)
  * @return The created symbol
  */
-static struct Symbol *addLabel(char const *name)
+static struct Symbol *addLabel(char const *symName)
 {
-	assert(name[0] != '.'); /* The symbol name must have been expanded prior */
-	struct Symbol *sym = sym_FindExactSymbol(name);
+	assert(symName[0] != '.'); /* The symbol name must have been expanded prior */
+	struct Symbol *sym = sym_FindExactSymbol(symName);
 
 	if (!sym) {
-		sym = createsymbol(name);
+		sym = createsymbol(symName);
 	} else if (sym_IsDefined(sym)) {
-		error("'%s' already defined at ", name);
+		error("'%s' already defined at ", symName);
 		dumpFilename(sym);
 		putc('\n', stderr);
 		return NULL;
@@ -500,7 +500,7 @@
 	sym->section = sect_GetSymbolSection();
 
 	if (sym && !sym->section)
-		error("Label \"%s\" created outside of a SECTION\n", name);
+		error("Label \"%s\" created outside of a SECTION\n", symName);
 	return sym;
 }
 
@@ -507,55 +507,55 @@
 /*
  * Add a local (.name or Parent.name) relocatable symbol
  */
-struct Symbol *sym_AddLocalLabel(char const *name)
+struct Symbol *sym_AddLocalLabel(char const *symName)
 {
 	if (!labelScope) {
-		error("Local label '%s' in main scope\n", name);
+		error("Local label '%s' in main scope\n", symName);
 		return NULL;
 	}
 
 	char fullname[MAXSYMLEN + 1];
 
-	if (name[0] == '.') {
+	if (symName[0] == '.') {
 		/* If symbol is of the form `.name`, expand to the full `Parent.name` name */
-		fullSymbolName(fullname, sizeof(fullname), name, labelScope);
-		name = fullname; /* Use the expanded name instead */
+		fullSymbolName(fullname, sizeof(fullname), symName, labelScope);
+		symName = fullname; /* Use the expanded name instead */
 	} else {
 		size_t i = 0;
 
 		/* Otherwise, check that `Parent` is in fact the current scope */
-		while (labelScope[i] && name[i] == labelScope[i])
+		while (labelScope[i] && symName[i] == labelScope[i])
 			i++;
 		/* Assuming no dots in `labelScope` */
-		assert(strchr(&name[i], '.')); /* There should be at least one dot, though */
-		size_t parentLen = i + (strchr(&name[i], '.') - name);
+		assert(strchr(&symName[i], '.')); /* There should be at least one dot, though */
+		size_t parentLen = i + (strchr(&symName[i], '.') - symName);
 
 		/*
-		 * Check that `labelScope[i]` ended the check, guaranteeing that `name` is at least
-		 * as long, and then that this was the entirety of the `Parent` part of `name`.
+		 * Check that `labelScope[i]` ended the check, guaranteeing that `symName` is at
+		 * least as long, and then that this was the entire `Parent` part of `symName`.
 		 */
-		if (labelScope[i] != '\0' || name[i] != '.') {
+		if (labelScope[i] != '\0' || symName[i] != '.') {
 			assert(parentLen <= INT_MAX);
-			error("Not currently in the scope of '%.*s'\n", (int)parentLen, name);
+			error("Not currently in the scope of '%.*s'\n", (int)parentLen, symName);
 		}
-		if (strchr(&name[parentLen + 1], '.')) /* There will at least be a terminator */
+		if (strchr(&symName[parentLen + 1], '.')) /* There will at least be a terminator */
 			fatalerror("'%s' is a nonsensical reference to a nested local label\n",
-				   name);
+				   symName);
 	}
 
-	return addLabel(name);
+	return addLabel(symName);
 }
 
 /*
  * Add a relocatable symbol
  */
-struct Symbol *sym_AddLabel(char const *name)
+struct Symbol *sym_AddLabel(char const *symName)
 {
-	struct Symbol *sym = addLabel(name);
+	struct Symbol *sym = addLabel(symName);
 
 	/* Set the symbol as the new scope */
 	if (sym)
-		labelScope = sym->name;
+		sym_SetCurrentSymbolScope(sym->name);
 	return sym;
 }
 
@@ -677,9 +677,9 @@
 	exportall = set;
 }
 
-static struct Symbol *createBuiltinSymbol(char const *name)
+static struct Symbol *createBuiltinSymbol(char const *symName)
 {
-	struct Symbol *sym = createsymbol(name);
+	struct Symbol *sym = createsymbol(symName);
 
 	sym->isBuiltin = true;
 	sym->hasCallback = true;
@@ -755,7 +755,7 @@
 #undef addNumber
 #undef addString
 
-	labelScope = NULL;
+	sym_SetCurrentSymbolScope(NULL);
 	anonLabelID = 0;
 
 	/* _PI is deprecated */