-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcc-truth-table.el
104 lines (84 loc) · 3.71 KB
/
cc-truth-table.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;;; cc-truth-table.el --- Truth Table -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Charles Choi
;; Author: Charles Choi <[email protected]>
;; Keywords: tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package includes functions to support the insertion of a truth table
;; input into a buffer. Primarily intended for use in Org mode so that the
;; the input values can be operated on with boolean functions defined in
;; `cc-digital-logic'.
;;
;;; Code:
(defun tj/int-to-binary-string (i)
"Convert integer I into its binary representation in string format.
This code written by Trey Jackson as SO example.
https://stackoverflow.com/questions/20568684/converting-number-to-base-2-binary-string-representation"
(let ((res ""))
(while (not (= i 0))
(setq res (concat (if (= 1 (logand i 1)) "1" "0") res))
(setq i (ash i -1)))
(if (string= res "")
(setq res "0"))
res))
(defun cc-int-to-binary-list (i)
"Generate list holding binary string representation of I."
(mapcar (lambda (x) (string x)) (tj/int-to-binary-string i)))
(defun py-range (start &optional end step)
"Python range emulation.
This function emulates the Python range function in Elisp.
START - start value of range.
END - end value of range.
STEP - step value for generated range.
Written by michaelJohn
https://stackoverflow.com/questions/15014996/range-data-type-or-generator-in-emacs-lisp-elisp"
(unless end
(setq end start
start 0))
(number-sequence start (1- end) step))
(defun cc/truth-table-input (bits)
"Generate list of strings holding a truth table input with 2^BITS rows."
(let* ((max (expt 2 bits))
(truth-table-range (py-range max))
(header-list-exponents
(mapcar 'number-to-string (reverse (py-range bits))))
(header-list (push "i" header-list-exponents))
(header (format "|%s|" (string-join header-list "|")))
(header-separator
(format "|%s|"
(string-join (make-list (1+ bits) "-")
"+")))
(result (list header-separator header)))
(dolist (e truth-table-range)
(setq-local binary-list (cc-int-to-binary-list e))
(setq-local binary-list-offset (- bits (length binary-list)))
(if (> binary-list-offset 0)
(dolist (_ (py-range 0 binary-list-offset))
(push "0" binary-list)))
(push (format "|%s|%s|" e (string-join binary-list "|")) result))
(reverse result)))
(defun cc/insert-truth-table-input (bits)
"Insert truth table input with 2^BITS rows into current buffer.
If BITS is greater than 4, then a prompt is asked to avoid creation of
large tables."
(interactive "nNumber of inputs (bits): ")
(if (> bits 4)
(if (y-or-n-p
(format
"This will generate a table with %s rows. Continue? "
(expt 2 bits)))
(let ((truth-table (cc/truth-table-input bits)))
(insert (string-join truth-table "\n"))))
(let ((truth-table (cc/truth-table-input bits)))
(insert (string-join truth-table "\n")))))
(provide 'cc-truth-table)
;;; cc-truth-table.el ends here