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