cgen: array << val

pull/3951/head
Alexander Medvednikov 2020-03-07 05:19:15 +01:00
parent c14c81ace6
commit 260f708bb2
2 changed files with 22 additions and 4 deletions

View File

@ -810,8 +810,8 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type {
} }
else {} else {}
} }
if !is_range {
node.container_type = typ node.container_type = typ
if !is_range {
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
index_type := c.expr(node.index) index_type := c.expr(node.index)
index_type_sym := c.table.get_type_symbol(index_type) index_type_sym := c.table.get_type_symbol(index_type)

View File

@ -218,7 +218,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
ast.ForStmt { ast.ForStmt {
g.write('while (') g.write('while (')
g.expr(it.cond) g.expr(it.cond)
g.writeln(') {') g.writeln(') { //1')
for stmt in it.stmts { for stmt in it.stmts {
g.stmt(stmt) g.stmt(stmt)
} }
@ -483,6 +483,7 @@ fn (g mut Gen) expr(node ast.Expr) {
// if it.left_type == table.string_type_idx { // if it.left_type == table.string_type_idx {
// g.write('/*$it.left_type str*/') // g.write('/*$it.left_type str*/')
// } // }
// string + string
if it.op == .plus && it.left_type == table.string_type_idx { if it.op == .plus && it.left_type == table.string_type_idx {
g.write('string_add(') g.write('string_add(')
g.expr(it.left) g.expr(it.left)
@ -490,6 +491,14 @@ fn (g mut Gen) expr(node ast.Expr) {
g.expr(it.right) g.expr(it.right)
g.write(')') 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 { else {
// if it.op == .dot { // if it.op == .dot {
// println('!! dot') // println('!! dot')
@ -634,8 +643,17 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
mut is_range := false mut is_range := false
match node.index { match node.index {
ast.RangeExpr { ast.RangeExpr {
// TODO should never be 0
if node.container_type != 0 {
sym := g.table.get_type_symbol(node.container_type)
is_range = true is_range = true
if sym.kind == .string {
g.write('string_substr(')
}
else if sym.kind == .array {
g.write('array_slice(') g.write('array_slice(')
}
}
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', ')
if it.has_low { if it.has_low {