allow slicing of fixed size arrays
parent
d048bf66b0
commit
9d630dff30
|
@ -246,6 +246,7 @@ fn test_slice() {
|
|||
assert a.len == 4
|
||||
}
|
||||
|
||||
|
||||
fn test_push_many() {
|
||||
mut a := [1, 2, 3]
|
||||
b := [4, 5, 6]
|
||||
|
@ -283,6 +284,8 @@ struct Foooj {
|
|||
|
||||
fn test_fixed() {
|
||||
mut nums := [4]int
|
||||
x := nums[1..3]
|
||||
assert x.len == 2
|
||||
assert nums[0] == 0
|
||||
assert nums[1] == 0
|
||||
assert nums[2] == 0
|
||||
|
|
|
@ -1014,22 +1014,32 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type {
|
|||
else {}
|
||||
}
|
||||
node.container_type = typ
|
||||
typ_sym := c.table.get_type_symbol(typ)
|
||||
if !is_range {
|
||||
typ_sym := c.table.get_type_symbol(typ)
|
||||
index_type := c.expr(node.index)
|
||||
index_type_sym := c.table.get_type_symbol(index_type)
|
||||
// println('index expr left=$typ_sym.name $node.pos.line_nr')
|
||||
if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_type_idxs) && index_type_sym.kind != .enum_) {
|
||||
// if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_type_idxs) && index_type_sym.kind != .enum_) {
|
||||
if typ_sym.kind in [.array, .array_fixed] && !(table.is_number(index_type) && index_type_sym.kind != .enum_) {
|
||||
c.error('non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)', node.pos)
|
||||
}
|
||||
else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {
|
||||
c.error('non-string map index (type `$typ_sym.name`)', node.pos)
|
||||
c.error('non-string map index (map type `$typ_sym.name`)', node.pos)
|
||||
}
|
||||
value_type := c.table.value_type(typ)
|
||||
if value_type != table.void_type {
|
||||
return value_type
|
||||
}
|
||||
}
|
||||
else if is_range {
|
||||
// array[1..2] => array
|
||||
// fixed_array[1..2] => array
|
||||
if typ_sym.kind == .array_fixed {
|
||||
elem_type := c.table.value_type(typ)
|
||||
idx := c.table.find_or_register_array(elem_type, 1)
|
||||
return table.new_type(idx)
|
||||
}
|
||||
}
|
||||
return typ
|
||||
}
|
||||
|
||||
|
|
|
@ -776,7 +776,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
|
|||
g.definitions.writeln(');')
|
||||
}
|
||||
if is_main {
|
||||
g.writeln('_vinit();')
|
||||
g.writeln('\t_vinit();')
|
||||
if g.is_importing_os() {
|
||||
if g.autofree {
|
||||
g.writeln('free(_const_os__args.data); // empty, inited in _vinit()')
|
||||
|
@ -792,12 +792,12 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
|
|||
// /////////
|
||||
if is_main {
|
||||
if g.autofree {
|
||||
g.writeln('_vcleanup();')
|
||||
g.writeln('\t_vcleanup();')
|
||||
}
|
||||
if g.is_test {
|
||||
verror('test files cannot have function `main`')
|
||||
}
|
||||
g.writeln('return 0;')
|
||||
g.writeln('\treturn 0;')
|
||||
}
|
||||
if g.defer_stmts.len > 0 {
|
||||
g.write_defer_stmts()
|
||||
|
@ -1567,11 +1567,27 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
|
|||
is_range = true
|
||||
if sym.kind == .string {
|
||||
g.write('string_substr(')
|
||||
g.expr(node.left)
|
||||
}
|
||||
else if sym.kind == .array {
|
||||
g.write('array_slice(')
|
||||
g.expr(node.left)
|
||||
}
|
||||
else if sym.kind == .array_fixed {
|
||||
// Convert a fixed array to V array when doing `fixed_arr[start..end]`
|
||||
g.write('array_slice(new_array_from_c_array(sizeof(')
|
||||
g.expr(node.left)
|
||||
g.write('), sizeof(')
|
||||
g.expr(node.left)
|
||||
g.write('), sizeof(')
|
||||
g.expr(node.left)
|
||||
g.write('[0]), ')
|
||||
g.expr(node.left)
|
||||
g.write(')')
|
||||
}
|
||||
else {
|
||||
g.expr(node.left)
|
||||
}
|
||||
g.expr(node.left)
|
||||
g.write(', ')
|
||||
if it.has_low {
|
||||
g.expr(it.low)
|
||||
|
@ -2603,7 +2619,7 @@ pub fn (g mut Gen) write_tests_main() {
|
|||
g.writeln('\tBenchedTests_end_testing(&bt);')
|
||||
}
|
||||
g.writeln('')
|
||||
g.writeln('_vcleanup();')
|
||||
g.writeln('\t_vcleanup();')
|
||||
g.writeln('\treturn g_test_fails > 0;')
|
||||
g.writeln('}')
|
||||
}
|
||||
|
|
|
@ -191,6 +191,7 @@ void reload_so();
|
|||
void _vinit();
|
||||
void _vcleanup();
|
||||
#define sigaction_size sizeof(sigaction);
|
||||
#define _ARR_LEN(a) ( (sizeof(a)) / (sizeof((a)[0])) );
|
||||
|
||||
// ============== wyhash ==============
|
||||
// Author: Wang Yi
|
||||
|
|
|
@ -149,6 +149,10 @@ pub fn is_number(typ Type) bool {
|
|||
*/
|
||||
|
||||
|
||||
pub fn is_number(typ Type) bool {
|
||||
return type_idx(typ) in number_type_idxs
|
||||
}
|
||||
|
||||
pub const (
|
||||
// primitive types
|
||||
void_type_idx = 1
|
||||
|
|
Loading…
Reference in New Issue