fmt: unwrap long single line ifs (#7608)
							parent
							
								
									e813583bc1
								
							
						
					
					
						commit
						06fdf34214
					
				|  | @ -208,7 +208,11 @@ fn handle_http_connection(mut con net.TcpConn, ctx &VdocHttpServerContext) { | |||
| 		return | ||||
| 	} | ||||
| 	urlpath := request_parts[1] | ||||
| 	filename := if urlpath == '/' { ctx.default_filename.trim_left('/') } else { urlpath.trim_left('/') } | ||||
| 	filename := if urlpath == '/' { | ||||
| 		ctx.default_filename.trim_left('/') | ||||
| 	} else { | ||||
| 		urlpath.trim_left('/') | ||||
| 	} | ||||
| 	if ctx.docs[filename].len == 0 { | ||||
| 		send_http_response(mut con, 404, ctx.content_type, 'file not found') | ||||
| 		return | ||||
|  | @ -379,8 +383,16 @@ fn doc_node_html(dd doc.DocNode, link string, head bool, tb &table.Table) string | |||
| 	md_content := markdown.to_html(dd.comment) | ||||
| 	hlighted_code := html_highlight(dd.content, tb) | ||||
| 	node_class := if dd.kind == .const_group { ' const' } else { '' } | ||||
| 	sym_name := if dd.parent_name.len > 0 && dd.parent_name != 'void' { '($dd.parent_name) $dd.name' } else { dd.name } | ||||
| 	tag := if dd.parent_name.len > 0 && dd.parent_name != 'void' { '${dd.parent_name}.$dd.name' } else { dd.name } | ||||
| 	sym_name := if dd.parent_name.len > 0 && dd.parent_name != 'void' { | ||||
| 		'($dd.parent_name) $dd.name' | ||||
| 	} else { | ||||
| 		dd.name | ||||
| 	} | ||||
| 	tag := if dd.parent_name.len > 0 && dd.parent_name != 'void' { | ||||
| 		'${dd.parent_name}.$dd.name' | ||||
| 	} else { | ||||
| 		dd.name | ||||
| 	} | ||||
| 	node_id := slug(tag) | ||||
| 	hash_link := if !head { ' <a href="#$node_id">#</a>' } else { '' } | ||||
| 	dnw.writeln('<section id="$node_id" class="doc-node$node_class">') | ||||
|  | @ -437,7 +449,11 @@ fn write_toc(dn doc.DocNode, nodes []doc.DocNode, mut toc strings.Builder) { | |||
| 
 | ||||
| fn (cfg DocConfig) write_content(cn &doc.DocNode, dcs &doc.Doc, mut hw strings.Builder) { | ||||
| 	base_dir := os.dir(os.real_path(cfg.input_path)) | ||||
| 	file_path_name := if cfg.is_multi { cn.file_path.replace('$base_dir/', '') } else { os.file_name(cn.file_path) } | ||||
| 	file_path_name := if cfg.is_multi { | ||||
| 		cn.file_path.replace('$base_dir/', '') | ||||
| 	} else { | ||||
| 		os.file_name(cn.file_path) | ||||
| 	} | ||||
| 	src_link := get_src_link(cfg.manifest.repo_url, file_path_name, cn.pos.line) | ||||
| 	if cn.content.len != 0 || (cn.name == 'Constants') { | ||||
| 		hw.write(doc_node_html(cn, src_link, false, dcs.table)) | ||||
|  | @ -463,7 +479,11 @@ fn (cfg DocConfig) gen_html(idx int) string { | |||
| 	} // write head
 | ||||
| 	// write css
 | ||||
| 	version := if cfg.manifest.version.len != 0 { cfg.manifest.version } else { '' } | ||||
| 	header_name := if cfg.is_multi && cfg.docs.len > 1 { os.file_name(os.real_path(cfg.input_path)) } else { dcs.head.name } | ||||
| 	header_name := if cfg.is_multi && cfg.docs.len > 1 { | ||||
| 		os.file_name(os.real_path(cfg.input_path)) | ||||
| 	} else { | ||||
| 		dcs.head.name | ||||
| 	} | ||||
| 	// write nav1
 | ||||
| 	if cfg.is_multi || cfg.docs.len > 1 { | ||||
| 		mut submod_prefix := '' | ||||
|  | @ -489,7 +509,11 @@ fn (cfg DocConfig) gen_html(idx int) string { | |||
| 					modules_toc.write('<ul>') | ||||
| 				} | ||||
| 				submod_name := cdoc.head.name.all_after(submod_prefix + '.') | ||||
| 				sub_selected_classes := if cdoc.head.name == dcs.head.name { ' class="active"' } else { '' } | ||||
| 				sub_selected_classes := if cdoc.head.name == dcs.head.name { | ||||
| 					' class="active"' | ||||
| 				} else { | ||||
| 					'' | ||||
| 				} | ||||
| 				modules_toc.write('<li$sub_selected_classes><a href="./${cdoc.head.name}.html">$submod_name</a></li>') | ||||
| 				if j == submodules.len - 1 { | ||||
| 					modules_toc.write('</ul>') | ||||
|  | @ -752,7 +776,11 @@ fn (mut cfg DocConfig) generate_docs_from_file() { | |||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	dirs := if cfg.is_multi { get_modules_list(cfg.input_path, []string{}) } else { [cfg.input_path] } | ||||
| 	dirs := if cfg.is_multi { | ||||
| 		get_modules_list(cfg.input_path, []string{}) | ||||
| 	} else { | ||||
| 		[cfg.input_path] | ||||
| 	} | ||||
| 	is_local_and_single := cfg.is_local && !cfg.is_multi | ||||
| 	for dirpath in dirs { | ||||
| 		mut dcs := doc.Doc{} | ||||
|  |  | |||
|  | @ -39,7 +39,11 @@ pub fn cp_all(src string, dst string, overwrite bool) ? { | |||
| 	} | ||||
| 	// single file copy
 | ||||
| 	if !is_dir(source_path) { | ||||
| 		adjusted_path := if is_dir(dest_path) { join_path(dest_path, file_name(source_path)) } else { dest_path } | ||||
| 		adjusted_path := if is_dir(dest_path) { | ||||
| 			join_path(dest_path, file_name(source_path)) | ||||
| 		} else { | ||||
| 			dest_path | ||||
| 		} | ||||
| 		if exists(adjusted_path) { | ||||
| 			if overwrite { | ||||
| 				rm(adjusted_path) | ||||
|  |  | |||
|  | @ -289,7 +289,11 @@ fn (b &Builder) print_warnings_and_errors() { | |||
| 	} | ||||
| 	if b.checker.nr_warnings > 0 && !b.pref.skip_warnings { | ||||
| 		for i, err in b.checker.warnings { | ||||
| 			kind := if b.pref.is_verbose { '$err.reporter warning #$b.checker.nr_warnings:' } else { 'warning:' } | ||||
| 			kind := if b.pref.is_verbose { | ||||
| 				'$err.reporter warning #$b.checker.nr_warnings:' | ||||
| 			} else { | ||||
| 				'warning:' | ||||
| 			} | ||||
| 			ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) | ||||
| 			eprintln(ferror) | ||||
| 			if err.details.len > 0 { | ||||
|  | @ -307,7 +311,11 @@ fn (b &Builder) print_warnings_and_errors() { | |||
| 	} | ||||
| 	if b.checker.nr_errors > 0 { | ||||
| 		for i, err in b.checker.errors { | ||||
| 			kind := if b.pref.is_verbose { '$err.reporter error #$b.checker.nr_errors:' } else { 'error:' } | ||||
| 			kind := if b.pref.is_verbose { | ||||
| 				'$err.reporter error #$b.checker.nr_errors:' | ||||
| 			} else { | ||||
| 				'error:' | ||||
| 			} | ||||
| 			ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) | ||||
| 			eprintln(ferror) | ||||
| 			if err.details.len > 0 { | ||||
|  |  | |||
|  | @ -778,7 +778,11 @@ fn (mut c Builder) cc_windows_cross() { | |||
| 	//
 | ||||
| 	cflags := c.get_os_cflags() | ||||
| 	// -I flags
 | ||||
| 	args += if c.pref.ccompiler == 'msvc' { cflags.c_options_before_target_msvc() } else { cflags.c_options_before_target() } | ||||
| 	args += if c.pref.ccompiler == 'msvc' { | ||||
| 		cflags.c_options_before_target_msvc() | ||||
| 	} else { | ||||
| 		cflags.c_options_before_target() | ||||
| 	} | ||||
| 	mut optimization_options := '' | ||||
| 	mut debug_options := '' | ||||
| 	if c.pref.is_prod { | ||||
|  | @ -804,7 +808,11 @@ fn (mut c Builder) cc_windows_cross() { | |||
| 	// add the thirdparty .o files, produced by all the #flag directives:
 | ||||
| 	args += ' ' + cflags.c_options_only_object_files() + ' ' | ||||
| 	args += ' $c.out_name_c ' | ||||
| 	args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() } | ||||
| 	args += if c.pref.ccompiler == 'msvc' { | ||||
| 		cflags.c_options_after_target_msvc() | ||||
| 	} else { | ||||
| 		cflags.c_options_after_target() | ||||
| 	} | ||||
| 	/* | ||||
| 	winroot := '${pref.default_module_path}/winroot' | ||||
| 	if !os.is_dir(winroot) { | ||||
|  |  | |||
|  | @ -163,7 +163,11 @@ fn find_vs(vswhere_dir string, host_arch string) ?VsInstallation { | |||
| fn find_msvc() ?MsvcResult { | ||||
| 	$if windows { | ||||
| 		processor_architecture := os.getenv('PROCESSOR_ARCHITECTURE') | ||||
| 		vswhere_dir := if processor_architecture == 'x86' { '%ProgramFiles%' } else { '%ProgramFiles(x86)%' } | ||||
| 		vswhere_dir := if processor_architecture == 'x86' { | ||||
| 			'%ProgramFiles%' | ||||
| 		} else { | ||||
| 			'%ProgramFiles(x86)%' | ||||
| 		} | ||||
| 		host_arch := if processor_architecture == 'x86' { 'X86' } else { 'X64' } | ||||
| 		wk := find_windows_kit_root(host_arch) or { return error('Unable to find windows sdk') } | ||||
| 		vs := find_vs(vswhere_dir, host_arch) or { return error('Unable to find visual studio') } | ||||
|  |  | |||
|  | @ -1239,7 +1239,11 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { | |||
| 		return call_expr.return_type | ||||
| 	} else if left_type_sym.kind == .array && method_name in ['insert', 'prepend'] { | ||||
| 		info := left_type_sym.info as table.Array | ||||
| 		arg_expr := if method_name == 'insert' { call_expr.args[1].expr } else { call_expr.args[0].expr } | ||||
| 		arg_expr := if method_name == 'insert' { | ||||
| 			call_expr.args[1].expr | ||||
| 		} else { | ||||
| 			call_expr.args[0].expr | ||||
| 		} | ||||
| 		arg_type := c.expr(arg_expr) | ||||
| 		arg_sym := c.table.get_type_symbol(arg_type) | ||||
| 		if !c.check_types(arg_type, info.elem_type) && !c.check_types(left_type, arg_type) { | ||||
|  | @ -1615,7 +1619,11 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { | |||
| 		} | ||||
| 	} | ||||
| 	for i, call_arg in call_expr.args { | ||||
| 		arg := if f.is_variadic && i >= f.params.len - 1 { f.params[f.params.len - 1] } else { f.params[i] } | ||||
| 		arg := if f.is_variadic && i >= f.params.len - 1 { | ||||
| 			f.params[f.params.len - 1] | ||||
| 		} else { | ||||
| 			f.params[i] | ||||
| 		} | ||||
| 		c.expected_type = arg.typ | ||||
| 		typ := c.expr(call_arg.expr) | ||||
| 		call_expr.args[i].typ = typ | ||||
|  | @ -2323,7 +2331,11 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { | |||
| 						right.position()) | ||||
| 				} else if right is ast.IntegerLiteral { | ||||
| 					if right.val == '1' { | ||||
| 						op := if assign_stmt.op == .plus_assign { token.Kind.inc } else { token.Kind.dec } | ||||
| 						op := if assign_stmt.op == .plus_assign { | ||||
| 							token.Kind.inc | ||||
| 						} else { | ||||
| 							token.Kind.dec | ||||
| 						} | ||||
| 						c.error('use `$op` instead of `$assign_stmt.op 1`', assign_stmt.pos) | ||||
| 					} | ||||
| 				} | ||||
|  | @ -2830,7 +2842,11 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { | |||
| 				node.pos) | ||||
| 		} | ||||
| 	} else if node.kind == 'pkgconfig' { | ||||
| 		args := if node.main.contains('--') { node.main.split(' ') } else { '--cflags --libs $node.main'.split(' ') } | ||||
| 		args := if node.main.contains('--') { | ||||
| 			node.main.split(' ') | ||||
| 		} else { | ||||
| 			'--cflags --libs $node.main'.split(' ') | ||||
| 		} | ||||
| 		mut m := pkgconfig.main(args) or { | ||||
| 			c.error(err, node.pos) | ||||
| 			return | ||||
|  | @ -3163,7 +3179,11 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type { | |||
| 	} | ||||
| 	if to_type_sym.kind == .sum_type { | ||||
| 		if node.expr_type in [table.any_int_type, table.any_flt_type] { | ||||
| 			node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.any_int_type { table.int_type } else { table.f64_type }) | ||||
| 			node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.any_int_type { | ||||
| 				table.int_type | ||||
| 			} else { | ||||
| 				table.f64_type | ||||
| 			}) | ||||
| 		} | ||||
| 		if !c.table.sumtype_has_variant(node.typ, node.expr_type) { | ||||
| 			c.error('cannot cast `$from_type_sym.name` to `$to_type_sym.name`', node.pos) | ||||
|  | @ -3835,7 +3855,11 @@ pub fn (mut c Checker) select_expr(mut node ast.SelectExpr) table.Type { | |||
| 							c.error('channel in `select` key must be predefined', expr.right.position()) | ||||
| 						} | ||||
| 						if expr.or_block.kind != .absent { | ||||
| 							err_prefix := if expr.or_block.kind == .block { 'or block' } else { 'error propagation' } | ||||
| 							err_prefix := if expr.or_block.kind == .block { | ||||
| 								'or block' | ||||
| 							} else { | ||||
| 								'error propagation' | ||||
| 							} | ||||
| 							c.error('$err_prefix not allowed in `select` key', expr.or_block.pos) | ||||
| 						} | ||||
| 					} | ||||
|  | @ -3942,7 +3966,11 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type { | |||
| 				if (infix.left is ast.Ident || | ||||
| 					infix.left is ast.SelectorExpr) && | ||||
| 					infix.right is ast.Type { | ||||
| 					is_variable := if mut infix.left is ast.Ident { infix.left.kind == .variable } else { true } | ||||
| 					is_variable := if mut infix.left is ast.Ident { | ||||
| 						infix.left.kind == .variable | ||||
| 					} else { | ||||
| 						true | ||||
| 					} | ||||
| 					// Register shadow variable or `as` variable with actual type
 | ||||
| 					if is_variable { | ||||
| 						// TODO: merge this code with match_expr because it has the same logic implemented
 | ||||
|  | @ -4378,11 +4406,19 @@ fn (mut c Checker) check_index_type(typ_sym &table.TypeSymbol, index_type table. | |||
| 	// index_type_sym.kind != .enum_) {
 | ||||
| 	if typ_sym.kind in [.array, .array_fixed, .string, .ustring] { | ||||
| 		if !(index_type.is_number() || index_type_sym.kind == .enum_) { | ||||
| 			type_str := if typ_sym.kind in [.string, .ustring] { 'non-integer string index `$index_type_sym.name`' } else { 'non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)' } | ||||
| 			type_str := if typ_sym.kind in [.string, .ustring] { | ||||
| 				'non-integer string index `$index_type_sym.name`' | ||||
| 			} else { | ||||
| 				'non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)' | ||||
| 			} | ||||
| 			c.error('$type_str', pos) | ||||
| 		} | ||||
| 		if index_type.has_flag(.optional) { | ||||
| 			type_str := if typ_sym.kind in [.string, .ustring] { '(type `$typ_sym.name`)' } else { '(array type `$typ_sym.name`)' } | ||||
| 			type_str := if typ_sym.kind in [.string, .ustring] { | ||||
| 				'(type `$typ_sym.name`)' | ||||
| 			} else { | ||||
| 				'(array type `$typ_sym.name`)' | ||||
| 			} | ||||
| 			c.error('cannot use optional as index $type_str', pos) | ||||
| 		} | ||||
| 	} | ||||
|  |  | |||
|  | @ -148,7 +148,11 @@ pub fn (mut d Doc) stmt(stmt ast.Stmt, filename string) ?DocNode { | |||
| 			node.parent_name = 'Constants' | ||||
| 			if d.extract_vars { | ||||
| 				for field in stmt.fields { | ||||
| 					ret_type := if field.typ == 0 { d.expr_typ_to_string(field.expr) } else { d.type_to_str(field.typ) } | ||||
| 					ret_type := if field.typ == 0 { | ||||
| 						d.expr_typ_to_string(field.expr) | ||||
| 					} else { | ||||
| 						d.type_to_str(field.typ) | ||||
| 					} | ||||
| 					node.children << DocNode{ | ||||
| 						name: field.name.all_after(d.orig_mod_name + '.') | ||||
| 						kind: .constant | ||||
|  | @ -180,7 +184,11 @@ pub fn (mut d Doc) stmt(stmt ast.Stmt, filename string) ?DocNode { | |||
| 			node.kind = .struct_ | ||||
| 			if d.extract_vars { | ||||
| 				for field in stmt.fields { | ||||
| 					ret_type := if field.typ == 0 && field.has_default_expr { d.expr_typ_to_string(field.default_expr) } else { d.type_to_str(field.typ) } | ||||
| 					ret_type := if field.typ == 0 && field.has_default_expr { | ||||
| 						d.expr_typ_to_string(field.default_expr) | ||||
| 					} else { | ||||
| 						d.type_to_str(field.typ) | ||||
| 					} | ||||
| 					node.children << DocNode{ | ||||
| 						name: field.name | ||||
| 						kind: .struct_field | ||||
|  | @ -287,7 +295,11 @@ pub fn (mut d Doc) file_ast(file_ast ast.File) map[string]DocNode { | |||
| 			continue | ||||
| 		} | ||||
| 		if node.parent_name !in contents { | ||||
| 			parent_node_kind := if node.parent_name == 'Constants' { SymbolKind.const_group } else { SymbolKind.typedef } | ||||
| 			parent_node_kind := if node.parent_name == 'Constants' { | ||||
| 				SymbolKind.const_group | ||||
| 			} else { | ||||
| 				SymbolKind.typedef | ||||
| 			} | ||||
| 			contents[node.parent_name] = DocNode{ | ||||
| 				name: node.parent_name | ||||
| 				kind: parent_node_kind | ||||
|  | @ -349,7 +361,11 @@ pub fn (mut d Doc) file_ast_with_pos(file_ast ast.File, pos int) map[string]DocN | |||
| // process based on a file path provided.
 | ||||
| pub fn (mut d Doc) generate() ? { | ||||
| 	// get all files
 | ||||
| 	d.base_path = if os.is_dir(d.base_path) { d.base_path } else { os.real_path(os.dir(d.base_path)) } | ||||
| 	d.base_path = if os.is_dir(d.base_path) { | ||||
| 		d.base_path | ||||
| 	} else { | ||||
| 		os.real_path(os.dir(d.base_path)) | ||||
| 	} | ||||
| 	d.is_vlib = 'vlib' !in d.base_path | ||||
| 	project_files := os.ls(d.base_path) or { return error_with_code(err, 0) } | ||||
| 	v_files := d.prefs.should_compile_filtered_files(d.base_path, project_files) | ||||
|  |  | |||
|  | @ -1446,39 +1446,51 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) { | |||
| 
 | ||||
| pub fn (mut f Fmt) if_expr(it ast.IfExpr) { | ||||
| 	dollar := if it.is_comptime { '$' } else { '' } | ||||
| 	single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0]) && | ||||
| 	mut single_line := it.branches.len == 2 && it.has_else && branch_is_single_line(it.branches[0]) && | ||||
| 		branch_is_single_line(it.branches[1]) && | ||||
| 		(it.is_expr || f.is_assign) | ||||
| 	f.single_line_if = single_line | ||||
| 	for i, branch in it.branches { | ||||
| 		if i == 0 { | ||||
| 			// first `if`
 | ||||
| 			f.comments(branch.comments, {}) | ||||
| 		} else { | ||||
| 			// `else`, close previous branch
 | ||||
| 			if branch.comments.len > 0 { | ||||
| 				f.writeln('}') | ||||
| 	if_start := f.line_len | ||||
| 	for { | ||||
| 		for i, branch in it.branches { | ||||
| 			if i == 0 { | ||||
| 				// first `if`
 | ||||
| 				f.comments(branch.comments, {}) | ||||
| 			} else { | ||||
| 				f.write('} ') | ||||
| 				// `else`, close previous branch
 | ||||
| 				if branch.comments.len > 0 { | ||||
| 					f.writeln('}') | ||||
| 					f.comments(branch.comments, {}) | ||||
| 				} else { | ||||
| 					f.write('} ') | ||||
| 				} | ||||
| 				f.write('${dollar}else ') | ||||
| 			} | ||||
| 			if i < it.branches.len - 1 || !it.has_else { | ||||
| 				f.write('${dollar}if ') | ||||
| 				f.expr(branch.cond) | ||||
| 				f.write(' ') | ||||
| 			} | ||||
| 			f.write('{') | ||||
| 			if single_line { | ||||
| 				f.write(' ') | ||||
| 			} else { | ||||
| 				f.writeln('') | ||||
| 			} | ||||
| 			f.stmts(branch.stmts) | ||||
| 			if single_line { | ||||
| 				f.write(' ') | ||||
| 			} | ||||
| 			f.write('${dollar}else ') | ||||
| 		} | ||||
| 		if i < it.branches.len - 1 || !it.has_else { | ||||
| 			f.write('${dollar}if ') | ||||
| 			f.expr(branch.cond) | ||||
| 			f.write(' ') | ||||
| 		} | ||||
| 		f.write('{') | ||||
| 		if single_line { | ||||
| 			f.write(' ') | ||||
| 		} else { | ||||
| 			f.writeln('') | ||||
| 		} | ||||
| 		f.stmts(branch.stmts) | ||||
| 		if single_line { | ||||
| 			f.write(' ') | ||||
| 		// When a single line if is really long, write it again as multiline
 | ||||
| 		if single_line && f.line_len > max_len.last() { | ||||
| 			single_line = false | ||||
| 			f.single_line_if = false | ||||
| 			f.out.go_back(f.line_len - if_start) | ||||
| 			f.line_len = if_start | ||||
| 			continue | ||||
| 		} | ||||
| 		break | ||||
| 	} | ||||
| 	f.write('}') | ||||
| 	f.single_line_if = false | ||||
|  | @ -1740,7 +1752,11 @@ pub fn (mut f Fmt) chan_init(mut it ast.ChanInit) { | |||
| 		it.elem_type = info.elem_type | ||||
| 	} | ||||
| 	is_mut := info.is_mut | ||||
| 	el_typ := if is_mut { it.elem_type.set_nr_muls(it.elem_type.nr_muls() - 1) } else { it.elem_type } | ||||
| 	el_typ := if is_mut { | ||||
| 		it.elem_type.set_nr_muls(it.elem_type.nr_muls() - 1) | ||||
| 	} else { | ||||
| 		it.elem_type | ||||
| 	} | ||||
| 	f.write('chan ') | ||||
| 	if is_mut { | ||||
| 		f.write('mut ') | ||||
|  |  | |||
|  | @ -1,5 +1,9 @@ | |||
| fn main() { | ||||
| 	a := if foo { 'TMP1/${b.nexpected_steps:1d}' } else { '${b.cstep:1d}/${b.nexpected_steps:1d}' } | ||||
| 	sprogress := if b.no_cstep { | ||||
| 		'TMP1/${b.nexpected_steps:1d}' | ||||
| 	} else { | ||||
| 		'${b.cstep:1d}/${b.nexpected_steps:1d}' | ||||
| 	} | ||||
| 	b := if bar { | ||||
| 		// comment
 | ||||
| 		'some str' | ||||
|  |  | |||
|  | @ -419,7 +419,11 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st | |||
| 				sym := g.table.get_type_symbol(field.typ) | ||||
| 				has_custom_str := sym.has_method('str') | ||||
| 				mut field_styp := g.typ(field.typ).replace('*', '') | ||||
| 				field_styp_fn_name := if has_custom_str { '${field_styp}_str' } else { fnames2strfunc[field_styp] } | ||||
| 				field_styp_fn_name := if has_custom_str { | ||||
| 					'${field_styp}_str' | ||||
| 				} else { | ||||
| 					fnames2strfunc[field_styp] | ||||
| 				} | ||||
| 				g.auto_str_funcs.write('indents, ') | ||||
| 				func := struct_auto_str_func(sym, field.typ, field_styp_fn_name, field.name) | ||||
| 				// reference types can be "nil"
 | ||||
|  | @ -529,7 +533,11 @@ fn (mut g Gen) gen_str_for_union_sum_type(info table.SumType, styp string, str_f | |||
| 			value_fmt = "'$value_fmt'" | ||||
| 		} | ||||
| 		typ_str := g.typ(typ) | ||||
| 		mut func_name := if typ_str in gen_fn_names { gen_fn_names[typ_str] } else { g.gen_str_for_type(typ) } | ||||
| 		mut func_name := if typ_str in gen_fn_names { | ||||
| 			gen_fn_names[typ_str] | ||||
| 		} else { | ||||
| 			g.gen_str_for_type(typ) | ||||
| 		} | ||||
| 		sym := g.table.get_type_symbol(typ) | ||||
| 		if sym.kind == .struct_ { | ||||
| 			func_name = 'indent_$func_name' | ||||
|  |  | |||
|  | @ -562,7 +562,11 @@ fn (g &Gen) cc_type2(t table.Type) string { | |||
| 		if sym.info.generic_types.len > 0 { | ||||
| 			mut sgtyps := '_T' | ||||
| 			for gt in sym.info.generic_types { | ||||
| 				gts := g.table.get_type_symbol(if gt.has_flag(.generic) { g.unwrap_generic(gt) } else { gt }) | ||||
| 				gts := g.table.get_type_symbol(if gt.has_flag(.generic) { | ||||
| 					g.unwrap_generic(gt) | ||||
| 				} else { | ||||
| 					gt | ||||
| 				}) | ||||
| 				sgtyps += '_$gts.cname' | ||||
| 			} | ||||
| 			styp += sgtyps | ||||
|  | @ -929,7 +933,11 @@ fn (mut g Gen) stmt(node ast.Stmt) { | |||
| 					g.enum_typedefs.write((1 << i).str()) | ||||
| 					cur_enum_offset = 0 | ||||
| 				} | ||||
| 				cur_value := if cur_enum_offset > 0 { '$cur_enum_expr+$cur_enum_offset' } else { cur_enum_expr } | ||||
| 				cur_value := if cur_enum_offset > 0 { | ||||
| 					'$cur_enum_expr+$cur_enum_offset' | ||||
| 				} else { | ||||
| 					cur_enum_expr | ||||
| 				} | ||||
| 				g.enum_typedefs.writeln(', // $cur_value') | ||||
| 				cur_enum_offset++ | ||||
| 			} | ||||
|  | @ -1237,7 +1245,11 @@ fn (mut g Gen) for_in(it ast.ForInStmt) { | |||
| 				// instead of
 | ||||
| 				// `int* val = ((int**)arr.data)[i];`
 | ||||
| 				// right := if it.val_is_mut { styp } else { styp + '*' }
 | ||||
| 				right := if it.val_is_mut { '(($styp)$tmp${op_field}data) + $i' } else { '(($styp*)$tmp${op_field}data)[$i]' } | ||||
| 				right := if it.val_is_mut { | ||||
| 					'(($styp)$tmp${op_field}data) + $i' | ||||
| 				} else { | ||||
| 					'(($styp*)$tmp${op_field}data)[$i]' | ||||
| 				} | ||||
| 				g.writeln('\t$styp ${c_name(it.val_var)} = $right;') | ||||
| 			} | ||||
| 		} | ||||
|  | @ -2835,13 +2847,21 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { | |||
| 	} | ||||
| 	left_type := g.unwrap_generic(node.left_type) | ||||
| 	left_sym := g.table.get_type_symbol(left_type) | ||||
| 	unaliased_left := if left_sym.kind == .alias { (left_sym.info as table.Alias).parent_type } else { left_type } | ||||
| 	unaliased_left := if left_sym.kind == .alias { | ||||
| 		(left_sym.info as table.Alias).parent_type | ||||
| 	} else { | ||||
| 		left_type | ||||
| 	} | ||||
| 	if node.op in [.key_is, .not_is] { | ||||
| 		g.is_expr(node) | ||||
| 		return | ||||
| 	} | ||||
| 	right_sym := g.table.get_type_symbol(node.right_type) | ||||
| 	unaliased_right := if right_sym.kind == .alias { (right_sym.info as table.Alias).parent_type } else { node.right_type } | ||||
| 	unaliased_right := if right_sym.kind == .alias { | ||||
| 		(right_sym.info as table.Alias).parent_type | ||||
| 	} else { | ||||
| 		node.right_type | ||||
| 	} | ||||
| 	if unaliased_left == table.ustring_type_idx && node.op != .key_in && node.op != .not_in { | ||||
| 		fn_name := match node.op { | ||||
| 			.plus { | ||||
|  |  | |||
|  | @ -106,7 +106,11 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl, skip bool) { | |||
| 				g.definitions.write('VV_LOCAL_SYMBOL ') | ||||
| 			} | ||||
| 		} | ||||
| 		fn_header := if msvc_attrs.len > 0 { '$type_name $msvc_attrs ${name}(' } else { '$type_name ${name}(' } | ||||
| 		fn_header := if msvc_attrs.len > 0 { | ||||
| 			'$type_name $msvc_attrs ${name}(' | ||||
| 		} else { | ||||
| 			'$type_name ${name}(' | ||||
| 		} | ||||
| 		g.definitions.write(fn_header) | ||||
| 		g.write(fn_header) | ||||
| 	} | ||||
|  | @ -874,7 +878,11 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type table.Type) { | |||
| 				g.checker_bug('ref_or_deref_arg arg.typ is 0', arg.pos) | ||||
| 			} | ||||
| 			arg_typ_sym := g.table.get_type_symbol(arg.typ) | ||||
| 			expected_deref_type := if expected_type.is_ptr() { expected_type.deref() } else { expected_type } | ||||
| 			expected_deref_type := if expected_type.is_ptr() { | ||||
| 				expected_type.deref() | ||||
| 			} else { | ||||
| 				expected_type | ||||
| 			} | ||||
| 			is_sum_type := g.table.get_type_symbol(expected_deref_type).kind == .sum_type | ||||
| 			if !((arg_typ_sym.kind == .function) || is_sum_type) { | ||||
| 				g.write('(voidptr)&/*qq*/') | ||||
|  |  | |||
|  | @ -41,7 +41,11 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp | |||
| 		// In case of `foo<T>()`
 | ||||
| 		// T is unwrapped and registered in the checker.
 | ||||
| 		if !generic_type.has_flag(.generic) { | ||||
| 			full_generic_fn_name := if fn_name.contains('.') { fn_name } else { p.prepend_mod(fn_name) } | ||||
| 			full_generic_fn_name := if fn_name.contains('.') { | ||||
| 				fn_name | ||||
| 			} else { | ||||
| 				p.prepend_mod(fn_name) | ||||
| 			} | ||||
| 			p.table.register_fn_gen_type(full_generic_fn_name, generic_type) | ||||
| 		} | ||||
| 	} | ||||
|  | @ -467,7 +471,11 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { | |||
| 	mut args := []table.Param{} | ||||
| 	mut is_variadic := false | ||||
| 	// `int, int, string` (no names, just types)
 | ||||
| 	argname := if p.tok.kind == .name && p.tok.lit.len > 0 && p.tok.lit[0].is_capital() { p.prepend_mod(p.tok.lit) } else { p.tok.lit } | ||||
| 	argname := if p.tok.kind == .name && p.tok.lit.len > 0 && p.tok.lit[0].is_capital() { | ||||
| 		p.prepend_mod(p.tok.lit) | ||||
| 	} else { | ||||
| 		p.tok.lit | ||||
| 	} | ||||
| 	types_only := p.tok.kind in [.amp, .ellipsis, .key_fn] || | ||||
| 		(p.peek_tok.kind == .comma && p.table.known_type(argname)) || p.peek_tok.kind == .rpar | ||||
| 	// TODO copy pasta, merge 2 branches
 | ||||
|  |  | |||
|  | @ -28,7 +28,11 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { | |||
| 	mut prev_guard := false | ||||
| 	for p.tok.kind in [.key_if, .key_else] { | ||||
| 		p.inside_if = true | ||||
| 		start_pos := if is_comptime { p.prev_tok.position().extend(p.tok.position()) } else { p.tok.position() } | ||||
| 		start_pos := if is_comptime { | ||||
| 			p.prev_tok.position().extend(p.tok.position()) | ||||
| 		} else { | ||||
| 			p.tok.position() | ||||
| 		} | ||||
| 		if p.tok.kind == .key_else { | ||||
| 			comments << p.eat_comments() | ||||
| 			p.check(.key_else) | ||||
|  |  | |||
|  | @ -1167,7 +1167,11 @@ pub fn (mut p Parser) name_expr() ast.Expr { | |||
| 		p.check(.dot) | ||||
| 		p.expr_mod = mod | ||||
| 	} | ||||
| 	lit0_is_capital := if p.tok.kind != .eof && p.tok.lit.len > 0 { p.tok.lit[0].is_capital() } else { false } | ||||
| 	lit0_is_capital := if p.tok.kind != .eof && p.tok.lit.len > 0 { | ||||
| 		p.tok.lit[0].is_capital() | ||||
| 	} else { | ||||
| 		false | ||||
| 	} | ||||
| 	// use heuristics to detect `func<T>()` from `var < expr`
 | ||||
| 	is_generic_call := !lit0_is_capital && p.peek_tok.kind == .lt && (match p.peek_tok2.kind { | ||||
| 		.name { | ||||
|  |  | |||
|  | @ -383,7 +383,11 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr { | |||
| 	// p.warn('unsafe')
 | ||||
| 	// }
 | ||||
| 	p.next() | ||||
| 	mut right := if op == .minus { p.expr(token.Precedence.call) } else { p.expr(token.Precedence.prefix) } | ||||
| 	mut right := if op == .minus { | ||||
| 		p.expr(token.Precedence.call) | ||||
| 	} else { | ||||
| 		p.expr(token.Precedence.prefix) | ||||
| 	} | ||||
| 	p.is_amp = false | ||||
| 	if mut right is ast.CastExpr { | ||||
| 		right.in_prexpr = true | ||||
|  |  | |||
|  | @ -596,7 +596,11 @@ pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int { | |||
| 
 | ||||
| pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, has_decl bool) int { | ||||
| 	name := if f.name.len == 0 { 'fn ${t.fn_type_source_signature(f)}' } else { f.name.clone() } | ||||
| 	cname := if f.name.len == 0 { 'anon_fn_${t.fn_type_signature(f)}' } else { util.no_dots(f.name.clone()) } | ||||
| 	cname := if f.name.len == 0 { | ||||
| 		'anon_fn_${t.fn_type_signature(f)}' | ||||
| 	} else { | ||||
| 		util.no_dots(f.name.clone()) | ||||
| 	} | ||||
| 	anon := f.name.len == 0 || is_anon | ||||
| 	// existing
 | ||||
| 	existing_idx := t.type_idxs[name] | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue