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

run vfmt on v/

This commit is contained in:
Alexander Medvednikov 2019-12-28 09:43:22 +01:00
parent e02d6a3b04
commit 379c79025b
4 changed files with 126 additions and 103 deletions

View File

@ -313,13 +313,13 @@ fn (p &Parser) peek_token() Token {
} }
fn (p &Parser) log(s string) { fn (p &Parser) log(s string) {
}
/* /*
if !p.pref.is_verbose { if !p.pref.is_verbose {
return return
} }
println(s) println(s)
*/ */
}
pub fn (p &Parser) save_state() ParserState { pub fn (p &Parser) save_state() ParserState {
return ParserState{ return ParserState{
@ -796,6 +796,7 @@ fn (p mut Parser) type_decl() {
is_sum := p.tok == .assign is_sum := p.tok == .assign
if is_sum { if is_sum {
p.next() p.next()
p.fspace()
} }
mut parent := Type{} mut parent := Type{}
// Sum type // Sum type
@ -838,7 +839,13 @@ fn (p mut Parser) type_decl() {
if done { if done {
break break
} }
p.fspace()
p.check(.pipe) p.check(.pipe)
p.fspace()
if p.tokens[p.token_idx - 2].line_nr < p.tokens[p.token_idx - 1].line_nr {
p.fgenln('\t')
//p.fgen_nl()
}
} }
if p.pass == .decl { if p.pass == .decl {
p.table.sum_types << name p.table.sum_types << name
@ -877,9 +884,10 @@ int typ;
is_public: is_pub is_public: is_pub
}) })
} }
if p.tok != .key_type { //if p.tok != .key_type {
p.fspace() p.fgen_nl()
} p.fgen_nl()
//}
} }
// current token is `(` // current token is `(`
@ -1262,9 +1270,9 @@ fn (p mut Parser) statements() string {
fn (p mut Parser) statements_no_rcbr() string { fn (p mut Parser) statements_no_rcbr() string {
p.open_scope() p.open_scope()
if !p.inside_if_expr { //if !p.inside_if_expr {
// p.genln('') // p.genln('')
} //}
mut i := 0 mut i := 0
mut last_st_typ := '' mut last_st_typ := ''
for p.tok != .rcbr && p.tok != .eof { for p.tok != .rcbr && p.tok != .eof {

View File

@ -8,16 +8,12 @@ import (
v.types v.types
) )
struct Foo {} struct Foo {}
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral |
StringLiteral | IntegerLiteral | FloatLiteral | VarDecl | VarDecl | FnDecl | Return
FnDecl | Return
pub type Stmt = Foo//VarDecl
pub type Stmt = Foo // VarDecl
pub struct IntegerLiteral { pub struct IntegerLiteral {
pub: pub:
val int val int
@ -25,7 +21,7 @@ pub:
pub struct FloatLiteral { pub struct FloatLiteral {
pub: pub:
//val f64 // val f64
val string val string
} }
@ -37,26 +33,25 @@ pub:
// module decleration // module decleration
pub struct Module { pub struct Module {
pub: pub:
name string name string
path string path string
expr Expr expr Expr
} }
// import statement // import statement
pub struct Import { pub struct Import {
pub: pub:
name string name string
expr Expr expr Expr
// imports map[string]string // imports map[string]string
} }
pub struct FnDecl { pub struct FnDecl {
pub: pub:
name string name string
//stmts []Stmt // stmts []Stmt
exprs []Expr exprs []Expr
typ types.Type typ types.Type
} }
pub struct Return { pub struct Return {
@ -79,56 +74,54 @@ pub struct Stmt {
} }
*/ */
pub struct VarDecl { pub struct VarDecl {
pub: pub:
name string name string
expr Expr expr Expr
typ types.Type typ types.Type
} }
pub struct Program { pub struct Program {
pub: pub:
exprs []Expr exprs []Expr
//stmts []Stmt // stmts []Stmt
} }
// A single identifier // A single identifier
struct Ident { struct Ident {
tok_kind token.TokenKind tok_kind token.TokenKind
value string value string
} }
pub struct BinaryExpr { pub struct BinaryExpr {
pub: pub:
tok_kind token.TokenKind tok_kind token.TokenKind
//op BinaryOp // op BinaryOp
op token.TokenKind op token.TokenKind
left Expr left Expr
//left_type Type // left_type Type
right Expr right Expr
//right_type Type // right_type Type
} }
pub struct UnaryExpr { pub struct UnaryExpr {
pub: pub:
// tok_kind token.TokenKind // tok_kind token.TokenKind
//op BinaryOp // op BinaryOp
op token.TokenKind op token.TokenKind
left Expr left Expr
} }
struct IfExpr { struct IfExpr {
tok_kind token.TokenKind tok_kind token.TokenKind
cond Expr cond Expr
body []Stmt body []Stmt
else_ []Stmt else_ []Stmt
} }
struct ReturnStmt { struct ReturnStmt {
tok_kind token.TokenKind // or pos tok_kind token.TokenKind // or pos
results []Expr results []Expr
} }
// string representaiton of expr // string representaiton of expr
@ -146,7 +139,9 @@ pub fn (x Expr) str() string {
IntegerLiteral { IntegerLiteral {
return '"$it.val"' return '"$it.val"'
} }
else { return '' } else {
return ''
}
} }
} }

View File

@ -12,12 +12,12 @@ import (
) )
struct Parser { struct Parser {
scanner &scanner.Scanner scanner &scanner.Scanner
mut: mut:
tok token.Token tok token.Token
peek_tok token.Token peek_tok token.Token
//vars []string // vars []string
table &table.Table table &table.Table
return_type types.Type return_type types.Type
} }
@ -35,12 +35,18 @@ pub fn parse_expr(text string, table &table.Table) ast.Expr {
pub fn (p mut Parser) get_type() types.Type { pub fn (p mut Parser) get_type() types.Type {
defer { defer {
p.next() p.next()
} }
match p.tok.lit { match p.tok.lit {
'int' { return types.int_type } 'int' {
'f64' { return types.f64_type } return types.int_type
'string' { return types.string_type } }
'f64' {
return types.f64_type
}
'string' {
return types.string_type
}
else { else {
verror('bad type lit') verror('bad type lit')
exit(1) exit(1)
@ -58,35 +64,34 @@ pub fn parse_file(text string, table &table.Table) ast.Program {
p.next() p.next()
p.next() p.next()
for { for {
//res := s.scan() // res := s.scan()
if p.tok.kind == .eof { if p.tok.kind == .eof {
break break
} }
//println('expr at ' + p.tok.str()) // println('expr at ' + p.tok.str())
expr,_ := p.expr(token.lowest_prec) expr,_ := p.expr(token.lowest_prec)
exprs << expr exprs << expr
} }
println('nr exprs = $exprs.len') println('nr exprs = $exprs.len')
println(exprs[0]) println(exprs[0])
return ast.Program{exprs} return ast.Program{
exprs}
} }
pub fn (p mut Parser) parse_block() []ast.Expr { pub fn (p mut Parser) parse_block() []ast.Expr {
mut exprs := []ast.Expr mut exprs := []ast.Expr
for { for {
//res := s.scan() // res := s.scan()
if p.tok.kind in [.eof, .rcbr] { if p.tok.kind in [.eof, .rcbr] {
break break
} }
//println('expr at ' + p.tok.str()) // println('expr at ' + p.tok.str())
expr,_ := p.expr(token.lowest_prec) expr,_ := p.expr(token.lowest_prec)
exprs << expr exprs << expr
} }
p.next() p.next()
println('nr exprs in block = $exprs.len') println('nr exprs in block = $exprs.len')
return exprs return exprs
} }
/* /*
@ -129,17 +134,29 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
mut node := ast.Expr{} mut node := ast.Expr{}
mut typ := types.void_type mut typ := types.void_type
match p.tok.kind { match p.tok.kind {
.key_module { return p.module_decl() } .key_module {
.key_import { return p.import_stmt() } return p.module_decl()
.key_fn { return p.fn_decl() } }
.key_return { return p.return_stmt() } .key_import {
return p.import_stmt()
}
.key_fn {
return p.fn_decl()
}
.key_return {
return p.return_stmt()
}
.name { .name {
if p.peek_tok.kind == .decl_assign { if p.peek_tok.kind == .decl_assign {
return p.var_decl() return p.var_decl()
} }
} }
.str { node, typ = p.parse_string_literal() } .str {
.number { node, typ = p.parse_number_literal() } node,typ = p.parse_string_literal()
}
.number {
node,typ = p.parse_number_literal()
}
.lpar { .lpar {
node,typ = p.expr(0) node,typ = p.expr(0)
if p.tok.kind != .rpar { if p.tok.kind != .rpar {
@ -158,7 +175,6 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
} }
} }
} }
// left binding power // left binding power
for rbp < p.tok.precedence() { for rbp < p.tok.precedence() {
prev_tok := p.tok prev_tok := p.tok
@ -170,9 +186,11 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
expr,t2 = p.expr(prev_tok.precedence() - 1) expr,t2 = p.expr(prev_tok.precedence() - 1)
node = ast.BinaryExpr{ node = ast.BinaryExpr{
left: node left: node
//left_type: t1 // left_type: t1
op: prev_tok.kind op: prev_tok.kind
// right: p.expr(prev_tok.precedence() - 1) // right: p.expr(prev_tok.precedence() - 1)
right: expr right: expr
} }
if !types.check(&typ, &t2) { if !types.check(&typ, &t2) {
@ -189,7 +207,7 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
} }
} }
} }
return node, typ return node,typ
} }
/* /*
@ -219,13 +237,14 @@ fn (p mut Parser) stmt() ast.Stmt {
} }
*/ */
fn (p mut Parser) parse_string_literal() (ast.Expr,types.Type) { fn (p mut Parser) parse_string_literal() (ast.Expr,types.Type) {
mut node := ast.Expr{} mut node := ast.Expr{}
node = ast.StringLiteral{ node = ast.StringLiteral{
val: p.tok.lit val: p.tok.lit
} }
p.next() p.next()
return node, types.string_type return node,types.string_type
} }
fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) { fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) {
@ -234,30 +253,31 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) {
mut typ := types.int_type mut typ := types.int_type
if lit.contains('.') { if lit.contains('.') {
node = ast.FloatLiteral{ node = ast.FloatLiteral{
//val: lit.f64() // val: lit.f64()
val: lit val: lit
} }
typ = types.int_type typ = types.int_type
} else { }
else {
node = ast.IntegerLiteral{ node = ast.IntegerLiteral{
val: lit.int() val: lit.int()
} }
typ = types.int_type typ = types.int_type
} }
p.next() p.next()
return node, typ return node,typ
} }
fn (p mut Parser) module_decl() (ast.Expr,types.Type) { fn (p mut Parser) module_decl() (ast.Expr,types.Type) {
// p.check(.key_module) // p.check(.key_module)
p.next() p.next()
return ast.Expr{}, types.void_type return ast.Expr{},types.void_type
} }
fn (p mut Parser) import_stmt() (ast.Expr,types.Type) { fn (p mut Parser) import_stmt() (ast.Expr,types.Type) {
// p.check(.key_import) // p.check(.key_import)
p.next() p.next()
return ast.Expr{}, types.void_type return ast.Expr{},types.void_type
} }
fn (p mut Parser) fn_decl() (ast.Expr,types.Type) { fn (p mut Parser) fn_decl() (ast.Expr,types.Type) {
@ -272,35 +292,39 @@ fn (p mut Parser) fn_decl() (ast.Expr,types.Type) {
if p.tok.kind == .name { if p.tok.kind == .name {
typ = p.get_type() typ = p.get_type()
p.return_type = typ p.return_type = typ
} }
p.check(.lcbr) p.check(.lcbr)
//p.check(.rcbr) // p.check(.rcbr)
println('OK!') println('OK!')
exprs := p.parse_block() exprs := p.parse_block()
mut node := ast.Expr{} mut node := ast.Expr{}
node = ast.FnDecl{name: name, exprs: exprs, typ: typ} node = ast.FnDecl{
return node, types.void_type name: name
exprs: exprs
typ: typ
}
return node,types.void_type
} }
fn (p mut Parser) return_stmt() (ast.Expr,types.Type) { fn (p mut Parser) return_stmt() (ast.Expr,types.Type) {
println('return st') println('return st')
p.next() p.next()
expr, t := p.expr(0) expr,t := p.expr(0)
if !types.check(p.return_type, t) { if !types.check(p.return_type, t) {
verror('bad ret type') verror('bad ret type')
} }
mut node := ast.Expr{} mut node := ast.Expr{}
node = ast.Return{expr: expr} node = ast.Return{
return node, types.void_type expr: expr
}
return node,types.void_type
} }
fn (p mut Parser) var_decl() (ast.Expr,types.Type) { fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
name := p.tok.lit name := p.tok.lit
p.next() p.next()
p.next() p.next()
expr,t :=p.expr(token.lowest_prec) expr,t := p.expr(token.lowest_prec)
if name in p.table.names { if name in p.table.names {
verror('redefinition of `$name`') verror('redefinition of `$name`')
} }
@ -311,10 +335,11 @@ fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
// TODO can't return VarDecl{} // TODO can't return VarDecl{}
node = ast.VarDecl{ node = ast.VarDecl{
name: name name: name
expr: expr//p.expr(token.lowest_prec) expr: expr // p.expr(token.lowest_prec)
typ: t typ: t
}//, ast.void_type } // , ast.void_type
return node, types.void_type return node,types.void_type
} }
fn verror(s string) { fn verror(s string) {

View File

@ -28,7 +28,7 @@ mut:
inter_end bool inter_end bool
debug bool debug bool
line_comment string line_comment string
//prev_tok TokenKind // prev_tok TokenKind
started bool started bool
fn_name string // needed for @FN fn_name string // needed for @FN
print_line_on_error bool print_line_on_error bool
@ -36,7 +36,7 @@ mut:
print_rel_paths_on_error bool print_rel_paths_on_error bool
quote byte // which quote is used to denote current string: ' or " quote byte // which quote is used to denote current string: ' or "
line_ends []int // the positions of source lines ends (i.e. \n signs) line_ends []int // the positions of source lines ends (i.e. \n signs)
nr_lines int // total number of lines in the source file that were scanned nr_lines int // total number of lines in the source file that were scanned
is_vh bool // Keep newlines is_vh bool // Keep newlines
is_fmt bool // Used only for skipping ${} in strings, since we need literal is_fmt bool // Used only for skipping ${} in strings, since we need literal
// string values when generating formatted code. // string values when generating formatted code.
@ -46,7 +46,7 @@ fn new_scanner_file(file_path string) &Scanner {
if !os.exists(file_path) { if !os.exists(file_path) {
verror("$file_path doesn't exist") verror("$file_path doesn't exist")
} }
mut raw_text := os.read_file(file_path)or{ mut raw_text := os.read_file(file_path) or {
verror('scanner: failed to open $file_path') verror('scanner: failed to open $file_path')
return 0 return 0
} }
@ -60,7 +60,7 @@ fn new_scanner_file(file_path string) &Scanner {
} }
} }
mut s := new_scanner(raw_text) mut s := new_scanner(raw_text)
//s.init_fmt() // s.init_fmt()
s.file_path = file_path s.file_path = file_path
return s return s
} }
@ -632,8 +632,8 @@ pub fn (s mut Scanner) scan() token.Token {
} }
return scan_res(.div, '') return scan_res(.div, '')
} }
else { else {}
}} }
$if windows { $if windows {
if c == `\0` { if c == `\0` {
return s.end_of_file() return s.end_of_file()
@ -689,8 +689,7 @@ fn (s mut Scanner) ident_string() string {
} }
// Don't allow \0 // Don't allow \0
if c == `0` && s.pos > 2 && s.text[s.pos - 1] == slash { if c == `0` && s.pos > 2 && s.text[s.pos - 1] == slash {
if s.pos < s.text.len - 1 && s.text[s.pos + 1].is_digit() { if s.pos < s.text.len - 1 && s.text[s.pos + 1].is_digit() {}
}
else { else {
s.error('0 character in a string literal') s.error('0 character in a string literal')
} }
@ -722,8 +721,7 @@ fn (s mut Scanner) ident_string() string {
if s.inside_string { if s.inside_string {
end++ end++
} }
if start > s.pos { if start > s.pos {}
}
else { else {
lit = s.text[start..end] lit = s.text[start..end]
} }
@ -899,6 +897,3 @@ pub fn vhash() string {
pub fn cescaped_path(s string) string { pub fn cescaped_path(s string) string {
return s.replace('\\', '\\\\') return s.replace('\\', '\\\\')
} }