aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2020-07-25 10:27:44 +0200
committerOmar Polo <op@omarpolo.com>2020-07-25 10:27:44 +0200
commit1f5886b440219912a014be764649edfe627daa60 (patch)
tree7e33ffb538ee75c4f2b521c82a3bbc926d89f56f
parentdd6ad538977f26a853f9b4061fdeb3467c8c00b4 (diff)
downloadsam.el-1f5886b440219912a014be764649edfe627daa60.tar.gz
sam.el-1f5886b440219912a014be764649edfe627daa60.tar.bz2
improved command parsing
* renamed `sam-parse-command' to `sam-parse-line' * `sam-parse-command' takes a command (e.g. "=#") and returns a cons of the string representation of the command ("=#") and the symbol to call (sam-cmd-charoffset). * added a small test for this
-rw-r--r--sam-test.el10
-rw-r--r--sam.el24
2 files changed, 22 insertions, 12 deletions
diff --git a/sam-test.el b/sam-test.el
index 1c6fafb..65536a6 100644
--- a/sam-test.el
+++ b/sam-test.el
@@ -6,7 +6,7 @@
(require 'ert)
(require 'sam)
-(ert-deftest sam-parse-command-test ()
+(ert-deftest sam-parse-line-test ()
(dolist (spec '(("3" "p" <- "3p")
("3" "p" <- "3 p")
("34" "p" <- "34\tp")
@@ -17,4 +17,12 @@
(should (string-equal exp-addr address))
(should (string-equal exp-cmd command))))))
+(ert-deftest sam-parse-command-test ()
+ (let ((spec '(("=#" -> "=#" sam-cmd-charoffset))))
+ (cl-loop
+ for (input _ exp-cmd exp-fn) in spec
+ do (cl-destructuring-bind (cmd . fn) (sam-parse-command input)
+ (should (string-equal exp-cmd cmd))
+ (should (string-equal exp-fn fn))))))
+
;; (ert-run-tests-interactively t)
diff --git a/sam.el b/sam.el
index d0e7e49..2af5252 100644
--- a/sam.el
+++ b/sam.el
@@ -68,7 +68,7 @@
(define-key sam-mode-map "\r" 'sam-newline)
(defconst sam-cmd-alist
- '(;; ("=#" . sam-cmd-charoffset)
+ '(("=#" . sam-cmd-charoffset)
("=" . sam-cmd-linenum)
("P" . sam-cmd-filename)
("p" . sam-cmd-print)
@@ -85,7 +85,7 @@
(with-current-buffer (sam-get-buffer)
(insert "?" err)))
-(defun sam-parse-command (s)
+(defun sam-parse-line (s)
"Returns an cons of `address' and `command'"
(let ((address "")
(command "")
@@ -129,20 +129,22 @@
(point-at-eol))
(pop kill-ring))
+(defun sam-parse-command (cmd)
+ (cl-loop
+ for (command . fn) in sam-cmd-alist
+ when (string-equal command cmd) return `(,command . ,fn)))
+
(defun sam-exec-command (cmd)
- ;; TODO: naïve
- (if (= (length cmd) 0)
- nil
- (let* ((c (substring cmd 0 1))
- (f (cdr (assoc c sam-cmd-alist))))
- (if f
- (funcall f)
- (sam-report-error (concat "unknown command ``" cmd "''"))))))
+ (let ((tmp (sam-parse-command cmd)))
+ (if tmp
+ (cl-destructuring-bind (cmd . fn) tmp
+ (funcall fn (substring cmd (length cmd))))
+ (sam-report-error (concat "unknown command: " cmd)))))
(defun sam-exec-line ()
"Run the sam command on this line."
(let* ((line (sam-get-line))
- (parsed (sam-parse-command line))
+ (parsed (sam-parse-line line))
(addr (car parsed))
(cmd (cdr parsed)))
(insert "\n")