fin-lisp.lisp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. (ql:quickload "cl-store")
  2. (ql:quickload "cl-ppcre")
  3. ;;;; Reimplementation of my bills tracker in Lisp
  4. (defun reload ()
  5. (load "~/Repos/fin-lisp/fin-lisp.lisp"))
  6. ;;; All records exist in this data structure
  7. ;;; nil on start and loaded in from file
  8. ;;; *records* represents as hash of months,
  9. ;;; where the key is the month stamp, eg 20210701
  10. ;;; and the value is the monthly expenses hash
  11. (defvar *records* (make-hash-table :test 'equalp))
  12. (defun reset-records ()
  13. (setf *records* (make-hash-table :test 'equalp)))
  14. ;; Called like: (add-month '202107)
  15. (defun add-month (month-key)
  16. (setf (gethash month-key *records*) (make-hash-table :test 'equalp))
  17. month-key)
  18. ;;; Taken from practical common lisp
  19. (defun prompt-read (prompt)
  20. (format *query-io* "~a: " prompt)
  21. (force-output *query-io*)
  22. (read-line *query-io*))
  23. (defun prompt-for-expense ()
  24. (list
  25. (prompt-read "Enter expense name")
  26. (parse-integer
  27. (prompt-read "Enter expense value"))))
  28. (defun add-expense-to-month (month)
  29. (if (gethash month *records*)
  30. (let ((innerhash (gethash month *records*))
  31. (exp-l (prompt-for-expense)))
  32. (setf (gethash (first exp-l) innerhash) (second exp-l)))
  33. ;;NIL))
  34. (add-expense-to-month (add-month month))))
  35. ;;; Given key for *records* hash,
  36. ;;; print expenses/values for month
  37. (defun dump-month (month-key)
  38. (format t "~a~C" month-key #\linefeed)
  39. (let ((month-hash)
  40. (exp-keys))
  41. (setf month-hash (gethash month-key *records*))
  42. (setf exp-keys (loop for key being the hash-keys of month-hash collect key))
  43. (dolist (exp-key exp-keys)
  44. (format t "~a : ~a~C" exp-key (gethash exp-key month-hash) #\linefeed))))
  45. ;;; Dump all records.
  46. ;;; This will also be used for data serialization at some point
  47. (defun dump-records ()
  48. (let ((record-key-list (loop for key being the hash-keys of *records* collect key)))
  49. (dolist (month-key record-key-list) (dump-month month-key))))
  50. (defun serialize-records (filename)
  51. (cl-store:store *records* filename))
  52. (defun deserialize-records (filename)
  53. ;(setf *records* (cl-store:restore (pathname filename))))
  54. (setf *records* (cl-store:restore filename)))
  55. ;; Import records from old perl version (plaintext file)
  56. (defun import-records (filename)
  57. (let ((old-file-lines
  58. (with-open-file (stream filename)
  59. (loop for line = (read-line stream nil)
  60. while line
  61. collect line)))
  62. (mre (ppcre:create-scanner "^(.*)[0-9]{4}$"))
  63. (ere (ppcre:create-scanner "^([A-Z].*)\ -\ \\\$([0-9]{1,4}) - PAID"))
  64. (cur-mon)
  65. (cur-exp))
  66. (loop for line in old-file-lines
  67. do (progn
  68. (if (ppcre:scan mre line) (setf cur-mon line))
  69. (if (ppcre:scan ere line)
  70. (progn
  71. (setf cur-exp (ppcre:register-groups-bind (first second) (ere line) :sharedp t (list first second)))
  72. (print cur-exp)
  73. (if (gethash cur-mon *records*)
  74. (let ((innerhash (gethash cur-mon *records*)))
  75. (setf (gethash (first cur-exp) innerhash) (second cur-exp))))
  76. (if (not (gethash cur-mon *records*))
  77. (progn
  78. (add-month cur-mon)
  79. (let ((innerhash (gethash cur-mon *records*)))
  80. (setf (gethash (first cur-exp) innerhash) (second cur-exp)))))))))))
  81. (defmacro generic-handler (form error-string)
  82. `(handler-case ,form
  83. (error (e)
  84. (format t "Invalid input: ~a ~%" ,error-string)
  85. (values 0 e))))
  86. ;; Util screen clearer
  87. (defun cls()
  88. (format t "~A[H~@*~A[J" #\escape))
  89. ;; Entry point
  90. (defun main ()
  91. (format t "~%")
  92. (format t "Available options:~%")
  93. (format t "1. Enter expense~%")
  94. (format t "2. Display month~%")
  95. (format t "3. Write records~%")
  96. (format t "4. Read records~%")
  97. (format t "5. Quit~%")
  98. (format t "6. Import Records~%")
  99. (let
  100. ((answer (prompt-read "Select an option")))
  101. (if (string= answer "1")
  102. (generic-handler
  103. (add-expense-to-month (prompt-read "Enter month"))
  104. "Invalid Input"))
  105. (if (string= answer "2")
  106. (generic-handler
  107. (dump-month (prompt-read "Enter month"))
  108. "Invalid month"))
  109. (if (string= answer "3")
  110. (generic-handler
  111. (serialize-records (prompt-read "Enter filename"))
  112. "Serialization error or invalid filename"))
  113. (if (string= answer "4")
  114. (generic-handler
  115. (deserialize-records (prompt-read "Enter filename"))
  116. "Deserialization error or invalid filename"))
  117. (if (string= answer "5")
  118. (quit))
  119. (if (string= answer "6")
  120. (generic-handler
  121. (import-records (prompt-read "Enter filename"))
  122. "Parsing error or invalid filename"))
  123. (main)))