From 260f708bb238b98f8831d23e90199e6ebf28cf23 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 7 Mar 2020 05:19:15 +0100 Subject: [PATCH] cgen: array << val --- vlib/v/checker/checker.v | 2 +- vlib/v/gen/cgen.v | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4b57abd07b..ab16488fc9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -810,8 +810,8 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type { } else {} } + node.container_type = typ if !is_range { - node.container_type = typ typ_sym := c.table.get_type_symbol(typ) index_type := c.expr(node.index) index_type_sym := c.table.get_type_symbol(index_type) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 428aca081a..bc55d45018 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -218,7 +218,7 @@ fn (g mut Gen) stmt(node ast.Stmt) { ast.ForStmt { g.write('while (') g.expr(it.cond) - g.writeln(') {') + g.writeln(') { //1') for stmt in it.stmts { g.stmt(stmt) } @@ -483,6 +483,7 @@ fn (g mut Gen) expr(node ast.Expr) { // if it.left_type == table.string_type_idx { // g.write('/*$it.left_type str*/') // } + // string + string if it.op == .plus && it.left_type == table.string_type_idx { g.write('string_add(') g.expr(it.left) @@ -490,6 +491,14 @@ fn (g mut Gen) expr(node ast.Expr) { 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(') + g.expr(it.left) + g.write(', ') + g.expr(it.right) + g.write(')') + } else { // if it.op == .dot { // println('!! dot') @@ -634,8 +643,17 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) { mut is_range := false match node.index { ast.RangeExpr { - is_range = true - g.write('array_slice(') + // TODO should never be 0 + if node.container_type != 0 { + sym := g.table.get_type_symbol(node.container_type) + is_range = true + if sym.kind == .string { + g.write('string_substr(') + } + else if sym.kind == .array { + g.write('array_slice(') + } + } g.expr(node.left) g.write(', ') if it.has_low {