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

v2: enum decl; type decl; [start..end] fix; %=

This commit is contained in:
Alexander Medvednikov 2020-02-10 23:19:50 +01:00
parent 26cfaa150e
commit 747f6829ab
3 changed files with 120 additions and 34 deletions

View File

@ -15,7 +15,7 @@ CastExpr | EnumVal
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt HashStmt | AssignStmt | EnumDecl | TypeDecl
// | IncDecStmt k // | IncDecStmt k
// Stand-alone expression in a statement list. // Stand-alone expression in a statement list.
pub struct ExprStmt { pub struct ExprStmt {
@ -367,10 +367,22 @@ pub:
name string name string
} }
pub struct EnumVal{ pub struct EnumVal {
pub: pub:
name string name string
}
pub struct EnumDecl {
pub:
name string
is_pub bool
vals []string
}
pub struct TypeDecl{
pub:
name string
is_pub bool
} }
pub struct AssignExpr { pub struct AssignExpr {

View File

@ -178,16 +178,16 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
.key_struct, .key_union, .key_interface { .key_struct, .key_union, .key_interface {
return p.struct_decl() return p.struct_decl()
} }
.key_enum {
return p.enum_decl()
}
.key_type {
return p.type_decl()
}
else { else {
p.error('wrong pub keyword usage') p.error('wrong pub keyword usage')
return ast.Stmt{} return ast.Stmt{}
} }
// .key_enum {
// return p.enum_decl()
// }
// .key_type {
// return p.type_decl()
// }
} }
} }
.lsbr { .lsbr {
@ -300,6 +300,8 @@ fn (p mut Parser) attr() ast.Attr {
} }
} }
/*
fn (p mut Parser) range_expr(low ast.Expr) ast.Expr { fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
// ,table.Type) { // ,table.Type) {
if p.tok.kind != .dotdot { if p.tok.kind != .dotdot {
@ -319,6 +321,7 @@ fn (p mut Parser) range_expr(low ast.Expr) ast.Expr {
} }
return node return node
} }
*/
/* /*
pub fn (p mut Parser) assign_stmt() ast.AssignStmt { pub fn (p mut Parser) assign_stmt() ast.AssignStmt {
@ -499,7 +502,7 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
} }
} }
// struct init // struct init
else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || p.tok.lit in ['array', 'string', 'ustring', 'mapnode', 'map']) && !p.tok.lit[p.tok.lit.len - 1].is_capital() { else if p.peek_tok.kind == .lcbr && (p.tok.lit[0].is_capital() || is_c || p.tok.lit in ['array', 'string', 'ustring', 'mapnode', 'map']) && !p.tok.lit[p.tok.lit.len - 1].is_capital() {
// || p.table.known_type(p.tok.lit)) { // || p.table.known_type(p.tok.lit)) {
typ = p.parse_type() typ = p.parse_type()
// p.warn('struct init typ=$typ.name') // p.warn('struct init typ=$typ.name')
@ -643,36 +646,49 @@ fn (p mut Parser) prefix_expr() (ast.Expr,table.Type) {
return expr,typ return expr,typ
} }
// fn (p mut Parser) index_expr(left ast.Expr, typ_ table.Type) (ast.Expr,table.Type) { fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
fn (p mut Parser) index_expr(left ast.Expr) ast.Expr { // left == `a` in `a[0]`
// mut typ := typ_
// println('index expr$p.tok.str() line=$p.tok.line_nr')
p.next() // [ p.next() // [
mut index_expr := ast.Expr{} mut index_expr := ast.Expr{}
if p.tok.kind == .dotdot || p.peek_tok.kind == .dotdot { if p.tok.kind == .dotdot {
// `numbers[..end]` // [..end]
index_expr = p.range_expr(left) p.next()
// typ = typ_ // set the type back to array high,_ := p.expr(0)
}
else {
// println('start index expr')
index_expr,_ = p.expr(0)
}
// println('end expr typ=$typ.name')
p.check(.rsbr) p.check(.rsbr)
// println('got ]') return ast.IndexExpr{
// /ti := table.int_type
mut node := ast.Expr{}
// println('index expr: $typ.typ.name')
node = ast.IndexExpr{
left: left left: left
index: index_expr
pos: p.tok.position() pos: p.tok.position()
// typ: typ index: ast.RangeExpr{
low: ast.Expr{}
high: high
}
}
}
expr,_ := p.expr(0) // `[expr]` or `[expr..]`
if p.tok.kind == .dotdot {
// [start..end] or [start..]
p.check(.dotdot)
mut high := ast.Expr{}
if p.tok.kind != .rsbr {
high,_ = p.expr(0)
}
p.check(.rsbr)
return ast.IndexExpr{
left: left
pos: p.tok.position()
index: ast.RangeExpr{
low: expr
high: high
}
}
}
// [expr]
p.check(.rsbr)
return ast.IndexExpr{
left: left
index: expr
pos: p.tok.position()
} }
return node
// return node,typ
} }
fn (p mut Parser) filter(typ table.Type) { fn (p mut Parser) filter(typ table.Type) {
@ -1061,6 +1077,10 @@ fn (p mut Parser) import_stmt() []ast.Import {
} }
fn (p mut Parser) const_decl() ast.ConstDecl { fn (p mut Parser) const_decl() ast.ConstDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
p.check(.key_const) p.check(.key_const)
p.check(.lpar) p.check(.lpar)
mut fields := []ast.Field mut fields := []ast.Field
@ -1093,6 +1113,11 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
p.next() p.next()
} }
p.check(.key_struct) p.check(.key_struct)
is_c := p.tok.lit == 'C' && p.peek_tok.kind == .dot
if is_c {
p.next() // C
p.next() // .
}
name := p.check_name() name := p.check_name()
p.check(.lcbr) p.check(.lcbr)
mut ast_fields := []ast.Field mut ast_fields := []ast.Field
@ -1294,6 +1319,10 @@ fn (p mut Parser) match_expr() (ast.Expr,table.Type) {
mut return_type := table.void_type mut return_type := table.void_type
for { for {
// p.tok.kind != .rcbr { // p.tok.kind != .rcbr {
// Sum type match
// if p.tok.kind == .name && p.tok.lit[0].is_capital() {
// } else {
// Expression match
match_expr,_ := p.expr(0) match_expr,_ := p.expr(0)
match_exprs << match_expr match_exprs << match_expr
p.warn('match block') p.warn('match block')
@ -1333,6 +1362,51 @@ fn (p mut Parser) match_expr() (ast.Expr,table.Type) {
return node,return_type return node,return_type
} }
fn (p mut Parser) enum_decl() ast.EnumDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
p.check(.key_enum)
name := p.check_name()
p.check(.lcbr)
mut vals := []string
for p.tok.kind != .eof && p.tok.kind != .rcbr {
val := p.check_name()
vals << val
p.warn('enum val $val')
}
p.check(.rcbr)
return ast.EnumDecl{
name: name
is_pub: is_pub
vals: vals
}
}
fn (p mut Parser) type_decl() ast.TypeDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
}
p.check(.key_type)
name := p.check_name()
// type SumType = A | B | c
if p.tok.kind == .assign {
p.next()
for {
p.check_name()
if p.tok.kind != .pipe {
break
}
p.check(.pipe)
}
}
return ast.TypeDecl{
name: name
}
}
fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.Type { fn (p mut Parser) add_unresolved(key string, expr ast.Expr) table.Type {
mut idx := p.unresolved_offset + p.unresolved.len mut idx := p.unresolved_offset + p.unresolved.len
if key in p.table.unresolved_idxs { if key in p.table.unresolved_idxs {

View File

@ -388,7 +388,7 @@ pub fn (tok Token) precedence() int {
return 3 return 3
} }
// `||` // `||`
.logical_or, .assign, .plus_assign, .minus_assign, .div_assign, .or_assign, .logical_or, .assign, .plus_assign, .minus_assign, .div_assign, .mod_assign, .or_assign,
// //
.left_shift_assign, .righ_shift_assign, .mult_assign { .left_shift_assign, .righ_shift_assign, .mult_assign {
return 2 return 2