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

v2: mod.Type

This commit is contained in:
Alexander Medvednikov 2020-02-16 11:48:29 +01:00
parent d4991164cf
commit 6d8ad58515
3 changed files with 116 additions and 86 deletions

View File

@ -94,6 +94,10 @@ pub fn (p mut Parser) parse_type() table.Type {
p.next() p.next()
p.check(.dot) p.check(.dot)
} }
if p.tok.kind == .question {
p.next()
}
mut name := p.tok.lit
// `module.Type` // `module.Type`
if p.peek_tok.kind == .dot { if p.peek_tok.kind == .dot {
// /if !(p.tok.lit in p.table.imports) { // /if !(p.tok.lit in p.table.imports) {
@ -103,8 +107,13 @@ pub fn (p mut Parser) parse_type() table.Type {
} }
p.next() p.next()
p.check(.dot) p.check(.dot)
name += '.' + p.tok.lit
} }
name := p.tok.lit // `Foo` in module `mod` means `mod.Foo`
else if p.mod != 'main' {
name = p.mod + '.' + name
}
// p.warn('get type $name')
match p.tok.kind { match p.tok.kind {
// func // func
.key_fn { .key_fn {

View File

@ -56,6 +56,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()
@ -73,7 +74,10 @@ pub fn parse_file(path string, table &table.Table) ast.File {
table: table table: table
file_name: path file_name: path
pref: &pref.Preferences{} pref: &pref.Preferences{}
scope: &ast.Scope{start_pos: 0, parent: 0} scope: &ast.Scope{
start_pos: 0
parent: 0
}
} }
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}
@ -99,9 +103,7 @@ pub fn parse_file(path string, table &table.Table) ast.File {
} }
// println('nr stmts = $stmts.len') // println('nr stmts = $stmts.len')
// println(stmts[0]) // println(stmts[0])
p.scope.end_pos = p.tok.pos p.scope.end_pos = p.tok.pos
return ast.File{ return ast.File{
path: path path: path
mod: module_decl mod: module_decl
@ -131,7 +133,6 @@ pub fn (p mut Parser) read_first_token() {
p.next() p.next()
} }
pub fn (p mut Parser) open_scope() { pub fn (p mut Parser) open_scope() {
p.scope = &ast.Scope{ p.scope = &ast.Scope{
parent: p.scope parent: p.scope
@ -293,6 +294,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
return ast.ExprStmt{ return ast.ExprStmt{
expr: expr expr: expr
// typ: typ // typ: typ
} }
} }
} }
@ -443,14 +445,15 @@ pub fn (p mut Parser) parse_ident(is_c bool) (ast.Ident,table.Type) {
// typ = var.typ // typ = var.typ
} }
// variable // variable
if known_var /* || p.tok.kind in [.comma, .decl_assign, .assign]*/ { if known_var {
// || p.tok.kind in [.comma, .decl_assign, .assign]
// println('#### IDENT: $var.name: $var.typ.typ.name - $var.typ.idx') // println('#### IDENT: $var.name: $var.typ.typ.name - $var.typ.idx')
ident.kind = .variable ident.kind = .variable
ident.info = ast.IdentVar{ ident.info = ast.IdentVar{}
// typ: typ // typ: typ
// name: ident.name // name: ident.name
// expr: p.expr(0)// var.expr // expr: p.expr(0)// var.expr
} // }
return ident,typ return ident,typ
} }
else { else {
@ -481,6 +484,7 @@ pub fn (p mut Parser) parse_ident(is_c bool) (ast.Ident,table.Type) {
kind: .blank_ident kind: .blank_ident
name: name name: name
// pos: p.tok.position() // pos: p.tok.position()
} }
return node,typ return node,typ
// p.error('parse_ident: unknown identifier `$name`') // p.error('parse_ident: unknown identifier `$name`')
@ -495,7 +499,7 @@ fn (p mut Parser) struct_init() (ast.Expr,table.Type) {
mut node := ast.Expr{} mut node := ast.Expr{}
typ := p.parse_type() typ := p.parse_type()
sym := p.table.get_type_symbol(typ) sym := p.table.get_type_symbol(typ)
// p.warn('struct init typ=$sym.name') p.warn('struct init typ=$sym.name')
p.check(.lcbr) p.check(.lcbr)
mut field_names := []string mut field_names := []string
mut exprs := []ast.Expr mut exprs := []ast.Expr
@ -588,7 +592,6 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
} }
} }
else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || p.tok.lit in ['array', 'string', 'ustring', 'mapnode', 'map']) && !p.tok.lit[p.tok.lit.len - 1].is_capital() { else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || p.tok.lit in ['array', 'string', 'ustring', 'mapnode', 'map']) && !p.tok.lit[p.tok.lit.len - 1].is_capital() {
p.warn('!! s init $p.tok.lit')
// || p.table.known_type(p.tok.lit)) { // || p.table.known_type(p.tok.lit)) {
return p.struct_init() return p.struct_init()
} }
@ -703,6 +706,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node = ie_node node = ie_node
typ = ie_typ typ = ie_typ
*/ */
} }
else if p.tok.kind == .key_as { else if p.tok.kind == .key_as {
p.next() p.next()
@ -1063,11 +1067,13 @@ fn (p mut Parser) if_expr() ast.Expr {
} }
} }
*/ */
node = ast.IfExpr{ node = ast.IfExpr{
cond: cond cond: cond
stmts: stmts stmts: stmts
else_stmts: else_stmts else_stmts: else_stmts
// typ: typ // typ: typ
pos: p.tok.position() pos: p.tok.position()
// left: left // left: left
@ -1118,7 +1124,6 @@ fn (p mut Parser) array_init() ast.Expr {
println(sym.name) println(sym.name)
} }
*/ */
/* TODO: joe /* TODO: joe
if p.tok.kind == .rsbr && int(p.expected_type) != 0 && p.table.get_type_symbol(p.expected_type).kind == .array { if p.tok.kind == .rsbr && int(p.expected_type) != 0 && p.table.get_type_symbol(p.expected_type).kind == .array {
// p.warn('[] expr') // p.warn('[] expr')
@ -1131,6 +1136,7 @@ fn (p mut Parser) array_init() ast.Expr {
return node,p.expected_type return node,p.expected_type
} }
*/ */
mut val_type := table.void_type mut val_type := table.void_type
mut exprs := []ast.Expr mut exprs := []ast.Expr
// mut is_fixed := false // mut is_fixed := false
@ -1176,6 +1182,7 @@ fn (p mut Parser) array_init() ast.Expr {
p.warn('fixed size array') p.warn('fixed size array')
} }
*/ */
} }
// idx := if is_fixed { p.table.find_or_register_array_fixed(val_type, fixed_size, 1) } else { p.table.find_or_register_array(val_type, 1) } // idx := if is_fixed { p.table.find_or_register_array_fixed(val_type, fixed_size, 1) } else { p.table.find_or_register_array(val_type, 1) }
// array_type := table.new_type(idx) // array_type := table.new_type(idx)
@ -1211,6 +1218,7 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {
fn (p mut Parser) module_decl() ast.Module { fn (p mut Parser) module_decl() ast.Module {
p.check(.key_module) p.check(.key_module)
name := p.check_name() name := p.check_name()
p.mod = name
return ast.Module{ return ast.Module{
name: name name: name
} }
@ -1331,7 +1339,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
t := table.TypeSymbol{ t := table.TypeSymbol{
parent: 0 parent: 0
kind: .struct_ kind: .struct_
name: name name: p.prepend_mod(name)
info: table.Struct{ info: table.Struct{
fields: fields fields: fields
} }
@ -1420,7 +1428,11 @@ fn (p mut Parser) var_decl() ast.VarDecl {
// p.fspace() // p.fspace()
} }
name := p.check_name() name := p.check_name()
p.next() if p.tok.kind == .comma {
p.check(.comma)
p.check_name()
}
p.next() // :=
// expr,typ := p.expr(0) // expr,typ := p.expr(0)
expr,_ := p.expr(0) expr,_ := p.expr(0)
// if _ := p.table.find_var(name) { // if _ := p.table.find_var(name) {
@ -1439,7 +1451,6 @@ fn (p mut Parser) var_decl() ast.VarDecl {
// is_mut: is_mut // is_mut: is_mut
// typ: typ // typ: typ
// }) // })
// typ_sym := p.table.get_type_symbol(typ) // typ_sym := p.table.get_type_symbol(typ)
// p.warn('var decl name=$name typ=$typ_sym.name') // p.warn('var decl name=$name typ=$typ_sym.name')
// println(p.table.names) // println(p.table.names)
@ -1449,6 +1460,7 @@ fn (p mut Parser) var_decl() ast.VarDecl {
is_mut: is_mut is_mut: is_mut
// typ: typ // typ: typ
pos: p.tok.position() pos: p.tok.position()
} }
p.scope.register_var(node) p.scope.register_var(node)
@ -1567,6 +1579,7 @@ fn (p mut Parser) match_expr() ast.Expr {
blocks: blocks blocks: blocks
match_exprs: match_exprs match_exprs: match_exprs
// typ: typ // typ: typ
cond: cond cond: cond
} }
return node return node
@ -1635,6 +1648,13 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
} }
} }
fn (p &Parser) prepend_mod(name string) string {
if p.builtin_mod || p.mod == 'main' {
return name
}
return '${p.mod}.${name}'
}
fn verror(s string) { fn verror(s string) {
println(s) println(s)
exit(1) exit(1)

View File

@ -336,6 +336,7 @@ pub fn (t mut Table) register_builtin_type_symbol(typ TypeSymbol) int {
[inline] [inline]
pub fn (t mut Table) register_type_symbol(typ TypeSymbol) int { pub fn (t mut Table) register_type_symbol(typ TypeSymbol) int {
// println('register_type_symbol( $typ.name )')
existing_idx := t.type_idxs[typ.name] existing_idx := t.type_idxs[typ.name]
if existing_idx > 0 { if existing_idx > 0 {
ex_type := t.types[existing_idx] ex_type := t.types[existing_idx]