From 24bcc7a93bcafb89b78f0d2670cabe01f6b9514b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 7 Mar 2020 14:31:40 +0100 Subject: [PATCH] cgen: array and fixed array fixes --- vlib/v/checker/checker.v | 4 ++++ vlib/v/gen/cgen.v | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 50d8c8d413..b861e5dc2f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -273,6 +273,10 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr) info := typ_sym.info as table.Array return info.elem_type } + // repeat() returns `array`, need to return `array_xxx` + else if typ_sym.kind == .array && name in ['repeat'] { + return typ + } if method := typ_sym.find_method(name) { for i, arg_expr in method_call_expr.args { c.expected_type = method.args[i].typ diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 034b858977..9fb7da5560 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -66,11 +66,18 @@ pub fn (g &Gen) styp(t string) string { pub fn (g mut Gen) write_array_types() { for typ in g.table.types { - if typ.kind != .array { - continue + if typ.kind == .array { + styp := typ.name.replace('.', '__') + g.definitions.writeln('typedef array $styp;') + } + else if typ.kind == .array_fixed { + styp := typ.name.replace('.', '__') + // array_fixed_char_300 => char x[300] + mut fixed := styp[12..] + len := styp.after('_') + fixed = fixed[..fixed.len - len.len - 1] + g.definitions.writeln('typedef $fixed $styp [$len];') } - styp := typ.name.replace('.', '__') - g.definitions.writeln('typedef array $styp;') } } @@ -491,6 +498,13 @@ fn (g mut Gen) expr(node ast.Expr) { g.expr(it.right) g.write(')') } + else if it.op == .eq && it.left_type == table.string_type_idx { + g.write('string_eq(') + g.expr(it.left) + g.write(', ') + g.expr(it.right) + g.write(')') + } // arr << val else if it.op == .left_shift && g.table.get_type_symbol(it.left_type).kind == .array { g.write('array_push(') @@ -553,7 +567,7 @@ fn (g mut Gen) expr(node ast.Expr) { key_typ_sym := g.table.get_type_symbol(it.key_type) value_typ_sym := g.table.get_type_symbol(it.value_type) size := it.vals.len - if size > 0 { + if size > 0 { g.write('new_map_init($size, sizeof($value_typ_sym.name), (${key_typ_sym.name}[$size]){') for expr in it.keys { g.expr(expr) @@ -576,6 +590,9 @@ fn (g mut Gen) expr(node ast.Expr) { if it.typ != 0 { typ_sym := g.table.get_type_symbol(it.typ) receiver_name = typ_sym.name + // if typ_sym.kind == .array { + // receiver_name = 'array' + // } } name := '${receiver_name}_$it.name'.replace('.', '__') g.write('${name}(')