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

cgen: fix returning optional consts; fix csv test

This commit is contained in:
Alexander Medvednikov 2020-04-08 17:21:36 +02:00
parent 3abbdd4a39
commit 7a218286b3
4 changed files with 48 additions and 20 deletions

View File

@ -16,7 +16,6 @@ const (
'vlib/crypto/rc4/rc4_test.v',
'vlib/encoding/base64/base64_memory_test.v',
'vlib/encoding/base64/base64_test.v',
'vlib/encoding/csv/csv_test.v',
'vlib/encoding/utf8/utf8_util_test.v',
'vlib/eventbus/eventbus_test.v',
'vlib/flag/flag_test.v',

View File

@ -112,10 +112,12 @@ fn (r mut Reader) read_record() ?[]string {
for {
// not quoted
if line[0] != `"` {
i = line.index(r.delimiter.str()) or {
// QTODO i = ...
j := line.index(r.delimiter.str()) or {
// last
break
}
i = j
fields << line[..i]
line = line[i+1..]
continue

View File

@ -1123,9 +1123,9 @@ fn (g mut Gen) expr(node ast.Expr) {
}
// g.write('/*pref*/')
g.write(it.op.str())
//g.write('(')
// g.write('(')
g.expr(it.right)
//g.write(')')
// g.write(')')
g.is_amp = false
}
ast.SizeOf {
@ -1793,7 +1793,7 @@ fn (g mut Gen) return_statement(node ast.Return) {
typ_sym := g.table.get_type_symbol(g.fn_decl.return_type)
mr_info := typ_sym.info as table.MultiReturn
mut styp := g.typ(g.fn_decl.return_type)
if fn_return_is_optional {
if fn_return_is_optional { // && !table.type_is(node.types[0], .optional) && node.types[0] !=
styp = styp[7..] // remove 'Option_'
g.write('opt_ok(& ($styp []) { ')
}
@ -1812,8 +1812,10 @@ fn (g mut Gen) return_statement(node ast.Return) {
} else if node.exprs.len == 1 {
// normal return
g.write(' ')
return_sym := g.table.get_type_symbol(node.types[0])
// `return opt_ok(expr)` for functions that expect an optional
if fn_return_is_optional && !table.type_is(node.types[0], .optional) {
if fn_return_is_optional && !table.type_is(node.types[0], .optional) && return_sym.name !=
'Option' {
mut is_none := false
mut is_error := false
expr0 := node.exprs[0]
@ -1831,7 +1833,7 @@ fn (g mut Gen) return_statement(node ast.Return) {
}
if !is_none && !is_error {
styp := g.typ(g.fn_decl.return_type)[7..] // remove 'Option_'
g.write('opt_ok(& ($styp []) { ')
g.write('/*:)$return_sym.name*/opt_ok(&($styp []) { ')
g.expr(node.exprs[0])
g.writeln(' }, sizeof($styp));')
return
@ -2268,12 +2270,17 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
verror('only V strings can be formatted with a ${sfmt} format')
}
g.write('%' + sfmt[1..])
} else if node.expr_types[i] in [table.string_type, table.bool_type] || sym.kind == .enum_ {
} else if node.expr_types[i] in [table.string_type, table.bool_type] || sym.kind ==
.enum_ {
g.write('%.*s')
} else {
match node.exprs[i] {
ast.EnumVal { g.write('%.*s') }
else { g.write('%d') }
ast.EnumVal {
g.write('%.*s')
}
else {
g.write('%d')
}
}
}
}
@ -2304,9 +2311,15 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
sym := g.table.get_type_symbol(node.expr_types[i])
if sym.kind == .enum_ {
is_var := match node.exprs[i] {
ast.SelectorExpr { true }
ast.Ident { true }
else { false }
ast.SelectorExpr {
true
}
ast.Ident {
true
}
else {
false
}
}
if is_var {
styp := g.typ(node.expr_types[i])
@ -2504,13 +2517,27 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
} else if sym.kind == .enum_ {
expr := node.args[0].expr
is_var := match expr {
ast.SelectorExpr { true }
ast.Ident { true }
else { false }
ast.SelectorExpr {
true
}
ast.Ident {
true
}
else {
false
}
}
g.write(if is_var { '${print_method}(${styp}_str(' } else { '${print_method}(tos3("' })
g.write(if is_var {
'${print_method}(${styp}_str('
} else {
'${print_method}(tos3("'
})
g.enum_expr(expr)
g.write(if is_var { '))' } else { '"))' })
g.write(if is_var {
'))'
} else {
'"))'
})
} else {
// `println(int_str(10))`
// sym := g.table.get_type_symbol(node.args[0].typ)

View File

@ -299,8 +299,8 @@ pub enum Precedence {
assign // =
eq // == or !=
// less_greater // > or <
sum // + or -
product // * or /
sum // + - | ^
product // * / << >> &
// mod // %
prefix // -X or !X
postfix // ++ or --