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

vfmt: fix in, |, or {}

This commit is contained in:
Alexander Medvednikov 2019-12-18 07:52:14 +03:00
parent 3d1db3519d
commit 02939d776b
4 changed files with 30 additions and 22 deletions

View File

@ -24,7 +24,7 @@
- wrap up memory management - wrap up memory management
- remove all compiler memory leaks - remove all compiler memory leaks
+ fix child <T> function calls + fix child <T> function calls
- enable vfmt + enable vfmt
+ bring back vdoc and regenerate all module docs + bring back vdoc and regenerate all module docs
+ optimize the parser + optimize the parser
- chat.vlang.io - chat.vlang.io
@ -36,7 +36,8 @@
- ui/orm demo: a simple gui client for postgres/mysql/sqlite - ui/orm demo: a simple gui client for postgres/mysql/sqlite
- ui demo: calculator - ui demo: calculator
- declarative ui with hot reload (similar to swiftui) - declarative ui with hot reload (similar to swiftui)
- "building a simple blog with vweb" tutorial + youtube video + "building a simple blog with vweb" tutorial +
- youtube video
+ fix interfaces + fix interfaces
+ fast.vlang.io + fast.vlang.io
+ bare metal support + bare metal support

View File

@ -35,6 +35,7 @@ fn (p mut Parser) comp_time() {
p.check(.lcbr) p.check(.lcbr)
os := os_from_string(name) os := os_from_string(name)
if ((!not && os != p.os) || (not && os == p.os)) && if ((!not && os != p.os) || (not && os == p.os)) &&
!p.scanner.is_fmt &&
!p.pref.output_cross_c !p.pref.output_cross_c
{ {
// `$if os {` for a different target, skip everything inside // `$if os {` for a different target, skip everything inside

View File

@ -37,6 +37,7 @@ fn (p mut Parser) in_optimization(typ string, ph int) {
} }
if p.tok != .rsbr { if p.tok != .rsbr {
p.check(.comma) p.check(.comma)
p.fspace()
} }
i++ i++
} }

View File

