shithub: rgbds

Download patch

ref: ec6d63bce3ead381e70a1aca0ad2501bee4d9fae
parent: bdcef6f252d6a39010f46c490a955d9aa951102c
author: Rangi <[email protected]>
date: Sun Nov 21 11:18:23 EST 2021

Allow underscores in gfx literals (#951)

Fixes #950

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -1204,6 +1204,7 @@
 		int c = peek();
 		int bit;
 
+		// Check for '_' after digits in case one of the digits is '_'
 		if (c == binDigits[0])
 			bit = 0;
 		else if (c == binDigits[1])
@@ -1229,7 +1230,7 @@
 	for (;; shiftChar()) {
 		int c = peek();
 
-		if (c >= 'a' && c <= 'f') /* Convert letters to right after digits */
+		if (c >= 'a' && c <= 'f')
 			c = c - 'a' + 10;
 		else if (c >= 'A' && c <= 'F')
 			c = c - 'A' + 10;
@@ -1257,7 +1258,7 @@
 
 static uint32_t readGfxConstant(void)
 {
-	uint32_t bp0 = 0, bp1 = 0;
+	uint32_t bitPlaneLower = 0, bitPlaneUpper = 0;
 	uint8_t width = 0;
 
 	dbgPrint("Reading gfx constant with digits [%c,%c,%c,%c]\n",
@@ -1266,6 +1267,7 @@
 		int c = peek();
 		uint32_t pixel;
 
+		// Check for '_' after digits in case one of the digits is '_'
 		if (c == gfxDigits[0])
 			pixel = 0;
 		else if (c == gfxDigits[1])
@@ -1274,12 +1276,14 @@
 			pixel = 2;
 		else if (c == gfxDigits[3])
 			pixel = 3;
+		else if (c == '_' && width > 0)
+			continue;
 		else
 			break;
 
 		if (width < 8) {
-			bp0 = bp0 << 1 | (pixel & 1);
-			bp1 = bp1 << 1 | (pixel >> 1);
+			bitPlaneLower = bitPlaneLower << 1 | (pixel & 1);
+			bitPlaneUpper = bitPlaneUpper << 1 | (pixel >> 1);
 		}
 		if (width < 9)
 			width++;
@@ -1291,7 +1295,7 @@
 		warning(WARNING_LARGE_CONSTANT,
 			"Graphics constant is too long, only first 8 pixels considered\n");
 
-	return bp1 << 8 | bp0;
+	return bitPlaneUpper << 8 | bitPlaneLower;
 }
 
 /* Functions to read identifiers & keywords */
--- a/src/asm/rgbasm.5
+++ b/src/asm/rgbasm.5
@@ -219,6 +219,10 @@
 .El
 .Pp
 Underscores are also accepted in numbers, except at the beginning of one.
+This can be useful for grouping digits, like
+.Ql 123_456
+or
+.Ql %1100_1001 .
 .Pp
 The "character constant" form yields the value the character maps to in the current charmap.
 For example, by default
--- a/test/asm/underscore-in-numeric-literal.asm
+++ b/test/asm/underscore-in-numeric-literal.asm
@@ -10,6 +10,7 @@
 	db &200 ; octal
 	db %11110000, %10 ; binary
 	dl 6.283185 ; fixed point
+	dw `01233210, `00332211 ; gfx
 
 ; with underscores
 	dw _1234 ; label
@@ -19,3 +20,9 @@
 	db &2_0_0_ ; octal
 	db %1111_0000, %1_0 ; binary
 	dl 6_._283_185 ; fixed point
+	dw `0123_3210, `00_33_22_11_ ; gfx
+
+; underscores as digits
+	opt g_ABC, b_X
+	db %_X_X__XX
+	dw `_A_B_C__
binary files a/test/asm/underscore-in-numeric-literal.out.bin b/test/asm/underscore-in-numeric-literal.out.bin differ