Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defvar *infix-precedence* '((* /)
- (+ -)
- (> < >= <= == !=)
- (&&)
- (||)))
- (defvar *unary-ops* '(+ -))
- (defun infix-to-prefix (expr ops stop_ops)
- (cond ((and (listp (car expr)) (not (equal (car (car expr)) 'op-wrapper)))
- (infix-to-prefix
- (nconc
- (infix-to-prefix (car expr) *infix-precedence* nil)
- (cdr expr))
- ops
- stop_ops))
- ((find (car expr) *unary-ops*)
- (infix-to-prefix
- (nconc
- (list (list 'op-wrapper
- (car expr)
- (car
- (infix-to-prefix
- (list (cadr expr))
- nil
- nil))))
- (cddr expr))
- ops
- stop_ops))
- ((equal (car ops) stop_ops)
- expr)
- ((find (cadr expr) (car ops))
- (infix-to-prefix
- (let ((e (infix-to-prefix
- (cddr expr)
- *infix-precedence*
- (car ops))))
- (nconc
- (list (list 'op-wrapper
- (cadr expr)
- (car expr)
- (car e)))
- (cdr e)))
- ops
- stop_ops))
- ((and (cadr expr) (listp (cadr expr)))
- (infix-to-prefix
- (nconc
- (list (nconc
- (list
- 'op-wrapper
- (car expr))
- (mapcar (lambda (x)
- (car (infix-to-prefix
- (list x)
- *infix-precedence*
- nil)))
- (cadr expr))))
- (cddr expr))
- ops
- stop_ops))
- (ops
- (infix-to-prefix
- (nconc (list (car expr))
- (cdr expr))
- (cdr ops)
- stop_ops))
- (T (car expr))))
- (defmacro op-wrapper (&rest args) args)
- (defmacro c-expr (&rest expr)
- (car (infix-to-prefix expr *infix-precedence* nil)))
- (defun == (left right) (equal left right))
- (defun != (left right) (not (equal left right)))
- (defun && (left right) (and left right))
- (defun || (left right) (or left right))
- (defun sum (x y) (+ x y))
- (print (c-expr - 2 + sum ( 10 20 ) == 28))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement