parser: ptr++,*(ptr+1)

pull/2737/head
BigBlack 2019-11-12 04:39:16 +08:00 committed by Alexander Medvednikov
parent bd34524a1c
commit 99169ae4ff
3 changed files with 18 additions and 2 deletions

View File

@ -146,6 +146,19 @@ fn (p mut Parser) name_expr() string {
p.next()
}
if p.tok == .lpar {
p.gen('*'.repeat(deref_nr))
p.gen('(')
p.check(.lpar)
mut temp_type := p.bool_expression()
p.gen(')')
p.check(.rpar)
for _ in 0..deref_nr {
temp_type = temp_type.replace_once('*', '')
}
return temp_type
}
mut name := p.lit
// Raw string (`s := r'hello \n ')
if name == 'r' && p.peek() == .str {

View File

@ -1623,7 +1623,7 @@ fn (p mut Parser) var_expr(v Var) string {
if !v.is_changed {
p.mark_var_changed(v)
}
if typ != 'int' {
if typ != 'int' && !typ.contains('*') {
if !p.pref.translated && !is_number_type(typ) {
p.error('cannot ++/-- value of type `$typ`')
}
@ -1635,7 +1635,7 @@ fn (p mut Parser) var_expr(v Var) string {
//return p.index_expr(typ, fn_ph)
}
else {
return 'void'
return typ
}
}
typ = p.index_expr(typ, fn_ph)

View File

@ -5,6 +5,9 @@ fn test_pointer_arithmetic() {
mut parr := *int(arr.data)
parr += 1
assert 2 == *parr
parr++
assert 3 == *parr
assert *(parr + 1) == 4
}
}