(bind-func cas_parse_terms
(lambda (ein:String*)
;; (println "Parse:" ein)
(set! cas_cntterms 0)
(let ((e (replace ein "ln" "log"))
(m1 (find_sexprs e))
(m2 (map (lambda (x:String*)
(if (regex_match "(\\+|\\-)" x)
(cas_parse_terms (trim_brackets x))
(list (cat (Str "(") (cas_expand_term (trim_brackets x)) (Str ")")))))
m1))
(m3 (map (lambda (l)
(if (null? l) (Str "")
(if (= (length l) 1) (car l)
(cat
(Str "(plus")
(concat (map (lambda (x:String*)
(cat
(Str " ")
(if (regex_match "^\\-" x)
(cas_expand_term
(cat (Str "(neg ")
(substring x 1 (length x))
(Str ")")))
(cas_expand_term (replace x "+" "")))))
l))
(Str ")")))))
m2))
(m4 (reverse (map (lambda (s1 s2)
(set! e (replace e s1 (replace_all s2 "+" "plus")))
e)
m1 m3)))
(m8 (regex_matches "(^|\\+|\\-)([^+-]|\\(plus)+" (if (> (length m4) 0) (car m4) e)))
(res (map (lambda (x:String*)
(if (regex_match "^\\-" x)
(let ((r1 (cas_expand_term (substring x 1 (length x))))
(r2 (cat (Str "(neg ") r1 (Str ")"))))
r2)
(if (regex_match "^\\+" x)
(cas_expand_term (substring x 1 (length x)))
(cas_expand_term x))))
m8)))
;;(println "out:" res "in:" ein "\n")
res)))