parser: enable module auto import (of `sync`) (#6271)

pull/6277/head
Uwe Krüger 2020-08-31 10:44:39 +02:00 committed by GitHub
parent b1a8e1e5b2
commit cbcba2e4cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 88 additions and 93 deletions

View File

@ -6,24 +6,21 @@
// The receive threads add all received numbers and send them to the
// main thread where the total sum is compare to the expected value.
import sync
import time
import os
fn do_rec(mut ch sync.Channel, mut resch sync.Channel, n int) {
fn do_rec(ch chan int, resch chan i64, n int) {
mut sum := i64(0)
for _ in 0 .. n {
mut a := 0
ch.pop(&a)
sum += a
sum += <-ch
}
println(sum)
resch.push(&sum)
resch <- sum
}
fn do_send(mut ch sync.Channel, start, end int) {
fn do_send(ch chan int, start, end int) {
for i in start .. end {
ch.push(&i)
ch <- i
}
}
@ -37,12 +34,12 @@ fn main() {
buflen := os.args[3].int()
nobj := os.args[4].int()
stopwatch := time.new_stopwatch({})
mut ch := sync.new_channel<int>(buflen)
mut resch := sync.new_channel<i64>(0)
ch := chan int{cap: buflen}
resch := chan i64{}
mut no := nobj
for i in 0 .. nrec {
n := no / (nrec - i)
go do_rec(mut ch, mut resch, n)
go do_rec(ch, resch, n)
no -= n
}
assert no == 0
@ -51,14 +48,12 @@ fn main() {
n := no / (nsend - i)
end := no
no -= n
go do_send(mut ch, no, end)
go do_send(ch, no, end)
}
assert no == 0
mut sum := i64(0)
for _ in 0 .. nrec {
mut r := i64(0)
resch.pop(&r)
sum += r
sum += <-resch
}
elapsed := stopwatch.elapsed()
rate := f64(nobj)/elapsed*time.microsecond

View File

@ -1,23 +1,19 @@
import sync
const (
num_iterations = 10000
)
fn do_send(mut ch sync.Channel) {
fn do_send(ch chan int) {
for i in 0 .. num_iterations {
ch.push(&i)
ch <- i
}
}
fn test_channel_buffered() {
mut ch := sync.new_channel<int>(1000)
go do_send(mut ch)
ch := chan int{cap: 1000}
go do_send(ch)
mut sum := i64(0)
for _ in 0 .. num_iterations {
a := 0
ch.pop(&a)
sum += a
sum += <-ch
}
assert sum == u64(num_iterations)*(num_iterations-1)/2
}

View File

@ -1,23 +1,19 @@
import sync
const (
num_iterations = 10000
)
fn do_send(mut ch sync.Channel) {
fn do_send(ch chan int) {
for i in 0 .. num_iterations {
ch.push(&i)
ch <- i
}
}
fn test_channel_unbuffered() {
mut ch := sync.new_channel<int>(0)
go do_send(mut ch)
ch := chan int{}
go do_send(ch)
mut sum := i64(0)
for _ in 0 .. num_iterations {
a := 0
ch.pop(&a)
sum += a
sum += <-ch
}
assert sum == u64(num_iterations)*(num_iterations-1)/2
}

View File

@ -1,38 +1,32 @@
import sync
fn do_rec(mut ch sync.Channel, mut resch sync.Channel) {
fn do_rec(ch chan int, resch chan i64) {
mut sum := i64(0)
for _ in 0 .. 2000 {
mut a := 0
ch.pop(&a)
sum += a
sum += <-ch
}
println(sum)
resch.push(&sum)
resch <- sum
}
fn do_send(mut ch sync.Channel) {
fn do_send(ch chan int) {
for i in 0 .. 2000 {
ch.push(&i)
ch <- i
}
}
fn test_channel_multi_unbuffered() {
mut ch := sync.new_channel<int>(0)
mut resch := sync.new_channel<i64>(0)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
ch := chan int{}
resch := chan i64{}
go do_rec(ch, resch)
go do_rec(ch, resch)
go do_rec(ch, resch)
go do_rec(ch, resch)
go do_send(ch)
go do_send(ch)
go do_send(ch)
go do_send(ch)
mut sum := i64(0)
for _ in 0 .. 4 {
mut r := i64(0)
resch.pop(&r)
sum += r
sum += <-resch
}
assert sum == i64(4) * 2000 * (2000 - 1) / 2
}

View File

@ -1,38 +1,32 @@
import sync
fn do_rec(mut ch sync.Channel, mut resch sync.Channel) {
fn do_rec(ch chan int, resch chan i64) {
mut sum := i64(0)
for _ in 0 .. 2000 {
mut a := 0
ch.pop(&a)
sum += a
sum += <-ch
}
println(sum)
resch.push(&sum)
resch <- sum
}
fn do_send(mut ch sync.Channel) {
fn do_send(ch chan int) {
for i in 0 .. 2000 {
ch.push(&i)
ch <- i
}
}
fn test_channel_multi_buffered() {
mut ch := sync.new_channel<int>(100)
mut resch := sync.new_channel<i64>(0)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_rec(mut ch, mut resch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
go do_send(mut ch)
ch := chan int{cap: 100}
resch := chan i64{}
go do_rec(ch, resch)
go do_rec(ch, resch)
go do_rec(ch, resch)
go do_rec(ch, resch)
go do_send(ch)
go do_send(ch)
go do_send(ch)
go do_send(ch)
mut sum := i64(0)
for _ in 0 .. 4 {
mut r := i64(0)
resch.pop(&r)
sum += r
sum += <-resch
}
assert sum == i64(4) * 2000 * (2000 - 1) / 2
}

View File

@ -197,7 +197,7 @@ Did you forget to add vlib to the path? (Use @vlib for default vlib)')
}
pub fn (v &Builder) get_user_files() []string {
if v.pref.path in ['vlib/builtin', 'vlib/strconv', 'vlib/strings', 'vlib/hash', 'vlib/time'] {
if v.pref.path in ['vlib/builtin', 'vlib/strconv', 'vlib/strings', 'vlib/hash'] {
// This means we are building a builtin module with `v build-module vlib/strings` etc
// get_builtin_files() has already added the files in this module,
// do nothing here to avoid duplicate definition errors.

View File

@ -5,7 +5,7 @@ import v.table
fn (mut p Parser) lock_expr() ast.LockExpr {
// TODO Handle aliasing sync
p.register_used_import('sync')
p.register_auto_import('sync')
pos := p.tok.position()
is_rlock := p.tok.kind == .key_rlock
p.next()

View File

@ -3,6 +3,8 @@
// that can be found in the LICENSE file.
module parser
import v.ast
// return true if file being parsed imports `mod`
pub fn (p &Parser) known_import(mod string) bool {
return mod in p.imports
@ -29,6 +31,20 @@ fn (mut p Parser) register_used_import(alias string) {
}
}
fn (mut p Parser) register_auto_import(alias string) {
if alias !in p.imports {
p.imports[alias] = alias
p.table.imports << alias
node := ast.Import{
pos: p.tok.position()
mod: alias
alias: alias
}
p.ast_imports << node
}
p.register_used_import(alias)
}
fn (mut p Parser) check_unused_imports() {
if p.pref.is_repl || p.pref.is_fmt {
// The REPL should be much more liberal, and should not warn about

View File

@ -52,10 +52,12 @@ pub fn (mut p Parser) parse_map_type() table.Type {
}
pub fn (mut p Parser) parse_chan_type() table.Type {
p.next()
if p.tok.kind != .name && p.tok.kind != .key_mut && p.tok.kind != .amp {
if p.peek_tok.kind != .name && p.peek_tok.kind != .key_mut && p.peek_tok.kind != .amp {
p.next()
return table.chan_type
}
p.register_auto_import('sync')
p.next()
elem_type := p.parse_type()
idx := p.table.find_or_register_chan(elem_type)
return table.new_type(idx)

View File

@ -282,6 +282,9 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
op := p.tok.kind
if op == .arrow {
p.register_auto_import('sync')
}
// mut typ := p.
// println('infix op=$op.str()')
precedence := p.tok.precedence()
@ -310,6 +313,9 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr {
if op == .amp {
p.is_amp = true
}
if op == .arrow {
p.register_auto_import('sync')
}
// if op == .mul && !p.inside_unsafe {
// p.warn('unsafe')
// }
@ -322,7 +328,8 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr {
mut or_stmts := []ast.Stmt{}
mut or_kind := ast.OrKind.absent
// allow `x := <-ch or {...}` to handle closed channel
if op == .arrow && p.tok.kind == .key_orelse {
if op == .arrow {
if p.tok.kind == .key_orelse {
p.next()
p.open_scope()
p.scope.register('errcode', ast.Var{
@ -340,10 +347,11 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr {
or_kind = .block
or_stmts = p.parse_block_no_scope(false)
p.close_scope()
}
if p.tok.kind == .question {
p.next()
or_kind = .propagate
}
if p.tok.kind == .question {
p.next()
or_kind = .propagate
}
}
return ast.PrefixExpr{
op: op

View File

@ -1,4 +1,3 @@
import sync
import time
const (

View File

@ -1,4 +1,3 @@
import sync
import time
fn incr(shared foo []int, index int) {

View File

@ -1,4 +1,3 @@
import sync
import time
struct St {

View File

@ -1,4 +1,3 @@
import sync
import time
struct St {

View File

@ -1,4 +1,3 @@
import sync
import time
struct St {

View File

@ -1,4 +1,3 @@
import sync
import time
struct St {

View File

@ -13,7 +13,7 @@ pub const (
// math.bits is needed by strconv.ftoa
pub const (
builtin_module_parts = ['math.bits', 'strconv', 'strconv.ftoa', 'hash', 'strings', 'time', 'builtin']
builtin_module_parts = ['math.bits', 'strconv', 'strconv.ftoa', 'hash', 'strings', 'builtin']
)
pub const (