29 lines
1018 B
Plaintext
29 lines
1018 B
Plaintext
(defndynamic null? [l] (= (length l) 0))
|
|
|
|
(defndynamic infix-prec [op]
|
|
(if (or (= op '+) (= op '-)) 1
|
|
(if (= 'pow op) 3
|
|
2)))
|
|
|
|
(defndynamic infix-op [expr operators operands]
|
|
(infix-parse expr
|
|
(cdr operators)
|
|
(cons (list (car operators) (cadr operands) (car operands))
|
|
(cddr operands))))
|
|
|
|
(defndynamic infix-parse [expr operators operands]
|
|
(if (null? expr)
|
|
(if (null? operators)
|
|
(car operands)
|
|
(infix-op expr operators operands))
|
|
(if (list? (car expr))
|
|
(infix-parse (cdr expr) operators (cons (infix-parse (car expr) '() '()) operands))
|
|
(if (symbol? (car expr))
|
|
(if (or (null? operators)
|
|
(> (infix-prec (car expr)) (infix-prec (car operators))))
|
|
(infix-parse (cdr expr) (cons (car expr) operators) operands)
|
|
(infix-op expr operators operands))
|
|
(infix-parse (cdr expr) operators (cons (car expr) operands))))))
|
|
|
|
(defmacro infix [:rest args] (infix-parse args '() '()))
|