(define process-multi-variable
(lambda (e em var)
(let* ((multi (string-length (caddr var)))
(ems (map variable-constructor (cdr em)))
;; (e2 (check-for-literal-match-or-pair e ems))
(e2 (check-for-literal-match e ems))
; (emlgth (if (> multi 1) (get-em-length ems var) 0))
(emlgth (get-em-length ems var))
(elgth (length e))
(matchlgth (if (> multi 1) (get-matching-length e2 var) 0))
; (globlgth (if (= elgth emlgth)
; (if (= multi 3) 0 matchlgth)
; (min matchlgth (- elgth emlgth))))
(globlgth (min matchlgth (- elgth emlgth)))
(matchedlst (list-head e globlgth))
(restlst (if (> globlgth 0) (list-tail e globlgth) '())))
;; (println 'process-multi multi 'e e 'em em 'emlgth emlgth 'elgth elgth 'matchlght matchlgth 'matched matchedlst 'res restlst)
(if (> multi 1)
(if (= matchlgth 0)
(if (= multi 2) ;; if ??
(list MATCH-FAILED) ;; then fail
(match-list e (cdr em))) ;; otherwise ??? means 0 or more so just skip forward var (not expr)
(list (match-expr matchedlst (string->symbol (string-append (cadr var) (caddr var) "list")))
(if (null? restlst) '() (match-list restlst (cdr em)))))
(if (<> (length e) (length em))
MATCH-FAILED
(list (match-expr (car e) (car em))
(if (null? (cdr e))
'()
(match-expr (cdr e) (cdr em)))))))))