cgen: GoStmt; experimental parallel parser

pull/4038/head
Alexander Medvednikov 2020-03-16 08:33:42 +01:00
parent c947e6ebe6
commit 0f160707a4
3 changed files with 72 additions and 16 deletions

View File

@ -49,8 +49,8 @@ mut:
items []voidptr items []voidptr
results []voidptr results []voidptr
ntask int // writing to this should be locked by ntask_mtx. ntask int // writing to this should be locked by ntask_mtx.
ntask_mtx &sync.Mutex ntask_mtx &Mutex
waitgroup &sync.WaitGroup waitgroup &WaitGroup
shared_context voidptr shared_context voidptr
thread_contexts []voidptr thread_contexts []voidptr
} }
@ -77,8 +77,8 @@ pub fn new_pool_processor(context PoolProcessorConfig) &PoolProcessor {
thread_contexts: [] thread_contexts: []
njobs: context.maxjobs njobs: context.maxjobs
ntask: 0 ntask: 0
ntask_mtx: sync.new_mutex() ntask_mtx: new_mutex()
waitgroup: sync.new_waitgroup() waitgroup: new_waitgroup()
thread_cb: context.callback thread_cb: context.callback
} }
return pool return pool
@ -149,15 +149,15 @@ pub fn (pool &PoolProcessor) get_item<T>(idx int) T {
return *(&T(pool.items[idx])) return *(&T(pool.items[idx]))
} }
// get_string_item - called by the worker callback. // get_string_item - called by the worker callback.
// It does not use generics so it does not mess up vfmt. // It does not use generics so it does not mess up vfmt.
// TODO: remove the need for this when vfmt becomes smarter. // TODO: remove the need for this when vfmt becomes smarter.
pub fn (pool &PoolProcessor) get_string_item(idx int) string { pub fn (pool &PoolProcessor) get_string_item(idx int) string {
return *(&string(pool.items[idx])) return *(&string(pool.items[idx]))
} }
// get_int_item - called by the worker callback. // get_int_item - called by the worker callback.
// It does not use generics so it does not mess up vfmt. // It does not use generics so it does not mess up vfmt.
// TODO: remove the need for this when vfmt becomes smarter. // TODO: remove the need for this when vfmt becomes smarter.
pub fn (pool &PoolProcessor) get_int_item(idx int) int { pub fn (pool &PoolProcessor) get_int_item(idx int) int {
return *(&int(pool.items[idx])) return *(&int(pool.items[idx]))

View File

@ -19,7 +19,7 @@ ConcatExpr | Type | AsCast
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
LineComment | MultiLineComment | AssertStmt | UnsafeStmt LineComment | MultiLineComment | AssertStmt | UnsafeStmt | GoStmt
// pub type Type = StructType | ArrayType // pub type Type = StructType | ArrayType
// pub struct StructType { // pub struct StructType {
// fields []Field // fields []Field
@ -504,6 +504,11 @@ mut:
right_type table.Type right_type table.Type
} }
pub struct GoStmt {
pub:
expr Expr
}
pub struct GotoLabel { pub struct GotoLabel {
pub: pub:
name string name string

View File

@ -11,6 +11,9 @@ import (
v.pref v.pref
term term
os os
//runtime
sync
//time
) )
const ( const (
@ -48,7 +51,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{} pref: &pref.Preferences{}
scope: scope scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0} // scope: &ast.Scope{start_pos: 0, parent: 0}
} }
p.init_parse_fns() p.init_parse_fns()
p.read_first_token() p.read_first_token()
@ -72,7 +75,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
parent: 0 parent: 0
} }
// comments_mode: comments_mode // comments_mode: comments_mode
} }
p.read_first_token() p.read_first_token()
// p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0} // p.scope = &ast.Scope{start_pos: p.tok.position(), parent: 0}
@ -110,7 +113,49 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
} }
} }
struct Queue {
mut:
idx int
mu sync.Mutex
paths []string
table &table.Table
parsed_ast_files []ast.File
}
fn (q mut Queue) run() {
q.mu.lock()
idx := q.idx
if idx >= q.paths.len {
q.mu.unlock()
return
}
q.idx++
q.mu.unlock()
path := q.paths[idx]
file := parse_file(path, q.table, .skip_comments)
q.mu.lock()
q.parsed_ast_files << file
q.mu.unlock()
}
pub fn parse_files(paths []string, table &table.Table) []ast.File { pub fn parse_files(paths []string, table &table.Table) []ast.File {
/*
println('\n\n\nparse_files()')
println(paths)
nr_cpus := runtime.nr_cpus()
println('nr_cpus= $nr_cpus')
mut q := &Queue{
paths: paths
table: table
}
for i in 0 .. nr_cpus {
go q.run()
}
time.sleep_ms(100)
return q.parsed_ast_files
*/
// ///////////////
mut files := []ast.File mut files := []ast.File
for path in paths { for path in paths {
files << parse_file(path, table, .skip_comments) files << parse_file(path, table, .skip_comments)
@ -324,6 +369,12 @@ pub fn (p mut Parser) stmt() ast.Stmt {
stmts: stmts stmts: stmts
} }
} }
.key_go {
p.next()
return ast.GoStmt{
expr: p.expr(0)
}
}
.key_goto { .key_goto {
p.next() p.next()
name := p.check_name() name := p.check_name()
@ -610,7 +661,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
p.expr_mod = '' p.expr_mod = ''
return ast.EnumVal{ return ast.EnumVal{
enum_name: enum_name // lp.prepend_mod(enum_name) enum_name: enum_name // lp.prepend_mod(enum_name)
val: val val: val
pos: p.tok.position() pos: p.tok.position()
mod: mod mod: mod
@ -941,7 +992,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
left: left left: left
right: right right: right
// right_type: typ // right_type: typ
op: op op: op
pos: pos pos: pos
} }
@ -1055,7 +1106,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
p.scope.register_var(ast.Var{ p.scope.register_var(ast.Var{
name: var_name name: var_name
// expr: cond // expr: cond
}) })
stmts := p.parse_block() stmts := p.parse_block()
// println('nr stmts=$stmts.len') // println('nr stmts=$stmts.len')
@ -1150,11 +1201,11 @@ fn (p mut Parser) if_expr() ast.Expr {
stmts: stmts stmts: stmts
else_stmts: else_stmts else_stmts: else_stmts
// typ: typ // typ: typ
pos: pos pos: pos
has_else: has_else has_else: has_else
// left: left // left: left
} }
return node return node
} }
@ -1329,7 +1380,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
fields << ast.Field{ fields << ast.Field{
name: name name: name
// typ: typ // typ: typ
} }
exprs << expr exprs << expr
// TODO: once consts are fixed reg here & update in checker // TODO: once consts are fixed reg here & update in checker