@ -160,7 +160,8 @@ fn (v mut V) new_parser_from_file(path string) Parser {
} }
mut p := v.new_parser(new_scanner_file(path)) mut p := v.new_parser(new_scanner_file(path))
p = { p = {
p|file_path:path, p |
file_path:path,
file_name:path.all_after(os.path_separator), file_name:path.all_after(os.path_separator),
file_platform:path_platform, file_platform:path_platform,
file_pcguard:path_pcguard, file_pcguard:path_pcguard,
@ -201,7 +202,8 @@ fn (v mut V) new_parser(scanner &Scanner) Parser {
import_table: new_import_table() import_table: new_import_table()
} }
$if js { $if js {
p.is_js=true} p.is_js = true
}
if p.pref.is_repl { if p.pref.is_repl {
p.scanner.should_print_line_on_error = false p.scanner.should_print_line_on_error = false
p.scanner.should_print_errors_in_color = false p.scanner.should_print_errors_in_color = false
@ -419,7 +421,7 @@ fn (p mut Parser) parse(pass Pass) {
} }
p.fgenln('\n') p.fgenln('\n')
p.builtin_mod = p.mod == 'builtin' p.builtin_mod = p.mod == 'builtin'
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}
@ -948,7 +950,7 @@ fn (p mut Parser) get_type() string {
p.fn_args(mut f) p.fn_args(mut f)
// Same line, it's a return type // Same line, it's a return type
if p.scanner.line_nr == line_nr { if p.scanner.line_nr == line_nr {
if p.tok in[.name,.mul,.amp,.lsbr,.question,.lpar] { if p.tok in [.name, .mul, .amp, .lsbr, .question, .lpar] {
f.typ = p.get_type() f.typ = p.get_type()
} }
else { else {
@ -1199,7 +1201,7 @@ fn (p mut Parser) free_var(v Var) {
// Clean up memory, only do this if -autofree was passed for now // Clean up memory, only do this if -autofree was passed for now
// if p.fileis('mem.v') {println('free_var() $v.name')} // if p.fileis('mem.v') {println('free_var() $v.name')}
// println(p.cur_fn.name) // println(p.cur_fn.name)
if p.cur_fn.name in['add','clone','free'] { if p.cur_fn.name in ['add', 'clone', 'free'] {
return return
} }
mut free_fn := 'free' mut free_fn := 'free'
@ -1494,7 +1496,7 @@ fn ($v.name mut $v.typ) ${p.cur_fn.name}(...) {
p.cgen.resetln('memcpy( (& $left), ($etype{$expr}), sizeof( $left ) );') p.cgen.resetln('memcpy( (& $left), ($etype{$expr}), sizeof( $left ) );')
} }
// check type for <<= >>= %= ^= &= |= // check type for <<= >>= %= ^= &= |=
else if tok in[.left_shift_assign,.righ_shift_assign,.mod_assign,.xor_assign,.and_assign,.or_assign] { else if tok in [.left_shift_assign, .righ_shift_assign, .mod_assign, .xor_assign, .and_assign, .or_assign] {
if !is_integer_type(p.assigned_type) { if !is_integer_type(p.assigned_type) {
p.error_with_token_index('cannot use ${tok.str()} assignment operator on non-integer type `$p.assigned_type`', errtok) p.error_with_token_index('cannot use ${tok.str()} assignment operator on non-integer type `$p.assigned_type`', errtok)
} }
@ -1628,7 +1630,7 @@ fn (p mut Parser) var_decl() {
// assigment // assigment
// if !p.builtin_mod && known_var { // if !p.builtin_mod && known_var {
if known_var { if known_var {
v := p.find_var(var_name) or{ v := p.find_var(var_name) or {
p.error_with_token_index('cannot find `$var_name`', var_token_idx) p.error_with_token_index('cannot find `$var_name`', var_token_idx)
break break
} }
@ -1677,7 +1679,7 @@ fn (p mut Parser) get_struct_type(name_ string,is_c bool,is_ptr bool) string {
} }
fn (p mut Parser) get_var_type(name string,is_ptr bool,deref_nr int) string { fn (p mut Parser) get_var_type(name string,is_ptr bool,deref_nr int) string {
v := p.find_var_check_new_var(name) or{ v := p.find_var_check_new_var(name) or {
return '' return ''
} }
if is_ptr { if is_ptr {
@ -1728,7 +1730,7 @@ fn (p mut Parser) get_var_type(name string,is_ptr bool,deref_nr int) string {
} }
fn (p mut Parser) get_const_type(name string,is_ptr bool) string { fn (p mut Parser) get_const_type(name string,is_ptr bool) string {
c := p.table.find_const(name) or{ c := p.table.find_const(name) or {
return '' return ''
} }
if is_ptr && !c.is_global { if is_ptr && !c.is_global {
@ -1757,7 +1759,7 @@ fn (p mut Parser) get_c_func_type(name string) string {
p.fn_call(mut f, 0, '', '') p.fn_call(mut f, 0, '', '')
p.is_c_fn_call = false p.is_c_fn_call = false
// C functions must be defined with `C.fn_name() fn_type` // C functions must be defined with `C.fn_name() fn_type`
cfn := p.table.find_fn(name) or{ cfn := p.table.find_fn(name) or {
// Is the user trying to do `var := C.foo()` or `bar(C.foo())` // Is the user trying to do `var := C.foo()` or `bar(C.foo())`
// without declaring `foo`? // without declaring `foo`?
// Do not allow it. // Do not allow it.
@ -1948,7 +1950,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)
} }
@ -1994,7 +1996,7 @@ pub:
return field.typ return field.typ
} }
// method // method
mut method := p.table.find_method(typ, field_name) or{ mut method := p.table.find_method(typ, field_name) or {
p.error_with_token_index('could not find method `$field_name`', fname_tidx) // should never happen p.error_with_token_index('could not find method `$field_name`', fname_tidx) // should never happen
exit(1) exit(1)
} }
@ -2204,7 +2206,7 @@ fn (p mut Parser) index_expr(typ_ string,fn_ph int) string {
// } // }
if is_indexer { if is_indexer {
l := p.cgen.cur_line.trim_space() l := p.cgen.cur_line.trim_space()
idx := l.last_index(' ') or{ idx := l.last_index(' ') or {
panic('idx') panic('idx')
} }
index_val := l[idx..].trim_space() index_val := l[idx..].trim_space()
@ -2285,6 +2287,7 @@ fn (p mut Parser) indot_expr() string {
if p.tok == .lsbr { if p.tok == .lsbr {
// a in [1,2,3] optimization => `a == 1 || a == 2 || a == 3` // a in [1,2,3] optimization => `a == 1 || a == 2 || a == 3`
// avoids an allocation // avoids an allocation
p.fspace()
p.in_optimization(typ, ph) p.in_optimization(typ, ph)
return 'bool' return 'bool'
} }
@ -2324,7 +2327,8 @@ fn (p mut Parser) assoc() string {
// println('assoc()') // println('assoc()')
p.next() p.next()
name := p.check_name() name := p.check_name()
var := p.find_var_or_const(name) or{ p.fspace()
var := p.find_var_or_const(name) or {
p.error('unknown variable `$name`') p.error('unknown variable `$name`')
exit(1) exit(1)
} }
@ -2332,13 +2336,14 @@ fn (p mut Parser) assoc() string {
p.mark_var_used(var) p.mark_var_used(var)
} }
p.check(.pipe) p.check(.pipe)
p.fgen_nl()
p.gen('($var.typ){') p.gen('($var.typ){')
typ := p.table.find_type(var.typ) typ := p.table.find_type(var.typ)
mut fields := []string // track the fields user is setting, the rest will be copied from the old object mut fields := []string // track the fields user is setting, the rest will be copied from the old object
for p.tok != .rcbr { for p.tok != .rcbr {
field := p.check_name() field := p.check_name()
// if !typ.has_field(field) { // if !typ.has_field(field) {
f := typ.find_field(field) or{ f := typ.find_field(field) or {
p.error('`$typ.name` has no field `$field`') p.error('`$typ.name` has no field `$field`')
exit(1) exit(1)
} }
@ -2456,7 +2461,7 @@ fn (p mut Parser) array_init() string {
if p.tok == .name && !p.inside_const { if p.tok == .name && !p.inside_const {
const_name := p.prepend_mod(p.lit) const_name := p.prepend_mod(p.lit)
if p.table.known_const(const_name) { if p.table.known_const(const_name) {
c := p.table.find_const(const_name) or{ c := p.table.find_const(const_name) or {
// p.error('unknown const `$p.lit`') // p.error('unknown const `$p.lit`')
exit(1) exit(1)
} }
@ -2765,7 +2770,7 @@ fn (p mut Parser) go_statement() {
if p.peek() == .dot { if p.peek() == .dot {
// Method // Method
var_name := p.lit var_name := p.lit
v := p.find_var(var_name) or{ v := p.find_var(var_name) or {
return return
} }
p.mark_var_used(v) p.mark_var_used(v)
@ -2773,7 +2778,7 @@ fn (p mut Parser) go_statement() {
p.next() p.next()
p.check(.dot) p.check(.dot)
typ := p.table.find_type(v.typ) typ := p.table.find_type(v.typ)
method := p.table.find_method(typ, p.lit) or{ method := p.table.find_method(typ, p.lit) or {
p.error_with_token_index('go method missing $var_name', gotoken_idx) p.error_with_token_index('go method missing $var_name', gotoken_idx)
return return
} }
@ -2782,7 +2787,7 @@ fn (p mut Parser) go_statement() {
else { else {
f_name := p.lit f_name := p.lit
// Normal function // Normal function
f := p.table.find_fn(p.prepend_mod(f_name)) or{ f := p.table.find_fn(p.prepend_mod(f_name)) or {
println(p.table.debug_fns()) println(p.table.debug_fns())
p.error_with_token_index('can not find function $f_name', gotoken_idx) p.error_with_token_index('can not find function $f_name', gotoken_idx)
return return
@ -2913,7 +2918,7 @@ fn (p mut Parser) defer_st() {
} }
fn (p mut Parser) check_and_register_used_imported_type(typ_name string) { fn (p mut Parser) check_and_register_used_imported_type(typ_name string) {
us_idx := typ_name.index('__') or{ us_idx := typ_name.index('__') or {
return return
} }
arg_mod := typ_name[..us_idx] arg_mod := typ_name[..us_idx]