@@ -134,12 +134,101 @@ fn ClosureInterpreter::visit_tuple(
134134 Tuple (evaluated_values)
135135}
136136
137+ ///|
138+ fn ClosureInterpreter ::syntax_iterable_to_iter(
139+ self : ClosureInterpreter ,
140+ expr : @syntax.Expr ,
141+ ) -> Iter [RuntimeValue ] raise ControlFlow {
142+ match expr {
143+ Infix (op~, lhs~, rhs~, ..) =>
144+ match op.name {
145+ Ident (name="..<") => {
146+ let start_val = self.visit(lhs)
147+ let end_val = self.visit(rhs)
148+ match (start_val, end_val) {
149+ (Int (start, ..), Int (end, ..)) =>
150+ start.until(end).map(fn(i) { Int (i, raw=None ) })
151+ _ => Iter ::empty()
152+ }
153+ }
154+ Ident (name="..=") => {
155+ let start_val = self.visit(lhs)
156+ let end_val = self.visit(rhs)
157+ match (start_val, end_val) {
158+ (Int (start, ..), Int (end, ..)) =>
159+ start.until(end, inclusive=true ).map(fn(i) { Int (i, raw=None ) })
160+ _ => Iter ::empty()
161+ }
162+ }
163+ _ => self.visit_iterable_expr(expr)
164+ }
165+ _ => self.visit_iterable_expr(expr)
166+ }
167+ }
168+
169+ ///|
170+ fn ClosureInterpreter ::visit_iterable_expr(
171+ self : ClosureInterpreter ,
172+ expr : @syntax.Expr ,
173+ ) -> Iter [RuntimeValue ] raise ControlFlow {
174+ match self.visit(expr) {
175+ Iter (iter) => iter
176+ value =>
177+ match self.method_call(value, "iter", @list.new()) {
178+ Iter (iter) => iter
179+ _ => self.error("iter method not found")
180+ }
181+ }
182+ }
183+
184+ ///|
185+ fn ClosureInterpreter ::visit_for_each_as_array(
186+ self : ClosureInterpreter ,
187+ binders : @list.List [@syntax.Binder? ],
188+ expr : @syntax.Expr ,
189+ body : @syntax.Expr ,
190+ ) -> RuntimeValue raise ControlFlow {
191+ let result : Array [RuntimeValue ] = []
192+ let binder_count = binders.length()
193+ for value in self.syntax_iterable_to_iter(expr) {
194+ self.push_scope(RuntimeLocation ::ControlFlow ("list comprehension"))
195+ defer self.pop_scope()
196+ match binders {
197+ @list.More (Some (binder), tail=@list.Empty ) =>
198+ self.current_pkg.env.set(binder.name, value)
199+ @list.More (
200+ Some (binder1),
201+ tail=@list.More (Some (binder2), tail=@list.Empty )
202+ ) =>
203+ match value {
204+ Tuple ([first, second]) => {
205+ self.current_pkg.env.set(binder1.name, first)
206+ self.current_pkg.env.set(binder2.name, second)
207+ }
208+ _ => ()
209+ }
210+ _ => if binder_count == 0 { () }
211+ }
212+ let value = self.visit(body) catch {
213+ Continue (_) => continue
214+ e => raise e
215+ }
216+ result.push(value)
217+ }
218+ Array (result)
219+ }
220+
137221///|
138222/// 处理数组表达式
139223fn ClosureInterpreter ::visit_array(
140224 self : ClosureInterpreter ,
141225 exprs : @list.List [@syntax.Expr ],
142226) -> RuntimeValue raise ControlFlow {
227+ match exprs {
228+ @list.More (ForEach (binders~, expr~, body~, ..), tail=@list.Empty ) =>
229+ return self.visit_for_each_as_array(binders, expr, body)
230+ _ => ()
231+ }
143232 let result_values = []
144233 for expr in exprs {
145234 match expr {
@@ -190,6 +279,40 @@ fn ClosureInterpreter::call_struct_constr(
190279 }
191280}
192281
282+ ///|
283+ fn ClosureInterpreter ::call_struct_constr_with_type_name(
284+ self : ClosureInterpreter ,
285+ type_name : @syntax.TypeName ,
286+ constr_name : String ,
287+ field_values : @list.List [@syntax.Argument ],
288+ ) -> RuntimeValue? raise ControlFlow {
289+ self.with_ident(type_name.name, (pkg, name) => {
290+ let ty = pkg.find_static_type(name)
291+ if pkg.struct_constrs.get(name) is Some (method_name) {
292+ let method_name = if constr_name == "" {
293+ method_name
294+ } else {
295+ constr_name
296+ }
297+ Some (self.execute_static_method_call(ty, method_name, field_values))
298+ } else {
299+ None
300+ }
301+ })
302+ }
303+
304+ ///|
305+ fn ClosureInterpreter ::constructor_runtime_package(
306+ self : ClosureInterpreter ,
307+ constr : @syntax.Constructor ,
308+ ) -> RuntimePackage {
309+ match constr.extra_info {
310+ Package (pkg_name) => self.find_pkg(pkg_name)
311+ TypeName (type_name) => self.with_ident(type_name.name, (pkg, _) => pkg)
312+ NoExtraInfo => self.current_pkg
313+ }
314+ }
315+
193316///|
194317/// 处理函数调用
195318fn ClosureInterpreter ::visit_apply(
@@ -203,16 +326,15 @@ fn ClosureInterpreter::visit_apply(
203326 // 处理构造函数调用,如 Some(5)
204327 Constr (constr~, ..) => {
205328 match constr.extra_info {
206- TypeName (type_name) => {
207- let constr_type_name = match type_name.name {
208- Ident (name~) => name
209- Dot (id~, ..) => id
210- }
211- if self.call_struct_constr( None , constr_type_name, args )
329+ TypeName (type_name) =>
330+ if self.call_struct_constr_with_type_name(
331+ type_name,
332+ constr.name.name,
333+ args,
334+ )
212335 is Some (result) {
213336 return result
214337 }
215- }
216338 Package (pkg_name) if self.call_struct_constr(
217339 Some (pkg_name),
218340 constr.name.name,
@@ -245,9 +367,10 @@ fn ClosureInterpreter::visit_apply(
245367 })
246368 .to_array()
247369 let constr_name = constr.name.name
370+ let pkg = self.constructor_runtime_package(constr)
248371 Constructor ({
249372 val: { name: constr_name, fields },
250- ty: self.current_pkg .find_static_type(constr_name),
373+ ty: pkg .find_static_type(constr_name),
251374 })
252375 }
253376 // 处理静态方法调用,如 Bool::default()
0 commit comments