@@ -14,6 +14,10 @@ use types::{ARRAY_INTERNAL_NAME, ARRAY_PUSH, ARRAY_WITH_CAPACITY};
14
14
15
15
const BUILTIN_RECEIVER : & str = "_INKO" ;
16
16
const ARRAY_LIT_VAR : & str = "$array" ;
17
+ const ITER_VAR : & str = "$iter" ;
18
+ const NEXT_CALL : & str = "next" ;
19
+ const SOME_CONS : & str = "Some" ;
20
+ const INTO_ITER_CALL : & str = "into_iter" ;
17
21
18
22
struct Comments {
19
23
nodes : Vec < ast:: Comment > ,
@@ -2476,6 +2480,9 @@ impl<'a> LowerToHir<'a> {
2476
2480
ast:: Expression :: While ( node) => {
2477
2481
Expression :: Loop ( self . while_expression ( * node) )
2478
2482
}
2483
+ ast:: Expression :: For ( node) => {
2484
+ Expression :: Scope ( self . for_expression ( * node) )
2485
+ }
2479
2486
ast:: Expression :: Scope ( node) => {
2480
2487
Expression :: Scope ( self . scope ( * node) )
2481
2488
}
@@ -3176,6 +3183,100 @@ impl<'a> LowerToHir<'a> {
3176
3183
Box :: new ( Loop { body, location : node. location } )
3177
3184
}
3178
3185
3186
+ fn for_expression ( & mut self , node : ast:: For ) -> Box < Scope > {
3187
+ let pat_loc = * node. pattern . location ( ) ;
3188
+ let iter_loc = * node. iterator . location ( ) ;
3189
+ let def_var = Expression :: DefineVariable ( Box :: new ( DefineVariable {
3190
+ resolved_type : types:: TypeRef :: Unknown ,
3191
+ variable_id : None ,
3192
+ mutable : false ,
3193
+ name : Identifier { name : ITER_VAR . to_string ( ) , location : pat_loc } ,
3194
+ value_type : None ,
3195
+ value : Expression :: Call ( Box :: new ( Call {
3196
+ kind : types:: CallKind :: Unknown ,
3197
+ receiver : Some ( self . expression ( node. iterator ) ) ,
3198
+ name : Identifier {
3199
+ name : INTO_ITER_CALL . to_string ( ) ,
3200
+ location : iter_loc,
3201
+ } ,
3202
+ arguments : Vec :: new ( ) ,
3203
+ parens : false ,
3204
+ in_mut : false ,
3205
+ usage : Usage :: Used ,
3206
+ location : iter_loc,
3207
+ } ) ) ,
3208
+ location : pat_loc,
3209
+ } ) ) ;
3210
+
3211
+ let loop_expr = Expression :: Loop ( Box :: new ( Loop {
3212
+ body : vec ! [ Expression :: Match ( Box :: new( Match {
3213
+ resolved_type: types:: TypeRef :: Unknown ,
3214
+ // iter.next
3215
+ expression: Expression :: Call ( Box :: new( Call {
3216
+ kind: types:: CallKind :: Unknown ,
3217
+ receiver: Some ( Expression :: IdentifierRef ( Box :: new(
3218
+ IdentifierRef {
3219
+ name: ITER_VAR . to_string( ) ,
3220
+ kind: types:: IdentifierKind :: Unknown ,
3221
+ usage: Usage :: Used ,
3222
+ location: iter_loc,
3223
+ } ,
3224
+ ) ) ) ,
3225
+ name: Identifier {
3226
+ name: NEXT_CALL . to_string( ) ,
3227
+ location: iter_loc,
3228
+ } ,
3229
+ arguments: Vec :: new( ) ,
3230
+ parens: false ,
3231
+ in_mut: false ,
3232
+ usage: Usage :: Used ,
3233
+ location: iter_loc,
3234
+ } ) ) ,
3235
+ cases: vec![
3236
+ // case Some(...) -> body
3237
+ MatchCase {
3238
+ variable_ids: Vec :: new( ) ,
3239
+ pattern: Pattern :: Constructor ( Box :: new(
3240
+ ConstructorPattern {
3241
+ constructor_id: None ,
3242
+ name: Constant {
3243
+ name: SOME_CONS . to_string( ) ,
3244
+ location: node. location,
3245
+ } ,
3246
+ values: vec![ self . pattern( node. pattern) ] ,
3247
+ location: node. location,
3248
+ } ,
3249
+ ) ) ,
3250
+ guard: None ,
3251
+ body: self . expressions( node. body) ,
3252
+ location: pat_loc,
3253
+ } ,
3254
+ // case _ -> break
3255
+ MatchCase {
3256
+ variable_ids: Vec :: new( ) ,
3257
+ pattern: Pattern :: Wildcard ( Box :: new( WildcardPattern {
3258
+ location: node. location,
3259
+ } ) ) ,
3260
+ guard: None ,
3261
+ body: vec![ Expression :: Break ( Box :: new( Break {
3262
+ location: node. location,
3263
+ } ) ) ] ,
3264
+ location: node. location,
3265
+ } ,
3266
+ ] ,
3267
+ location: node. location,
3268
+ write_result: false ,
3269
+ } ) ) ] ,
3270
+ location : node. location ,
3271
+ } ) ) ;
3272
+
3273
+ Box :: new ( Scope {
3274
+ resolved_type : types:: TypeRef :: Unknown ,
3275
+ body : vec ! [ def_var, loop_expr] ,
3276
+ location : node. location ,
3277
+ } )
3278
+ }
3279
+
3179
3280
fn scope ( & mut self , node : ast:: Scope ) -> Box < Scope > {
3180
3281
Box :: new ( Scope {
3181
3282
resolved_type : types:: TypeRef :: Unknown ,
0 commit comments