From 9b4dec7b98878d820ce1d4672e25d990afc31a2b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 5 Jul 2022 23:17:00 +0300 Subject: [PATCH] parser: anonymous structs (part 1) --- vlib/v/markused/walker.v | 2 +- vlib/v/parser/expr.v | 4 +++- vlib/v/parser/parse_type.v | 7 +++++++ vlib/v/parser/parser.v | 6 +++--- vlib/v/parser/struct.v | 10 +++++----- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 110e8916d2..40207c6132 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -353,7 +353,7 @@ fn (mut w Walker) expr(node_ ast.Expr) { } } ast.None {} - ast.Nil{} + ast.Nil {} ast.ParExpr { w.expr(node.expr) } diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 019d2baa22..c4aaa0b2ff 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -133,7 +133,9 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { node = p.select_expr() } .key_nil { - node = ast.Nil{pos:p.tok.pos()} + node = ast.Nil{ + pos: p.tok.pos() + } p.next() } .number { diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index fb62be59ef..6d2860ae0a 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -423,6 +423,13 @@ pub fn (mut p Parser) parse_type() ast.Type { nr_muls++ p.next() } + // Anon structs + if p.tok.kind == .key_struct { + p.struct_decl(true) + p.next() + return p.table.find_type_idx('anon_struct') // TODO + } + language := p.parse_language() mut typ := ast.void_type is_array := p.tok.kind == .lsbr diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f7122bb45f..ee4aa7d362 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -620,7 +620,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt { return p.fn_decl() } .key_struct, .key_union { - return p.struct_decl() + return p.struct_decl(false) } .key_interface { return p.interface_decl() @@ -662,7 +662,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt { return p.fn_decl() } .key_struct { - return p.struct_decl() + return p.struct_decl(false) } .dollar { if p.peek_tok.kind == .eof { @@ -689,7 +689,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt { return p.enum_decl() } .key_union { - return p.struct_decl() + return p.struct_decl(false) } .comment { return p.comment_stmt() diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index affd50a570..83ceafb3ff 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -7,7 +7,7 @@ import v.ast import v.token import v.util -fn (mut p Parser) struct_decl() ast.StructDecl { +fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { p.top_level_statement_start() // save attributes, they will be changed later in fields attrs := p.attrs @@ -39,7 +39,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { if p.disallow_declarations_in_script_mode() { return ast.StructDecl{} } - mut name := p.check_name() + mut name := if is_anon { '' } else { p.check_name()} if name.len == 1 && name[0].is_capital() { p.error_with_pos('single letter capital names are reserved for generic template types.', name_pos) @@ -322,7 +322,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { p.check(.rcbr) } is_minify := attrs.contains('minify') - mut t := ast.TypeSymbol{ + mut sym := ast.TypeSymbol{ kind: .struct_ language: language name: name @@ -341,11 +341,11 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } is_pub: is_pub } - if p.table.has_deep_child_no_ref(&t, name) { + if p.table.has_deep_child_no_ref(&sym, name) { p.error_with_pos('invalid recursive struct `$orig_name`', name_pos) return ast.StructDecl{} } - mut ret := p.table.register_sym(t) + mut ret := p.table.register_sym(sym) // allow duplicate c struct declarations if ret == -1 && language != .c { p.error_with_pos('cannot register struct `$name`, another type with this name exists',