ForEach

Iterates over the elements of a container.

声明

语法

ForEach(expr var, expr container, stmt* body)

参数

Argument

描述

var

the variable each element is bound to.

container

the list, set or dictionary being iterated.

body

the statements run for each element.

返回值

无。

描述

ForEach is the for x in c of LPython. It has no Fortran spelling: a Fortran do loop always counts, and is a DoLoop.

The iteration order is the order of the container, which for a Set is unspecified.

示例

(ForEach
  :var (Var
    :v (SymbolRef 1 "i")
  )
  :container (Var
    :v (SymbolRef 1 "s")
  )
  :body [
    (Assignment
      :target (Var
        :v (SymbolRef 1 "total")
      )
      :value (IntegerBinOp
        :left (Var
          :v (SymbolRef 1 "total")
        )
        :op :Add
        :right (Var
          :v (SymbolRef 1 "i")
        )
        :type (Integer
          :kind 4
        )
        :value nil
      )
      :overloaded nil
      :realloc_lhs false
      :move_allocation false
    )
  ]
)

It comes from this complete ASR text document:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "main" (Program
        :symtab (SymbolTable
          :id 1
          :symbols {
            "i" (Variable
              :parent_symtab 1
              :name "i"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Integer
                :kind 4
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
            )
            "s" (Variable
              :parent_symtab 1
              :name "s"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Set
                :type (Integer
                  :kind 4
                )
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
            )
            "total" (Variable
              :parent_symtab 1
              :name "total"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Integer
                :kind 4
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
            )
          }
        )
        :name "main"
        :dependencies []
        :body [
          (ForEach
            :var (Var
              :v (SymbolRef 1 "i")
            )
            :container (Var
              :v (SymbolRef 1 "s")
            )
            :body [
              (Assignment
                :target (Var
                  :v (SymbolRef 1 "total")
                )
                :value (IntegerBinOp
                  :left (Var
                    :v (SymbolRef 1 "total")
                  )
                  :op :Add
                  :right (Var
                    :v (SymbolRef 1 "i")
                  )
                  :type (Integer
                    :kind 4
                  )
                  :value nil
                )
                :overloaded nil
                :realloc_lhs false
                :move_allocation false
              )
            ]
          )
        ]
      )
    }
  )
  :items []
)

也可以看看

DoLoop, Set, List, Dict