1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: handle comments in const blocks

This commit is contained in:
Alexander Medvednikov 2020-05-12 00:09:59 +02:00
parent b74f4ee3ec
commit 1c8e14c77c
8 changed files with 60 additions and 27 deletions

View File

@ -132,6 +132,7 @@ pub:
pos token.Position pos token.Position
pub mut: pub mut:
typ table.Type typ table.Type
comment Comment
} }
pub struct ConstDecl { pub struct ConstDecl {
@ -499,12 +500,14 @@ pub:
pos token.Position pos token.Position
} }
/*
pub struct ReturnStmt { pub struct ReturnStmt {
pub: pub:
tok_kind token.Kind // or pos tok_kind token.Kind // or pos
results []Expr results []Expr
pos token.Position pos token.Position
} }
*/
// #include etc // #include etc
pub struct HashStmt { pub struct HashStmt {

View File

@ -206,27 +206,7 @@ fn (mut f Fmt) stmt(node ast.Stmt) {
f.writeln('}') f.writeln('}')
} }
ast.ConstDecl { ast.ConstDecl {
if it.is_pub { f.const_decl(it)
f.write('pub ')
}
f.writeln('const (')
mut max := 0
for field in it.fields {
if field.name.len > max {
max = field.name.len
}
}
f.indent++
for field in it.fields {
name := field.name.after('.')
f.write('$name ')
f.write(strings.repeat(` `, max - field.name.len))
f.write('= ')
f.expr(field.expr)
f.writeln('')
}
f.indent--
f.writeln(')\n')
} }
ast.DeferStmt { ast.DeferStmt {
f.writeln('defer {') f.writeln('defer {')
@ -1074,3 +1054,31 @@ fn (mut f Fmt) struct_init(it ast.StructInit) {
f.write('}') f.write('}')
} }
} }
fn (mut f Fmt) const_decl(it ast.ConstDecl) {
if it.is_pub {
f.write('pub ')
}
f.writeln('const (')
mut max := 0
for field in it.fields {
if field.name.len > max {
max = field.name.len
}
}
f.indent++
for field in it.fields {
if field.comment.text != '' {
f.comment(field.comment)
// f.writeln('// ' + field.comment.text)
}
name := field.name.after('.')
f.write('$name ')
f.write(strings.repeat(` `, max - field.name.len))
f.write('= ')
f.expr(field.expr)
f.writeln('')
}
f.indent--
f.writeln(')\n')
}

View File

@ -1,6 +1,8 @@
const ( const (
// pi
pi = 3.14 pi = 3.14
phi = 1.618 phi = 1.618
// Euler's constant
eulers = 2.7182 eulers = 2.7182
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']

View File

@ -1,6 +1,8 @@
const ( const (
// pi
pi=3.14 pi=3.14
phi=1.618 phi=1.618
//Euler's constant
eulers=2.7182 eulers=2.7182
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']

View File

@ -57,6 +57,15 @@ pub fn (mut p Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr {
p.close_scope() p.close_scope()
p.inside_or_expr = false p.inside_or_expr = false
} }
if p.tok.kind == .question {
// `foo()?`
p.next()
is_or_block_used = true
//mut s := ast.Stmt{}
//s = ast.ReturnStmt{}
or_stmts << ast.Return{}
}
node := ast.CallExpr{ node := ast.CallExpr{
name: fn_name name: fn_name
args: args args: args

View File

@ -9,7 +9,9 @@ import v.token
fn (mut p Parser) if_expr() ast.IfExpr { fn (mut p Parser) if_expr() ast.IfExpr {
p.inside_if_expr = true p.inside_if_expr = true
defer { p.inside_if_expr = false } defer {
p.inside_if_expr = false
}
pos := p.tok.position() pos := p.tok.position()
mut branches := []ast.IfBranch{} mut branches := []ast.IfBranch{}
mut has_else := false mut has_else := false
@ -144,8 +146,8 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
// (this replaces the old `it`) // (this replaces the old `it`)
// TODO doesn't work right now (fixed, uncomment when merging) // TODO doesn't work right now (fixed, uncomment when merging)
// p.scope.register(var_name, ast.Var{ // p.scope.register(var_name, ast.Var{
// name: var_name // name: var_name
// typ: typ.to_ptr() // typ: typ.to_ptr()
// }) // })
// println(var_name) // println(var_name)
} }

View File

@ -485,8 +485,9 @@ pub fn (mut p Parser) stmt() ast.Stmt {
} }
} else if p.tok.kind == .name && p.peek_tok.kind == .name { } else if p.tok.kind == .name && p.peek_tok.kind == .name {
p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position()) p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position())
} else if p.tok.kind == .name && !p.inside_if_expr && !p.inside_or_expr && p.peek_tok.kind in [.rcbr, .eof] { } else if p.tok.kind == .name && !p.inside_if_expr && !p.inside_match && !p.inside_or_expr &&
p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position()) p.peek_tok.kind in [.rcbr, .eof] {
// p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position())
} }
epos := p.tok.position() epos := p.tok.position()
expr := p.expr(0) expr := p.expr(0)
@ -1084,8 +1085,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
p.next() // ( p.next() // (
mut fields := []ast.ConstField{} mut fields := []ast.ConstField{}
for p.tok.kind != .rpar { for p.tok.kind != .rpar {
mut comment := ast.Comment{}
if p.tok.kind == .comment { if p.tok.kind == .comment {
p.comment() comment = p.comment()
} }
pos := p.tok.position() pos := p.tok.position()
name := p.prepend_mod(p.check_name()) name := p.prepend_mod(p.check_name())
@ -1097,6 +1099,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
name: name name: name
expr: expr expr: expr
pos: pos pos: pos
comment: comment
} }
fields << field fields << field
p.global_scope.register(field.name, field) p.global_scope.register(field.name, field)

View File

@ -462,10 +462,14 @@ pub fn (t &Table) check(got, expected Type) bool {
if exp_idx == any_type_idx || got_idx == any_type_idx { if exp_idx == any_type_idx || got_idx == any_type_idx {
return true return true
} }
// TODO i64 as int etc
if (exp_idx in pointer_type_idxs || exp_idx in number_type_idxs) && (got_idx in pointer_type_idxs || if (exp_idx in pointer_type_idxs || exp_idx in number_type_idxs) && (got_idx in pointer_type_idxs ||
got_idx in number_type_idxs) { got_idx in number_type_idxs) {
return true return true
} }
//if exp_idx in pointer_type_idxs && got_idx in pointer_type_idxs {
//return true
//}
// see hack in checker IndexExpr line #691 // see hack in checker IndexExpr line #691
if (got_idx == byte_type_idx && exp_idx == byteptr_type_idx) || (exp_idx == byte_type_idx && if (got_idx == byte_type_idx && exp_idx == byteptr_type_idx) || (exp_idx == byte_type_idx &&
got_idx == byteptr_type_idx) { got_idx == byteptr_type_idx) {