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

View File

@ -12,12 +12,12 @@ import (
)
struct Parser {
scanner &scanner.Scanner
scanner &scanner.Scanner
mut:
tok token.Token
peek_tok token.Token
//vars []string
table &table.Table
tok token.Token
peek_tok token.Token
// vars []string
table &table.Table
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 {
defer {
p.next()
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)
@ -58,35 +64,34 @@ pub fn parse_file(text string, table &table.Table) ast.Program {
p.next()
p.next()
for {
//res := s.scan()
// res := s.scan()
if p.tok.kind == .eof {
break
}
//println('expr at ' + p.tok.str())
// println('expr at ' + p.tok.str())
expr,_ := p.expr(token.lowest_prec)
exprs << expr
}
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 {
pub fn (p mut Parser) parse_block() []ast.Expr {
mut exprs := []ast.Expr
for {
//res := s.scan()
// res := s.scan()
if p.tok.kind in [.eof, .rcbr] {
break
}
//println('expr at ' + p.tok.str())
// println('expr at ' + p.tok.str())
expr,_ := p.expr(token.lowest_prec)
exprs << 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 {
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
@ -170,9 +186,11 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
expr,t2 = p.expr(prev_tok.precedence() - 1)
node = ast.BinaryExpr{
left: node
//left_type: t1
// left_type: t1
op: prev_tok.kind
// right: p.expr(prev_tok.precedence() - 1)
right: expr
}
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) {
mut node := ast.Expr{}
node = ast.StringLiteral{
val: p.tok.lit
}
p.next()
return node, types.string_type
return node,types.string_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
if lit.contains('.') {
node = ast.FloatLiteral{
//val: lit.f64()
// val: lit.f64()
val: lit
}
typ = types.int_type
} else {
}
else {
node = ast.IntegerLiteral{
val: lit.int()
}
typ = types.int_type
}
p.next()
return node, typ
return node,typ
}
fn (p mut Parser) module_decl() (ast.Expr,types.Type) {
// p.check(.key_module)
p.next()
return ast.Expr{}, types.void_type
}
return ast.Expr{},types.void_type
}
fn (p mut Parser) import_stmt() (ast.Expr,types.Type) {
// p.check(.key_import)
p.next()
return ast.Expr{}, types.void_type
return ast.Expr{},types.void_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 {
typ = p.get_type()
p.return_type = typ
}
p.check(.lcbr)
//p.check(.rcbr)
// p.check(.rcbr)
println('OK!')
exprs := p.parse_block()
mut node := ast.Expr{}
node = ast.FnDecl{name: name, exprs: exprs, typ: typ}
return node, types.void_type
node = ast.FnDecl{
name: name
exprs: exprs
typ: typ
}
return node,types.void_type
}
fn (p mut Parser) return_stmt() (ast.Expr,types.Type) {
println('return st')
p.next()
expr, t := p.expr(0)
expr,t := p.expr(0)
if !types.check(p.return_type, t) {
verror('bad ret type')
}
mut node := ast.Expr{}
node = ast.Return{expr: expr}
return node, types.void_type
node = ast.Return{
expr: expr
}
return node,types.void_type
}
fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
name := p.tok.lit
p.next()
p.next()
expr,t :=p.expr(token.lowest_prec)
expr,t := p.expr(token.lowest_prec)
if name in p.table.names {
verror('redefinition of `$name`')
}
@ -311,10 +335,11 @@ fn (p mut Parser) var_decl() (ast.Expr,types.Type) {
// TODO can't return VarDecl{}
node = ast.VarDecl{
name: name
expr: expr//p.expr(token.lowest_prec)
expr: expr // p.expr(token.lowest_prec)
typ: t
}//, ast.void_type
return node, types.void_type
} // , ast.void_type
return node,types.void_type
}
fn verror(s string) {

View File

@ -28,7 +28,7 @@ mut:
inter_end bool
debug bool
line_comment string
//prev_tok TokenKind
// prev_tok TokenKind
started bool
fn_name string // needed for @FN
print_line_on_error bool
@ -36,7 +36,7 @@ mut:
print_rel_paths_on_error bool
quote byte // which quote is used to denote current string: ' or "
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_fmt bool // Used only for skipping ${} in strings, since we need literal
// string values when generating formatted code.
@ -46,7 +46,7 @@ fn new_scanner_file(file_path string) &Scanner {
if !os.exists(file_path) {
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')
return 0
}
@ -60,7 +60,7 @@ fn new_scanner_file(file_path string) &Scanner {
}
}
mut s := new_scanner(raw_text)
//s.init_fmt()
// s.init_fmt()
s.file_path = file_path
return s
}
@ -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('\\', '\\\\')
}