fmt: use the new match syntax

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

View File

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