From adb1d3f8c91381684b8e81a3c13a7fa615bc578c Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 23 Feb 2020 19:33:07 +0800 Subject: [PATCH] scanner: enable 0oxx to handle octals --- vlib/builtin/int_test.v | 7 +++++++ vlib/compiler/scanner.v | 16 ++++++---------- vlib/v/scanner/scanner.v | 16 ++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index 9f93e3a8c5..de22637478 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -113,3 +113,10 @@ fn test_hex() { x := u64(10) assert x.hex() == '0xa' } + +fn test_oct() { + x1 := 0o12 + assert x1 == 10 + x2 := 012 + assert x2 == 10 +} diff --git a/vlib/compiler/scanner.v b/vlib/compiler/scanner.v index 941c0ca04c..b7ac8761d6 100644 --- a/vlib/compiler/scanner.v +++ b/vlib/compiler/scanner.v @@ -122,7 +122,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string { mut i := start mut i1 := 0 for i < end { - if txt[i] != num_sep { + if txt[i] != num_sep && txt[i] != `o` { b[i1]=txt[i] i1++ } @@ -172,17 +172,13 @@ fn (s mut Scanner) ident_hex_number() string { fn (s mut Scanner) ident_oct_number() string { start_pos := s.pos + s.pos += 2 // skip '0o' for { if s.pos >= s.text.len { break } c := s.text[s.pos] - if c.is_digit() { - if !c.is_oct_digit() && c != num_sep { - s.error('malformed octal constant') - } - } - else { + if !c.is_oct_digit() && c != num_sep { break } s.pos++ @@ -253,12 +249,12 @@ fn (s mut Scanner) ident_number() string { if s.expect('0x', s.pos) { return s.ident_hex_number() } + if s.expect('0o', s.pos) { + return s.ident_oct_number() + } if s.expect('0.', s.pos) || s.expect('0e', s.pos) { return s.ident_dec_number() } - if s.text[s.pos] == `0` { - return s.ident_oct_number() - } return s.ident_dec_number() } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 1a87307127..fff6d8926c 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -116,7 +116,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string { mut i := start mut i1 := 0 for i < end { - if txt[i] != num_sep { + if txt[i] != num_sep && txt[i] != `o` { b[i1] = txt[i] i1++ } @@ -168,17 +168,13 @@ fn (s mut Scanner) ident_hex_number() string { fn (s mut Scanner) ident_oct_number() string { start_pos := s.pos + s.pos += 2 // skip '0o' for { if s.pos >= s.text.len { break } c := s.text[s.pos] - if c.is_digit() { - if !c.is_oct_digit() && c != num_sep { - s.error('malformed octal constant') - } - } - else { + if !c.is_oct_digit() && c != num_sep { break } s.pos++ @@ -247,12 +243,12 @@ fn (s mut Scanner) ident_number() string { if s.expect('0x', s.pos) { return s.ident_hex_number() } + if s.expect('0o', s.pos) { + return s.ident_oct_number() + } if s.expect('0.', s.pos) || s.expect('0e', s.pos) { return s.ident_dec_number() } - if s.text[s.pos] == `0` { - return s.ident_oct_number() - } return s.ident_dec_number() }