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:
parent
f3c917e0aa
commit
f508955b64
136
vlib/v/ast/ast.v
136
vlib/v/ast/ast.v
@ -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
|
||||
}
|
||||
|
||||
@ -105,22 +97,20 @@ pub:
|
||||
|
||||
pub struct StructField {
|
||||
pub:
|
||||
name string
|
||||
pos token.Position
|
||||
comment Comment
|
||||
name string
|
||||
pos token.Position
|
||||
comment Comment
|
||||
default_expr string // token literal //Expr
|
||||
mut:
|
||||
typ table.Type
|
||||
typ table.Type
|
||||
}
|
||||
|
||||
pub struct Field {
|
||||
pub:
|
||||
name string
|
||||
// type_idx int
|
||||
pos token.Position
|
||||
pos token.Position
|
||||
mut:
|
||||
typ table.Type
|
||||
// typ2 Type
|
||||
}
|
||||
|
||||
pub struct ConstField {
|
||||
@ -142,15 +132,15 @@ pub:
|
||||
|
||||
pub struct StructDecl {
|
||||
pub:
|
||||
pos token.Position
|
||||
name string
|
||||
fields []StructField
|
||||
is_pub bool
|
||||
mut_pos int // mut:
|
||||
pub_pos int // pub:
|
||||
pub_mut_pos int // pub mut:
|
||||
is_c bool
|
||||
is_union bool
|
||||
pos token.Position
|
||||
name string
|
||||
fields []StructField
|
||||
is_pub bool
|
||||
mut_pos int // mut:
|
||||
pub_pos int // pub:
|
||||
pub_mut_pos int // pub mut:
|
||||
is_c bool
|
||||
is_union bool
|
||||
}
|
||||
|
||||
pub struct InterfaceDecl {
|
||||
@ -176,7 +166,6 @@ pub:
|
||||
pos token.Position
|
||||
mod string
|
||||
alias string
|
||||
// expr Expr
|
||||
}
|
||||
|
||||
pub struct FnDecl {
|
||||
@ -203,21 +192,19 @@ pub:
|
||||
|
||||
pub struct CallExpr {
|
||||
pub:
|
||||
// tok token.Token
|
||||
pos token.Position
|
||||
left Expr // `user` in `user.register()`
|
||||
is_method bool
|
||||
mod string
|
||||
pos token.Position
|
||||
left Expr // `user` in `user.register()`
|
||||
is_method bool
|
||||
mod string
|
||||
mut:
|
||||
name string
|
||||
args []CallArg
|
||||
name string
|
||||
args []CallArg
|
||||
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
|
||||
is_c bool
|
||||
or_block OrExpr
|
||||
left_type table.Type // type of `user`
|
||||
receiver_type table.Type // User
|
||||
return_type table.Type
|
||||
}
|
||||
|
||||
pub struct CallArg {
|
||||
@ -243,15 +230,12 @@ pub enum Expr {
|
||||
Integer(IntegerExpr)
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
pub struct Stmt {
|
||||
pos int
|
||||
//end int
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
pub struct Var {
|
||||
pub:
|
||||
name string
|
||||
@ -264,23 +248,21 @@ mut:
|
||||
|
||||
pub struct GlobalDecl {
|
||||
pub:
|
||||
name string
|
||||
expr Expr
|
||||
name string
|
||||
expr Expr
|
||||
has_expr bool
|
||||
mut:
|
||||
typ table.Type
|
||||
typ table.Type
|
||||
}
|
||||
|
||||
pub struct File {
|
||||
pub:
|
||||
path string
|
||||
mod Module
|
||||
imports []Import
|
||||
stmts []Stmt
|
||||
scope &Scope
|
||||
// TODO: consider parent instead of field
|
||||
path string
|
||||
mod Module
|
||||
imports []Import
|
||||
stmts []Stmt
|
||||
scope &Scope
|
||||
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,13 +342,12 @@ pub:
|
||||
|
||||
pub struct IndexExpr {
|
||||
pub:
|
||||
// op token.Kind
|
||||
pos token.Position
|
||||
left Expr
|
||||
index Expr // [0], [start..end] etc
|
||||
pos token.Position
|
||||
left Expr
|
||||
index Expr // [0], [start..end] etc
|
||||
mut:
|
||||
left_type table.Type // array, map, fixed array
|
||||
is_setter bool
|
||||
is_setter bool
|
||||
}
|
||||
|
||||
pub struct IfExpr {
|
||||
@ -384,9 +364,9 @@ mut:
|
||||
|
||||
pub struct IfBranch {
|
||||
pub:
|
||||
cond Expr
|
||||
stmts []Stmt
|
||||
pos token.Position
|
||||
cond Expr
|
||||
stmts []Stmt
|
||||
pos token.Position
|
||||
comment Comment
|
||||
}
|
||||
|
||||
@ -396,7 +376,7 @@ pub:
|
||||
cond Expr
|
||||
branches []MatchBranch
|
||||
pos token.Position
|
||||
is_mut bool // `match mut ast_node {`
|
||||
is_mut bool // `match mut ast_node {`
|
||||
mut:
|
||||
is_expr bool // returns a value
|
||||
return_type table.Type
|
||||
@ -407,15 +387,14 @@ mut:
|
||||
|
||||
pub struct MatchBranch {
|
||||
pub:
|
||||
exprs []Expr
|
||||
stmts []Stmt
|
||||
pos token.Position
|
||||
exprs []Expr
|
||||
stmts []Stmt
|
||||
pos token.Position
|
||||
comment Comment // comment above `xxx {`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@ -650,10 +628,8 @@ mut:
|
||||
// `or { ... }`
|
||||
pub struct OrExpr {
|
||||
pub:
|
||||
stmts []Stmt
|
||||
stmts []Stmt
|
||||
is_used bool // if the or{} block is written down or left out
|
||||
// var_name string
|
||||
// expr Expr
|
||||
}
|
||||
|
||||
pub struct Assoc {
|
||||
@ -681,11 +657,10 @@ mut:
|
||||
|
||||
pub struct Comment {
|
||||
pub:
|
||||
text string
|
||||
text string
|
||||
is_multi bool
|
||||
line_nr int
|
||||
pos token.Position
|
||||
//same_line bool
|
||||
line_nr int
|
||||
pos token.Position
|
||||
}
|
||||
|
||||
pub struct ConcatExpr {
|
||||
@ -713,10 +688,11 @@ pub fn expr_is_blank_ident(expr Expr) bool {
|
||||
[inline]
|
||||
pub fn expr_is_call(expr Expr) bool {
|
||||
return match expr {
|
||||
CallExpr{
|
||||
CallExpr {
|
||||
true
|
||||
}
|
||||
else {
|
||||
false}
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
@ -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{}')
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user