mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gen.golang: array append & basic strings
This commit is contained in:
parent
5b97ad15ad
commit
4032838aba
|
@ -1579,18 +1579,18 @@ pub fn (mut f Gen) enum_val(node ast.EnumVal) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Gen) ident(node ast.Ident) {
|
pub fn (mut f Gen) ident(node ast.Ident) {
|
||||||
if node.info is ast.IdentVar {
|
// if node.info is ast.IdentVar {
|
||||||
if node.info.is_mut {
|
// if node.info.is_mut {
|
||||||
f.write(node.info.share.str() + ' ')
|
// f.write(node.info.share.str() + ' ')
|
||||||
}
|
// }
|
||||||
var_info := node.var_info()
|
// var_info := node.var_info()
|
||||||
if var_info.is_static {
|
// if var_info.is_static {
|
||||||
f.write('static ')
|
// f.write('static ')
|
||||||
}
|
// }
|
||||||
if var_info.is_volatile {
|
// if var_info.is_volatile {
|
||||||
f.write('volatile ')
|
// f.write('volatile ')
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
f.write_language_prefix(node.language)
|
f.write_language_prefix(node.language)
|
||||||
if node.name == 'it' && f.it_name != '' && f.in_lambda_depth == 0 { // allow `it` in lambdas
|
if node.name == 'it' && f.it_name != '' && f.in_lambda_depth == 0 { // allow `it` in lambdas
|
||||||
f.write(f.it_name)
|
f.write(f.it_name)
|
||||||
|
@ -1708,18 +1708,32 @@ pub fn (mut f Gen) infix_expr(node ast.InfixExpr) {
|
||||||
// start_pos := f.out.len
|
// start_pos := f.out.len
|
||||||
// start_len := f.line_len
|
// start_len := f.line_len
|
||||||
f.expr(node.left)
|
f.expr(node.left)
|
||||||
|
left_type_sym := f.table.final_sym(node.left_type)
|
||||||
|
is_array_push := left_type_sym.kind == .array && node.op == .left_shift
|
||||||
is_one_val_array_init := node.op in [.key_in, .not_in] && node.right is ast.ArrayInit
|
is_one_val_array_init := node.op in [.key_in, .not_in] && node.right is ast.ArrayInit
|
||||||
&& (node.right as ast.ArrayInit).exprs.len == 1
|
&& (node.right as ast.ArrayInit).exprs.len == 1
|
||||||
if is_one_val_array_init {
|
if is_one_val_array_init {
|
||||||
// `var in [val]` => `var == val`
|
// `var in [val]` => `var == val`
|
||||||
op := if node.op == .key_in { ' == ' } else { ' != ' }
|
op := if node.op == .key_in { ' == ' } else { ' != ' }
|
||||||
f.write(op)
|
f.write(op)
|
||||||
|
} else if is_array_push {
|
||||||
|
f.write(' = ')
|
||||||
} else {
|
} else {
|
||||||
f.write(' $node.op.str() ')
|
f.write(' $node.op.str() ')
|
||||||
}
|
}
|
||||||
if is_one_val_array_init {
|
if is_one_val_array_init {
|
||||||
// `var in [val]` => `var == val`
|
// `var in [val]` => `var == val`
|
||||||
f.expr((node.right as ast.ArrayInit).exprs[0])
|
f.expr((node.right as ast.ArrayInit).exprs[0])
|
||||||
|
} else if is_array_push {
|
||||||
|
f.write('append(')
|
||||||
|
f.expr(node.left)
|
||||||
|
f.write(', ')
|
||||||
|
f.expr(node.right)
|
||||||
|
right_type_sym := f.table.final_sym(node.right_type)
|
||||||
|
if right_type_sym.kind == .array {
|
||||||
|
f.write('...')
|
||||||
|
}
|
||||||
|
f.write(')')
|
||||||
} else {
|
} else {
|
||||||
f.expr(node.right)
|
f.expr(node.right)
|
||||||
}
|
}
|
||||||
|
@ -2191,18 +2205,8 @@ pub fn (mut f Gen) char_literal(node ast.CharLiteral) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Gen) string_literal(node ast.StringLiteral) {
|
pub fn (mut f Gen) string_literal(node ast.StringLiteral) {
|
||||||
use_double_quote := true
|
|
||||||
if node.is_raw {
|
if node.is_raw {
|
||||||
f.write('r')
|
f.write('`$node.val`')
|
||||||
} else if node.language == ast.Language.c {
|
|
||||||
f.write('c')
|
|
||||||
}
|
|
||||||
if node.is_raw {
|
|
||||||
if use_double_quote {
|
|
||||||
f.write('"$node.val"')
|
|
||||||
} else {
|
|
||||||
f.write("'$node.val'")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
unescaped_val := node.val.replace('$golang.bs$golang.bs', '\x01').replace_each([
|
unescaped_val := node.val.replace('$golang.bs$golang.bs', '\x01').replace_each([
|
||||||
"$golang.bs'",
|
"$golang.bs'",
|
||||||
|
@ -2210,40 +2214,19 @@ pub fn (mut f Gen) string_literal(node ast.StringLiteral) {
|
||||||
'$golang.bs"',
|
'$golang.bs"',
|
||||||
'"',
|
'"',
|
||||||
])
|
])
|
||||||
if use_double_quote {
|
|
||||||
s := unescaped_val.replace_each(['\x01', '$golang.bs$golang.bs', '"', '$golang.bs"'])
|
s := unescaped_val.replace_each(['\x01', '$golang.bs$golang.bs', '"', '$golang.bs"'])
|
||||||
f.write('"$s"')
|
f.write('"$s"')
|
||||||
} else {
|
|
||||||
s := unescaped_val.replace_each(['\x01', '$golang.bs$golang.bs', "'", "$golang.bs'"])
|
|
||||||
f.write("'$s'")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut f Gen) string_inter_literal(node ast.StringInterLiteral) {
|
pub fn (mut f Gen) string_inter_literal(node ast.StringInterLiteral) {
|
||||||
mut quote := "'"
|
mut quote := '"'
|
||||||
for val in node.vals {
|
|
||||||
if val.contains('\\"') {
|
|
||||||
quote = '"'
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if val.contains("\\'") {
|
|
||||||
quote = "'"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if val.contains('"') {
|
|
||||||
quote = "'"
|
|
||||||
}
|
|
||||||
if val.contains("'") {
|
|
||||||
quote = '"'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO: this code is very similar to ast.Expr.str()
|
// TODO: this code is very similar to ast.Expr.str()
|
||||||
// serkonda7: it can not fully be replaced tho as ´f.expr()´ and `ast.Expr.str()`
|
// serkonda7: it can not fully be replaced tho as ´f.expr()´ and `ast.Expr.str()`
|
||||||
// work too different for the various exprs that are interpolated
|
// work too different for the various exprs that are interpolated
|
||||||
f.write(quote)
|
f.write(quote)
|
||||||
for i, val in node.vals {
|
for i, val in node.vals {
|
||||||
f.write(val)
|
f.write(val.replace("$golang.bs'", "'"))
|
||||||
if i >= node.exprs.len {
|
if i >= node.exprs.len {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
21
vlib/v/gen/golang/tests/array.go.vv
Normal file
21
vlib/v/gen/golang/tests/array.go.vv
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
module main
|
||||||
|
|
||||||
|
#import "fmt"
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
mut array_int := [1,2]
|
||||||
|
mut array_int_b := [6,7]
|
||||||
|
mut array_string := ['one', 'two']
|
||||||
|
mut array_string_b := ['six', 'seven']
|
||||||
|
|
||||||
|
array_int << 3
|
||||||
|
array_int << [4,5]
|
||||||
|
array_int << array_int_b
|
||||||
|
|
||||||
|
array_string << 'three'
|
||||||
|
array_string << ['four', 'five']
|
||||||
|
array_string << array_string_b
|
||||||
|
|
||||||
|
#fmt.Printf("%v\n", array_int)
|
||||||
|
#fmt.Printf("%v\n", array_string)
|
||||||
|
}
|
3
vlib/v/gen/golang/tests/array.go.vv.out
Normal file
3
vlib/v/gen/golang/tests/array.go.vv.out
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[1 2 3 4 5 6 7]
|
||||||
|
[one two three four five six seven]
|
||||||
|
|
|
@ -4,7 +4,7 @@ module main
|
||||||
|
|
||||||
//fn println(x any){
|
//fn println(x any){
|
||||||
fn println(x string){
|
fn println(x string){
|
||||||
#fmt.Println(x)
|
#fmt.Println(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
|
|
31
vlib/v/gen/golang/tests/string.go.vv
Normal file
31
vlib/v/gen/golang/tests/string.go.vv
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
module main
|
||||||
|
|
||||||
|
#import "fmt"
|
||||||
|
|
||||||
|
fn println(x string){
|
||||||
|
#fmt.Println(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// basic literals
|
||||||
|
basic_a := 'What\'s your name?'
|
||||||
|
basic_b := "It's nice to meet you \"Bob\""
|
||||||
|
println(basic_a)
|
||||||
|
println(basic_b)
|
||||||
|
|
||||||
|
// raw
|
||||||
|
raw_a := r'I am a "raw" string'
|
||||||
|
raw_b := r"I am also a 'raw' string"
|
||||||
|
println(raw_a)
|
||||||
|
println(raw_b)
|
||||||
|
|
||||||
|
// TODO: interpolation
|
||||||
|
// apple := 'apple'
|
||||||
|
// bananna := 'bananna'
|
||||||
|
// number_one := 1
|
||||||
|
// number_two := 2
|
||||||
|
// inter_a := 'I have $number_one ${apple}\'s and $number_two ${bananna}\'s'
|
||||||
|
// inter_b := "I have \"$number_one ${apple}'s\" and \"$number_two ${bananna}'s\""
|
||||||
|
// println(inter_a)
|
||||||
|
// println(inter_b)
|
||||||
|
}
|
4
vlib/v/gen/golang/tests/string.go.vv.out
Normal file
4
vlib/v/gen/golang/tests/string.go.vv.out
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
What's your name?
|
||||||
|
It's nice to meet you "Bob"
|
||||||
|
I am a "raw" string
|
||||||
|
I am also a 'raw' string
|
Loading…
Reference in New Issue
Block a user