Implement STRFMT and more printf-like format specifiers for string interpolation (#646)

Fixes #570
Fixes #178

Use errors for inapplicable format spec flags instead of -Wstring-format
This commit is contained in:
Rangi
2020-12-29 16:53:15 -05:00
committed by GitHub
parent aa27e714d4
commit c0ce1da4c3
16 changed files with 675 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
ERROR: bracketed-symbols.asm(16):
Print types are only allowed for numbers
Formatting string as type 'X'
ERROR: bracketed-symbols.asm(20):
"Label" does not have a constant value
ERROR: bracketed-symbols.asm(21):

24
test/asm/strfmt.asm Normal file
View File

@@ -0,0 +1,24 @@
VAL EQUS STRFMT("Hello %s! I am %d years old today!", "world", $f)
PRINTT "{VAL}\n"
N = -42
PRINTT STRFMT("signed %010d == unsigned %010u\n", N, N)
N = 112
FMT EQUS "X"
PRINTT STRFMT("\tdb %#03{s:FMT} %% 26\t; %#03{FMT}\n", N, N % 26)
TEMPLATE EQUS "\"%s are %s\\n\""
PRINTT STRFMT(TEMPLATE, "roses", "red")
PRINTT STRFMT(TEMPLATE, "violets", "blue")
PRINTT STRFMT(TEMPLATE, "void", 0, "extra")
PRINTT STRCAT(STRFMT(STRFMT("%%%s.%d%s", "", 9, "f"), _PI), \
STRFMT(" ~ %s\n", STRFMT("%s%x", "thr", 238)))
PRINTT STRFMT("%d eol %", 1)
PRINTT "\n"
PRINTT STRFMT("invalid %w spec\n", 42)
PRINTT STRFMT("one=%d two=%d three=%d\n", 1)

11
test/asm/strfmt.err Normal file
View File

@@ -0,0 +1,11 @@
ERROR: strfmt.asm(14):
Formatting number as type 's'
ERROR: strfmt.asm(14):
STRFMT: 1 unformatted argument(s)
ERROR: strfmt.asm(19):
STRFMT: Illegal '%' at end of format string
ERROR: strfmt.asm(22):
STRFMT: Invalid format spec for argument 1
ERROR: strfmt.asm(24):
STRFMT: Not enough arguments for format spec
error: Assembly aborted (5 errors)!

10
test/asm/strfmt.out Normal file
View File

@@ -0,0 +1,10 @@
Hello world! I am 15 years old today!
signed -000000042 == unsigned 4294967254
db $70 % 26 ; $08
roses are red
violets are blue
void are 0
3.141586304 ~ three
1 eol %
invalid % spec
one=1 two=% three=%

View File

@@ -0,0 +1,15 @@
n equ 300
m equ -42
f equ -123.0456
s equs "hello"
printt "<{ -6d:n}> <{+06u:n}> <{5x:n}> <{#16b:n}>\n"
printt "<{u:m}> <{+3d:m}> <{#016o:m}>\n"
printt "<{f:_PI}> <{06f:f}> <{.10f:f}>\n"
printt "<{#-10s:s}> <{10s:s}>\n"
foo: macro
printt "<{\1}>\n"
endm
foo -6d:n ; space is trimmed

View File

@@ -0,0 +1,3 @@
ERROR: string-formatting.asm(9):
Formatting string with prefix flag '#'
error: Assembly aborted (1 errors)!

View File

@@ -0,0 +1,5 @@
< 300 > <+00300> < 12c> < %100101100>
<4294967254> <-42> <&000037777777726>
<3.14159> <-00123> <-123.0455932618>
<hello > < hello>
<300 >