Skip to content

Commit 42bb199

Browse files
parse-error
1 parent 6ce8b36 commit 42bb199

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

exercises/practice/wordy/.meta/example.el

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
44

55
;;; Code:
66

7+
(define-error 'parse-error "parse error")
8+
79
(defun calculate (left tokens)
810
(if (null tokens)
911
left
1012
(let ((operation (car tokens)))
1113
(cond
14+
((null (cdr tokens)) (signal 'parse-error nil))
1215
((string= operation "plus") (calculate (+ left (string-to-number (cadr tokens))) (drop 2 tokens)))
1316
((string= operation "minus") (calculate (- left (string-to-number (cadr tokens))) (drop 2 tokens)))
14-
((and (string= operation "multiplied") (string= (cadr tokens) "by")) (calculate (* left (string-to-number (caddr tokens))) (drop 3 tokens)))
15-
((and (string= operation "divided") (string= (cadr tokens) "by")) (calculate (/ left (string-to-number (caddr tokens))) (drop 3 tokens)))
16-
(t (error "unknown operation"))))))
17+
((string= operation "multiplied-by") (calculate (* left (string-to-number (cadr tokens))) (drop 2 tokens)))
18+
((string= operation "divided-by") (calculate (/ left (string-to-number (cadr tokens))) (drop 2 tokens)))
19+
(t (signal 'parse-error nil))))))
1720

1821
(defun answer (question)
19-
(unless (string= (substring question -1) "?") (error "syntax error"))
20-
(let ((tokens (split-string (substring question 0 -1))))
21-
(unless (string= (car tokens) "What") (error "unknown operation"))
22-
(unless (string= (cadr tokens) "is") (error "unknown operation"))
22+
(unless (string= (substring question -1) "?") (signal 'parse-error nil))
23+
(let ((tokens (split-string (string-replace " by" "-by" (substring question 0 -1)))))
24+
(unless (string= (car tokens) "What") (signal 'parse-error nil))
25+
(unless (string= (cadr tokens) "is") (signal 'parse-error nil))
26+
(unless (cddr tokens) (signal 'parse-error nil))
2327
(calculate (string-to-number (caddr tokens)) (drop 3 tokens))))
2428

2529

exercises/practice/wordy/.meta/tests.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@
1212
[88bf4b28-0de3-4883-93c7-db1b14aa806e]
1313
description = "just a number"
1414

15+
[18983214-1dfc-4ebd-ac77-c110dde699ce]
16+
description = "just a zero"
17+
18+
[607c08ee-2241-4288-916d-dae5455c87e6]
19+
description = "just a negative number"
20+
1521
[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
1622
description = "addition"
1723

24+
[bb9f2082-171c-46ad-ad4e-c3f72087c1b5]
25+
description = "addition with a left hand zero"
26+
27+
[6fa05f17-405a-4742-80ae-5d1a8edb0d5d]
28+
description = "addition with a right hand zero"
29+
1830
[79e49e06-c5ae-40aa-a352-7a3a01f70015]
1931
description = "more addition"
2032

exercises/practice/wordy/wordy-test.el

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@
1313
(should (= 5 (answer "What is 5?"))))
1414

1515

16+
(ert-deftest just-a-zero ()
17+
(should (= 0 (answer "What is 0?"))))
18+
19+
20+
(ert-deftest just-a-negative-number ()
21+
(should (= -123 (answer "What is -123?"))))
22+
23+
1624
(ert-deftest addition ()
1725
(should (= 2 (answer "What is 1 plus 1?"))))
1826

1927

28+
(ert-deftest addition-with-a-left-hand-zero ()
29+
(should (= 2 (answer "What is 0 plus 2?"))))
30+
31+
32+
(ert-deftest addition-with-a-right-hand-zero ()
33+
(should (= 3 (answer "What is 3 plus 0?"))))
34+
35+
2036
(ert-deftest more-addition ()
2137
(should (= 55 (answer "What is 53 plus 2?"))))
2238

@@ -70,35 +86,35 @@
7086

7187

7288
(ert-deftest unknown-operation ()
73-
(should-error (answer "What is 52 cubed?")))
89+
(should-error (answer "What is 52 cubed?") :type 'parse-error))
7490

7591

7692
(ert-deftest non-math-question ()
77-
(should-error (answer "Who is the President of the United States?")))
93+
(should-error (answer "Who is the President of the United States?") :type 'parse-error))
7894

7995

8096
(ert-deftest reject-problem-missing-an-operand ()
81-
(should-error (answer "What is 1 plus?")))
97+
(should-error (answer "What is 1 plus?") :type 'parse-error))
8298

8399

84100
(ert-deftest reject-problem-with-no-operands-or-operators ()
85-
(should-error (answer "What is?")))
101+
(should-error (answer "What is?") :type 'parse-error))
86102

87103

88104
(ert-deftest reject-two-operations-in-a-row ()
89-
(should-error (answer "What is 1 plus plus 2?")))
105+
(should-error (answer "What is 1 plus plus 2?") :type 'parse-error))
90106

91107

92108
(ert-deftest reject-two-numbers-in-a-row ()
93-
(should-error (answer "What is 1 plus 2 1?")))
109+
(should-error (answer "What is 1 plus 2 1?") :type 'parse-error))
94110

95111

96112
(ert-deftest reject-postfix-notation ()
97-
(should-error (answer "What is 1 2 plus?")))
113+
(should-error (answer "What is 1 2 plus?") :type 'parse-error))
98114

99115

100116
(ert-deftest reject-prefix-notation ()
101-
(should-error (answer "What is plus 1 2?")))
117+
(should-error (answer "What is plus 1 2?") :type 'parse-error))
102118

103119

104120
(provide 'wordy-test)

0 commit comments

Comments
 (0)