cgen: minor fixes

pull/3953/head
Alexander Medvednikov 2020-03-07 16:23:10 +01:00
parent 24bcc7a93b
commit 63032c4bb7
4 changed files with 31 additions and 13 deletions

View File

@ -1208,7 +1208,7 @@ pub fn (s string) reverse() string {
str: malloc(s.len) str: malloc(s.len)
} }
for i := s.len - 1; i >= 0; i-- { for i := s.len - 1; i >= 0; i-- {
res[s.len - i - 1] = s[i] res.str[s.len - i - 1] = s[i]
} }
return res return res
} }

View File

@ -490,11 +490,11 @@ mut:
pub struct MapInit { pub struct MapInit {
pub: pub:
pos token.Position pos token.Position
keys []Expr keys []Expr
vals []Expr vals []Expr
mut: mut:
typ table.Type typ table.Type
key_type table.Type key_type table.Type
value_type table.Type value_type table.Type
} }
@ -510,8 +510,10 @@ pub:
pub struct CastExpr { pub struct CastExpr {
pub: pub:
typ table.Type typ table.Type
expr Expr expr Expr // `buf`
arg Expr // `n` in `string(buf, n)`
has_arg bool
} }
pub struct AssertStmt { pub struct AssertStmt {

View File

@ -415,10 +415,21 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
ast.CastExpr { ast.CastExpr {
styp := g.table.type_to_str(it.typ) if it.typ == table.string_type_idx {
g.write('($styp)(') g.write('tos(')
g.expr(it.expr) g.expr(it.expr)
g.write(')') if it.has_arg {
g.write(',')
g.expr(it.arg)
}
g.write(')')
}
else {
styp := g.table.type_to_str(it.typ)
g.write('($styp)(')
g.expr(it.expr)
g.write(')')
}
} }
ast.CharLiteral { ast.CharLiteral {
g.write("'$it.val'") g.write("'$it.val'")

View File

@ -530,7 +530,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
// `map[string]int` initialization // `map[string]int` initialization
if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr { if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr {
map_type := p.parse_map_type() map_type := p.parse_map_type()
return ast.MapInit { return ast.MapInit{
typ: map_type typ: map_type
} }
} }
@ -562,16 +562,21 @@ pub fn (p mut Parser) name_expr() ast.Expr {
to_typ := p.parse_type() to_typ := p.parse_type()
p.check(.lpar) p.check(.lpar)
mut expr := ast.Expr{} mut expr := ast.Expr{}
mut arg := ast.Expr{}
mut has_arg := false
expr = p.expr(0) expr = p.expr(0)
// TODO, string(b, len) // TODO, string(b, len)
if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx { if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx {
p.check(.comma) p.check(.comma)
p.expr(0) // len arg = p.expr(0) // len
has_arg = true
} }
p.check(.rpar) p.check(.rpar)
node = ast.CastExpr{ node = ast.CastExpr{
typ: to_typ typ: to_typ
expr: expr expr: expr
arg: arg
has_arg: has_arg
} }
p.expr_mod = '' p.expr_mod = ''
return node return node