Add 0b prefixed binary numbers

This commit is contained in:
Nigel Tao committed 2021-01-28 21:54:06 +11:00
1 parent 1a63b53e13
commit 8c4c714ad1
4 files changed
+21 -8

No files matched your search

+10 -4
View File
@@ -250,10 +250,16 @@ func appendComment(buf []byte, comments []string, line uint32, indent int, other
func appendNum(buf []byte, s string) []byte {
groupLen := uint32(6)
if (len(s) >= 2) && (s[0] == '0') && ((s[1] == 'X') || (s[1] == 'x')) {
buf = append(buf, "0x"...)
s = s[2:]
groupLen = 4
if (len(s) >= 2) && (s[0] == '0') {
if (s[1] == 'X') || (s[1] == 'x') {
buf = append(buf, "0x"...)
s = s[2:]
groupLen = 4
} else if (s[1] == 'B') || (s[1] == 'b') {
buf = append(buf, "0b"...)
s = s[2:]
groupLen = 4
}
}
nonUnderscores := uint32(0)
+6
View File
@@ -210,6 +210,10 @@ func hexaNumericUnderscore(c byte) bool {
return ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f') || (c == '_') || ('0' <= c && c <= '9')
}
func zeroOneUnderscore(c byte) bool {
return (c == '_') || ('0' <= c && c <= '1')
}
func numeric(c byte) bool {
return ('0' <= c && c <= '9')
}
@@ -336,6 +340,8 @@ loop:
if c == '0' && j < len(src) {
if next := src[j]; next == 'x' || next == 'X' {
j, isDigit = j+1, hexaNumericUnderscore
} else if next == 'b' || next == 'B' {
j, isDigit = j+1, zeroOneUnderscore
} else if numeric(next) {
return nil, nil, fmt.Errorf("token: legacy octal syntax at %s:%d", filename, line)
}