cgen: array init fix;

pull/4033/head
Alexander Medvednikov 2020-03-15 07:42:45 +01:00
parent 2d5c7c8e93
commit 8e2537a366
6 changed files with 15 additions and 13 deletions

View File

@ -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 { 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) { if !is_c && !good_type_name(name) {
p.error('bad struct name, e.g. use `HttpRequest` instead of `HTTPRequest`') p.error('bad struct name, e.g. use `HttpRequest` instead of `HTTPRequest`')

View File

@ -513,8 +513,9 @@ fn (g mut Gen) expr(node ast.Expr) {
type_sym := g.table.get_type_symbol(it.typ) type_sym := g.table.get_type_symbol(it.typ)
if type_sym.kind != .array_fixed { if type_sym.kind != .array_fixed {
elem_sym := g.table.get_type_symbol(it.elem_type) 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), ') elem_type_str := g.typ(it.elem_type)
g.writeln('(${elem_sym.name}[]){\t') 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 { for expr in it.exprs {
g.expr(expr) g.expr(expr)
g.write(', ') g.write(', ')
@ -708,7 +709,8 @@ fn (g mut Gen) expr(node ast.Expr) {
if type_sym.kind != .void { if type_sym.kind != .void {
tmp = g.new_tmp_var() 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.expr(it.cond)
g.writeln(';') // $it.blocks.len') g.writeln(';') // $it.blocks.len')
for j, branch in it.branches { for j, branch in it.branches {

View File

@ -75,7 +75,7 @@ void foo(int a) {
for (int i = 0; for (int i = 0;
i < 10; i++) { 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, 1, 2, 3,
}); });
array_int nums2 = array_slice(nums, 0, 2); array_int nums2 = array_slice(nums, 0, 2);
@ -83,15 +83,15 @@ i < 10; i++) {
array_int nums4 = array_slice(nums, 1, nums.len); array_int nums4 = array_slice(nums, 1, nums.len);
int number = (*(int*)array_get(nums, 0)); int number = (*(int*)array_get(nums, 0));
array_set(&nums, 1, &(int[]) { 10 }); 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, 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){ (User){
0}, 0},
}); });
bool b = (*(bool*)array_get(bools, 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"), tos3("a"), tos3("b"),
}); });
string s = (*(string*)array_get(mystrings, 0)); string s = (*(string*)array_get(mystrings, 0));

View File

@ -66,7 +66,7 @@ void function2() {
} }
void init_array() { 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, 4, 2, 3,
}); });
} }

View File

@ -10,7 +10,7 @@ void User_foo(User* u);
void User_foo(User* u) { void User_foo(User* u) {
int age = u->age; 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(""), tos3(""),
}), u->age); }), u->age);
} }

View File

@ -41,13 +41,13 @@ int main() {
string e = tos3("hello"); string e = tos3("hello");
e = testb(111); e = testb(111);
e = tos3("world"); 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, 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"), 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, a,
}); });
Foo af_idx_el = (*(Foo*)array_get(arr_foo, 0)); Foo af_idx_el = (*(Foo*)array_get(arr_foo, 0));