Guest User

Untitled

a guest
Oct 8th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. (eval-when (:compile-toplevel :execute :load-toplevel)
  2. (defvar *infix-precedence* '((* /)
  3. (+ -)
  4. (> < >= <= == !=)
  5. (&&)
  6. (||)))
  7.  
  8. (defvar *unary-ops* '(+ -))
  9.  
  10. (defun infix-to-prefix (expr ops stop-ops)
  11. (let ((first (car expr))
  12. (rest (cdr expr)))
  13. (cond
  14. ((and (listp first) (not (eq (car first) 'op-wrapper)))
  15. (infix-to-prefix (nconc
  16. (infix-to-prefix first *infix-precedence* nil)
  17. rest)
  18. ops
  19. stop-ops))
  20. ((find first *unary-ops*)
  21. (infix-to-prefix (list*
  22. (list 'op-wrapper
  23. first
  24. (car (infix-to-prefix
  25. (list (car rest))
  26. nil
  27. nil)))
  28. (cdr rest))
  29. ops
  30. stop-ops))
  31. ((equal (car ops) stop-ops) expr)
  32. ((find (car rest) (car ops))
  33. (infix-to-prefix
  34. (let ((e (infix-to-prefix (cdr rest)
  35. *infix-precedence*
  36. (car ops))))
  37. (list* (list 'op-wrapper (car rest) first (car e))
  38. (cdr e)))
  39. ops
  40. stop-ops))
  41. ((consp (car rest))
  42. (infix-to-prefix
  43. (list* (list* 'op-wrapper
  44. first
  45. (mapcar (lambda (x)
  46. (car (infix-to-prefix
  47. (list x)
  48. *infix-precedence*
  49. nil)))
  50. (car rest)))
  51. (cdr rest))
  52. ops
  53. stop-ops))
  54. (ops (infix-to-prefix (list* first (cdr expr))
  55. (cdr ops)
  56. stop-ops))
  57. (t first)))))
  58.  
  59. (defmacro op-wrapper (&rest args) args)
  60.  
  61. (defmacro c-expr (&rest expr)
  62. (car (infix-to-prefix expr *infix-precedence* nil)))
  63.  
  64. (defun == (left right) (equal left right))
  65. (defun != (left right) (not (equal left right)))
  66. (defun && (left right) (and left right))
  67. (defun || (left right) (or left right))
  68.  
  69. ; (trace infix-to-prefix)
  70.  
  71. (defun sum (x y) (+ x y))
  72.  
  73. (print (c-expr - 2 + sum ( 10 20 ) == 28 ))
Add Comment
Please, Sign In to add comment