Improve string/interpolation formatting (#1491)

- The '#' component for type 's' now escapes the string characters
- The '#' component for type 'f' now prints a precision suffix
- The new 'q' component specifies a precision value
This commit is contained in:
Sylvie
2024-09-01 12:54:26 -04:00
committed by GitHub
parent 2fb76ce584
commit 6b8d33529e
13 changed files with 128 additions and 48 deletions

View File

@@ -16,7 +16,8 @@ def fr = MUL(20.0, 0.32)
def q8 = 1.25q8
def q16 = 1.25Q16
def q24 = 1.25q.24
println "Q8 ${x:q8} Q16 ${x:q16} Q24 ${x:q24}"
println "{#f:q8} {#f:q16} {#f:q24}"
println "Q8 {#x:q8} Q16 {#x:q16} Q24 {#x:q24}"
def qerr = 1.25q32
println qerr
println "{q0f:qerr}"

View File

@@ -1,3 +1,5 @@
error: fixed-point-precision.asm(21):
error: fixed-point-precision.asm(22):
Fixed-point constant precision must be between 1 and 31
error: Assembly aborted (1 error)!
error: fixed-point-precision.asm(23):
Fixed-point constant precision 0 invalid, defaulting to 16
error: Assembly aborted (2 errors)!

View File

@@ -4,5 +4,6 @@
`16.12`: 16.119995 -> $00101eb8
`6.283185`: 6.283188 -> $0006487f
32% of 20 = 6.40015 (~6.40) (~~6)
0.00488q16 1.25000q16 320.00000q16
Q8 $140 Q16 $14000 Q24 $1400000
$14000
1.25000

View File

@@ -7,10 +7,7 @@ MACRO compare
def v1 = \3(\4q\1, \5q\1, \1)
def v2 = \3(\4q\2, \5q\2, \2)
endc
opt Q\1
print "{.4f:v1} == "
opt Q\2
println "{.4f:v2}"
println "{.4q\1f:v1} == {.4q\2f:v2}"
ENDM
compare 8, 16, mul, 6.0, 7.0

View File

@@ -2,15 +2,16 @@ println STRFMT("%+d %++d", 42, 42)
println STRFMT("%#x %##x", 42, 42)
println STRFMT("%-4d %--4d", 42, 42)
println STRFMT("%.f %..f", 42.0, 42.0)
println STRFMT("%qf %q.16f", 42.0, 42.0)
DEF N = 42
println "{5d:N} {5d5:N}"
println "{x:N} {xx:N}"
println STRFMT("%+s", "hello")
println STRFMT("%#s", "hello")
println STRFMT("%0s", "hello")
println STRFMT("%.5s", "hello")
println STRFMT("%q16s", "hello")
println STRFMT("%#d", 42)
println STRFMT("%.5d", 42)

View File

@@ -6,20 +6,24 @@ error: invalid-format.asm(3):
STRFMT: Invalid format spec for argument 2
error: invalid-format.asm(4):
STRFMT: Invalid format spec for argument 2
error: invalid-format.asm(7):
Invalid format spec '5d5'
error: invalid-format.asm(5):
Fixed-point constant precision 0 invalid, defaulting to 16
error: invalid-format.asm(5):
STRFMT: Invalid format spec for argument 2
error: invalid-format.asm(8):
Invalid format spec '5d5'
error: invalid-format.asm(9):
Invalid format spec 'xx'
error: invalid-format.asm(10):
Formatting string with sign flag '+'
error: invalid-format.asm(11):
Formatting string with prefix flag '#'
Formatting string with sign flag '+'
error: invalid-format.asm(12):
Formatting string with padding flag '0'
error: invalid-format.asm(13):
Formatting string with fractional width
error: invalid-format.asm(15):
Formatting type 'd' with prefix flag '#'
error: invalid-format.asm(14):
Formatting string with fractional precision
error: invalid-format.asm(16):
Formatting type 'd' with exact flag '#'
error: invalid-format.asm(17):
Formatting type 'd' with fractional width
error: Assembly aborted (12 errors)!
error: Assembly aborted (14 errors)!

View File

@@ -2,6 +2,7 @@
$2a %x
42 %4d
42 %f
42.00000 %16f
42 42
2a 2a
hello

View File

@@ -3,11 +3,12 @@ def m equ -42
def f equ -123.0456
def pi equ 3.14159
def s equs "hello"
def t equs "\"\\t\" is '\t'"
println "<{ -6d:n}> <{+06u:n}> <{5x:n}> <{#16b:n}>"
println "<{u:m}> <{+3d:m}> <{#016o:m}>"
println "<{f:pi}> <{06.f:f}> <{.10f:f}>"
println "<{#-10s:s}> <{10s:s}>"
println "\"{#-20s:t}\", \"{#20s:t}\", \"{20s:t}\""
macro foo
println "\1 <{\1}>"

View File

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

View File

@@ -1,5 +1,5 @@
< 300 > <+00300> < 12c> < %100101100>
<4294967254> <-42> <&000037777777726>
<3.14159> <-00123> <-123.0455932617>
<hello > < hello>
"\"\\t\" is '\t' ", " \"\\t\" is '\t'", " "\t" is ' '"
-6d:n <300 >