v2: remove type fields from parser & some cleanup

pull/3931/head
Joe Conigliaro 2020-03-05 23:57:05 +11:00
parent 38a54b08e3
commit 272eaaa704
4 changed files with 21 additions and 69 deletions

View File

@ -175,7 +175,6 @@ pub:
pub struct Return {
pub:
pos token.Position
expected_type table.Type // TODO: remove once checker updated
exprs []Expr
}

View File

@ -300,16 +300,11 @@ pub fn (c mut Checker) selector_expr(selector_expr ast.SelectorExpr) table.Type
// TODO: non deferred
pub fn (c mut Checker) return_stmt(return_stmt ast.Return) {
mut got_types := []table.Type
c.expected_type = c.fn_return_type
if return_stmt.exprs.len == 0 {
return
}
for expr in return_stmt.exprs {
typ := c.expr(expr)
got_types << typ
}
expected_type := return_stmt.expected_type
expected_type := c.fn_return_type
expected_type_sym := c.table.get_type_symbol(expected_type)
exp_is_optional := table.type_is_optional(expected_type)
mut expected_types := [expected_type]
@ -317,6 +312,11 @@ pub fn (c mut Checker) return_stmt(return_stmt ast.Return) {
mr_info := expected_type_sym.info as table.MultiReturn
expected_types = mr_info.types
}
mut got_types := []table.Type
for expr in return_stmt.exprs {
typ := c.expr(expr)
got_types << typ
}
// allow `none` & `error (Option)` return types for function that returns optional
if exp_is_optional && table.type_idx(got_types[0]) in [table.none_type_idx, c.table.type_idxs['Option']] {
return

View File

@ -120,28 +120,11 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
})
// p.table.register_var(var)
}
//
/*
arg := table.Var{
name: arg_name
typ: arg_type
}
args << arg
// p.table.register_var(arg)
arg := table.Var{
name: arg_name
typ: typ
}
args << arg
p.table.register_var(arg)
*/
// Return type
mut typ := table.void_type
if p.tok.kind.is_start_of_type() {
typ = p.parse_type()
}
p.return_type = typ
// Register
if is_method {
type_sym := p.table.get_type_symbol(rec_type)

View File

@ -34,16 +34,13 @@ mut:
peek_tok token.Token
// vars []string
table &table.Table
return_type table.Type // current function's return type
is_c bool
//
// prefix_parse_fns []PrefixParseFn
inside_if bool
pref &pref.Preferences // Preferences shared from V struct
builtin_mod bool
mod string
expr_mod string
expected_type table.Type
scope &ast.Scope
imports map[string]string
ast_imports []ast.Import
@ -482,20 +479,12 @@ pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident {
fn (p mut Parser) struct_init() ast.StructInit {
typ := p.parse_type()
p.expr_mod = ''
sym := p.table.get_type_symbol(typ)
// sym := p.table.get_type_symbol(typ)
// p.warn('struct init typ=$sym.name')
p.check(.lcbr)
mut field_names := []string
mut exprs := []ast.Expr
mut i := 0
// TODO `if sym.info is table.Struct`
mut is_struct := false
match sym.info {
table.Struct {
is_struct = true
}
else {}
}
is_short_syntax := !(p.peek_tok.kind == .colon || p.tok.kind == .rcbr) // `Vec{a,b,c}`
// p.warn(is_short_syntax.str())
for p.tok.kind != .rcbr {
@ -508,20 +497,6 @@ fn (p mut Parser) struct_init() ast.StructInit {
field_name = p.check_name()
field_names << field_name
}
// Set expected type for this field's expression
// p.warn('$sym.name field $field_name')
if is_struct {
info := sym.info as table.Struct
if is_short_syntax {}
else {
field := sym.find_field(field_name) or {
p.error('field `${sym.name}.$field_name` not found')
continue
}
p.expected_type = field.typ
}
// p.warn('setting exp $field.typ')
}
if !is_short_syntax {
p.check(.colon)
expr := p.expr(0)
@ -812,6 +787,13 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
}
else if p.tok.kind.is_infix() {
node = p.infix_expr(node)
match node {
ast.PrefixExpr {
println('IS PREFIX')
return node
}
else {}
}
}
// Postfix
else if p.tok.kind in [.inc, .dec] {
@ -950,6 +932,12 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
precedence := p.tok.precedence()
pos := p.tok.position()
p.next()
if op == .mul && p.peek_tok.kind in [.assign, .decl_assign] {
return ast.PrefixExpr{
op: op
right: p.expr(0)
}
}
mut right := ast.Expr{}
right = p.expr(precedence)
mut expr := ast.Expr{}
@ -975,19 +963,6 @@ fn (p &Parser) is_addative() bool {
fn (p mut Parser) enum_val() ast.EnumVal {
p.check(.dot)
val := p.check_name()
/*
if p.expected_type == 0 {
p.error('wtf')
}
*/
/*
sym := p.table.get_type_symbol(p.expected_type) //or {
//return ast.EnumVal{val:val}
//}
p.warn(sym.name)
*/
return ast.EnumVal{
val: val
pos: p.tok.position()
@ -1469,18 +1444,14 @@ fn (p mut Parser) return_stmt() ast.Return {
p.next()
// return expressions
mut exprs := []ast.Expr
// return type idents
// mut got_tis := []table.Type
if table.type_idx(p.return_type) == table.void_type_idx {
if p.tok.kind == .rcbr {
return ast.Return{
pos: p.tok.position()
}
}
for {
// expr,ti := p.expr(0)
expr := p.expr(0)
exprs << expr
// got_tis << ti
if p.tok.kind == .comma {
p.check(.comma)
}
@ -1489,7 +1460,6 @@ fn (p mut Parser) return_stmt() ast.Return {
}
}
stmt := ast.Return{
expected_type: p.return_type
exprs: exprs
pos: p.tok.position()
}