all: vfmt (new const rule)

pull/8265/head
Alexander Medvednikov 2021-01-24 10:54:27 +01:00
parent 12ff1c2702
commit 29d6e40f29
13 changed files with 114 additions and 107 deletions

View File

@ -1,9 +1,9 @@
## V 0.2.3 -## V 0.2.4
*Not yet released* -*Not yet released*
- Allow interfaces to define fields, not just methods.
## V 0.2.2 ## V 0.2.2 - 0.2.3
*22 Jan 2021* *22 Jan 2021*
- Allow interfaces to define fields, not just methods.
- `vweb` now uses struct embedding: `app.vweb.text('hello') => app.text('hello')`. - `vweb` now uses struct embedding: `app.vweb.text('hello') => app.text('hello')`.
- Consts can now be declared outside of `const()` blocks: `const x = 0`. - Consts can now be declared outside of `const()` blocks: `const x = 0`.
- Overloading of `>`, `<`, `!=`, `==`, `<=` and `>=` operators. - Overloading of `>`, `<`, `!=`, `==`, `<=` and `>=` operators.
@ -22,6 +22,8 @@
- Advanced vdoc search on mobile layout. - Advanced vdoc search on mobile layout.
- string's `left()`/`right` were removed in favor of slicing syntax: `str[..pos]`. - string's `left()`/`right` were removed in favor of slicing syntax: `str[..pos]`.
- gg: native graphics mode on macOS/iOS (using Cocoa Drawing API). - gg: native graphics mode on macOS/iOS (using Cocoa Drawing API).
- Full path to consts must be specified everywhere. This allows shadowing consts safely and
makes it easy to distinguish them from local variables.
## V 0.2.1 ## V 0.2.1
*30 Dec 2020* *30 Dec 2020*

View File

