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

pull/4321/head
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 TypeDecl = AliasTypeDecl | SumTypeDecl | FnTypeDecl
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | 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
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 | 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
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 pub type ScopeObject = ConstField | GlobalDecl | Var
@ -54,7 +47,6 @@ pub:
pub struct FloatLiteral { pub struct FloatLiteral {
pub: pub:
// val f64
val string val string
} }
@ -105,22 +97,20 @@ pub:
pub struct StructField { pub struct StructField {
pub: pub:
name string name string
pos token.Position pos token.Position
comment Comment comment Comment
default_expr string // token literal //Expr default_expr string // token literal //Expr
mut: mut:
typ table.Type typ table.Type
} }
pub struct Field { pub struct Field {
pub: pub:
name string name string
// type_idx int pos token.Position
pos token.Position
mut: mut:
typ table.Type typ table.Type
// typ2 Type
} }
pub struct ConstField { pub struct ConstField {
@ -142,15 +132,15 @@ pub:
pub struct StructDecl { pub struct StructDecl {
pub: pub:
pos token.Position pos token.Position
name string name string
fields []StructField fields []StructField
is_pub bool is_pub bool
mut_pos int // mut: mut_pos int // mut:
pub_pos int // pub: pub_pos int // pub:
pub_mut_pos int // pub mut: pub_mut_pos int // pub mut:
is_c bool is_c bool
is_union bool is_union bool
} }
pub struct InterfaceDecl { pub struct InterfaceDecl {
@ -176,7 +166,6 @@ pub:
pos token.Position pos token.Position
mod string mod string
alias string alias string
// expr Expr
} }
pub struct FnDecl { pub struct FnDecl {
@ -203,21 +192,19 @@ pub:
pub struct CallExpr { pub struct CallExpr {
pub: pub:
// tok token.Token pos token.Position
pos token.Position left Expr // `user` in `user.register()`
left Expr // `user` in `user.register()` is_method bool
is_method bool mod string
mod string
mut: mut:
name string name string
args []CallArg args []CallArg
expected_arg_types []table.Type expected_arg_types []table.Type
is_c bool is_c bool
or_block OrExpr or_block OrExpr
// has_or_block bool left_type table.Type // type of `user`
left_type table.Type // type of `user` receiver_type table.Type // User
receiver_type table.Type // User return_type table.Type
return_type table.Type
} }
pub struct CallArg { pub struct CallArg {
@ -243,15 +230,12 @@ pub enum Expr {
Integer(IntegerExpr) Integer(IntegerExpr)
} }
*/ */
/* /*
pub struct Stmt { pub struct Stmt {
pos int pos int
//end int //end int
} }
*/ */
pub struct Var { pub struct Var {
pub: pub:
name string name string
@ -264,23 +248,21 @@ mut:
pub struct GlobalDecl { pub struct GlobalDecl {
pub: pub:
name string name string
expr Expr expr Expr
has_expr bool has_expr bool
mut: mut:
typ table.Type typ table.Type
} }
pub struct File { pub struct File {
pub: pub:
path string path string
mod Module mod Module
imports []Import imports []Import
stmts []Stmt stmts []Stmt
scope &Scope scope &Scope
// TODO: consider parent instead of field
global_scope &Scope global_scope &Scope
//comments []Comment
} }
pub struct IdentFn { pub struct IdentFn {
@ -335,7 +317,6 @@ pub fn (i &Ident) var_info() IdentVar {
pub struct InfixExpr { pub struct InfixExpr {
pub: pub:
// op BinaryOp
op token.Kind op token.Kind
pos token.Position pos token.Position
left Expr left Expr
@ -361,13 +342,12 @@ pub:
pub struct IndexExpr { pub struct IndexExpr {
pub: pub:
// op token.Kind pos token.Position
pos token.Position left Expr
left Expr index Expr // [0], [start..end] etc
index Expr // [0], [start..end] etc
mut: mut:
left_type table.Type // array, map, fixed array left_type table.Type // array, map, fixed array
is_setter bool is_setter bool
} }
pub struct IfExpr { pub struct IfExpr {
@ -384,9 +364,9 @@ mut:
pub struct IfBranch { pub struct IfBranch {
pub: pub:
cond Expr cond Expr
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
comment Comment comment Comment
} }
@ -396,7 +376,7 @@ pub:
cond Expr cond Expr
branches []MatchBranch branches []MatchBranch
pos token.Position pos token.Position
is_mut bool // `match mut ast_node {` is_mut bool // `match mut ast_node {`
mut: mut:
is_expr bool // returns a value is_expr bool // returns a value
return_type table.Type return_type table.Type
@ -407,15 +387,14 @@ mut:
pub struct MatchBranch { pub struct MatchBranch {
pub: pub:
exprs []Expr exprs []Expr
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
comment Comment // comment above `xxx {` comment Comment // comment above `xxx {`
} }
pub struct CompIf { pub struct CompIf {
pub: pub:
// cond Expr
val string val string
stmts []Stmt stmts []Stmt
is_not bool is_not bool
@ -454,7 +433,6 @@ pub:
init Stmt // i := 0; init Stmt // i := 0;
has_init bool has_init bool
cond Expr // i < 10; cond Expr // i < 10;
// inc Stmt // i++;
inc Expr // i++; inc Expr // i++;
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
@ -545,13 +523,13 @@ pub:
typ table.Type 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 struct DeferStmt {
pub: pub:
stmts []Stmt stmts []Stmt
mut: mut:
// TODO: handle this differently
// v1 excludes non current os ifdefs so
// the defer's never get added in the first place
ifdef string ifdef string
} }
@ -650,10 +628,8 @@ mut:
// `or { ... }` // `or { ... }`
pub struct OrExpr { pub struct OrExpr {
pub: pub:
stmts []Stmt stmts []Stmt
is_used bool // if the or{} block is written down or left out is_used bool // if the or{} block is written down or left out
// var_name string
// expr Expr
} }
pub struct Assoc { pub struct Assoc {
@ -681,11 +657,10 @@ mut:
pub struct Comment { pub struct Comment {
pub: pub:
text string text string
is_multi bool is_multi bool
line_nr int line_nr int
pos token.Position pos token.Position
//same_line bool
} }
pub struct ConcatExpr { pub struct ConcatExpr {
@ -713,10 +688,11 @@ pub fn expr_is_blank_ident(expr Expr) bool {
[inline] [inline]
pub fn expr_is_call(expr Expr) bool { pub fn expr_is_call(expr Expr) bool {
return match expr { return match expr {
CallExpr{ CallExpr {
true true
} }
else { else {
false} false
}
} }
} }

View File

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

View File

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

View File

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

View File

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