fmt: interface decl fixes (#5829)

pull/5825/head
Enzo 2020-07-14 18:52:28 +02:00 committed by GitHub
parent 60ce938149
commit d301611de7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 30 deletions

View File

@ -179,6 +179,7 @@ pub struct InterfaceDecl {
pub: pub:
name string name string
field_names []string field_names []string
is_pub bool
methods []FnDecl methods []FnDecl
pos token.Position pos token.Position
} }
@ -213,9 +214,9 @@ pub:
pub struct AnonFn { pub struct AnonFn {
pub: pub:
decl FnDecl decl FnDecl
pub mut: pub mut:
typ table.Type typ table.Type
} }
pub struct FnDecl { pub struct FnDecl {
@ -390,23 +391,23 @@ pub fn (i &Ident) var_info() IdentVar {
pub struct InfixExpr { pub struct InfixExpr {
pub: pub:
op token.Kind op token.Kind
pos token.Position pos token.Position
left Expr left Expr
right Expr right Expr
pub mut: pub mut:
left_type table.Type left_type table.Type
right_type table.Type right_type table.Type
auto_locked string auto_locked string
} }
pub struct PostfixExpr { pub struct PostfixExpr {
pub: pub:
op token.Kind op token.Kind
expr Expr expr Expr
pos token.Position pos token.Position
pub mut: pub mut:
auto_locked string auto_locked string
} }
pub struct PrefixExpr { pub struct PrefixExpr {
@ -440,20 +441,20 @@ pub mut:
pub struct IfBranch { pub struct IfBranch {
pub: pub:
cond Expr cond Expr
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
body_pos token.Position body_pos token.Position
comments []Comment comments []Comment
pub mut: pub mut:
smartcast bool // should only be true if cond is `x is sumtype`, it will be set in checker - if_expr smartcast bool // should only be true if cond is `x is sumtype`, it will be set in checker - if_expr
left_as_name string // only used in x is SumType check left_as_name string // only used in x is SumType check
} }
pub struct UnsafeExpr { pub struct UnsafeExpr {
pub: pub:
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
} }
pub struct LockExpr { pub struct LockExpr {

View File

@ -75,7 +75,7 @@ pub fn fmt(file ast.File, table &table.Table, is_debug bool) string {
// for comment in file.comments { println('$comment.line_nr $comment.text') } // for comment in file.comments { println('$comment.line_nr $comment.text') }
f.imports(f.file.imports) // now that we have all autoimports, handle them f.imports(f.file.imports) // now that we have all autoimports, handle them
res := f.out.str().trim_space() + '\n' res := f.out.str().trim_space() + '\n'
bounded_import_pos := util.imin(res.len,f.import_pos) bounded_import_pos := util.imin(res.len, f.import_pos)
return res[..bounded_import_pos] + f.out_imports.str() + res[bounded_import_pos..] // + '\n' return res[..bounded_import_pos] + f.out_imports.str() + res[bounded_import_pos..] // + '\n'
} }
@ -190,7 +190,7 @@ fn (mut f Fmt) adjust_complete_line() {
} }
} }
pub fn (mut f Fmt) set_current_module_name(cmodname string){ pub fn (mut f Fmt) set_current_module_name(cmodname string) {
f.cur_mod = cmodname f.cur_mod = cmodname
f.table.cmod_prefix = cmodname + '.' f.table.cmod_prefix = cmodname + '.'
} }
@ -428,12 +428,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
// f.imports(f.file.imports) // f.imports(f.file.imports)
} }
ast.InterfaceDecl { ast.InterfaceDecl {
f.writeln('interface $it.name {') f.interface_decl(it)
for method in it.methods {
f.write('\t')
f.writeln(method.stringify(f.table, f.cur_mod).after('fn '))
}
f.writeln('}\n')
} }
ast.Module { ast.Module {
f.mod(it) f.mod(it)
@ -662,6 +657,19 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('}\n') f.writeln('}\n')
} }
pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
if node.is_pub {
f.write('pub ')
}
name := node.name.after('.')
f.writeln('interface $name {')
for method in node.methods {
f.write('\t')
f.writeln(method.stringify(f.table, f.cur_mod).after('fn '))
}
f.writeln('}\n')
}
pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) { pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) {
mut is_pe_amp_ce := false mut is_pe_amp_ce := false
mut ce := ast.CastExpr{} mut ce := ast.CastExpr{}
@ -1053,7 +1061,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
} }
ast.UnsafeExpr { ast.UnsafeExpr {
f.writeln('unsafe {') f.writeln('unsafe {')
f.stmts(it.stmts) f.stmts(node.stmts)
f.writeln('}') f.writeln('}')
} }
} }

View File

@ -0,0 +1,5 @@
module module_fmt
pub interface MyInterface {
fun()
}

View File

@ -396,6 +396,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
return ast.InterfaceDecl{ return ast.InterfaceDecl{
name: interface_name name: interface_name
methods: methods methods: methods
is_pub: is_pub
pos: start_pos pos: start_pos
} }
} }