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

vfmt: fix mod.Type; run vfmt on ast.v

This commit is contained in:
Alexander Medvednikov 2020-04-09 15:33:46 +02:00
parent f3c917e0aa
commit f508955b64
5 changed files with 73 additions and 96 deletions

View File

@ -10,16 +10,9 @@ import (
pub type TypeDecl = AliasTypeDecl | SumTypeDecl | FnTypeDecl
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr |
PostfixExpr | AssignExpr | PrefixExpr | IndexExpr | RangeExpr | MatchExpr | CastExpr |
EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | ConcatExpr |
Type | AsCast | TypeOf | StringInterLiteral
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | AssignExpr | PrefixExpr | IndexExpr | RangeExpr | MatchExpr | CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | ConcatExpr | Type | AsCast | TypeOf | StringInterLiteral
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
Comment | AssertStmt | UnsafeStmt | GoStmt | Block | InterfaceDecl
pub type Stmt = GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | Comment | AssertStmt | UnsafeStmt | GoStmt | Block | InterfaceDecl
pub type ScopeObject = ConstField | GlobalDecl | Var
@ -54,7 +47,6 @@ pub:
pub struct FloatLiteral {
pub:
// val f64
val string
}
@ -116,11 +108,9 @@ mut:
pub struct Field {
pub:
name string
// type_idx int
pos token.Position
mut:
typ table.Type
// typ2 Type
}
pub struct ConstField {
@ -176,7 +166,6 @@ pub:
pos token.Position
mod string
alias string
// expr Expr
}
pub struct FnDecl {
@ -203,7 +192,6 @@ pub:
pub struct CallExpr {
pub:
// tok token.Token
pos token.Position
left Expr // `user` in `user.register()`
is_method bool
@ -214,7 +202,6 @@ mut:
expected_arg_types []table.Type
is_c bool
or_block OrExpr
// has_or_block bool
left_type table.Type // type of `user`
receiver_type table.Type // User
return_type table.Type
@ -243,15 +230,12 @@ pub enum Expr {
Integer(IntegerExpr)
}
*/
/*
pub struct Stmt {
pos int
//end int
}
*/
pub struct Var {
pub:
name string
@ -278,9 +262,7 @@ pub:
imports []Import
stmts []Stmt
scope &Scope
// TODO: consider parent instead of field
global_scope &Scope
//comments []Comment
}
pub struct IdentFn {
@ -335,7 +317,6 @@ pub fn (i &Ident) var_info() IdentVar {
pub struct InfixExpr {
pub:
// op BinaryOp
op token.Kind
pos token.Position
left Expr
@ -361,7 +342,6 @@ pub:
pub struct IndexExpr {
pub:
// op token.Kind
pos token.Position
left Expr
index Expr // [0], [start..end] etc
@ -415,7 +395,6 @@ pub:
pub struct CompIf {
pub:
// cond Expr
val string
stmts []Stmt
is_not bool
@ -454,7 +433,6 @@ pub:
init Stmt // i := 0;
has_init bool
cond Expr // i < 10;
// inc Stmt // i++;
inc Expr // i++;
stmts []Stmt
pos token.Position
@ -545,13 +523,13 @@ pub:
typ table.Type
}
// TODO: handle this differently
// v1 excludes non current os ifdefs so
// the defer's never get added in the first place
pub struct DeferStmt {
pub:
stmts []Stmt
mut:
// TODO: handle this differently
// v1 excludes non current os ifdefs so
// the defer's never get added in the first place
ifdef string
}
@ -652,8 +630,6 @@ pub struct OrExpr {
pub:
stmts []Stmt
is_used bool // if the or{} block is written down or left out
// var_name string
// expr Expr
}
pub struct Assoc {
@ -685,7 +661,6 @@ pub:
is_multi bool
line_nr int
pos token.Position
//same_line bool
}
pub struct ConcatExpr {
@ -717,6 +692,7 @@ pub fn expr_is_call(expr Expr) bool {
true
}
else {
false}
false
}
}
}

View File

@ -35,7 +35,7 @@ mut:
}
pub fn new_checker(table &table.Table, pref &pref.Preferences) Checker {
return checker.Checker{
return Checker{
table: table
pref: pref
}

View File

@ -29,7 +29,7 @@ mut:
}
pub fn fmt(file ast.File, table &table.Table) string {
mut f := fmt.Fmt{
mut f := Fmt{
out: strings.new_builder(1000)
table: table
indent: 0
@ -206,7 +206,8 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
if it.is_pub {
f.write('pub ')
}
f.writeln('enum $it.name {')
name := it.name.after('.')
f.writeln('enum $name {')
for val in it.vals {
f.writeln('\t' + val)
}
@ -277,7 +278,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
}
ast.GlobalDecl {
f.write('__global $it.name ')
f.write(f.table.type_to_str(it.typ))
f.write(f.type_to_str(it.typ))
if it.has_expr {
f.write(' = ')
f.expr(it.expr)
@ -343,7 +344,7 @@ fn (f mut Fmt) type_decl(node ast.TypeDecl) {
if it.is_pub {
f.write('pub ')
}
ptype := f.table.type_to_str(it.parent_type)
ptype := f.type_to_str(it.parent_type)
f.write('type $it.name $ptype')
}
ast.SumTypeDecl {
@ -353,7 +354,7 @@ fn (f mut Fmt) type_decl(node ast.TypeDecl) {
f.write('type $it.name = ')
mut sum_type_names := []string
for t in it.sub_types {
sum_type_names << f.table.type_to_str(t)
sum_type_names << f.type_to_str(t)
}
f.write(sum_type_names.join(' | '))
}
@ -407,7 +408,7 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
f.writeln('}\n')
}
fn (f Fmt) type_to_str(t table.Type) string {
fn (f &Fmt) type_to_str(t table.Type) string {
res := f.table.type_to_str(t)
return res.replace(f.cur_mod + '.', '')
}
@ -417,7 +418,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.ArrayInit {
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
// `x := []string`
f.write(f.table.type_to_str(it.typ))
f.write(f.type_to_str(it.typ))
} else {
// `[1,2,3]`
// type_sym := f.table.get_type_symbol(it.typ)
@ -435,7 +436,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
}
ast.AsCast {
type_str := f.table.type_to_str(it.typ)
type_str := f.type_to_str(it.typ)
f.expr(it.expr)
f.write(' as $type_str')
}
@ -461,7 +462,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.val.str())
}
ast.CastExpr {
f.write(f.table.type_to_str(it.typ) + '(')
f.write(f.type_to_str(it.typ) + '(')
f.expr(it.expr)
f.write(')')
}
@ -599,7 +600,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
if it.type_name != '' {
f.writeln(it.type_name)
} else {
f.writeln(f.table.type_to_str(it.typ))
f.writeln(f.type_to_str(it.typ))
}
f.writeln(')')
}
@ -631,7 +632,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ)
name := short_module(type_sym.name)
name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str?
// `Foo{}` on one line if there are no fields
if it.fields.len == 0 {
f.write('$name{}')

View File

@ -71,7 +71,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
} else {
}
// println('start cgen2')
mut g := gen.Gen{
mut g := Gen{
out: strings.new_builder(1000)
typedefs: strings.new_builder(100)
typedefs2: strings.new_builder(100)

View File

@ -42,7 +42,7 @@ mut:
// for tests
pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
s := scanner.new_scanner(text, .skip_comments)
mut p := parser.Parser{
mut p := Parser{
scanner: s
table: table
pref: &pref.Preferences{}
@ -63,7 +63,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment
// panic(err)
// }
mut stmts := []ast.Stmt
mut p := parser.Parser{
mut p := Parser{
scanner: scanner.new_scanner_file(path, comments_mode)
table: table
file_name: path