(define impc:ir:compiler:loop
(lambda (ast types)
(let* ((os (make-string 0))
(loop-num (llvm:count++))
(loop-label (string-append "loop" (number->string loop-num) ":"))
(closeloop-label (string-append "closeloop" (number->string loop-num) ":"))
(after-label (string-append "after" (number->string loop-num) ":"))
(loop (string-append "%loop" (number->string loop-num)))
(closeloop (string-append "%closeloop" (number->string loop-num)))
(cmp (string-append "%cmp" (number->string loop-num)))
(after (string-append "%after" (number->string loop-num)))
(iterator (string-append "%" (symbol->string (caar ast)))) ; "Loop" (number->string loop-num)))
(iterator-type (impc:ir:get-type-str (cdr (assoc-strcmp (caar ast) types))))
(startstr (impc:ir:compiler (cadar ast) types (impc:ir:get-type-from-str iterator-type)))
(start (impc:ir:gname))
(start-value? (> (length (car ast)) 2))
(numstr (impc:ir:compiler (if start-value?
(caddar ast)
(cadar ast))
types
(cdr (assoc-strcmp (caar ast) types))))
(num (impc:ir:gname)) ;(ir:eval (cadar ast) os stack sym-table))
(bodystr (impc:ir:compiler (cdr ast) types)))
(emit "; setup loop\n" os)
;(print num 'numstr numstr)
(emit numstr os)
(if start-value? (emit startstr os))