vfmt: minor fixes
parent
aa0ad9d922
commit
519f2990f2
|
@ -199,8 +199,8 @@ fn (p mut Parser) comp_time() {
|
||||||
fn (p mut Parser) chash() {
|
fn (p mut Parser) chash() {
|
||||||
hash := p.lit.trim_space()
|
hash := p.lit.trim_space()
|
||||||
// println('chsh() file=$p.file hash="$hash"')
|
// println('chsh() file=$p.file hash="$hash"')
|
||||||
p.fgen_nl()
|
|
||||||
p.next()
|
p.next()
|
||||||
|
p.fgen_nl()
|
||||||
if hash.starts_with('flag ') {
|
if hash.starts_with('flag ') {
|
||||||
if p.first_pass() {
|
if p.first_pass() {
|
||||||
mut flag := hash[5..]
|
mut flag := hash[5..]
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
strings
|
strings
|
||||||
)
|
)
|
||||||
|
@ -221,10 +223,10 @@ fn (p mut Parser) fn_decl() {
|
||||||
mut f := Fn{
|
mut f := Fn{
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
is_public: is_pub || p.is_vh // functions defined in .vh are always public
|
is_public: is_pub || p.is_vh // functions defined in .vh are always public
|
||||||
|
|
||||||
is_unsafe: p.attr == 'unsafe_fn'
|
is_unsafe: p.attr == 'unsafe_fn'
|
||||||
is_deprecated: p.attr == 'deprecated'
|
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
|
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 {
|
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 {
|
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() {
|
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
|
generic := f.is_generic
|
||||||
p.fn_call_args(mut f)
|
p.fn_call_args(mut f)
|
||||||
if generic {
|
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 ('))
|
p.cgen.resetln(line.replace('$cgen_name (', '$f.name ('))
|
||||||
// println('calling inst $f.name: $p.cgen.cur_line')
|
// println('calling inst $f.name: $p.cgen.cur_line')
|
||||||
}
|
}
|
||||||
|
@ -885,7 +887,7 @@ fn (p mut Parser) fn_args(f mut Fn) {
|
||||||
typ: typ
|
typ: typ
|
||||||
is_arg: true
|
is_arg: true
|
||||||
// is_mut: is_mut
|
// is_mut: is_mut
|
||||||
|
|
||||||
line_nr: p.scanner.line_nr
|
line_nr: p.scanner.line_nr
|
||||||
token_idx: p.cur_tok_index()
|
token_idx: p.cur_tok_index()
|
||||||
}
|
}
|
||||||
|
@ -1107,7 +1109,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
|
||||||
$if !js {
|
$if !js {
|
||||||
fmt := p.typ_to_fmt(typ, 0)
|
fmt := p.typ_to_fmt(typ, 0)
|
||||||
if fmt != '' && typ != 'bool' {
|
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", '))
|
p.cgen.resetln(p.cgen.cur_line.replace(f.name + ' (', '/*opt*/printf ("' + fmt + '$nl", '))
|
||||||
continue
|
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
|
types << varg_type
|
||||||
values << '$ref_deref$varg_value'
|
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
|
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 {
|
fn (fns []Fn) contains(f Fn) bool {
|
||||||
|
@ -1655,3 +1657,4 @@ pub fn (f &Fn) str_for_error() string {
|
||||||
}
|
}
|
||||||
return s + ')'
|
return s + ')'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,13 @@
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
module compiler
|
module compiler
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
os
|
os
|
||||||
strings
|
strings
|
||||||
compiler.x64
|
compiler.x64
|
||||||
//time
|
// time
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Parser {
|
struct Parser {
|
||||||
|
@ -215,7 +217,7 @@ fn (v mut V) new_parser(scanner &Scanner) Parser {
|
||||||
|
|
||||||
// __global scan_time i64
|
// __global scan_time i64
|
||||||
fn (p mut Parser) scan_tokens() {
|
fn (p mut Parser) scan_tokens() {
|
||||||
//t := time.ticks()
|
// t := time.ticks()
|
||||||
for {
|
for {
|
||||||
res := p.scanner.scan()
|
res := p.scanner.scan()
|
||||||
p.tokens << Token{
|
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
|
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
|
// 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
|
// 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
|
// 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.table.register_module(fq_mod)
|
||||||
p.mod = fq_mod
|
p.mod = fq_mod
|
||||||
if p.pass == .imports {
|
if p.pass == .imports {
|
||||||
|
@ -464,6 +466,7 @@ fn (p mut Parser) parse(pass Pass) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.key_pub {
|
.key_pub {
|
||||||
|
p.fspace()
|
||||||
next := p.peek()
|
next := p.peek()
|
||||||
match next {
|
match next {
|
||||||
.key_fn {
|
.key_fn {
|
||||||
|
@ -779,7 +782,7 @@ fn (p mut Parser) type_decl() {
|
||||||
// TODO dirty C typedef hacks for DOOM
|
// TODO dirty C typedef hacks for DOOM
|
||||||
// Unknown type probably means it's a struct, and it's used before the struct is defined,
|
// Unknown type probably means it's a struct, and it's used before the struct is defined,
|
||||||
// so specify "struct"
|
// 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.gen_typedef('typedef $_struct $nt_pair; //type alias name="$name" parent=`$parent.name`')
|
||||||
p.table.register_type(Type{
|
p.table.register_type(Type{
|
||||||
name: name
|
name: name
|
||||||
|
@ -1588,7 +1591,7 @@ fn (p mut Parser) var_decl() {
|
||||||
if is_decl_assign && var_names.len == 1 && var_names[0] == '_' {
|
if is_decl_assign && var_names.len == 1 && var_names[0] == '_' {
|
||||||
p.error_with_token_index('use `=` instead of `:=`', var_token_idxs.last())
|
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)
|
t := p.gen_var_decl(p.var_decl_name, is_static)
|
||||||
if t == 'void' {
|
if t == 'void' {
|
||||||
_,fn_name := p.is_expr_fn_call(p.token_idx - 3)
|
_,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
|
// field
|
||||||
if has_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 {
|
field := p.table.find_field(typ, struct_field) or {
|
||||||
p.error_with_token_index('missing field: $struct_field in type $typ.name', fname_tidx)
|
p.error_with_token_index('missing field: $struct_field in type $typ.name', fname_tidx)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
@ -2958,7 +2961,7 @@ fn (p mut Parser) check_unused_imports() {
|
||||||
mut output := ''
|
mut output := ''
|
||||||
for alias, mod in p.import_table.imports {
|
for alias, mod in p.import_table.imports {
|
||||||
if !p.import_table.is_used_import(alias) {
|
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'
|
output += '\n * $mod_alias'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,11 +24,12 @@ fn (scanner mut Scanner) fgen(s_ string) {
|
||||||
|
|
||||||
[if vfmt]
|
[if vfmt]
|
||||||
fn (scanner mut Scanner) fgenln(s_ string) {
|
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 {
|
if scanner.fmt_line_empty && scanner.fmt_indent > 0 {
|
||||||
s = strings.repeat(`\t`, scanner.fmt_indent) + s
|
s = strings.repeat(`\t`, scanner.fmt_indent) + s
|
||||||
}
|
}
|
||||||
scanner.fmt_lines << s
|
scanner.fmt_lines << s
|
||||||
|
//println('s="$s"')
|
||||||
//scanner.fmt_lines << '//!'
|
//scanner.fmt_lines << '//!'
|
||||||
scanner.fmt_lines << '\n'
|
scanner.fmt_lines << '\n'
|
||||||
//scanner.fmt_out.writeln(s)
|
//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().replace('\n\n\n', '\n').trim_space()
|
||||||
//s := p.scanner.fmt_out.str().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\n\n', '\n\n',
|
||||||
' \n', '\n',
|
' \n', '\n',
|
||||||
') or{', ') or {',
|
') or{', ') or {',
|
||||||
])
|
])
|
||||||
|
*/
|
||||||
|
//.replace('\n\n\n\n', '\n\n')
|
||||||
|
.replace(' \n', '\n')
|
||||||
|
.replace(') or{', ') or {')
|
||||||
|
|
||||||
if s == '' {
|
if s == '' {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if !p.file_name.contains('float.v') {return}
|
if !p.file_path.contains('fn.v') {return}
|
||||||
path := os.tmpdir() + '/' + p.file_name
|
path := os.tmpdir() + '/' + p.file_name
|
||||||
println('generating ${path}')
|
println('generating ${path}')
|
||||||
mut out := os.create(path) or {
|
mut out := os.create(path) or {
|
||||||
|
@ -251,7 +258,8 @@ fn (p &Parser) gen_fmt() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
println('replacing ${p.file_path}...\n')
|
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()
|
out.close()
|
||||||
os.mv(path, p.file_path)
|
os.mv(path, p.file_path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +1,30 @@
|
||||||
module os
|
module os
|
||||||
|
|
||||||
// (Must be realized in Syscall) (Must be specified)
|
// (Must be realized in Syscall) (Must be specified)
|
||||||
// File modes.
|
// File modes.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
O_RDONLY = 1 // open the file read-only.
|
O_RDONLY = 1 // open the file read-only.
|
||||||
O_WRONLY = 2 // open the file write-only.
|
O_WRONLY = 2 // open the file write-only.
|
||||||
O_RDWR = 3 // open the file read-write.
|
O_RDWR = 3 // open the file read-write.
|
||||||
|
O_APPEND = 8 // append data to the file when writing.
|
||||||
O_APPEND = 8 // append data to the file when writing.
|
O_CREATE = 16 // create a new file if none exists.
|
||||||
O_CREATE = 16 // create a new file if none exists.
|
O_EXCL = 32 // used with O_CREATE, file must not exist.
|
||||||
O_EXCL = 32 // used with O_CREATE, file must not exist.
|
O_SYNC = 64 // open for synchronous I/O.
|
||||||
O_SYNC = 64 // open for synchronous I/O.
|
O_TRUNC = 128 // truncate regular writable file when opened.
|
||||||
O_TRUNC = 128 // truncate regular writable file when opened.
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
|
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
|
||||||
const (
|
const (
|
||||||
S_IFMT = 0xF000 // type of file
|
S_IFMT = 0xF000 // type of file
|
||||||
S_IFDIR = 0x4000 // directory
|
S_IFDIR = 0x4000 // directory
|
||||||
S_IFLNK = 0xa000 // link
|
S_IFLNK = 0xa000 // link
|
||||||
)
|
)
|
||||||
|
|
||||||
const(
|
const (
|
||||||
STD_INPUT_HANDLE = -10
|
STD_INPUT_HANDLE = -10
|
||||||
STD_OUTPUT_HANDLE = -11
|
STD_OUTPUT_HANDLE = -11
|
||||||
STD_ERROR_HANDLE = -12
|
STD_ERROR_HANDLE = -12
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue