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.check(.dot)
}
if p.tok.kind == .question {
p.next()
}
mut name := p.tok.lit
// `module.Type`
if p.peek_tok.kind == .dot {
// /if !(p.tok.lit in p.table.imports) {
@ -103,8 +107,13 @@ pub fn (p mut Parser) parse_type() table.Type {
}
p.next()
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 {
// func
.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{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
}
p.init_parse_fns()
p.read_first_token()
@ -73,7 +74,10 @@ pub fn parse_file(path string, table &table.Table) ast.File {
table: table
file_name: path
pref: &pref.Preferences{}
scope: &ast.Scope{start_pos: 0, parent: 0}
scope: &ast.Scope{
start_pos: 0
parent: 0
}
}
p.read_first_token()
// 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(stmts[0])
p.scope.end_pos = p.tok.pos
return ast.File{
path: path
mod: module_decl
@ -131,7 +133,6 @@ pub fn (p mut Parser) read_first_token() {
p.next()
}
pub fn (p mut Parser) open_scope() {
p.scope = &ast.Scope{
parent: p.scope
@ -293,6 +294,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
return ast.ExprStmt{
expr: expr
// typ: typ
}
}
}
@ -443,14 +445,15 @@ pub fn (p mut Parser) parse_ident(is_c bool) (ast.Ident,table.Type) {
// typ = var.typ
}
// 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')
ident.kind = .variable
ident.info = ast.IdentVar{
ident.info = ast.IdentVar{}
// typ: typ
// name: ident.name
// expr: p.expr(0)// var.expr
}
// }
return ident,typ
}
else {
@ -481,6 +484,7 @@ pub fn (p mut Parser) parse_ident(is_c bool) (ast.Ident,table.Type) {
kind: .blank_ident
name: name
// pos: p.tok.position()
}
return node,typ
// 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{}
typ := p.parse_type()
sym := p.table.get_type_symbol(typ)
// p.warn('struct init typ=$sym.name')
p.warn('struct init typ=$sym.name')
p.check(.lcbr)
mut field_names := []string
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() {
p.warn('!! s init $p.tok.lit')
// || p.table.known_type(p.tok.lit)) {
return p.struct_init()
}
@ -703,6 +706,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
node = ie_node
typ = ie_typ
*/
}
else if p.tok.kind == .key_as {
p.next()
@ -1063,11 +1067,13 @@ fn (p mut Parser) if_expr() ast.Expr {
}
}
*/
node = ast.IfExpr{
cond: cond
stmts: stmts
else_stmts: else_stmts
// typ: typ
pos: p.tok.position()
// left: left
@ -1118,7 +1124,6 @@ fn (p mut Parser) array_init() ast.Expr {
println(sym.name)
}
*/
/* TODO: joe
if p.tok.kind == .rsbr && int(p.expected_type) != 0 && p.table.get_type_symbol(p.expected_type).kind == .array {
// p.warn('[] expr')
@ -1131,6 +1136,7 @@ fn (p mut Parser) array_init() ast.Expr {
return node,p.expected_type
}
*/
mut val_type := table.void_type
mut exprs := []ast.Expr
// mut is_fixed := false
@ -1176,6 +1182,7 @@ fn (p mut Parser) array_init() ast.Expr {
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) }
// 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 {
p.check(.key_module)
name := p.check_name()
p.mod = name
return ast.Module{
name: name
}
@ -1331,7 +1339,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
t := table.TypeSymbol{
parent: 0
kind: .struct_
name: name
name: p.prepend_mod(name)
info: table.Struct{
fields: fields
}
@ -1420,7 +1428,11 @@ fn (p mut Parser) var_decl() ast.VarDecl {
// p.fspace()
}
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,_ := p.expr(0)
// if _ := p.table.find_var(name) {
@ -1439,7 +1451,6 @@ fn (p mut Parser) var_decl() ast.VarDecl {
// is_mut: is_mut
// typ: typ
// })
// typ_sym := p.table.get_type_symbol(typ)
// p.warn('var decl name=$name typ=$typ_sym.name')
// println(p.table.names)
@ -1449,6 +1460,7 @@ fn (p mut Parser) var_decl() ast.VarDecl {
is_mut: is_mut
// typ: typ
pos: p.tok.position()
}
p.scope.register_var(node)
@ -1567,6 +1579,7 @@ fn (p mut Parser) match_expr() ast.Expr {
blocks: blocks
match_exprs: match_exprs
// typ: typ
cond: cond
}
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) {
println(s)
exit(1)

View File

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