v2: remove type fields from parser & some cleanup
							parent
							
								
									38a54b08e3
								
							
						
					
					
						commit
						272eaaa704
					
				| 
						 | 
					@ -175,7 +175,6 @@ pub:
 | 
				
			||||||
pub struct Return {
 | 
					pub struct Return {
 | 
				
			||||||
pub:
 | 
					pub:
 | 
				
			||||||
	pos           token.Position
 | 
						pos           token.Position
 | 
				
			||||||
	expected_type table.Type // TODO: remove once checker updated
 | 
					 | 
				
			||||||
	exprs         []Expr
 | 
						exprs         []Expr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -300,16 +300,11 @@ pub fn (c mut Checker) selector_expr(selector_expr ast.SelectorExpr) table.Type
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: non deferred
 | 
					// TODO: non deferred
 | 
				
			||||||
pub fn (c mut Checker) return_stmt(return_stmt ast.Return) {
 | 
					pub fn (c mut Checker) return_stmt(return_stmt ast.Return) {
 | 
				
			||||||
	mut got_types := []table.Type
 | 
					 | 
				
			||||||
	c.expected_type = c.fn_return_type
 | 
						c.expected_type = c.fn_return_type
 | 
				
			||||||
	if return_stmt.exprs.len == 0 {
 | 
						if return_stmt.exprs.len == 0 {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for expr in return_stmt.exprs {
 | 
						expected_type := c.fn_return_type
 | 
				
			||||||
		typ := c.expr(expr)
 | 
					 | 
				
			||||||
		got_types << typ
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	expected_type := return_stmt.expected_type
 | 
					 | 
				
			||||||
	expected_type_sym := c.table.get_type_symbol(expected_type)
 | 
						expected_type_sym := c.table.get_type_symbol(expected_type)
 | 
				
			||||||
	exp_is_optional := table.type_is_optional(expected_type)
 | 
						exp_is_optional := table.type_is_optional(expected_type)
 | 
				
			||||||
	mut expected_types := [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
 | 
							mr_info := expected_type_sym.info as table.MultiReturn
 | 
				
			||||||
		expected_types = mr_info.types
 | 
							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
 | 
						// 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']] {
 | 
						if exp_is_optional && table.type_idx(got_types[0]) in [table.none_type_idx, c.table.type_idxs['Option']] {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -120,28 +120,11 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		// p.table.register_var(var)
 | 
							// 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
 | 
						// Return type
 | 
				
			||||||
	mut typ := table.void_type
 | 
						mut typ := table.void_type
 | 
				
			||||||
	if p.tok.kind.is_start_of_type() {
 | 
						if p.tok.kind.is_start_of_type() {
 | 
				
			||||||
		typ = p.parse_type()
 | 
							typ = p.parse_type()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	p.return_type = typ
 | 
					 | 
				
			||||||
	// Register
 | 
						// Register
 | 
				
			||||||
	if is_method {
 | 
						if is_method {
 | 
				
			||||||
		type_sym := p.table.get_type_symbol(rec_type)
 | 
							type_sym := p.table.get_type_symbol(rec_type)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -34,16 +34,13 @@ mut:
 | 
				
			||||||
	peek_tok      token.Token
 | 
						peek_tok      token.Token
 | 
				
			||||||
	// vars []string
 | 
						// vars []string
 | 
				
			||||||
	table         &table.Table
 | 
						table         &table.Table
 | 
				
			||||||
	return_type   table.Type // current function's return type
 | 
					 | 
				
			||||||
	is_c          bool
 | 
						is_c          bool
 | 
				
			||||||
	//
 | 
					 | 
				
			||||||
	// prefix_parse_fns []PrefixParseFn
 | 
						// prefix_parse_fns []PrefixParseFn
 | 
				
			||||||
	inside_if     bool
 | 
						inside_if     bool
 | 
				
			||||||
	pref          &pref.Preferences // Preferences shared from V struct
 | 
						pref          &pref.Preferences // Preferences shared from V struct
 | 
				
			||||||
	builtin_mod   bool
 | 
						builtin_mod   bool
 | 
				
			||||||
	mod           string
 | 
						mod           string
 | 
				
			||||||
	expr_mod      string
 | 
						expr_mod      string
 | 
				
			||||||
	expected_type table.Type
 | 
					 | 
				
			||||||
	scope         &ast.Scope
 | 
						scope         &ast.Scope
 | 
				
			||||||
	imports       map[string]string
 | 
						imports       map[string]string
 | 
				
			||||||
	ast_imports   []ast.Import
 | 
						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 {
 | 
					fn (p mut Parser) struct_init() ast.StructInit {
 | 
				
			||||||
	typ := p.parse_type()
 | 
						typ := p.parse_type()
 | 
				
			||||||
	p.expr_mod = ''
 | 
						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.warn('struct init typ=$sym.name')
 | 
				
			||||||
	p.check(.lcbr)
 | 
						p.check(.lcbr)
 | 
				
			||||||
	mut field_names := []string
 | 
						mut field_names := []string
 | 
				
			||||||
	mut exprs := []ast.Expr
 | 
						mut exprs := []ast.Expr
 | 
				
			||||||
	mut i := 0
 | 
						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}`
 | 
						is_short_syntax := !(p.peek_tok.kind == .colon || p.tok.kind == .rcbr) // `Vec{a,b,c}`
 | 
				
			||||||
	// p.warn(is_short_syntax.str())
 | 
						// p.warn(is_short_syntax.str())
 | 
				
			||||||
	for p.tok.kind != .rcbr {
 | 
						for p.tok.kind != .rcbr {
 | 
				
			||||||
| 
						 | 
					@ -508,20 +497,6 @@ fn (p mut Parser) struct_init() ast.StructInit {
 | 
				
			||||||
			field_name = p.check_name()
 | 
								field_name = p.check_name()
 | 
				
			||||||
			field_names << field_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 {
 | 
							if !is_short_syntax {
 | 
				
			||||||
			p.check(.colon)
 | 
								p.check(.colon)
 | 
				
			||||||
			expr := p.expr(0)
 | 
								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() {
 | 
							else if p.tok.kind.is_infix() {
 | 
				
			||||||
			node = p.infix_expr(node)
 | 
								node = p.infix_expr(node)
 | 
				
			||||||
 | 
								match node {
 | 
				
			||||||
 | 
									ast.PrefixExpr {
 | 
				
			||||||
 | 
										println('IS PREFIX')
 | 
				
			||||||
 | 
										return node
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									else {}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// Postfix
 | 
							// Postfix
 | 
				
			||||||
		else if p.tok.kind in [.inc, .dec] {
 | 
							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()
 | 
						precedence := p.tok.precedence()
 | 
				
			||||||
	pos := p.tok.position()
 | 
						pos := p.tok.position()
 | 
				
			||||||
	p.next()
 | 
						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{}
 | 
						mut right := ast.Expr{}
 | 
				
			||||||
	right = p.expr(precedence)
 | 
						right = p.expr(precedence)
 | 
				
			||||||
	mut expr := ast.Expr{}
 | 
						mut expr := ast.Expr{}
 | 
				
			||||||
| 
						 | 
					@ -975,19 +963,6 @@ fn (p &Parser) is_addative() bool {
 | 
				
			||||||
fn (p mut Parser) enum_val() ast.EnumVal {
 | 
					fn (p mut Parser) enum_val() ast.EnumVal {
 | 
				
			||||||
	p.check(.dot)
 | 
						p.check(.dot)
 | 
				
			||||||
	val := p.check_name()
 | 
						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{
 | 
						return ast.EnumVal{
 | 
				
			||||||
		val: val
 | 
							val: val
 | 
				
			||||||
		pos: p.tok.position()
 | 
							pos: p.tok.position()
 | 
				
			||||||
| 
						 | 
					@ -1469,18 +1444,14 @@ fn (p mut Parser) return_stmt() ast.Return {
 | 
				
			||||||
	p.next()
 | 
						p.next()
 | 
				
			||||||
	// return expressions
 | 
						// return expressions
 | 
				
			||||||
	mut exprs := []ast.Expr
 | 
						mut exprs := []ast.Expr
 | 
				
			||||||
	// return type idents
 | 
						if p.tok.kind == .rcbr {
 | 
				
			||||||
	// mut got_tis := []table.Type
 | 
					 | 
				
			||||||
	if table.type_idx(p.return_type) == table.void_type_idx {
 | 
					 | 
				
			||||||
		return ast.Return{
 | 
							return ast.Return{
 | 
				
			||||||
			pos: p.tok.position()
 | 
								pos: p.tok.position()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		// expr,ti := p.expr(0)
 | 
					 | 
				
			||||||
		expr := p.expr(0)
 | 
							expr := p.expr(0)
 | 
				
			||||||
		exprs << expr
 | 
							exprs << expr
 | 
				
			||||||
		// got_tis << ti
 | 
					 | 
				
			||||||
		if p.tok.kind == .comma {
 | 
							if p.tok.kind == .comma {
 | 
				
			||||||
			p.check(.comma)
 | 
								p.check(.comma)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -1489,7 +1460,6 @@ fn (p mut Parser) return_stmt() ast.Return {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	stmt := ast.Return{
 | 
						stmt := ast.Return{
 | 
				
			||||||
		expected_type: p.return_type
 | 
					 | 
				
			||||||
		exprs: exprs
 | 
							exprs: exprs
 | 
				
			||||||
		pos: p.tok.position()
 | 
							pos: p.tok.position()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue