fin-lisp.lisp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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-Za-z].*).*$([0-9]{1,4}).*"))
  64. (ere (ppcre:create-scanner "^([A-Z].*)\ -\ \\\$([0-9]{1,4}) - PAID"))
  65. (cur-mon)
  66. (cur-exp))
  67. (loop for line in old-file-lines
  68. do (progn
  69. ;;; (if (ppcre:scan mre line) (progn
  70. ;;; (let ((result)
  71. ;;; (month-num))
  72. ;;; (setf result (ppcre:register-groups-bind (first second) (mre line) :sharedp t (list first second)))
  73. ;;; (setf month-num (cdr (assoc '(first result) *month-table*)))
  74. ;;; (setf cur-mon (concatenate (second result) month-num)))))
  75. (if (ppcre:scan mre line) (setf cur-mon line))
  76. (if (ppcre:scan ere line)
  77. (progn
  78. (setf cur-exp (ppcre:register-groups-bind (first second) (ere line) :sharedp t (list first second)))
  79. (print cur-exp)
  80. (if (gethash cur-mon *records*)
  81. (let ((innerhash (gethash cur-mon *records*)))
  82. (setf (gethash (first cur-exp) innerhash) (second cur-exp))))
  83. (if (not (gethash cur-mon *records*))
  84. (progn
  85. (add-month cur-mon)
  86. (let ((innerhash (gethash cur-mon *records*)))
  87. (setf (gethash (first cur-exp) innerhash) (second cur-exp)))))))))))
  88. ;; Entry point
  89. (defun main ()
  90. (format t "Available options:~C" #\linefeed)
  91. (format t "1. Enter expense~C" #\linefeed)
  92. (format t "2. Display month~C" #\linefeed)
  93. (format t "3. Write records~C" #\linefeed)
  94. (format t "4. Read records~C" #\linefeed)
  95. (format t "5. Quit~C" #\linefeed)
  96. (format t "6. Import Records~C" #\linefeed)
  97. (let
  98. ((answer (prompt-read "Select an option")))
  99. (if (string= answer "1")
  100. (add-expense-to-month
  101. (prompt-read "Enter month")))
  102. (if (string= answer "2")
  103. (dump-month
  104. (prompt-read "Enter month")))
  105. (if (string= answer "3")
  106. (serialize-records (prompt-read "Enter filename")))
  107. (if (string= answer "4")
  108. (deserialize-records (prompt-read "Enter filename")))
  109. (if (string= answer "5")
  110. (quit))
  111. (if (string= answer "6")
  112. (import-records (prompt-read "Enter filename"))))
  113. (main))