1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

scanner: make 0o prefix the only way to define octals

This commit is contained in:
SleepyRoy
2020-02-24 06:43:04 +08:00
committed by GitHub
parent 9d2a60bb11
commit 7d2eb4f604
7 changed files with 50 additions and 10 deletions

View File

@@ -92,7 +92,7 @@ pub fn (g mut Gen) generate_elf_footer() {
mut f := os.create(g.out_name) or {
panic(err)
}
os.chmod(g.out_name, 0775) // make it an executable
os.chmod(g.out_name, 0o775) // make it an executable
f.write_bytes(g.buf.data, g.buf.len)
f.close()
println('x64 elf binary has been successfully generated')

View File

@@ -340,6 +340,19 @@ pub fn (s mut Scanner) scan() token.Token {
}
// `123`, `.123`
else if c.is_digit() || (c == `.` && nextc.is_digit()) {
if !s.inside_string {
// In C ints with `0` prefix are octal (in V they're decimal), so discarding heading zeros is needed.
mut start_pos := s.pos
for start_pos < s.text.len && s.text[start_pos] == `0` {
start_pos++
}
mut prefix_zero_num := start_pos - s.pos // how many prefix zeros should be jumped
// for 0b, 0o, 0x the heading zero shouldn't be jumped
if c == `0` && start_pos < s.text.len && !s.text[start_pos].is_digit() {
prefix_zero_num--
}
s.pos += prefix_zero_num // jump these zeros
}
num := s.ident_number()
return s.scan_res(.number, num)
}