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) {
}
/*
if !p.pref.is_verbose {
return
}
println(s)
*/
}
pub fn (p &Parser) save_state() ParserState {
return ParserState{
@ -796,6 +796,7 @@ fn (p mut Parser) type_decl() {
is_sum := p.tok == .assign
if is_sum {
p.next()
p.fspace()
}
mut parent := Type{}
// Sum type
@ -838,7 +839,13 @@ fn (p mut Parser) type_decl() {
if done {
break
}
p.fspace()
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 {
p.table.sum_types << name
@ -877,9 +884,10 @@ int typ;
is_public: is_pub
})
}
if p.tok != .key_type {
p.fspace()
}
//if p.tok != .key_type {
p.fgen_nl()
p.fgen_nl()
//}
}
// current token is `(`
@ -1262,9 +1270,9 @@ fn (p mut Parser) statements() string {
fn (p mut Parser) statements_no_rcbr() string {
p.open_scope()
if !p.inside_if_expr {
//if !p.inside_if_expr {
// p.genln('')
}
//}
mut i := 0
mut last_st_typ := ''
for p.tok != .rcbr && p.tok != .eof {

View File

@ -8,16 +8,12 @@ import (
v.types
)
struct Foo {}
pub type Expr = BinaryExpr | UnaryExpr | IfExpr |
StringLiteral | IntegerLiteral | FloatLiteral | VarDecl |
FnDecl | Return
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral | FloatLiteral |
VarDecl | FnDecl | Return
pub type Stmt = Foo // VarDecl
pub struct IntegerLiteral {
pub:
val int
@ -56,7 +52,6 @@ pub:
// stmts []Stmt
exprs []Expr
typ types.Type
}
pub struct Return {
@ -79,12 +74,12 @@ pub struct Stmt {
}
*/
pub struct VarDecl {
pub:
name string
expr Expr
typ types.Type
}
pub struct Program {
@ -92,8 +87,6 @@ pub:
exprs []Expr
// stmts []Stmt
}
// A single identifier
struct Ident {
tok_kind token.TokenKind
@ -146,7 +139,9 @@ pub fn (x Expr) str() string {
IntegerLiteral {
return '"$it.val"'
}
else { return '' }
else {
return ''
}
}
}

View File

@ -38,9 +38,15 @@ pub fn (p mut Parser) get_type() types.Type {
p.next()
}
match p.tok.lit {
'int' { return types.int_type }
'f64' { return types.f64_type }
'string' { return types.string_type }
'int' {
return types.int_type
}
'f64' {
return types.f64_type
}
'string' {
return types.string_type
}
else {
verror('bad type lit')
exit(1)
@ -68,12 +74,12 @@ pub fn parse_file(text string, table &table.Table) ast.Program {
}
println('nr exprs = $exprs.len')
println(exprs[0])
return ast.Program{exprs}
return ast.Program{
exprs}
}
pub fn (p mut Parser) parse_block() []ast.Expr {
mut exprs := []ast.Expr
for {
// res := s.scan()
if p.tok.kind in [.eof, .rcbr] {
@ -86,7 +92,6 @@ pub fn (p mut Parser) parse_block() []ast.Expr {
p.next()
println('nr exprs in block = $exprs.len')
return exprs
}
/*
@ -129,17 +134,29 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
mut node := ast.Expr{}
mut typ := types.void_type
match p.tok.kind {
.key_module { return p.module_decl() }
.key_import { return p.import_stmt() }
.key_fn { return p.fn_decl() }
.key_return { return p.return_stmt() }
.key_module {
return p.module_decl()
}
.key_import {
return p.import_stmt()
}
.key_fn {
return p.fn_decl()
}
.key_return {
return p.return_stmt()
}
.name {
if p.peek_tok.kind == .decl_assign {
return p.var_decl()
}
}
.str { node, typ = p.parse_string_literal() }
.number { node, typ = p.parse_number_literal() }
.str {
node,typ = p.parse_string_literal()
}
.number {
node,typ = p.parse_number_literal()
}
.lpar {
node,typ = p.expr(0)
if p.tok.kind != .rpar {
@ -158,7 +175,6 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
}
}
}
// left binding power
for rbp < p.tok.precedence() {
prev_tok := p.tok
@ -171,8 +187,10 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
node = ast.BinaryExpr{
left: node
// left_type: t1
op: prev_tok.kind
// right: p.expr(prev_tok.precedence() - 1)
right: expr
}
if !types.check(&typ, &t2) {
@ -219,6 +237,7 @@ fn (p mut Parser) stmt() ast.Stmt {
}
*/
fn (p mut Parser) parse_string_literal() (ast.Expr,types.Type) {
mut node := ast.Expr{}
node = ast.StringLiteral{
@ -238,7 +257,8 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,types.Type) {
val: lit
}
typ = types.int_type
} else {
}
else {
node = ast.IntegerLiteral{
val: lit.int()
}
@ -272,15 +292,17 @@ fn (p mut Parser) fn_decl() (ast.Expr,types.Type) {
if p.tok.kind == .name {
typ = p.get_type()
p.return_type = typ
}
p.check(.lcbr)
// p.check(.rcbr)
println('OK!')
exprs := p.parse_block()
mut node := ast.Expr{}
node = ast.FnDecl{name: name, exprs: exprs, typ: typ}
node = ast.FnDecl{
name: name
exprs: exprs
typ: typ
}
return node,types.void_type
}
@ -292,7 +314,9 @@ fn (p mut Parser) return_stmt() (ast.Expr,types.Type) {
verror('bad ret type')
}
mut node := ast.Expr{}
node = ast.Return{expr: expr}
node = ast.Return{
expr: expr
}
return node,types.void_type
}
@ -312,6 +336,7 @@ fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
node = ast.VarDecl{
name: name
expr: expr // p.expr(token.lowest_prec)
typ: t
} // , ast.void_type
return node,types.void_type

View File

@ -632,8 +632,8 @@ pub fn (s mut Scanner) scan() token.Token {
}
return scan_res(.div, '')
}
else {
}}
else {}
}
$if windows {
if c == `\0` {
return s.end_of_file()
@ -689,8 +689,7 @@ fn (s mut Scanner) ident_string() string {
}
// Don't allow \0
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 {
s.error('0 character in a string literal')
}
@ -722,8 +721,7 @@ fn (s mut Scanner) ident_string() string {
if s.inside_string {
end++
}
if start > s.pos {
}
if start > s.pos {}
else {
lit = s.text[start..end]
}
@ -899,6 +897,3 @@ pub fn vhash() string {
pub fn cescaped_path(s string) string {
return s.replace('\\', '\\\\')
}