parser: fix !

pull/5241/head
Alexander Medvednikov 2020-06-06 12:43:35 +02:00
parent 100b3986b8
commit 40bd1be85f
4 changed files with 14 additions and 13 deletions

View File

@ -245,6 +245,7 @@ pub:
pub mut: pub mut:
name string name string
is_method bool is_method bool
is_mut bool // !
args []CallArg args []CallArg
expected_arg_types []table.Type expected_arg_types []table.Type
language table.Language language table.Language

View File

@ -799,6 +799,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
} }
if method.args[0].is_mut { if method.args[0].is_mut {
c.fail_if_immutable(call_expr.left) c.fail_if_immutable(call_expr.left)
call_expr.is_mut = true
} }
if method.return_type == table.void_type && method.ctdefine.len > 0 && method.ctdefine !in if method.return_type == table.void_type && method.ctdefine.len > 0 && method.ctdefine !in
c.pref.compile_defines { c.pref.compile_defines {

View File

@ -470,7 +470,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
} }
if field.has_default_expr { if field.has_default_expr {
f.write(' = ') f.write(' = ')
f.struct_field_expr( field.default_expr ) f.struct_field_expr(field.default_expr)
} }
// f.write('// $field.pos.line_nr') // f.write('// $field.pos.line_nr')
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr { if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
@ -487,7 +487,6 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('}\n') f.writeln('}\n')
} }
pub fn (mut f Fmt) struct_field_expr(fexpr ast.Expr) { pub fn (mut f Fmt) struct_field_expr(fexpr ast.Expr) {
mut is_pe_amp_ce := false mut is_pe_amp_ce := false
mut ce := ast.CastExpr{} mut ce := ast.CastExpr{}
@ -633,7 +632,6 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
ktyp = minfo.key_type ktyp = minfo.key_type
vtyp = minfo.value_type vtyp = minfo.value_type
} }
f.write('map[') f.write('map[')
f.write(f.type_to_str(ktyp)) f.write(f.type_to_str(ktyp))
f.write(']') f.write(']')
@ -912,6 +910,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
f.write('.' + node.name + '(') f.write('.' + node.name + '(')
f.call_args(node.args) f.call_args(node.args)
f.write(')') f.write(')')
if node.is_mut {
// f.write('!')
}
f.or_expr(node.or_block) f.or_expr(node.or_block)
} else { } else {
f.write_language_prefix(node.language) f.write_language_prefix(node.language)
@ -1033,12 +1034,8 @@ pub fn (mut f Fmt) mark_module_as_used(name string) {
fn (mut f Fmt) write_language_prefix(lang table.Language) { fn (mut f Fmt) write_language_prefix(lang table.Language) {
match lang { match lang {
.c { .c { f.write('C.') }
f.write('C.') .js { f.write('JS.') }
}
.js {
f.write('JS.')
}
else {} else {}
} }
} }

View File

@ -1000,6 +1000,10 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
// p.close_scope() // p.close_scope()
// } // }
} }
// ! in mutable methods
if p.tok.kind == .not && p.peek_tok.kind == .lpar {
p.next()
}
// Method call // Method call
// TODO move to fn.v call_expr() // TODO move to fn.v call_expr()
if p.tok.kind == .lpar { if p.tok.kind == .lpar {
@ -1030,14 +1034,12 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
or_stmts = p.parse_block_no_scope() or_stmts = p.parse_block_no_scope()
p.close_scope() p.close_scope()
} }
if p.tok.kind == .not {
p.next()
}
if p.tok.kind == .question {
// `foo()?` // `foo()?`
if p.tok.kind == .question {
p.next() p.next()
or_kind = .propagate or_kind = .propagate
} }
//
end_pos := p.tok.position() end_pos := p.tok.position()
pos := token.Position{ pos := token.Position{
line_nr: name_pos.line_nr line_nr: name_pos.line_nr