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

fmt: use the new match syntax

This commit is contained in:
Alexander Medvednikov 2020-06-18 20:07:48 +02:00
parent 3b6e66db0d
commit d4b17fb0c3
2 changed files with 105 additions and 94 deletions

View File

@ -35,6 +35,7 @@ pub mut:
is_debug bool
mod2alias map[string]string // for `import time as t`, will contain: 'time'=>'t'
use_short_fn_args bool
it_name string // the name to replace `it` with
}
pub fn fmt(file ast.File, table &table.Table, is_debug bool) string {
@ -143,8 +144,8 @@ pub fn (mut f Fmt) imports(imports []ast.Import) {
pub fn (f Fmt) imp_stmt_str(imp ast.Import) string {
is_diff := imp.alias != imp.mod && !imp.mod.ends_with('.' + imp.alias)
imp_alias_suffix := if is_diff { ' as ${imp.alias}' } else { '' }
return '${imp.mod}${imp_alias_suffix}'
imp_alias_suffix := if is_diff { ' as $imp.alias' } else { '' }
return '$imp.mod$imp_alias_suffix'
}
pub fn (mut f Fmt) stmts(stmts []ast.Stmt) {
@ -161,7 +162,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
}
match node {
ast.AssignStmt {
for i, left in it.left {
for i, left in node.left {
if left is ast.Ident {
ident := left as ast.Ident
var_info := ident.var_info()
@ -169,19 +170,18 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
f.write('mut ')
}
f.expr(left)
if i < it.left.len - 1 {
if i < node.left.len - 1 {
f.write(', ')
}
}
else {
} else {
f.expr(left)
}
}
f.is_assign = true
f.write(' $it.op.str() ')
for i, val in it.right {
f.write(' $node.op.str() ')
for i, val in node.right {
f.expr(val)
if i < it.right.len - 1 {
if i < node.right.len - 1 {
f.write(', ')
}
}
@ -192,19 +192,19 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
}
ast.AssertStmt {
f.write('assert ')
f.expr(it.expr)
f.expr(node.expr)
f.writeln('')
}
ast.Attr {
f.writeln('[$it.name]')
f.writeln('[$node.name]')
}
ast.Block {
f.writeln('{')
f.stmts(it.stmts)
f.stmts(node.stmts)
f.writeln('}')
}
ast.BranchStmt {
match it.tok.kind {
match node.tok.kind {
.key_break { f.writeln('break') }
.key_continue { f.writeln('continue') }
else {}
@ -216,7 +216,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
ast.CompIf {
inversion := if it.is_not { '!' } else { '' }
is_opt := if it.is_opt { ' ?' } else { '' }
f.writeln('\$if ${inversion}${it.val}${is_opt} {')
f.writeln('\$if $inversion$it.val$is_opt {')
f.stmts(it.stmts)
if it.has_else {
f.writeln('} \$else {')
@ -378,20 +378,20 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
match node {
ast.AliasTypeDecl {
if it.is_pub {
if node.is_pub {
f.write('pub ')
}
ptype := f.type_to_str(it.parent_type)
f.write('type $it.name $ptype')
ptype := f.type_to_str(node.parent_type)
f.write('type $node.name $ptype')
}
ast.FnTypeDecl {
if it.is_pub {
if node.is_pub {
f.write('pub ')
}
typ_sym := f.table.get_type_symbol(it.typ)
typ_sym := f.table.get_type_symbol(node.typ)
fn_typ_info := typ_sym.info as table.FnType
fn_info := fn_typ_info.func
fn_name := it.name.replace(f.cur_mod + '.', '')
fn_name := node.name.replace(f.cur_mod + '.', '')
f.write('type $fn_name = fn (')
for i, arg in fn_info.args {
f.write(arg.name)
@ -424,12 +424,12 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) {
}
}
ast.SumTypeDecl {
if it.is_pub {
if node.is_pub {
f.write('pub ')
}
f.write('type $it.name = ')
f.write('type $node.name = ')
mut sum_type_names := []string{}
for t in it.sub_types {
for t in node.sub_types {
sum_type_names << f.type_to_str(t)
}
sum_type_names.sort()
@ -536,54 +536,54 @@ fn (f &Fmt) type_to_str(t table.Type) string {
pub fn (mut f Fmt) expr(node ast.Expr) {
if f.is_debug {
eprintln('expr: ${node.position():-42} | node: ${typeof(node):-20} | ${node.str()}')
eprintln('expr: ${node.position():-42} | node: ${typeof(node):-20} | $node.str()')
}
match node {
ast.AnonFn {
f.fn_decl(it.decl)
f.fn_decl(node.decl)
}
ast.ArrayInit {
f.array_init(it)
f.array_init(node)
}
ast.AsCast {
type_str := f.type_to_str(it.typ)
f.expr(it.expr)
type_str := f.type_to_str(node.typ)
f.expr(node.expr)
f.write(' as $type_str')
}
ast.Assoc {
f.writeln('{')
// f.indent++
f.writeln('\t$it.var_name |')
f.writeln('\t$node.var_name |')
// TODO StructInit copy pasta
for i, field in it.fields {
for i, field in node.fields {
f.write('\t$field: ')
f.expr(it.exprs[i])
f.expr(node.exprs[i])
f.writeln('')
}
// f.indent--
f.write('}')
}
ast.BoolLiteral {
f.write(it.val.str())
f.write(node.val.str())
}
ast.CastExpr {
f.write(f.type_to_str(it.typ) + '(')
f.expr(it.expr)
f.write(f.type_to_str(node.typ) + '(')
f.expr(node.expr)
f.write(')')
}
ast.CallExpr {
f.call_expr(it)
f.call_expr(node)
}
ast.CharLiteral {
f.write('`$it.val`')
f.write('`$node.val`')
}
ast.ComptimeCall {
if it.is_vweb {
if node.is_vweb {
f.write('$' + 'vweb.html()')
}
}
ast.ConcatExpr {
for i, val in it.vals {
for i, val in node.vals {
if i != 0 {
f.write(' + ')
}
@ -591,21 +591,26 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
}
ast.EnumVal {
name := f.short_module(it.enum_name)
f.write(name + '.' + it.val)
name := f.short_module(node.enum_name)
f.write(name + '.' + node.val)
}
ast.FloatLiteral {
f.write(it.val)
f.write(node.val)
}
ast.IfExpr {
f.if_expr(it)
f.if_expr(node)
}
ast.Ident {
f.write_language_prefix(it.language)
if it.kind == .blank_ident {
f.write_language_prefix(node.language)
if true {
} else {
}
if node.name == 'it' && f.it_name != '' {
f.write(f.it_name)
} else if node.kind == .blank_ident {
f.write('_')
} else {
name := f.short_module(it.name)
name := f.short_module(node.name)
// f.write('<$it.name => $name>')
f.write(name)
if name.contains('.') {
@ -614,34 +619,34 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
}
ast.IfGuardExpr {
f.write(it.var_name + ' := ')
f.expr(it.expr)
f.write(node.var_name + ' := ')
f.expr(node.expr)
}
ast.InfixExpr {
f.expr(it.left)
f.expr(node.left)
if f.is_inside_interp {
f.write('$it.op.str()')
f.write('$node.op.str()')
} else {
f.write(' $it.op.str() ')
f.write(' $node.op.str() ')
f.wrap_long_line()
}
f.expr(it.right)
f.expr(node.right)
}
ast.IndexExpr {
f.expr(it.left)
f.expr(node.left)
f.write('[')
f.expr(it.index)
f.expr(node.index)
f.write(']')
}
ast.IntegerLiteral {
f.write(it.val)
f.write(node.val)
}
ast.MapInit {
if it.keys.len == 0 {
mut ktyp := it.key_type
mut vtyp := it.value_type
if node.keys.len == 0 {
mut ktyp := node.key_type
mut vtyp := node.value_type
if vtyp == 0 {
typ_sym := f.table.get_type_symbol(it.typ)
typ_sym := f.table.get_type_symbol(node.typ)
minfo := typ_sym.info as table.Map
ktyp = minfo.key_type
vtyp = minfo.value_type
@ -655,18 +660,18 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
f.writeln('{')
f.indent++
for i, key in it.keys {
for i, key in node.keys {
f.expr(key)
// f.write(strings.repeat(` `, max - field.name.len))
f.write(': ')
f.expr(it.vals[i])
f.expr(node.vals[i])
f.writeln('')
}
f.indent--
f.write('}')
}
ast.MatchExpr {
f.match_expr(it)
f.match_expr(node)
}
ast.None {
f.write('none')
@ -678,51 +683,51 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
ast.ParExpr {
f.write('(')
f.expr(it.expr)
f.expr(node.expr)
f.write(')')
}
ast.PostfixExpr {
f.expr(it.expr)
f.write(it.op.str())
f.expr(node.expr)
f.write(node.op.str())
}
ast.PrefixExpr {
f.write(it.op.str())
f.expr(it.right)
f.write(node.op.str())
f.expr(node.right)
}
ast.RangeExpr {
f.expr(it.low)
f.expr(node.low)
f.write('..')
f.expr(it.high)
f.expr(node.high)
}
ast.SelectorExpr {
f.expr(it.expr)
f.expr(node.expr)
f.write('.')
f.write(it.field_name)
f.write(node.field_name)
}
ast.SizeOf {
f.write('sizeof(')
if it.type_name != '' {
f.write(it.type_name)
if node.type_name != '' {
f.write(node.type_name)
} else {
f.write(f.type_to_str(it.typ))
f.write(f.type_to_str(node.typ))
}
f.write(')')
}
ast.SqlExpr {}
ast.StringLiteral {
if it.is_raw {
if node.is_raw {
f.write('r')
}
if it.val.contains("'") && !it.val.contains('"') {
f.write('"$it.val"')
if node.val.contains("'") && !node.val.contains('"') {
f.write('"$node.val"')
} else {
f.write("'$it.val'")
f.write("'$node.val'")
}
}
ast.StringInterLiteral {
// TODO: this code is very similar to ast.Expr.str()
mut contains_single_quote := false
for val in it.vals {
for val in node.vals {
if val.contains("'") {
contains_single_quote = true
break
@ -734,20 +739,20 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write("'")
}
f.is_inside_interp = true
for i, val in it.vals {
for i, val in node.vals {
f.write(val)
if i >= it.exprs.len {
if i >= node.exprs.len {
break
}
f.write('$')
fspec_str, needs_braces := it.get_fspec_braces(i)
fspec_str, needs_braces := node.get_fspec_braces(i)
if needs_braces {
f.write('{')
f.expr(it.exprs[i])
f.expr(node.exprs[i])
f.write(fspec_str)
f.write('}')
} else {
f.expr(it.exprs[i])
f.expr(node.exprs[i])
}
}
f.is_inside_interp = false
@ -758,24 +763,24 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
}
}
ast.StructInit {
f.struct_init(it)
f.struct_init(node)
}
ast.Type {
f.write(f.type_to_str(it.typ))
f.write(f.type_to_str(node.typ))
}
ast.TypeOf {
f.write('typeof(')
f.expr(it.expr)
f.expr(node.expr)
f.write(')')
}
ast.Likely {
if it.is_likely {
if node.is_likely {
f.write('_likely_')
} else {
f.write('_unlikely_')
}
f.write('(')
f.expr(it.expr)
f.expr(node.expr)
f.write(')')
}
}
@ -889,7 +894,7 @@ pub fn (mut f Fmt) short_module(name string) string {
if aname == '' {
return symname
}
return '${aname}.${symname}'
return '${aname}.$symname'
}
pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
@ -984,7 +989,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
f.write_language_prefix(node.language)
name := f.short_module(node.name)
f.mark_module_as_used(name)
f.write('${name}')
f.write('$name')
if node.generic_type != 0 && node.generic_type != table.void_type {
f.write('<')
f.write(f.type_to_str(node.generic_type))
@ -1004,6 +1009,10 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
f.write('mut ')
}
f.expr(it.cond)
if it.cond is ast.Ident {
ident := it.cond as ast.Ident
f.it_name = ident.name
}
f.writeln(' {')
f.indent++
mut single_line := true
@ -1063,11 +1072,13 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
}
f.indent--
f.write('}')
f.it_name = ''
}
pub fn (mut f Fmt) remove_new_line() {
mut i := 0
for i = f.out.len - 1; i >= 0; i-- {
for i = f.out.len - 1; i >= 0; i--
{
if !f.out.buf[i].is_space() { // != `\n` {
break
}
@ -1127,7 +1138,7 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
for _ in 0 .. ainfo.nr_dims {
f.write('[]')
}
f.write('map[${mk}]${mv}')
f.write('map[$mk]$mv')
f.write('{')
if it.has_len {
f.write('len: ')

View File

@ -599,10 +599,10 @@ fn (mut g Gen) stmt(node ast.Stmt) {
g.gen_assert_stmt(it)
}
ast.AssignStmt {
g.gen_assign_stmt(it)
g.gen_assign_stmt(node)
}
ast.Attr {
g.attrs << it.name
g.attrs << node.name
g.writeln('// Attr: [$it.name]')
}
ast.Block {