v2: match expr fixes & tmp typeof skip

pull/3908/merge
Joe Conigliaro 2020-03-02 20:53:38 +11:00
parent a8f07157dd
commit f57a651e3b
3 changed files with 31 additions and 8 deletions

View File

@ -12,7 +12,7 @@ pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLitera
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr | CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr |
ConcatExpr ConcatExpr | TypeName
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 |
@ -27,6 +27,12 @@ pub struct StructType {
pub struct ArrayType {} pub struct ArrayType {}
pub struct TypeName {
pub:
name string
typ table.Type
}
// | IncDecStmt k // | IncDecStmt k
// Stand-alone expression in a statement list. // Stand-alone expression in a statement list.
pub struct ExprStmt { pub struct ExprStmt {

View File

@ -175,6 +175,10 @@ fn (c mut Checker) check_assign_expr(assign_expr ast.AssignExpr) {
pub fn (c mut Checker) call_expr(call_expr ast.CallExpr) table.Type { pub fn (c mut Checker) call_expr(call_expr ast.CallExpr) table.Type {
fn_name := call_expr.name fn_name := call_expr.name
// TODO: impl typeof properly (probably not going to be a fn call)
if fn_name == 'typeof' {
return table.string_type
}
mut found := false mut found := false
// start hack: until v1 is fixed and c definitions are added for these // start hack: until v1 is fixed and c definitions are added for these
if fn_name == 'C.calloc' { if fn_name == 'C.calloc' {
@ -553,6 +557,9 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
ast.StructInit { ast.StructInit {
return c.check_struct_init(it) return c.check_struct_init(it)
} }
ast.TypeName {
return it.typ
}
/* /*
ast.UnaryExpr { ast.UnaryExpr {
c.expr(it.left) c.expr(it.left)
@ -656,7 +663,12 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
if i < node.match_exprs.len { if i < node.match_exprs.len {
match_expr := node.match_exprs[i] match_expr := node.match_exprs[i]
c.expected_type = t c.expected_type = t
c.expr(match_expr) typ := c.expr(match_expr)
typ_sym := c.table.get_type_symbol(typ)
// TODO:
if typ_sym.kind == .sum_type {
}
} }
c.stmts(block.stmts) c.stmts(block.stmts)
// If the last statement is an expression, return its type // If the last statement is an expression, return its type

View File

@ -1732,16 +1732,21 @@ fn (p mut Parser) match_expr() ast.Expr {
mut match_exprs := []ast.Expr mut match_exprs := []ast.Expr
// mut return_type := table.void_type // mut return_type := table.void_type
for { for {
p.open_scope()
// Sum type match // Sum type match
if p.tok.kind == .name && (p.tok.lit[0].is_capital() || p.peek_tok.kind == .dot) { if p.tok.kind == .name && (p.tok.lit[0].is_capital() || p.peek_tok.kind == .dot) {
// if sym.kind == .sum_type { // if sym.kind == .sum_type {
// p.warn('is sum') // p.warn('is sum')
// p.parse_type() typ := p.parse_type()
p.check_name() typ_sym := p.table.get_type_symbol(typ)
if p.tok.kind == .dot { match_exprs << ast.TypeName{
p.check(.dot) name: typ_sym.name
p.check_name() typ: typ
} }
p.scope.register_var(ast.VarDecl{
name: 'it'
typ: typ
})
} }
else { else {
// Expression match // Expression match
@ -1778,7 +1783,7 @@ fn (p mut Parser) match_expr() ast.Expr {
} }
} }
*/ */
p.close_scope()
if p.tok.kind == .rcbr { if p.tok.kind == .rcbr {
break break
} }