mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: more cleanup (#3765)
This commit is contained in:
parent
392eb1fb55
commit
67756c53dc
@ -16,7 +16,6 @@ pub struct Checker {
|
|||||||
mut:
|
mut:
|
||||||
file_name string
|
file_name string
|
||||||
scope &ast.Scope
|
scope &ast.Scope
|
||||||
resolved []table.Type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_checker(table &table.Table) Checker {
|
pub fn new_checker(table &table.Table) Checker {
|
||||||
|
@ -46,13 +46,11 @@ pub fn (p mut Parser) call_args() []ast.Expr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) fn_decl() ast.FnDecl {
|
fn (p mut Parser) fn_decl() ast.FnDecl {
|
||||||
//p.table.clear_vars()
|
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
is_pub := p.tok.kind == .key_pub
|
is_pub := p.tok.kind == .key_pub
|
||||||
if is_pub {
|
if is_pub {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
//p.table.clear_vars()
|
|
||||||
p.check(.key_fn)
|
p.check(.key_fn)
|
||||||
// C.
|
// C.
|
||||||
is_c := p.tok.kind == .name && p.tok.lit == 'C'
|
is_c := p.tok.kind == .name && p.tok.lit == 'C'
|
||||||
|
@ -35,8 +35,6 @@ mut:
|
|||||||
// vars []string
|
// vars []string
|
||||||
table &table.Table
|
table &table.Table
|
||||||
return_type table.Type // current function's return type
|
return_type table.Type // current function's return type
|
||||||
// scope_level int
|
|
||||||
// var_idx int
|
|
||||||
is_c bool
|
is_c bool
|
||||||
//
|
//
|
||||||
// prefix_parse_fns []PrefixParseFn
|
// prefix_parse_fns []PrefixParseFn
|
||||||
@ -161,7 +159,7 @@ pub fn (p mut Parser) parse_block() []ast.Stmt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
println('parse block')
|
//println('parse block')
|
||||||
p.close_scope()
|
p.close_scope()
|
||||||
// println('nr exprs in block = $exprs.len')
|
// println('nr exprs in block = $exprs.len')
|
||||||
return stmts
|
return stmts
|
||||||
@ -455,8 +453,7 @@ pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) struct_init() (ast.Expr,table.Type) {
|
fn (p mut Parser) struct_init() ast.StructInit {
|
||||||
mut node := ast.Expr{}
|
|
||||||
typ := p.parse_type()
|
typ := p.parse_type()
|
||||||
sym := p.table.get_type_symbol(typ)
|
sym := p.table.get_type_symbol(typ)
|
||||||
p.warn('struct init typ=$sym.name')
|
p.warn('struct init typ=$sym.name')
|
||||||
@ -492,20 +489,18 @@ fn (p mut Parser) struct_init() (ast.Expr,table.Type) {
|
|||||||
exprs << expr
|
exprs << expr
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
node = ast.StructInit{
|
node := ast.StructInit{
|
||||||
typ: typ
|
typ: typ
|
||||||
exprs: exprs
|
exprs: exprs
|
||||||
fields: field_names
|
fields: field_names
|
||||||
pos: p.tok.position()
|
pos: p.tok.position()
|
||||||
}
|
}
|
||||||
p.check(.rcbr)
|
p.check(.rcbr)
|
||||||
return node,typ
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
|
pub fn (p mut Parser) name_expr() ast.Expr {
|
||||||
mut node := ast.Expr{}
|
mut node := ast.Expr{}
|
||||||
typ := table.void_type
|
|
||||||
// mut typ := table.unresolved_type
|
|
||||||
is_c := p.tok.lit == 'C' && p.peek_tok.kind == .dot
|
is_c := p.tok.lit == 'C' && p.peek_tok.kind == .dot
|
||||||
if is_c {
|
if is_c {
|
||||||
p.next()
|
p.next()
|
||||||
@ -518,7 +513,7 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
|
|||||||
// `map[string]int` initialization
|
// `map[string]int` initialization
|
||||||
if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr {
|
if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr {
|
||||||
map_type := p.parse_map_type(0)
|
map_type := p.parse_map_type(0)
|
||||||
return node,typ
|
return node
|
||||||
}
|
}
|
||||||
// p.warn('name expr $p.tok.lit')
|
// p.warn('name expr $p.tok.lit')
|
||||||
// fn call or type cast
|
// fn call or type cast
|
||||||
@ -533,7 +528,6 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
|
|||||||
mut expr := ast.Expr{}
|
mut expr := ast.Expr{}
|
||||||
expr,_ = p.expr(0)
|
expr,_ = p.expr(0)
|
||||||
// TODO, string(b, len)
|
// TODO, string(b, len)
|
||||||
// if table.type_idx(to_typ) == table.string_type_idx && p.tok.kind == .comma {
|
|
||||||
if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx {
|
if p.tok.kind == .comma && table.type_idx(to_typ) == table.string_type_idx {
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
p.expr(0) // len
|
p.expr(0) // len
|
||||||
@ -543,11 +537,11 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
|
|||||||
typ: to_typ
|
typ: to_typ
|
||||||
expr: expr
|
expr: expr
|
||||||
}
|
}
|
||||||
return node,to_typ
|
return node
|
||||||
}
|
}
|
||||||
// fn call
|
// fn call
|
||||||
else {
|
else {
|
||||||
println('calling $p.tok.lit')
|
// println('calling $p.tok.lit')
|
||||||
x := p.call_expr() // TODO `node,typ :=` should work
|
x := p.call_expr() // TODO `node,typ :=` should work
|
||||||
node = x
|
node = x
|
||||||
}
|
}
|
||||||
@ -561,7 +555,7 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
|
|||||||
ident = p.parse_ident(is_c)
|
ident = p.parse_ident(is_c)
|
||||||
node = ident
|
node = ident
|
||||||
}
|
}
|
||||||
return node,typ
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
||||||
@ -571,7 +565,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
|||||||
// Prefix
|
// Prefix
|
||||||
match p.tok.kind {
|
match p.tok.kind {
|
||||||
.name {
|
.name {
|
||||||
node,typ = p.name_expr()
|
node = p.name_expr()
|
||||||
}
|
}
|
||||||
.str {
|
.str {
|
||||||
node,typ = p.string_expr()
|
node,typ = p.string_expr()
|
||||||
@ -589,7 +583,7 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
|||||||
}
|
}
|
||||||
// -1, -a, !x, &x, ~x
|
// -1, -a, !x, &x, ~x
|
||||||
.minus, .amp, .mul, .not, .bit_not {
|
.minus, .amp, .mul, .not, .bit_not {
|
||||||
node,typ = p.prefix_expr()
|
node = p.prefix_expr()
|
||||||
}
|
}
|
||||||
// .amp {
|
// .amp {
|
||||||
// p.next()
|
// p.next()
|
||||||
@ -704,16 +698,14 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
|||||||
return node,typ
|
return node,typ
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) prefix_expr() (ast.Expr,table.Type) {
|
fn (p mut Parser) prefix_expr() ast.PrefixExpr {
|
||||||
op := p.tok.kind
|
op := p.tok.kind
|
||||||
p.next()
|
p.next()
|
||||||
right,typ := p.expr(1)
|
right,_ := p.expr(1)
|
||||||
mut expr := ast.Expr{}
|
return ast.PrefixExpr{
|
||||||
expr = ast.PrefixExpr{
|
|
||||||
op: op
|
op: op
|
||||||
right: right
|
right: right
|
||||||
}
|
}
|
||||||
return expr,typ
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
|
||||||
@ -1167,22 +1159,21 @@ fn (p mut Parser) array_init() ast.Expr {
|
|||||||
fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {
|
fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {
|
||||||
lit := p.tok.lit
|
lit := p.tok.lit
|
||||||
mut node := ast.Expr{}
|
mut node := ast.Expr{}
|
||||||
mut ti := table.int_type
|
mut typ := table.int_type
|
||||||
if lit.contains('.') {
|
if lit.contains('.') {
|
||||||
node = ast.FloatLiteral{
|
node = ast.FloatLiteral{
|
||||||
// val: lit.f64()
|
// val: lit.f64()
|
||||||
val: lit
|
val: lit
|
||||||
}
|
}
|
||||||
ti = table.f64_type
|
typ = table.f64_type
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
node = ast.IntegerLiteral{
|
node = ast.IntegerLiteral{
|
||||||
val: lit.int()
|
val: lit.int()
|
||||||
}
|
}
|
||||||
// ti = table.int_ti
|
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
return node,ti
|
return node,typ
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) module_decl() ast.Module {
|
fn (p mut Parser) module_decl() ast.Module {
|
||||||
|
@ -362,7 +362,7 @@ pub fn (t &Table) check(got, expected Type) bool {
|
|||||||
exp_type_sym := t.get_type_symbol(expected)
|
exp_type_sym := t.get_type_symbol(expected)
|
||||||
got_idx := type_idx(got)
|
got_idx := type_idx(got)
|
||||||
exp_idx := type_idx(expected)
|
exp_idx := type_idx(expected)
|
||||||
println('check: $got_type_sym.name, $exp_type_sym.name')
|
//println('check: $got_type_sym.name, $exp_type_sym.name')
|
||||||
if exp_type_sym.kind == .voidptr {
|
if exp_type_sym.kind == .voidptr {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user