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

vfmt: minor fixes

This commit is contained in:
Alexander Medvednikov 2019-12-19 05:41:12 +03:00
parent aa0ad9d922
commit 519f2990f2
5 changed files with 51 additions and 39 deletions

View File

@ -199,8 +199,8 @@ fn (p mut Parser) comp_time() {
fn (p mut Parser) chash() {
hash := p.lit.trim_space()
// println('chsh() file=$p.file hash="$hash"')
p.fgen_nl()
p.next()
p.fgen_nl()
if hash.starts_with('flag ') {
if p.first_pass() {
mut flag := hash[5..]

View File

@ -3,6 +3,8 @@
// that can be found in the LICENSE file.
module compiler
import (
strings
)
@ -221,10 +223,10 @@ fn (p mut Parser) fn_decl() {
mut f := Fn{
mod: p.mod
is_public: is_pub || p.is_vh // functions defined in .vh are always public
is_unsafe: p.attr == 'unsafe_fn'
is_deprecated: p.attr == 'deprecated'
comptime_define: if p.attr.starts_with('if ') {p.attr[3..]}else {''}
comptime_define: if p.attr.starts_with('if ') { p.attr[3..] } else { '' }
}
is_live := p.attr == 'live' && !p.pref.is_so && p.pref.is_live
if p.attr == 'live' && p.first_pass() && !p.pref.is_live && !p.pref.is_so {
@ -634,7 +636,7 @@ fn (p mut Parser) skip_fn_body() {
}
fn (p &Parser) get_linkage_prefix() string {
return if p.pref.ccompiler == 'msvc' && p.attr == 'live' && p.pref.is_so {'__declspec(dllexport) '}else if p.attr == 'inline' {'static inline '}else {''}
return if p.pref.ccompiler == 'msvc' && p.attr == 'live' && p.pref.is_so { '__declspec(dllexport) ' } else if p.attr == 'inline' { 'static inline ' } else { '' }
}
fn (p mut Parser) check_unused_and_mut_vars() {
@ -840,7 +842,7 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s
generic := f.is_generic
p.fn_call_args(mut f)
if generic {
line := if p.cgen.is_tmp {p.cgen.tmp_line}else {p.cgen.cur_line}
line := if p.cgen.is_tmp { p.cgen.tmp_line } else { p.cgen.cur_line }
p.cgen.resetln(line.replace('$cgen_name (', '$f.name ('))
// println('calling inst $f.name: $p.cgen.cur_line')
}
@ -885,7 +887,7 @@ fn (p mut Parser) fn_args(f mut Fn) {
typ: typ
is_arg: true
// is_mut: is_mut
line_nr: p.scanner.line_nr
token_idx: p.cur_tok_index()
}
@ -1107,7 +1109,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
$if !js {
fmt := p.typ_to_fmt(typ, 0)
if fmt != '' && typ != 'bool' {
nl := if f.name == 'println' {'\\n'}else {''}
nl := if f.name == 'println' { '\\n' } else { '' }
p.cgen.resetln(p.cgen.cur_line.replace(f.name + ' (', '/*opt*/printf ("' + fmt + '$nl", '))
continue
}
@ -1434,7 +1436,7 @@ fn (p mut Parser) fn_call_vargs(f Fn) (string,[]string) {
}
}
}
ref_deref := if last_arg.typ.ends_with('*') && !varg_type.ends_with('*') {'&'}else if !last_arg.typ.ends_with('*') && varg_type.ends_with('*') {'*'}else {''}
ref_deref := if last_arg.typ.ends_with('*') && !varg_type.ends_with('*') { '&' } else if !last_arg.typ.ends_with('*') && varg_type.ends_with('*') { '*' } else { '' }
types << varg_type
values << '$ref_deref$varg_value'
}
@ -1613,7 +1615,7 @@ fn (p &Parser) find_misspelled_local_var(name string, min_match f32) string {
closest_var = var.name
}
}
return if closest >= min_match {closest_var}else {''}
return if closest >= min_match { closest_var } else { '' }
}
fn (fns []Fn) contains(f Fn) bool {
@ -1655,3 +1657,4 @@ pub fn (f &Fn) str_for_error() string {
}
return s + ')'
}

View File

@ -3,11 +3,13 @@
// that can be found in the LICENSE file.
module compiler
import (
os
strings
compiler.x64
//time
// time
)
struct Parser {
@ -215,7 +217,7 @@ fn (v mut V) new_parser(scanner &Scanner) Parser {
// __global scan_time i64
fn (p mut Parser) scan_tokens() {
//t := time.ticks()
// t := time.ticks()
for {
res := p.scanner.scan()
p.tokens << Token{
@ -429,9 +431,9 @@ fn (p mut Parser) parse(pass Pass) {
p.can_chash = p.mod in ['ui', 'darwin', 'clipboard', 'webview'] // TODO tmp remove
// Import pass - the first and the smallest pass that only analyzes imports
// if we are a building module get the full module name from v.mod
fq_mod := if p.pref.build_mode == .build_module && p.v.mod.ends_with(p.mod) {p.v.mod}
fq_mod := if p.pref.build_mode == .build_module && p.v.mod.ends_with(p.mod) { p.v.mod }
// fully qualify the module name, eg base64 to encoding.base64
else {p.table.qualify_module(p.mod, p.file_path)}
else { p.table.qualify_module(p.mod, p.file_path) }
p.table.register_module(fq_mod)
p.mod = fq_mod
if p.pass == .imports {
@ -464,6 +466,7 @@ fn (p mut Parser) parse(pass Pass) {
}
}
.key_pub {
p.fspace()
next := p.peek()
match next {
.key_fn {
@ -779,7 +782,7 @@ fn (p mut Parser) type_decl() {
// TODO dirty C typedef hacks for DOOM
// Unknown type probably means it's a struct, and it's used before the struct is defined,
// so specify "struct"
_struct := if parent.cat != .array && parent.cat != .func && !p.table.known_type(parent.name) {'struct'}else {''}
_struct := if parent.cat != .array && parent.cat != .func && !p.table.known_type(parent.name) { 'struct' } else { '' }
p.gen_typedef('typedef $_struct $nt_pair; //type alias name="$name" parent=`$parent.name`')
p.table.register_type(Type{
name: name
@ -1588,7 +1591,7 @@ fn (p mut Parser) var_decl() {
if is_decl_assign && var_names.len == 1 && var_names[0] == '_' {
p.error_with_token_index('use `=` instead of `:=`', var_token_idxs.last())
}
p.var_decl_name = if var_names.len > 1 {'_V_mret_${p.token_idx}_' + var_names.join('_')}else {var_names[0]}
p.var_decl_name = if var_names.len > 1 { '_V_mret_${p.token_idx}_' + var_names.join('_') } else { var_names[0] }
t := p.gen_var_decl(p.var_decl_name, is_static)
if t == 'void' {
_,fn_name := p.is_expr_fn_call(p.token_idx - 3)
@ -1970,7 +1973,7 @@ fn (p mut Parser) dot(str_typ_ string, method_ph int) string {
}
// field
if has_field {
struct_field := if typ.name != 'Option' {p.table.var_cgen_name(field_name)}else {field_name}
struct_field := if typ.name != 'Option' { p.table.var_cgen_name(field_name) } else { field_name }
field := p.table.find_field(typ, struct_field) or {
p.error_with_token_index('missing field: $struct_field in type $typ.name', fname_tidx)
exit(1)
@ -2958,7 +2961,7 @@ fn (p mut Parser) check_unused_imports() {
mut output := ''
for alias, mod in p.import_table.imports {
if !p.import_table.is_used_import(alias) {
mod_alias := if alias == mod {alias}else {'$alias ($mod)'}
mod_alias := if alias == mod { alias } else { '$alias ($mod)' }
output += '\n * $mod_alias'
}
}

View File

@ -24,11 +24,12 @@ fn (scanner mut Scanner) fgen(s_ string) {
[if vfmt]
fn (scanner mut Scanner) fgenln(s_ string) {
mut s := s_//.trim_space()
mut s := s_.trim_right(' ')
if scanner.fmt_line_empty && scanner.fmt_indent > 0 {
s = strings.repeat(`\t`, scanner.fmt_indent) + s
}
scanner.fmt_lines << s
//println('s="$s"')
//scanner.fmt_lines << '//!'
scanner.fmt_lines << '\n'
//scanner.fmt_out.writeln(s)
@ -235,15 +236,21 @@ fn (p &Parser) gen_fmt() {
}
//s := p.scanner.fmt_out.str().replace('\n\n\n', '\n').trim_space()
//s := p.scanner.fmt_out.str().trim_space()
s := p.scanner.fmt_lines.join('').trim_space().replace_each([
//p.scanner.fgenln('// nice')
s := p.scanner.fmt_lines.join('')/*.replace_each([
'\n\n\n\n', '\n\n',
' \n', '\n',
') or{', ') or {',
])
*/
//.replace('\n\n\n\n', '\n\n')
.replace(' \n', '\n')
.replace(') or{', ') or {')
if s == '' {
return
}
//if !p.file_name.contains('float.v') {return}
if !p.file_path.contains('fn.v') {return}
path := os.tmpdir() + '/' + p.file_name
println('generating ${path}')
mut out := os.create(path) or {
@ -251,7 +258,8 @@ fn (p &Parser) gen_fmt() {
return
}
println('replacing ${p.file_path}...\n')
out.writeln(s)//p.scanner.fmt_out.str().trim_space())
out.writeln(s.trim_space())//p.scanner.fmt_out.str().trim_space())
out.writeln('')
out.close()
os.mv(path, p.file_path)
}

View File

@ -1,32 +1,30 @@
module os
// (Must be realized in Syscall) (Must be specified)
// File modes.
const (
O_RDONLY = 1 // open the file read-only.
O_WRONLY = 2 // open the file write-only.
O_RDWR = 3 // open the file read-write.
O_APPEND = 8 // append data to the file when writing.
O_CREATE = 16 // create a new file if none exists.
O_EXCL = 32 // used with O_CREATE, file must not exist.
O_SYNC = 64 // open for synchronous I/O.
O_TRUNC = 128 // truncate regular writable file when opened.
O_RDONLY = 1 // open the file read-only.
O_WRONLY = 2 // open the file write-only.
O_RDWR = 3 // open the file read-write.
O_APPEND = 8 // append data to the file when writing.
O_CREATE = 16 // create a new file if none exists.
O_EXCL = 32 // used with O_CREATE, file must not exist.
O_SYNC = 64 // open for synchronous I/O.
O_TRUNC = 128 // truncate regular writable file when opened.
)
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
const (
S_IFMT = 0xF000 // type of file
S_IFMT = 0xF000 // type of file
S_IFDIR = 0x4000 // directory
S_IFLNK = 0xa000 // link
)
const(
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
const (
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
)