mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: array init fix;
This commit is contained in:
parent
2d5c7c8e93
commit
8e2537a366
@ -76,7 +76,7 @@ fn (p mut Parser) struct_decl(generic_param_types []string) {
|
||||
}
|
||||
}
|
||||
if name.len == 1 && !p.pref.building_v && !p.pref.is_repl {
|
||||
p.warn('struct names must have more than one character')
|
||||
p.warn('struct names must have more than one character ("$name", len=$name.len, $p.pref.building_v)')
|
||||
}
|
||||
if !is_c && !good_type_name(name) {
|
||||
p.error('bad struct name, e.g. use `HttpRequest` instead of `HTTPRequest`')
|
||||
|
@ -513,8 +513,9 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
type_sym := g.table.get_type_symbol(it.typ)
|
||||
if type_sym.kind != .array_fixed {
|
||||
elem_sym := g.table.get_type_symbol(it.elem_type)
|
||||
g.write('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($type_sym.name), ')
|
||||
g.writeln('(${elem_sym.name}[]){\t')
|
||||
elem_type_str := g.typ(it.elem_type)
|
||||
g.write('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($elem_type_str), ')
|
||||
g.writeln('($elem_type_str[]){\t')
|
||||
for expr in it.exprs {
|
||||
g.expr(expr)
|
||||
g.write(', ')
|
||||
@ -708,7 +709,8 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
if type_sym.kind != .void {
|
||||
tmp = g.new_tmp_var()
|
||||
}
|
||||
g.write('$type_sym.name $tmp = ')
|
||||
styp := g.typ(it.expr_type)
|
||||
g.write('$styp $tmp = ')
|
||||
g.expr(it.cond)
|
||||
g.writeln(';') // $it.blocks.len')
|
||||
for j, branch in it.branches {
|
||||
|
@ -75,7 +75,7 @@ void foo(int a) {
|
||||
for (int i = 0;
|
||||
i < 10; i++) {
|
||||
}
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (int[]){
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(int), (int[]){
|
||||
1, 2, 3,
|
||||
});
|
||||
array_int nums2 = array_slice(nums, 0, 2);
|
||||
@ -83,15 +83,15 @@ i < 10; i++) {
|
||||
array_int nums4 = array_slice(nums, 1, nums.len);
|
||||
int number = (*(int*)array_get(nums, 0));
|
||||
array_set(&nums, 1, &(int[]) { 10 });
|
||||
array_bool bools = new_array_from_c_array(2, 2, sizeof(array_bool), (bool[]){
|
||||
array_bool bools = new_array_from_c_array(2, 2, sizeof(bool), (bool[]){
|
||||
true, false,
|
||||
});
|
||||
array_User users = new_array_from_c_array(1, 1, sizeof(array_User), (User[]){
|
||||
array_User users = new_array_from_c_array(1, 1, sizeof(User), (User[]){
|
||||
(User){
|
||||
0},
|
||||
});
|
||||
bool b = (*(bool*)array_get(bools, 0));
|
||||
array_string mystrings = new_array_from_c_array(2, 2, sizeof(array_string), (string[]){
|
||||
array_string mystrings = new_array_from_c_array(2, 2, sizeof(string), (string[]){
|
||||
tos3("a"), tos3("b"),
|
||||
});
|
||||
string s = (*(string*)array_get(mystrings, 0));
|
||||
|
@ -66,7 +66,7 @@ void function2() {
|
||||
}
|
||||
|
||||
void init_array() {
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(array_int), (int[]){
|
||||
array_int nums = new_array_from_c_array(3, 3, sizeof(int), (int[]){
|
||||
4, 2, 3,
|
||||
});
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ void User_foo(User* u);
|
||||
|
||||
void User_foo(User* u) {
|
||||
int age = u->age;
|
||||
array_string zzz = array_repeat(new_array_from_c_array(1, 1, sizeof(array_string), (string[]){
|
||||
array_string zzz = array_repeat(new_array_from_c_array(1, 1, sizeof(string), (string[]){
|
||||
tos3(""),
|
||||
}), u->age);
|
||||
}
|
||||
|
@ -41,13 +41,13 @@ int main() {
|
||||
string e = tos3("hello");
|
||||
e = testb(111);
|
||||
e = tos3("world");
|
||||
array_int f = new_array_from_c_array(4, 4, sizeof(array_int), (int[]){
|
||||
array_int f = new_array_from_c_array(4, 4, sizeof(int), (int[]){
|
||||
testa(), 2, 3, 4,
|
||||
});
|
||||
array_string g = new_array_from_c_array(2, 2, sizeof(array_string), (string[]){
|
||||
array_string g = new_array_from_c_array(2, 2, sizeof(string), (string[]){
|
||||
testb(1), tos3("hello"),
|
||||
});
|
||||
array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(array_Foo), (Foo[]){
|
||||
array_Foo arr_foo = new_array_from_c_array(1, 1, sizeof(Foo), (Foo[]){
|
||||
a,
|
||||
});
|
||||
Foo af_idx_el = (*(Foo*)array_get(arr_foo, 0));
|
||||
|
Loading…
Reference in New Issue
Block a user