@ -131,10 +131,10 @@ fn (vd VDoc) render_search_index(out Output) {
fn (mut vd VDoc) render_static_html(out Output) { fn (mut vd VDoc) render_static_html(out Output) {
vd.assets = { vd.assets = {
'doc_css': vd.get_resource(css_js_assets[0], out) 'doc_css': vd.get_resource(main.css_js_assets[0], out)
'normalize_css': vd.get_resource(css_js_assets[1], out) 'normalize_css': vd.get_resource(main.css_js_assets[1], out)
'doc_js': vd.get_resource(css_js_assets[2], out) 'doc_js': vd.get_resource(main.css_js_assets[2], out)
'dark_mode_js': vd.get_resource(css_js_assets[3], out) 'dark_mode_js': vd.get_resource(main.css_js_assets[3], out)
'light_icon': vd.get_resource('light.svg', out) 'light_icon': vd.get_resource('light.svg', out)
'dark_icon': vd.get_resource('dark.svg', out) 'dark_icon': vd.get_resource('dark.svg', out)
'menu_icon': vd.get_resource('menu.svg', out) 'menu_icon': vd.get_resource('menu.svg', out)
@ -144,7 +144,7 @@ fn (mut vd VDoc) render_static_html(out Output) {
fn (vd VDoc) get_resource(name string, out Output) string { fn (vd VDoc) get_resource(name string, out Output) string {
cfg := vd.cfg cfg := vd.cfg
path := os.join_path(res_path, name) path := os.join_path(main.res_path, name)
mut res := os.read_file(path) or { panic('vdoc: could not read $path') } mut res := os.read_file(path) or { panic('vdoc: could not read $path') }
/* /*
if minify { if minify {
@ -294,7 +294,7 @@ fn (vd VDoc) gen_html(d doc.Doc) string {
symbols_toc_str := symbols_toc.str() symbols_toc_str := symbols_toc.str()
modules_toc.free() modules_toc.free()
symbols_toc.free() symbols_toc.free()
return html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}', return main.html_content.replace('{{ title }}', d.head.name).replace('{{ head_name }}',
header_name).replace('{{ version }}', version).replace('{{ light_icon }}', vd.assets['light_icon']).replace('{{ dark_icon }}', header_name).replace('{{ version }}', version).replace('{{ light_icon }}', vd.assets['light_icon']).replace('{{ dark_icon }}',
vd.assets['dark_icon']).replace('{{ menu_icon }}', vd.assets['menu_icon']).replace('{{ head_assets }}', vd.assets['dark_icon']).replace('{{ menu_icon }}', vd.assets['menu_icon']).replace('{{ head_assets }}',
if cfg.inline_assets { if cfg.inline_assets {
@ -452,7 +452,7 @@ fn doc_node_html(dn doc.DocNode, link string, head bool, include_examples bool,
dnw.write('${tabs[2]}<div class="title"><$head_tag>$dn.kind $sym_name$hash_link</$head_tag>') dnw.write('${tabs[2]}<div class="title"><$head_tag>$dn.kind $sym_name$hash_link</$head_tag>')
} }
if link.len != 0 { if link.len != 0 {
dnw.write('<a class="link" rel="noreferrer" target="_blank" href="$link">$link_svg</a>') dnw.write('<a class="link" rel="noreferrer" target="_blank" href="$link">$main.link_svg</a>')
} }
dnw.write('</div>') dnw.write('</div>')
} }

View File

@ -48,7 +48,7 @@ fn check_path(vexe string, dir string, tests []string) int {
println(found) println(found)
println('============\n') println('============\n')
println('diff:') println('diff:')
println(util.color_compare_strings(diff_cmd, found, expected)) println(util.color_compare_strings(main.diff_cmd, found, expected))
println('============\n') println('============\n')
nb_fail++ nb_fail++
} else { } else {

View File

@ -38,12 +38,12 @@ const show_warnings = '-hide-warnings' !in vet_options
fn main() { fn main() {
opt := Options{ opt := Options{
is_verbose: is_verbose is_verbose: main.is_verbose
} }
mut vet := Vet{ mut vet := Vet{
opt: opt opt: opt
} }
mut paths := cmdline.only_non_options(vet_options) mut paths := cmdline.only_non_options(main.vet_options)
vtmp := os.getenv('VTMP') vtmp := os.getenv('VTMP')
if vtmp != '' { if vtmp != '' {
// `v test-cleancode` passes also `-o tmpfolder` as well as all options in VFLAGS // `v test-cleancode` passes also `-o tmpfolder` as well as all options in VFLAGS
@ -57,7 +57,7 @@ fn main() {
} }
if path.ends_with('.v') || path.ends_with('.vv') { if path.ends_with('.v') || path.ends_with('.vv') {
if path.contains('cmd/tools/vvet/tests/') { if path.contains('cmd/tools/vvet/tests/') {
if is_force || paths.len == 1 { if main.is_force || paths.len == 1 {
vet.vet_file(path, true) vet.vet_file(path, true)
continue continue
} else { } else {
@ -79,7 +79,7 @@ fn main() {
files << vfiles files << vfiles
files << vvfiles files << vvfiles
for file in files { for file in files {
if !is_force && file.ends_with('.vv') && file.contains('cmd/tools/vvet/tests/') { if !main.is_force && file.ends_with('.vv') && file.contains('cmd/tools/vvet/tests/') {
continue continue
} }
vet.vet_file(file, false) vet.vet_file(file, false)
@ -90,7 +90,7 @@ fn main() {
warnings := vet.errors.filter(it.kind == .warning) warnings := vet.errors.filter(it.kind == .warning)
errors := vet.errors.filter(it.kind == .error) errors := vet.errors.filter(it.kind == .error)
errors_vfmt := vet.errors.filter(it.kind == .error && it.fix == .vfmt) errors_vfmt := vet.errors.filter(it.kind == .error && it.fix == .vfmt)
if show_warnings { if main.show_warnings {
for err in warnings { for err in warnings {
eprintln('$err.file_path:$err.pos.line_nr: warning: $err.message') eprintln('$err.file_path:$err.pos.line_nr: warning: $err.message')
} }

View File

@ -94,7 +94,7 @@ fn main() {
} }
// Start calling the correct functions/external tools // Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below. // Note for future contributors: Please add new subcommands in the `match` block below.
if command in simple_cmd { if command in main.simple_cmd {
// External tools // External tools
util.launch_tool(prefs.is_verbose, 'v' + command, os.args[1..]) util.launch_tool(prefs.is_verbose, 'v' + command, os.args[1..])
return return

View File

@ -1673,7 +1673,7 @@ struct Node<T> {
## Constants ## Constants
```v ```v oksyntax
const ( const (
pi = 3.14 pi = 3.14
world = '世界' world = '世界'

View File

@ -122,7 +122,7 @@ pub fn start() Benchmark {
pub fn (mut b Benchmark) measure(label string) i64 { pub fn (mut b Benchmark) measure(label string) i64 {
b.ok() b.ok()
res := b.step_timer.elapsed().microseconds() res := b.step_timer.elapsed().microseconds()
println(b.step_message_with_label(b_spent, 'in $label')) println(b.step_message_with_label(benchmark.b_spent, 'in $label'))
b.step() b.step()
return res return res
} }
@ -174,17 +174,17 @@ pub fn (b &Benchmark) step_message(msg string) string {
// step_message_ok returns a string describing the current step with an standard "OK" label. // step_message_ok returns a string describing the current step with an standard "OK" label.
pub fn (b &Benchmark) step_message_ok(msg string) string { pub fn (b &Benchmark) step_message_ok(msg string) string {
return b.step_message_with_label(b_ok, msg) return b.step_message_with_label(benchmark.b_ok, msg)
} }
// step_message_fail returns a string describing the current step with an standard "FAIL" label. // step_message_fail returns a string describing the current step with an standard "FAIL" label.
pub fn (b &Benchmark) step_message_fail(msg string) string { pub fn (b &Benchmark) step_message_fail(msg string) string {
return b.step_message_with_label(b_fail, msg) return b.step_message_with_label(benchmark.b_fail, msg)
} }
// step_message_skip returns a string describing the current step with an standard "SKIP" label. // step_message_skip returns a string describing the current step with an standard "SKIP" label.
pub fn (b &Benchmark) step_message_skip(msg string) string { pub fn (b &Benchmark) step_message_skip(msg string) string {
return b.step_message_with_label(b_skip, msg) return b.step_message_with_label(benchmark.b_skip, msg)
} }
// total_message returns a string with total summary of the benchmark run. // total_message returns a string with total summary of the benchmark run.

View File

@ -118,7 +118,7 @@ pub fn (instance BitField) get_bit(bitnr int) int {
if bitnr >= instance.size { if bitnr >= instance.size {
return 0 return 0
} }
return int((instance.field[bitslot(bitnr)] >> (bitnr % slot_size)) & u32(1)) return int((instance.field[bitslot(bitnr)] >> (bitnr % bitfield.slot_size)) & u32(1))
} }
// set_bit sets bit number 'bit_nr' to 1 (count from 0). // set_bit sets bit number 'bit_nr' to 1 (count from 0).
@ -224,8 +224,8 @@ pub fn join(input1 BitField, input2 BitField) BitField {
output.field[i] = input1.field[i] output.field[i] = input1.field[i]
} }
// find offset bit and offset slot // find offset bit and offset slot
offset_bit := input1.size % slot_size offset_bit := input1.size % bitfield.slot_size
offset_slot := input1.size / slot_size offset_slot := input1.size / bitfield.slot_size
for i in 0 .. zbitnslots(input2.size) { for i in 0 .. zbitnslots(input2.size) {
output.field[i + offset_slot] |= u32(input2.field[i] << u32(offset_bit)) output.field[i + offset_slot] |= u32(input2.field[i] << u32(offset_bit))
} }
@ -242,13 +242,13 @@ pub fn join(input1 BitField, input2 BitField) BitField {
* input. * input.
* If offset_bit is zero, no additional copies needed. * If offset_bit is zero, no additional copies needed.
*/ */
if (output_size - 1) % slot_size < (input2.size - 1) % slot_size { if (output_size - 1) % bitfield.slot_size < (input2.size - 1) % bitfield.slot_size {
for i in 0 .. zbitnslots(input2.size) { for i in 0 .. zbitnslots(input2.size) {
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(slot_size - offset_bit)) output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(bitfield.slot_size - offset_bit))
} }
} else if (output_size - 1) % slot_size > (input2.size - 1) % slot_size { } else if (output_size - 1) % bitfield.slot_size > (input2.size - 1) % bitfield.slot_size {
for i in 0 .. zbitnslots(input2.size) - 1 { for i in 0 .. zbitnslots(input2.size) - 1 {
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(slot_size - offset_bit)) output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(bitfield.slot_size - offset_bit))
} }
} }
return output return output
@ -287,10 +287,10 @@ pub fn (instance BitField) cmp(input BitField) bool {
pub fn (instance BitField) pop_count() int { pub fn (instance BitField) pop_count() int {
size := instance.size size := instance.size
bitnslots := zbitnslots(size) bitnslots := zbitnslots(size)
tail := size % slot_size tail := size % bitfield.slot_size
mut count := 0 mut count := 0
for i in 0 .. bitnslots - 1 { for i in 0 .. bitnslots - 1 {
for j in 0 .. slot_size { for j in 0 .. bitfield.slot_size {
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) { if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
count++ count++
} }
@ -344,17 +344,17 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
start = end // or panic? start = end // or panic?
} }
mut output := new(end - start) mut output := new(end - start)
start_offset := start % slot_size start_offset := start % bitfield.slot_size
end_offset := (end - 1) % slot_size end_offset := (end - 1) % bitfield.slot_size
start_slot := start / slot_size start_slot := start / bitfield.slot_size
end_slot := (end - 1) / slot_size end_slot := (end - 1) / bitfield.slot_size
output_slots := zbitnslots(end - start) output_slots := zbitnslots(end - start)
if output_slots > 1 { if output_slots > 1 {
if start_offset != 0 { if start_offset != 0 {
for i in 0 .. output_slots - 1 { for i in 0 .. output_slots - 1 {
output.field[i] = u32(input.field[start_slot + i] >> u32(start_offset)) output.field[i] = u32(input.field[start_slot + i] >> u32(start_offset))
output.field[i] = output.field[i] | u32(input.field[start_slot + i + output.field[i] = output.field[i] | u32(input.field[start_slot + i +
1] << u32(slot_size - start_offset)) 1] << u32(bitfield.slot_size - start_offset))
} }
} else { } else {
for i in 0 .. output_slots - 1 { for i in 0 .. output_slots - 1 {
@ -363,25 +363,25 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
} }
} }
if start_offset > end_offset { if start_offset > end_offset {
output.field[(end - start - 1) / slot_size] = u32(input.field[end_slot - 1] >> u32(start_offset)) output.field[(end - start - 1) / bitfield.slot_size] = u32(input.field[end_slot - 1] >> u32(start_offset))
mut mask := u32((1 << (end_offset + 1)) - 1) mut mask := u32((1 << (end_offset + 1)) - 1)
mask = input.field[end_slot] & mask mask = input.field[end_slot] & mask
mask = u32(mask << u32(slot_size - start_offset)) mask = u32(mask << u32(bitfield.slot_size - start_offset))
output.field[(end - start - 1) / slot_size] |= mask output.field[(end - start - 1) / bitfield.slot_size] |= mask
} else if start_offset == 0 { } else if start_offset == 0 {
mut mask := u32(0) mut mask := u32(0)
if end_offset == slot_size - 1 { if end_offset == bitfield.slot_size - 1 {
mask = u32(-1) mask = u32(-1)
} else { } else {
mask = u32(u32(1) << u32(end_offset + 1)) mask = u32(u32(1) << u32(end_offset + 1))
mask = mask - u32(1) mask = mask - u32(1)
} }
output.field[(end - start - 1) / slot_size] = (input.field[end_slot] & mask) output.field[(end - start - 1) / bitfield.slot_size] = (input.field[end_slot] & mask)
} else { } else {
mut mask := u32(((1 << (end_offset - start_offset + 1)) - 1) << start_offset) mut mask := u32(((1 << (end_offset - start_offset + 1)) - 1) << start_offset)
mask = input.field[end_slot] & mask mask = input.field[end_slot] & mask
mask = u32(mask >> u32(start_offset)) mask = u32(mask >> u32(start_offset))
output.field[(end - start - 1) / slot_size] |= mask output.field[(end - start - 1) / bitfield.slot_size] |= mask
} }
return output return output
} }
@ -393,13 +393,13 @@ pub fn (instance BitField) reverse() BitField {
bitnslots := zbitnslots(size) bitnslots := zbitnslots(size)
mut output := new(size) mut output := new(size)
for i := 0; i < (bitnslots - 1); i++ { for i := 0; i < (bitnslots - 1); i++ {
for j in 0 .. slot_size { for j in 0 .. bitfield.slot_size {
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) { if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
output.set_bit(size - i * slot_size - j - 1) output.set_bit(size - i * bitfield.slot_size - j - 1)
} }
} }
} }
bits_in_last_input_slot := (size - 1) % slot_size + 1 bits_in_last_input_slot := (size - 1) % bitfield.slot_size + 1
for j in 0 .. bits_in_last_input_slot { for j in 0 .. bits_in_last_input_slot {
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) { if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
output.set_bit(bits_in_last_input_slot - j - 1) output.set_bit(bits_in_last_input_slot - j - 1)
@ -419,7 +419,7 @@ pub fn (mut instance BitField) resize(new_size int) {
} }
instance.field = field.clone() instance.field = field.clone()
instance.size = new_size instance.size = new_size
if new_size < old_size && new_size % slot_size != 0 { if new_size < old_size && new_size % bitfield.slot_size != 0 {
instance.clear_tail() instance.clear_tail()
} }
} }
@ -453,7 +453,7 @@ pub fn (instance BitField) rotate(offset int) BitField {
// Internal functions // Internal functions
// clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated // clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
fn (mut instance BitField) clear_tail() { fn (mut instance BitField) clear_tail() {
tail := instance.size % slot_size tail := instance.size % bitfield.slot_size
if tail != 0 { if tail != 0 {
// create a mask for the tail // create a mask for the tail
mask := u32((1 << tail) - 1) mask := u32((1 << tail) - 1)
@ -464,12 +464,12 @@ fn (mut instance BitField) clear_tail() {
// bitmask is the bitmask needed to access a particular bit at offset bitnr // bitmask is the bitmask needed to access a particular bit at offset bitnr
fn bitmask(bitnr int) u32 { fn bitmask(bitnr int) u32 {
return u32(u32(1) << u32(bitnr % slot_size)) return u32(u32(1) << u32(bitnr % bitfield.slot_size))
} }
// bitslot is the slot index (i.e. the integer) where a particular bit is located // bitslot is the slot index (i.e. the integer) where a particular bit is located
fn bitslot(size int) int { fn bitslot(size int) int {
return size / slot_size return size / bitfield.slot_size
} }
// min returns the minimum of 2 integers; it is here to avoid importing math just for that // min returns the minimum of 2 integers; it is here to avoid importing math just for that
@ -483,5 +483,5 @@ fn min(input1 int, input2 int) int {
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length // zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
fn zbitnslots(length int) int { fn zbitnslots(length int) int {
return (length - 1) / slot_size + 1 return (length - 1) / bitfield.slot_size + 1
} }

View File

@ -307,7 +307,7 @@ fn test_fixed() {
nums[1] = 7 nums[1] = 7
assert nums[1] == 7 assert nums[1] == 7
nums2 := [5]int{} // c_n nums2 := [5]int{} // c_n
assert nums2[c_n - 1] == 0 assert nums2[main.c_n - 1] == 0
} }
fn modify(mut numbers []int) { fn modify(mut numbers []int) {
@ -1101,12 +1101,12 @@ const (
) )
fn test_multidimensional_array_initialization_with_consts() { fn test_multidimensional_array_initialization_with_consts() {
mut data := [][][]int{len: grid_size_1, init: [][]int{len: grid_size_2, init: []int{len: grid_size_3, init: cell_value}}} mut data := [][][]int{len: main.grid_size_1, init: [][]int{len: main.grid_size_2, init: []int{len: main.grid_size_3, init: main.cell_value}}}
assert data.len == grid_size_1 assert data.len == main.grid_size_1
assert data[0].len == grid_size_2 assert data[0].len == main.grid_size_2
assert data[0][0].len == grid_size_3 assert data[0][0].len == main.grid_size_3
assert data[0][0][0] == cell_value assert data[0][0][0] == main.cell_value
assert data[1][1][1] == cell_value assert data[1][1][1] == main.cell_value
} }
fn test_byteptr_vbytes() { fn test_byteptr_vbytes() {

View File

@ -63,7 +63,7 @@ pub fn trailing_zeros_16(x u16) int {
return 16 return 16
} }
// see comment in trailing_zeros_64 // see comment in trailing_zeros_64
return int(de_bruijn32tab[u32(x & -x) * de_bruijn32 >> (32 - 5)]) return int(bits.de_bruijn32tab[u32(x & -x) * bits.de_bruijn32 >> (32 - 5)])
} }
// trailing_zeros_32 returns the number of trailing zero bits in x; the result is 32 for x == 0. // trailing_zeros_32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
@ -72,7 +72,7 @@ pub fn trailing_zeros_32(x u32) int {
return 32 return 32
} }
// see comment in trailing_zeros_64 // see comment in trailing_zeros_64
return int(de_bruijn32tab[(x & -x) * de_bruijn32 >> (32 - 5)]) return int(bits.de_bruijn32tab[(x & -x) * bits.de_bruijn32 >> (32 - 5)])
} }
// trailing_zeros_64 returns the number of trailing zero bits in x; the result is 64 for x == 0. // trailing_zeros_64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
@ -91,7 +91,7 @@ pub fn trailing_zeros_64(x u64) int {
// find by how many bits it was shifted by looking at which six bit // find by how many bits it was shifted by looking at which six bit
// substring ended up at the top of the word. // substring ended up at the top of the word.
// (Knuth, volume 4, section 7.3.1) // (Knuth, volume 4, section 7.3.1)
return int(de_bruijn64tab[(x & -x) * de_bruijn64 >> (64 - 6)]) return int(bits.de_bruijn64tab[(x & -x) * bits.de_bruijn64 >> (64 - 6)])
} }
// --- OnesCount --- // --- OnesCount ---
@ -132,9 +132,9 @@ pub fn ones_count_64(x u64) int {
// Per "Hacker's Delight", the first line can be simplified // Per "Hacker's Delight", the first line can be simplified
// more, but it saves at best one instruction, so we leave // more, but it saves at best one instruction, so we leave
// it alone for clarity. // it alone for clarity.
mut y := (x >> u64(1) & (m0 & max_u64)) + (x & (m0 & max_u64)) mut y := (x >> u64(1) & (bits.m0 & bits.max_u64)) + (x & (bits.m0 & bits.max_u64))
y = (y >> u64(2) & (m1 & max_u64)) + (y & (m1 & max_u64)) y = (y >> u64(2) & (bits.m1 & bits.max_u64)) + (y & (bits.m1 & bits.max_u64))
y = ((y >> 4) + y) & (m2 & max_u64) y = ((y >> 4) + y) & (bits.m2 & bits.max_u64)
y += y >> 8 y += y >> 8
y += y >> 16 y += y >> 16
y += y >> 32 y += y >> 32
@ -202,18 +202,18 @@ pub fn reverse_16(x u16) u16 {
// reverse_32 returns the value of x with its bits in reversed order. // reverse_32 returns the value of x with its bits in reversed order.
[inline] [inline]
pub fn reverse_32(x u32) u32 { pub fn reverse_32(x u32) u32 {
mut y := ((x >> u32(1) & (m0 & max_u32)) | ((x & (m0 & max_u32)) << 1)) mut y := ((x >> u32(1) & (bits.m0 & bits.max_u32)) | ((x & (bits.m0 & bits.max_u32)) << 1))
y = ((y >> u32(2) & (m1 & max_u32)) | ((y & (m1 & max_u32)) << u32(2))) y = ((y >> u32(2) & (bits.m1 & bits.max_u32)) | ((y & (bits.m1 & bits.max_u32)) << u32(2)))
y = ((y >> u32(4) & (m2 & max_u32)) | ((y & (m2 & max_u32)) << u32(4))) y = ((y >> u32(4) & (bits.m2 & bits.max_u32)) | ((y & (bits.m2 & bits.max_u32)) << u32(4)))
return reverse_bytes_32(u32(y)) return reverse_bytes_32(u32(y))
} }
// reverse_64 returns the value of x with its bits in reversed order. // reverse_64 returns the value of x with its bits in reversed order.
[inline] [inline]
pub fn reverse_64(x u64) u64 { pub fn reverse_64(x u64) u64 {
mut y := ((x >> u64(1) & (m0 & max_u64)) | ((x & (m0 & max_u64)) << 1)) mut y := ((x >> u64(1) & (bits.m0 & bits.max_u64)) | ((x & (bits.m0 & bits.max_u64)) << 1))
y = ((y >> u64(2) & (m1 & max_u64)) | ((y & (m1 & max_u64)) << 2)) y = ((y >> u64(2) & (bits.m1 & bits.max_u64)) | ((y & (bits.m1 & bits.max_u64)) << 2))
y = ((y >> u64(4) & (m2 & max_u64)) | ((y & (m2 & max_u64)) << 4)) y = ((y >> u64(4) & (bits.m2 & bits.max_u64)) | ((y & (bits.m2 & bits.max_u64)) << 4))
return reverse_bytes_64(y) return reverse_bytes_64(y)
} }
@ -231,7 +231,7 @@ pub fn reverse_bytes_16(x u16) u16 {
// This function's execution time does not depend on the inputs. // This function's execution time does not depend on the inputs.
[inline] [inline]
pub fn reverse_bytes_32(x u32) u32 { pub fn reverse_bytes_32(x u32) u32 {
y := ((x >> u32(8) & (m3 & max_u32)) | ((x & (m3 & max_u32)) << u32(8))) y := ((x >> u32(8) & (bits.m3 & bits.max_u32)) | ((x & (bits.m3 & bits.max_u32)) << u32(8)))
return u32((y >> 16) | (y << 16)) return u32((y >> 16) | (y << 16))
} }
@ -240,8 +240,8 @@ pub fn reverse_bytes_32(x u32) u32 {
// This function's execution time does not depend on the inputs. // This function's execution time does not depend on the inputs.
[inline] [inline]
pub fn reverse_bytes_64(x u64) u64 { pub fn reverse_bytes_64(x u64) u64 {
mut y := ((x >> u64(8) & (m3 & max_u64)) | ((x & (m3 & max_u64)) << u64(8))) mut y := ((x >> u64(8) & (bits.m3 & bits.max_u64)) | ((x & (bits.m3 & bits.max_u64)) << u64(8)))
y = ((y >> u64(16) & (m4 & max_u64)) | ((y & (m4 & max_u64)) << u64(16))) y = ((y >> u64(16) & (bits.m4 & bits.max_u64)) | ((y & (bits.m4 & bits.max_u64)) << u64(16)))
return (y >> 32) | (y << 32) return (y >> 32) | (y << 32)
} }
@ -385,13 +385,13 @@ pub fn mul_32(x u32, y u32) (u32, u32) {
// //
// This function's execution time does not depend on the inputs. // This function's execution time does not depend on the inputs.
pub fn mul_64(x u64, y u64) (u64, u64) { pub fn mul_64(x u64, y u64) (u64, u64) {
x0 := x & mask32 x0 := x & bits.mask32
x1 := x >> 32 x1 := x >> 32
y0 := y & mask32 y0 := y & bits.mask32
y1 := y >> 32 y1 := y >> 32
w0 := x0 * y0 w0 := x0 * y0
t := x1 * y0 + (w0 >> 32) t := x1 * y0 + (w0 >> 32)
mut w1 := t & mask32 mut w1 := t & bits.mask32
w2 := t >> 32 w2 := t >> 32
w1 += x0 * y1 w1 += x0 * y1
hi := x1 * y1 + w2 + (w1 >> 32) hi := x1 * y1 + w2 + (w1 >> 32)
@ -406,7 +406,7 @@ pub fn mul_64(x u64, y u64) (u64, u64) {
// div_32 panics for y == 0 (division by zero) or y <= hi (quotient overflow). // div_32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
pub fn div_32(hi u32, lo u32, y u32) (u32, u32) { pub fn div_32(hi u32, lo u32, y u32) (u32, u32) {
if y != 0 && y <= hi { if y != 0 && y <= hi {
panic(overflow_error) panic(bits.overflow_error)
} }
z := (u64(hi) << 32) | u64(lo) z := (u64(hi) << 32) | u64(lo)
quo := u32(z / u64(y)) quo := u32(z / u64(y))
@ -421,39 +421,39 @@ pub fn div_32(hi u32, lo u32, y u32) (u32, u32) {
pub fn div_64(hi u64, lo u64, y1 u64) (u64, u64) { pub fn div_64(hi u64, lo u64, y1 u64) (u64, u64) {
mut y := y1 mut y := y1
if y == 0 { if y == 0 {
panic(overflow_error) panic(bits.overflow_error)
} }
if y <= hi { if y <= hi {
panic(overflow_error) panic(bits.overflow_error)
} }
s := u32(leading_zeros_64(y)) s := u32(leading_zeros_64(y))
y <<= s y <<= s
yn1 := y >> 32 yn1 := y >> 32
yn0 := y & mask32 yn0 := y & bits.mask32
un32 := (hi << s) | (lo >> (64 - s)) un32 := (hi << s) | (lo >> (64 - s))
un10 := lo << s un10 := lo << s
un1 := un10 >> 32 un1 := un10 >> 32
un0 := un10 & mask32 un0 := un10 & bits.mask32
mut q1 := un32 / yn1 mut q1 := un32 / yn1
mut rhat := un32 - q1 * yn1 mut rhat := un32 - q1 * yn1
for q1 >= two32 || q1 * yn0 > two32 * rhat + un1 { for q1 >= bits.two32 || q1 * yn0 > bits.two32 * rhat + un1 {
q1-- q1--
rhat += yn1 rhat += yn1
if rhat >= two32 { if rhat >= bits.two32 {
break break
} }
} }
un21 := un32 * two32 + un1 - q1 * y un21 := un32 * bits.two32 + un1 - q1 * y
mut q0 := un21 / yn1 mut q0 := un21 / yn1
rhat = un21 - q0 * yn1 rhat = un21 - q0 * yn1
for q0 >= two32 || q0 * yn0 > two32 * rhat + un0 { for q0 >= bits.two32 || q0 * yn0 > bits.two32 * rhat + un0 {
q0-- q0--
rhat += yn1 rhat += yn1
if rhat >= two32 { if rhat >= bits.two32 {
break break
} }
} }
return q1 * two32 + q0, (un21 * two32 + un0 - q0 * y) >> s return q1 * bits.two32 + q0, (un21 * bits.two32 + un0 - q0 * y) >> s
} }
// rem_32 returns the remainder of (hi, lo) divided by y. Rem32 panics // rem_32 returns the remainder of (hi, lo) divided by y. Rem32 panics

View File

@ -48,19 +48,19 @@ const (
fn test_big_sb() { fn test_big_sb() {
mut sb := strings.new_builder(100) mut sb := strings.new_builder(100)
mut sb2 := strings.new_builder(10000) mut sb2 := strings.new_builder(10000)
for i in 0 .. maxn { for i in 0 .. main.maxn {
sb.writeln(i.str()) sb.writeln(i.str())
sb2.write('+') sb2.write('+')
} }
s := sb.str() s := sb.str()
lines := s.split_into_lines() lines := s.split_into_lines()
assert lines.len == maxn assert lines.len == main.maxn
assert lines[0] == '0' assert lines[0] == '0'
assert lines[1] == '1' assert lines[1] == '1'
assert lines[777] == '777' assert lines[777] == '777'
assert lines[98765] == '98765' assert lines[98765] == '98765'
println(sb2.len) println(sb2.len)
assert sb2.len == maxn assert sb2.len == main.maxn
} }
fn test_byte_write() { fn test_byte_write() {

View File

@ -45,6 +45,7 @@ pub mut:
single_line_fields bool // should struct fields be on a single line single_line_fields bool // should struct fields be on a single line
it_name string // the name to replace `it` with it_name string // the name to replace `it` with
inside_lambda bool inside_lambda bool
inside_const bool
is_mbranch_expr bool // math a { x...y { } } is_mbranch_expr bool // math a { x...y { } }
} }
@ -1149,7 +1150,7 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
// This allows using the variable `minute` inside time's functions // This allows using the variable `minute` inside time's functions
// and also makes it clear that a module const is being used // and also makes it clear that a module const is being used
// (since V's conts are no longer ALL_CAP). // (since V's conts are no longer ALL_CAP).
if !node.name.contains('.') { if !node.name.contains('.') && !f.inside_const {
full_name := f.cur_mod + '.' + node.name full_name := f.cur_mod + '.' + node.name
if obj := f.file.global_scope.find(full_name) { if obj := f.file.global_scope.find(full_name) {
if obj is ast.ConstField { if obj is ast.ConstField {
@ -2068,6 +2069,10 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
f.writeln('const ()\n') f.writeln('const ()\n')
return return
} }
f.inside_const = true
defer {
f.inside_const = false
}
f.write('const ') f.write('const ')
if it.is_block { if it.is_block {
f.writeln('(') f.writeln('(')

View File

@ -778,8 +778,8 @@ pub fn (mut g Gen) write(s string) {
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: $s') eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: $s')
} }
if g.indent > 0 && g.empty_line { if g.indent > 0 && g.empty_line {
if g.indent < tabs.len { if g.indent < gen.tabs.len {
g.out.write(tabs[g.indent]) g.out.write(gen.tabs[g.indent])
} else { } else {
for _ in 0 .. g.indent { for _ in 0 .. g.indent {
g.out.write('\t') g.out.write('\t')
@ -795,8 +795,8 @@ pub fn (mut g Gen) writeln(s string) {
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: $s') eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: $s')
} }
if g.indent > 0 && g.empty_line { if g.indent > 0 && g.empty_line {
if g.indent < tabs.len { if g.indent < gen.tabs.len {
g.out.write(tabs[g.indent]) g.out.write(gen.tabs[g.indent])
} else { } else {
for _ in 0 .. g.indent { for _ in 0 .. g.indent {
g.out.write('\t') g.out.write('\t')
@ -2058,7 +2058,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
if right_sym.kind == .function && is_decl { if right_sym.kind == .function && is_decl {
if is_inside_ternary && is_decl { if is_inside_ternary && is_decl {
g.out.write(tabs[g.indent - g.inside_ternary]) g.out.write(gen.tabs[g.indent - g.inside_ternary])
} }
func := right_sym.info as table.FnType func := right_sym.info as table.FnType
ret_styp := g.typ(func.func.return_type) ret_styp := g.typ(func.func.return_type)
@ -2070,7 +2070,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} else { } else {
if is_decl { if is_decl {
if is_inside_ternary { if is_inside_ternary {
g.out.write(tabs[g.indent - g.inside_ternary]) g.out.write(gen.tabs[g.indent - g.inside_ternary])
} }
g.write('$styp ') g.write('$styp ')
} }
@ -2087,7 +2087,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
if is_inside_ternary && is_decl { if is_inside_ternary && is_decl {
g.write(';\n$cur_line') g.write(';\n$cur_line')
g.out.write(tabs[g.indent]) g.out.write(gen.tabs[g.indent])
g.expr(left) g.expr(left)
} }
g.is_assign_lhs = false g.is_assign_lhs = false
@ -2797,7 +2797,7 @@ fn (mut g Gen) expr(node ast.Expr) {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs { cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0) line := g.go_before_stmt(0)
g.out.write(tabs[g.indent]) g.out.write(gen.tabs[g.indent])
line line
} else { } else {
'' ''
@ -3363,7 +3363,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else { } else {
64 64
} }
g.write('_us${bitsize}_${cmp_str[int(node.op) - int(token.Kind.eq)]}(') g.write('_us${bitsize}_${gen.cmp_str[int(node.op) - int(token.Kind.eq)]}(')
g.expr(node.left) g.expr(node.left)
g.write(',') g.write(',')
g.expr(node.right) g.expr(node.right)
@ -3376,7 +3376,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else { } else {
64 64
} }
g.write('_us${bitsize}_${cmp_rev[int(node.op) - int(token.Kind.eq)]}(') g.write('_us${bitsize}_${gen.cmp_rev[int(node.op) - int(token.Kind.eq)]}(')
g.expr(node.right) g.expr(node.right)
g.write(',') g.write(',')
g.expr(node.left) g.expr(node.left)
@ -4138,7 +4138,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs { cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0) line := g.go_before_stmt(0)
g.out.write(tabs[g.indent]) g.out.write(gen.tabs[g.indent])
line line
} else { } else {
'' ''
@ -4270,7 +4270,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs { cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0) line := g.go_before_stmt(0)
g.out.write(tabs[g.indent]) g.out.write(gen.tabs[g.indent])
line line
} else { } else {
'' ''
@ -4697,7 +4697,7 @@ const (
fn (mut g Gen) struct_init(struct_init ast.StructInit) { fn (mut g Gen) struct_init(struct_init ast.StructInit) {
styp := g.typ(struct_init.typ) styp := g.typ(struct_init.typ)
mut shared_styp := '' // only needed for shared &St{... mut shared_styp := '' // only needed for shared &St{...
if styp in skip_struct_init { if styp in gen.skip_struct_init {
// needed for c++ compilers // needed for c++ compilers
g.go_back_out(3) g.go_back_out(3)
return return
@ -5025,7 +5025,7 @@ fn (mut g Gen) write_builtin_types() {
mut builtin_types := []table.TypeSymbol{} // builtin types mut builtin_types := []table.TypeSymbol{} // builtin types
// builtin types need to be on top // builtin types need to be on top
// everything except builtin will get sorted // everything except builtin will get sorted
for builtin_name in builtins { for builtin_name in gen.builtins {
builtin_types << g.table.types[g.table.type_idxs[builtin_name]] builtin_types << g.table.types[g.table.type_idxs[builtin_name]]
} }
g.write_types(builtin_types) g.write_types(builtin_types)
@ -5037,7 +5037,7 @@ fn (mut g Gen) write_builtin_types() {
fn (mut g Gen) write_sorted_types() { fn (mut g Gen) write_sorted_types() {
mut types := []table.TypeSymbol{} // structs that need to be sorted mut types := []table.TypeSymbol{} // structs that need to be sorted
for typ in g.table.types { for typ in g.table.types {
if typ.name !in builtins { if typ.name !in gen.builtins {
types << typ types << typ
} }
} }
@ -5509,7 +5509,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string
[inline] [inline]
fn c_name(name_ string) string { fn c_name(name_ string) string {
name := util.no_dots(name_) name := util.no_dots(name_)
if name in c_reserved { if name in gen.c_reserved {
return 'v_$name' return 'v_$name'
} }
return name return name