match: shadow variable instead of `it`
							parent
							
								
									c27a10b956
								
							
						
					
					
						commit
						c4b7d7cab3
					
				|  | @ -124,6 +124,26 @@ fn (var p Parser) match_expr() ast.MatchExpr { | ||||||
| 				p.parse_type() | 				p.parse_type() | ||||||
| 			} | 			} | ||||||
| 			is_sum_type = true | 			is_sum_type = true | ||||||
|  | 			// Make sure a variable used for the sum type match
 | ||||||
|  | 			var var_name := '' | ||||||
|  | 			match cond { | ||||||
|  | 				ast.Ident { | ||||||
|  | 					var_name = it.name | ||||||
|  | 				} | ||||||
|  | 				else { | ||||||
|  | 					// p.error('only variables can be used in sum types matches')
 | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			if var_name != '' { | ||||||
|  | 				// Register a shadow variable with the actual type
 | ||||||
|  | 				// (this replaces the old `it`)
 | ||||||
|  | 				// TODO doesn't work right now
 | ||||||
|  | 				p.scope.register(var_name, ast.Var{ | ||||||
|  | 					name: var_name | ||||||
|  | 					typ: table.type_to_ptr(typ) | ||||||
|  | 				}) | ||||||
|  | 				// println(var_name)
 | ||||||
|  | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			// Expression match
 | 			// Expression match
 | ||||||
| 			for { | 			for { | ||||||
|  | @ -172,4 +192,3 @@ fn (var p Parser) match_expr() ast.MatchExpr { | ||||||
| 		is_mut: is_mut | 		is_mut: is_mut | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -16,7 +16,7 @@ fn handle(e Expr) string { | ||||||
| 	match e { | 	match e { | ||||||
| 		IntegerLiteral { | 		IntegerLiteral { | ||||||
| 			assert it.val == '12' | 			assert it.val == '12' | ||||||
| 			// assert e.val == '12'
 | 			// assert e.val == '12' // TODO
 | ||||||
| 			return 'int' | 			return 'int' | ||||||
| 		} | 		} | ||||||
| 		IfExpr { | 		IfExpr { | ||||||
|  | @ -31,5 +31,5 @@ fn test_expr() { | ||||||
| 		val: '12' | 		val: '12' | ||||||
| 	} | 	} | ||||||
| 	assert handle(expr) == 'int' | 	assert handle(expr) == 'int' | ||||||
| 	// assert expr is IntegerLiteral
 | 	// assert expr is IntegerLiteral // TODO
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue