cgen: map_get/map_set

pull/4072/head
Alexander Medvednikov 2020-03-19 10:07:31 +01:00
parent 6c56528cd7
commit b5cb1b1fe2
1 changed files with 20 additions and 1 deletions

View File

@ -65,7 +65,7 @@ pub fn (g mut Gen) typ(t table.Type) string {
if styp.starts_with('C__') {
styp = styp[3..]
}
if styp in ['stat', 'dirent*', 'tm', 'tm*'] {
if styp in ['stat', 'dirent*', 'tm', 'tm*', 'winsize'] {
// TODO perf and other C structs
styp = 'struct $styp'
}
@ -1261,6 +1261,25 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
g.write('))')
}
}
else if sym.kind == .map {
info := sym.info as table.Map
elem_type_str := g.typ(info.value_type)
if g.is_assign_expr {
g.is_array_set = true
g.write('map_set(&')
g.expr(node.left)
g.write(', ')
g.expr(node.index)
g.write(', &($elem_type_str[]) { ')
}
else {
g.write('(*($elem_type_str*)map_get2(')
g.expr(node.left)
g.write(', ')
g.expr(node.index)
g.write('))')
}
}
else if sym.kind == .string && !table.type_is_ptr(node.container_type) {
g.write('string_at(')
g.expr(node.left)