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

cgen: fix struct init; match SumType1, SumType2

This commit is contained in:
Alexander Medvednikov 2020-03-06 14:14:33 +01:00
parent 3de3b38dc1
commit 6b6031a3d9
2 changed files with 40 additions and 30 deletions

View File

@ -555,7 +555,8 @@ fn (g mut Gen) expr(node ast.Expr) {
// `user := User{name: 'Bob'}` // `user := User{name: 'Bob'}`
ast.StructInit { ast.StructInit {
type_sym := g.table.get_type_symbol(it.typ) type_sym := g.table.get_type_symbol(it.typ)
g.writeln('($type_sym.name){') styp := g.typ(type_sym.name)
g.writeln('($styp){')
for i, field in it.fields { for i, field in it.fields {
g.write('\t.$field = ') g.write('\t.$field = ')
g.expr(it.exprs[i]) g.expr(it.exprs[i])

View File

@ -27,24 +27,24 @@ type PostfixParseFn fn()ast.Expr
struct Parser { struct Parser {
scanner &scanner.Scanner scanner &scanner.Scanner
file_name string file_name string
mut: mut:
tok token.Token tok token.Token
peek_tok token.Token peek_tok token.Token
// vars []string // vars []string
table &table.Table table &table.Table
is_c bool is_c bool
// prefix_parse_fns []PrefixParseFn // prefix_parse_fns []PrefixParseFn
inside_if bool inside_if bool
pref &pref.Preferences // Preferences shared from V struct pref &pref.Preferences // Preferences shared from V struct
builtin_mod bool builtin_mod bool
mod string mod string
attr string attr string
expr_mod string expr_mod string
scope &ast.Scope scope &ast.Scope
imports map[string]string imports map[string]string
ast_imports []ast.Import ast_imports []ast.Import
} }
// for tests // for tests
@ -222,7 +222,7 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
p.error('wrong pub keyword usage') p.error('wrong pub keyword usage')
return ast.Stmt{} return ast.Stmt{}
} }
} }
} }
.lsbr { .lsbr {
return p.attribute() return p.attribute()
@ -409,6 +409,7 @@ fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
} }
*/ */
pub fn (p &Parser) error(s string) { pub fn (p &Parser) error(s string) {
print_backtrace() print_backtrace()
mut path := p.file_name mut path := p.file_name
@ -870,7 +871,7 @@ fn (p mut Parser) dot_expr(left ast.Expr) ast.Expr {
p.filter() p.filter()
// wrong tok position when using defer // wrong tok position when using defer
// defer { // defer {
// p.close_scope() // p.close_scope()
// } // }
} }
// Method call // Method call
@ -927,6 +928,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
} }
@ -973,7 +975,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon { else if p.peek_tok.kind in [.decl_assign, .assign, .semicolon] || p.tok.kind == .semicolon {
mut init := ast.Stmt{} mut init := ast.Stmt{}
mut cond := ast.Expr{} mut cond := ast.Expr{}
//mut inc := ast.Stmt{} // mut inc := ast.Stmt{}
mut inc := ast.Expr{} mut inc := ast.Expr{}
if p.peek_tok.kind in [.assign, .decl_assign] { if p.peek_tok.kind in [.assign, .decl_assign] {
init = p.var_decl_and_assign_stmt() init = p.var_decl_and_assign_stmt()
@ -996,7 +998,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
} }
p.check(.semicolon) p.check(.semicolon)
if p.tok.kind != .lcbr { if p.tok.kind != .lcbr {
//inc = p.stmt() // inc = p.stmt()
inc = p.expr(0) inc = p.expr(0)
} }
stmts := p.parse_block() stmts := p.parse_block()
@ -1037,6 +1039,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
p.scope.register_var(ast.VarDecl{ p.scope.register_var(ast.VarDecl{
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')
@ -1309,12 +1312,13 @@ 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
// p.table.register_const(table.Var{ // p.table.register_const(table.Var{
// name: name // name: name
// // typ: typ // // typ: typ
// }) // })
} }
p.check(.rpar) p.check(.rpar)
@ -1620,6 +1624,11 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
name: 'it' name: 'it'
typ: typ typ: typ
}) })
// TODO
if p.tok.kind == .comma {
p.next()
p.parse_type()
}
} }
else { else {
// Expression match // Expression match