syntax.test 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. ;;;; syntax.test --- test suite for Guile's syntactic forms -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2003, 2004, 2005, 2006, 2009, 2010,
  4. ;;;; 2011, 2012 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-syntax)
  20. #:use-module (ice-9 regex)
  21. #:use-module (test-suite lib))
  22. (define exception:generic-syncase-error
  23. "source expression failed to match")
  24. (define exception:unexpected-syntax
  25. "unexpected syntax")
  26. (define exception:bad-expression
  27. "Bad expression")
  28. (define exception:missing/extra-expr
  29. "Missing or extra expression")
  30. (define exception:missing-expr
  31. "Missing expression")
  32. (define exception:missing-body-expr
  33. "no expressions in body")
  34. (define exception:extra-expr
  35. "Extra expression")
  36. (define exception:illegal-empty-combination
  37. "Illegal empty combination")
  38. (define exception:bad-lambda
  39. "bad lambda")
  40. (define exception:bad-let
  41. "bad let$")
  42. (define exception:bad-letrec
  43. "bad letrec$")
  44. (define exception:bad-letrec*
  45. "bad letrec\\*$")
  46. (define exception:bad-set!
  47. "bad set!")
  48. (define exception:bad-quote
  49. '(quote . "bad syntax"))
  50. (define exception:bad-bindings
  51. "Bad bindings")
  52. (define exception:bad-binding
  53. "Bad binding")
  54. (define exception:duplicate-binding
  55. "duplicate bound variable")
  56. (define exception:bad-body
  57. "^bad body")
  58. (define exception:bad-formals
  59. "invalid argument list")
  60. (define exception:bad-formal
  61. "Bad formal")
  62. (define exception:duplicate-formals
  63. "duplicate identifier in argument list")
  64. (define exception:missing-clauses
  65. "Missing clauses")
  66. (define exception:misplaced-else-clause
  67. "Misplaced else clause")
  68. (define exception:bad-case-clause
  69. "Bad case clause")
  70. (define exception:bad-case-labels
  71. "Bad case labels")
  72. (define exception:bad-cond-clause
  73. "Bad cond clause")
  74. (define exception:too-many-args
  75. "too many arguments")
  76. (define exception:zero-expression-sequence
  77. "sequence of zero expressions")
  78. ;; (put 'pass-if-syntax-error 'scheme-indent-function 1)
  79. (define-syntax pass-if-syntax-error
  80. (syntax-rules ()
  81. ((_ name pat exp)
  82. (pass-if name
  83. (catch 'syntax-error
  84. (lambda () exp (error "expected syntax-error exception"))
  85. (lambda (k who what where form . maybe-subform)
  86. (if (if (pair? pat)
  87. (and (eq? who (car pat))
  88. (string-match (cdr pat) what))
  89. (string-match pat what))
  90. #t
  91. (error "unexpected syntax-error exception" what pat))))))))
  92. (with-test-prefix "expressions"
  93. (with-test-prefix "Bad argument list"
  94. (pass-if-syntax-error "improper argument list of length 1"
  95. exception:generic-syncase-error
  96. (eval '(let ((foo (lambda (x y) #t)))
  97. (foo . 1))
  98. (interaction-environment)))
  99. (pass-if-syntax-error "improper argument list of length 2"
  100. exception:generic-syncase-error
  101. (eval '(let ((foo (lambda (x y) #t)))
  102. (foo 1 . 2))
  103. (interaction-environment))))
  104. (with-test-prefix "missing or extra expression"
  105. ;; R5RS says:
  106. ;; *Note:* In many dialects of Lisp, the empty combination, (),
  107. ;; is a legitimate expression. In Scheme, combinations must
  108. ;; have at least one subexpression, so () is not a syntactically
  109. ;; valid expression.
  110. ;; Fixed on 2001-3-3
  111. (pass-if-syntax-error "empty parentheses \"()\""
  112. exception:unexpected-syntax
  113. (eval '()
  114. (interaction-environment)))))
  115. (with-test-prefix "quote"
  116. #t)
  117. (with-test-prefix "quasiquote"
  118. (with-test-prefix "unquote"
  119. (pass-if "repeated execution"
  120. (let ((foo (let ((i 0)) (lambda () (set! i (+ i 1)) `(,i)))))
  121. (and (equal? (foo) '(1)) (equal? (foo) '(2))))))
  122. (with-test-prefix "unquote-splicing"
  123. (pass-if "extra arguments"
  124. (equal? (eval '(quasiquote ((unquote-splicing (list 1 2) (list 3 4))))
  125. (interaction-environment))
  126. '(1 2 3 4)))))
  127. (with-test-prefix "begin"
  128. (pass-if "valid (begin)"
  129. (eval '(begin (begin) #t) (interaction-environment)))
  130. (if (not (include-deprecated-features))
  131. (pass-if-syntax-error "invalid (begin)"
  132. exception:zero-expression-sequence
  133. (eval '(begin (if #t (begin)) #t) (interaction-environment)))))
  134. (define-syntax matches?
  135. (syntax-rules (<>)
  136. ((_ (op arg ...) pat) (let ((x (op arg ...)))
  137. (matches? x pat)))
  138. ((_ x ()) (null? x))
  139. ((_ x (a . b)) (and (pair? x)
  140. (matches? (car x) a)
  141. (matches? (cdr x) b)))
  142. ((_ x <>) #t)
  143. ((_ x pat) (equal? x 'pat))))
  144. (with-test-prefix "lambda"
  145. (with-test-prefix "bad formals"
  146. (pass-if-syntax-error "(lambda)"
  147. exception:bad-lambda
  148. (eval '(lambda)
  149. (interaction-environment)))
  150. (pass-if-syntax-error "(lambda . \"foo\")"
  151. exception:bad-lambda
  152. (eval '(lambda . "foo")
  153. (interaction-environment)))
  154. (pass-if-syntax-error "(lambda \"foo\")"
  155. exception:bad-lambda
  156. (eval '(lambda "foo")
  157. (interaction-environment)))
  158. (pass-if-syntax-error "(lambda \"foo\" #f)"
  159. exception:bad-formals
  160. (eval '(lambda "foo" #f)
  161. (interaction-environment)))
  162. (pass-if-syntax-error "(lambda (x 1) 2)"
  163. exception:bad-formals
  164. (eval '(lambda (x 1) 2)
  165. (interaction-environment)))
  166. (pass-if-syntax-error "(lambda (1 x) 2)"
  167. exception:bad-formals
  168. (eval '(lambda (1 x) 2)
  169. (interaction-environment)))
  170. (pass-if-syntax-error "(lambda (x \"a\") 2)"
  171. exception:bad-formals
  172. (eval '(lambda (x "a") 2)
  173. (interaction-environment)))
  174. (pass-if-syntax-error "(lambda (\"a\" x) 2)"
  175. exception:bad-formals
  176. (eval '(lambda ("a" x) 2)
  177. (interaction-environment))))
  178. (with-test-prefix "duplicate formals"
  179. ;; Fixed on 2001-3-3
  180. (pass-if-syntax-error "(lambda (x x) 1)"
  181. exception:duplicate-formals
  182. (eval '(lambda (x x) 1)
  183. (interaction-environment)))
  184. ;; Fixed on 2001-3-3
  185. (pass-if-syntax-error "(lambda (x x x) 1)"
  186. exception:duplicate-formals
  187. (eval '(lambda (x x x) 1)
  188. (interaction-environment))))
  189. (with-test-prefix "bad body"
  190. (pass-if-syntax-error "(lambda ())"
  191. exception:bad-lambda
  192. (eval '(lambda ())
  193. (interaction-environment)))))
  194. (with-test-prefix "let"
  195. (with-test-prefix "bindings"
  196. (pass-if-exception "late binding"
  197. exception:unbound-var
  198. (let ((x 1) (y x)) y)))
  199. (with-test-prefix "bad bindings"
  200. (pass-if-syntax-error "(let)"
  201. exception:bad-let
  202. (eval '(let)
  203. (interaction-environment)))
  204. (pass-if-syntax-error "(let 1)"
  205. exception:bad-let
  206. (eval '(let 1)
  207. (interaction-environment)))
  208. (pass-if-syntax-error "(let (x))"
  209. exception:bad-let
  210. (eval '(let (x))
  211. (interaction-environment)))
  212. (pass-if-syntax-error "(let ((x)))"
  213. exception:bad-let
  214. (eval '(let ((x)))
  215. (interaction-environment)))
  216. (pass-if-syntax-error "(let (x) 1)"
  217. exception:bad-let
  218. (eval '(let (x) 1)
  219. (interaction-environment)))
  220. (pass-if-syntax-error "(let ((x)) 3)"
  221. exception:bad-let
  222. (eval '(let ((x)) 3)
  223. (interaction-environment)))
  224. (pass-if-syntax-error "(let ((x 1) y) x)"
  225. exception:bad-let
  226. (eval '(let ((x 1) y) x)
  227. (interaction-environment)))
  228. (pass-if-syntax-error "(let ((1 2)) 3)"
  229. exception:bad-let
  230. (eval '(let ((1 2)) 3)
  231. (interaction-environment))))
  232. (with-test-prefix "duplicate bindings"
  233. (pass-if-syntax-error "(let ((x 1) (x 2)) x)"
  234. exception:duplicate-binding
  235. (eval '(let ((x 1) (x 2)) x)
  236. (interaction-environment))))
  237. (with-test-prefix "bad body"
  238. (pass-if-syntax-error "(let ())"
  239. exception:bad-let
  240. (eval '(let ())
  241. (interaction-environment)))
  242. (pass-if-syntax-error "(let ((x 1)))"
  243. exception:bad-let
  244. (eval '(let ((x 1)))
  245. (interaction-environment)))))
  246. (with-test-prefix "named let"
  247. (with-test-prefix "initializers"
  248. (pass-if "evaluated in outer environment"
  249. (let ((f -))
  250. (eqv? (let f ((n (f 1))) n) -1))))
  251. (with-test-prefix "bad bindings"
  252. (pass-if-syntax-error "(let x (y))"
  253. exception:bad-let
  254. (eval '(let x (y))
  255. (interaction-environment))))
  256. (with-test-prefix "bad body"
  257. (pass-if-syntax-error "(let x ())"
  258. exception:bad-let
  259. (eval '(let x ())
  260. (interaction-environment)))
  261. (pass-if-syntax-error "(let x ((y 1)))"
  262. exception:bad-let
  263. (eval '(let x ((y 1)))
  264. (interaction-environment)))))
  265. (with-test-prefix "let*"
  266. (with-test-prefix "bindings"
  267. (pass-if "(let* ((x 1) (x 2)) ...)"
  268. (let* ((x 1) (x 2))
  269. (= x 2)))
  270. (pass-if "(let* ((x 1) (x x)) ...)"
  271. (let* ((x 1) (x x))
  272. (= x 1)))
  273. (pass-if "(let ((x 1) (y 2)) (let* () ...))"
  274. (let ((x 1) (y 2))
  275. (let* ()
  276. (and (= x 1) (= y 2))))))
  277. (with-test-prefix "bad bindings"
  278. (pass-if-syntax-error "(let*)"
  279. exception:generic-syncase-error
  280. (eval '(let*)
  281. (interaction-environment)))
  282. (pass-if-syntax-error "(let* 1)"
  283. exception:generic-syncase-error
  284. (eval '(let* 1)
  285. (interaction-environment)))
  286. (pass-if-syntax-error "(let* (x))"
  287. exception:generic-syncase-error
  288. (eval '(let* (x))
  289. (interaction-environment)))
  290. (pass-if-syntax-error "(let* (x) 1)"
  291. exception:generic-syncase-error
  292. (eval '(let* (x) 1)
  293. (interaction-environment)))
  294. (pass-if-syntax-error "(let* ((x)) 3)"
  295. exception:generic-syncase-error
  296. (eval '(let* ((x)) 3)
  297. (interaction-environment)))
  298. (pass-if-syntax-error "(let* ((x 1) y) x)"
  299. exception:generic-syncase-error
  300. (eval '(let* ((x 1) y) x)
  301. (interaction-environment)))
  302. (pass-if-syntax-error "(let* x ())"
  303. exception:generic-syncase-error
  304. (eval '(let* x ())
  305. (interaction-environment)))
  306. (pass-if-syntax-error "(let* x (y))"
  307. exception:generic-syncase-error
  308. (eval '(let* x (y))
  309. (interaction-environment)))
  310. (pass-if-syntax-error "(let* ((1 2)) 3)"
  311. exception:generic-syncase-error
  312. (eval '(let* ((1 2)) 3)
  313. (interaction-environment))))
  314. (with-test-prefix "bad body"
  315. (pass-if-syntax-error "(let* ())"
  316. exception:generic-syncase-error
  317. (eval '(let* ())
  318. (interaction-environment)))
  319. (pass-if-syntax-error "(let* ((x 1)))"
  320. exception:generic-syncase-error
  321. (eval '(let* ((x 1)))
  322. (interaction-environment)))))
  323. (with-test-prefix "letrec"
  324. (with-test-prefix "bindings"
  325. (pass-if-syntax-error "initial bindings are undefined"
  326. exception:used-before-defined
  327. (let ((x 1))
  328. ;; FIXME: the memoizer does initialize the var to undefined, but
  329. ;; the Scheme evaluator has no way of checking what's an
  330. ;; undefined value. Not sure how to do this.
  331. (throw 'unresolved)
  332. (letrec ((x 1) (y x)) y))))
  333. (with-test-prefix "bad bindings"
  334. (pass-if-syntax-error "(letrec)"
  335. exception:bad-letrec
  336. (eval '(letrec)
  337. (interaction-environment)))
  338. (pass-if-syntax-error "(letrec 1)"
  339. exception:bad-letrec
  340. (eval '(letrec 1)
  341. (interaction-environment)))
  342. (pass-if-syntax-error "(letrec (x))"
  343. exception:bad-letrec
  344. (eval '(letrec (x))
  345. (interaction-environment)))
  346. (pass-if-syntax-error "(letrec (x) 1)"
  347. exception:bad-letrec
  348. (eval '(letrec (x) 1)
  349. (interaction-environment)))
  350. (pass-if-syntax-error "(letrec ((x)) 3)"
  351. exception:bad-letrec
  352. (eval '(letrec ((x)) 3)
  353. (interaction-environment)))
  354. (pass-if-syntax-error "(letrec ((x 1) y) x)"
  355. exception:bad-letrec
  356. (eval '(letrec ((x 1) y) x)
  357. (interaction-environment)))
  358. (pass-if-syntax-error "(letrec x ())"
  359. exception:bad-letrec
  360. (eval '(letrec x ())
  361. (interaction-environment)))
  362. (pass-if-syntax-error "(letrec x (y))"
  363. exception:bad-letrec
  364. (eval '(letrec x (y))
  365. (interaction-environment)))
  366. (pass-if-syntax-error "(letrec ((1 2)) 3)"
  367. exception:bad-letrec
  368. (eval '(letrec ((1 2)) 3)
  369. (interaction-environment))))
  370. (with-test-prefix "duplicate bindings"
  371. (pass-if-syntax-error "(letrec ((x 1) (x 2)) x)"
  372. exception:duplicate-binding
  373. (eval '(letrec ((x 1) (x 2)) x)
  374. (interaction-environment))))
  375. (with-test-prefix "bad body"
  376. (pass-if-syntax-error "(letrec ())"
  377. exception:bad-letrec
  378. (eval '(letrec ())
  379. (interaction-environment)))
  380. (pass-if-syntax-error "(letrec ((x 1)))"
  381. exception:bad-letrec
  382. (eval '(letrec ((x 1)))
  383. (interaction-environment)))))
  384. (with-test-prefix "letrec*"
  385. (with-test-prefix "bindings"
  386. (pass-if-syntax-error "initial bindings are undefined"
  387. exception:used-before-defined
  388. (begin
  389. ;; FIXME: the memoizer does initialize the var to undefined, but
  390. ;; the Scheme evaluator has no way of checking what's an
  391. ;; undefined value. Not sure how to do this.
  392. (throw 'unresolved)
  393. (letrec* ((x y) (y 1)) y))))
  394. (with-test-prefix "bad bindings"
  395. (pass-if-syntax-error "(letrec*)"
  396. exception:bad-letrec*
  397. (eval '(letrec*)
  398. (interaction-environment)))
  399. (pass-if-syntax-error "(letrec* 1)"
  400. exception:bad-letrec*
  401. (eval '(letrec* 1)
  402. (interaction-environment)))
  403. (pass-if-syntax-error "(letrec* (x))"
  404. exception:bad-letrec*
  405. (eval '(letrec* (x))
  406. (interaction-environment)))
  407. (pass-if-syntax-error "(letrec* (x) 1)"
  408. exception:bad-letrec*
  409. (eval '(letrec* (x) 1)
  410. (interaction-environment)))
  411. (pass-if-syntax-error "(letrec* ((x)) 3)"
  412. exception:bad-letrec*
  413. (eval '(letrec* ((x)) 3)
  414. (interaction-environment)))
  415. (pass-if-syntax-error "(letrec* ((x 1) y) x)"
  416. exception:bad-letrec*
  417. (eval '(letrec* ((x 1) y) x)
  418. (interaction-environment)))
  419. (pass-if-syntax-error "(letrec* x ())"
  420. exception:bad-letrec*
  421. (eval '(letrec* x ())
  422. (interaction-environment)))
  423. (pass-if-syntax-error "(letrec* x (y))"
  424. exception:bad-letrec*
  425. (eval '(letrec* x (y))
  426. (interaction-environment)))
  427. (pass-if-syntax-error "(letrec* ((1 2)) 3)"
  428. exception:bad-letrec*
  429. (eval '(letrec* ((1 2)) 3)
  430. (interaction-environment))))
  431. (with-test-prefix "duplicate bindings"
  432. (pass-if-syntax-error "(letrec* ((x 1) (x 2)) x)"
  433. exception:duplicate-binding
  434. (eval '(letrec* ((x 1) (x 2)) x)
  435. (interaction-environment))))
  436. (with-test-prefix "bad body"
  437. (pass-if-syntax-error "(letrec* ())"
  438. exception:bad-letrec*
  439. (eval '(letrec* ())
  440. (interaction-environment)))
  441. (pass-if-syntax-error "(letrec* ((x 1)))"
  442. exception:bad-letrec*
  443. (eval '(letrec* ((x 1)))
  444. (interaction-environment))))
  445. (with-test-prefix "referencing previous values"
  446. (pass-if (equal? (letrec ((a (cons 'foo 'bar))
  447. (b a))
  448. b)
  449. '(foo . bar)))
  450. (pass-if (equal? (let ()
  451. (define a (cons 'foo 'bar))
  452. (define b a)
  453. b)
  454. '(foo . bar)))))
  455. (with-test-prefix "if"
  456. (with-test-prefix "missing or extra expressions"
  457. (pass-if-syntax-error "(if)"
  458. exception:generic-syncase-error
  459. (eval '(if)
  460. (interaction-environment)))
  461. (pass-if-syntax-error "(if 1 2 3 4)"
  462. exception:generic-syncase-error
  463. (eval '(if 1 2 3 4)
  464. (interaction-environment)))))
  465. (with-test-prefix "cond"
  466. (with-test-prefix "cond is hygienic"
  467. (pass-if "bound 'else is handled correctly"
  468. (eq? (let ((else 'ok)) (cond (else))) 'ok))
  469. (with-test-prefix "bound '=> is handled correctly"
  470. (pass-if "#t => 'ok"
  471. (let ((=> 'foo))
  472. (eq? (cond (#t => 'ok)) 'ok)))
  473. (pass-if "else =>"
  474. (let ((=> 'foo))
  475. (eq? (cond (else =>)) 'foo)))
  476. (pass-if "else => identity"
  477. (let ((=> 'foo))
  478. (eq? (cond (else => identity)) identity)))))
  479. (with-test-prefix "SRFI-61"
  480. (pass-if "always available"
  481. (cond-expand (srfi-61 #t) (else #f)))
  482. (pass-if "single value consequent"
  483. (eq? 'ok (cond (#t identity => (lambda (x) 'ok)) (else #f))))
  484. (pass-if "single value alternate"
  485. (eq? 'ok (cond (#t not => (lambda (x) #f)) (else 'ok))))
  486. (pass-if-exception "doesn't affect standard =>"
  487. exception:wrong-num-args
  488. (cond ((values 1 2) => (lambda (x y) #t))))
  489. (pass-if "multiple values consequent"
  490. (equal? '(2 1) (cond ((values 1 2)
  491. (lambda (one two)
  492. (and (= 1 one) (= 2 two))) =>
  493. (lambda (one two) (list two one)))
  494. (else #f))))
  495. (pass-if "multiple values alternate"
  496. (eq? 'ok (cond ((values 2 3 4)
  497. (lambda args (equal? '(1 2 3) args)) =>
  498. (lambda (x y z) #f))
  499. (else 'ok))))
  500. (pass-if "zero values"
  501. (eq? 'ok (cond ((values) (lambda () #t) => (lambda () 'ok))
  502. (else #f))))
  503. (pass-if "bound => is handled correctly"
  504. (let ((=> 'ok))
  505. (eq? 'ok (cond (#t identity =>) (else #f)))))
  506. (pass-if-syntax-error "missing recipient"
  507. '(cond . "wrong number of receiver expressions")
  508. (eval '(cond (#t identity =>))
  509. (interaction-environment)))
  510. (pass-if-syntax-error "extra recipient"
  511. '(cond . "wrong number of receiver expressions")
  512. (eval '(cond (#t identity => identity identity))
  513. (interaction-environment))))
  514. (with-test-prefix "bad or missing clauses"
  515. (pass-if-syntax-error "(cond)"
  516. exception:generic-syncase-error
  517. (eval '(cond)
  518. (interaction-environment)))
  519. (pass-if-syntax-error "(cond #t)"
  520. '(cond . "invalid clause")
  521. (eval '(cond #t)
  522. (interaction-environment)))
  523. (pass-if-syntax-error "(cond 1)"
  524. '(cond . "invalid clause")
  525. (eval '(cond 1)
  526. (interaction-environment)))
  527. (pass-if-syntax-error "(cond 1 2)"
  528. '(cond . "invalid clause")
  529. (eval '(cond 1 2)
  530. (interaction-environment)))
  531. (pass-if-syntax-error "(cond 1 2 3)"
  532. '(cond . "invalid clause")
  533. (eval '(cond 1 2 3)
  534. (interaction-environment)))
  535. (pass-if-syntax-error "(cond 1 2 3 4)"
  536. '(cond . "invalid clause")
  537. (eval '(cond 1 2 3 4)
  538. (interaction-environment)))
  539. (pass-if-syntax-error "(cond ())"
  540. '(cond . "invalid clause")
  541. (eval '(cond ())
  542. (interaction-environment)))
  543. (pass-if-syntax-error "(cond () 1)"
  544. '(cond . "invalid clause")
  545. (eval '(cond () 1)
  546. (interaction-environment)))
  547. (pass-if-syntax-error "(cond (1) 1)"
  548. '(cond . "invalid clause")
  549. (eval '(cond (1) 1)
  550. (interaction-environment)))
  551. (pass-if-syntax-error "(cond (else #f) (#t #t))"
  552. '(cond . "else must be the last clause")
  553. (eval '(cond (else #f) (#t #t))
  554. (interaction-environment))))
  555. (with-test-prefix "wrong number of arguments"
  556. (pass-if-exception "=> (lambda (x y) #t)"
  557. exception:wrong-num-args
  558. (cond (1 => (lambda (x y) #t))))))
  559. (with-test-prefix "case"
  560. (pass-if "clause with empty labels list"
  561. (case 1 (() #f) (else #t)))
  562. (with-test-prefix "case handles '=> correctly"
  563. (pass-if "(1 2 3) => list"
  564. (equal? (case 1 ((1 2 3) => list))
  565. '(1)))
  566. (pass-if "else => list"
  567. (equal? (case 6
  568. ((1 2 3) 'wrong)
  569. (else => list))
  570. '(6)))
  571. (with-test-prefix "bound '=> is handled correctly"
  572. (pass-if "(1) => 'ok"
  573. (let ((=> 'foo))
  574. (eq? (case 1 ((1) => 'ok)) 'ok)))
  575. (pass-if "else =>"
  576. (let ((=> 'foo))
  577. (eq? (case 1 (else =>)) 'foo)))
  578. (pass-if "else => list"
  579. (let ((=> 'foo))
  580. (eq? (case 1 (else => identity)) identity))))
  581. (pass-if-syntax-error "missing recipient"
  582. '(case . "wrong number of receiver expressions")
  583. (eval '(case 1 ((1) =>))
  584. (interaction-environment)))
  585. (pass-if-syntax-error "extra recipient"
  586. '(case . "wrong number of receiver expressions")
  587. (eval '(case 1 ((1) => identity identity))
  588. (interaction-environment))))
  589. (with-test-prefix "case is hygienic"
  590. (pass-if-syntax-error "bound 'else is handled correctly"
  591. '(case . "invalid clause")
  592. (eval '(let ((else #f)) (case 1 (else #f)))
  593. (interaction-environment))))
  594. (with-test-prefix "bad or missing clauses"
  595. (pass-if-syntax-error "(case)"
  596. exception:generic-syncase-error
  597. (eval '(case)
  598. (interaction-environment)))
  599. (pass-if-syntax-error "(case . \"foo\")"
  600. exception:generic-syncase-error
  601. (eval '(case . "foo")
  602. (interaction-environment)))
  603. (pass-if-syntax-error "(case 1)"
  604. exception:generic-syncase-error
  605. (eval '(case 1)
  606. (interaction-environment)))
  607. (pass-if-syntax-error "(case 1 . \"foo\")"
  608. exception:generic-syncase-error
  609. (eval '(case 1 . "foo")
  610. (interaction-environment)))
  611. (pass-if-syntax-error "(case 1 \"foo\")"
  612. '(case . "invalid clause")
  613. (eval '(case 1 "foo")
  614. (interaction-environment)))
  615. (pass-if-syntax-error "(case 1 ())"
  616. '(case . "invalid clause")
  617. (eval '(case 1 ())
  618. (interaction-environment)))
  619. (pass-if-syntax-error "(case 1 (\"foo\"))"
  620. '(case . "invalid clause")
  621. (eval '(case 1 ("foo"))
  622. (interaction-environment)))
  623. (pass-if-syntax-error "(case 1 (\"foo\" \"bar\"))"
  624. '(case . "invalid clause")
  625. (eval '(case 1 ("foo" "bar"))
  626. (interaction-environment)))
  627. (pass-if-syntax-error "(case 1 ((2) \"bar\") . \"foo\")"
  628. exception:generic-syncase-error
  629. (eval '(case 1 ((2) "bar") . "foo")
  630. (interaction-environment)))
  631. (pass-if-syntax-error "(case 1 ((2) \"bar\") (else))"
  632. '(case . "invalid clause")
  633. (eval '(case 1 ((2) "bar") (else))
  634. (interaction-environment)))
  635. (pass-if-syntax-error "(case 1 (else #f) . \"foo\")"
  636. exception:generic-syncase-error
  637. (eval '(case 1 (else #f) . "foo")
  638. (interaction-environment)))
  639. (pass-if-syntax-error "(case 1 (else #f) ((1) #t))"
  640. '(case . "else must be the last clause")
  641. (eval '(case 1 (else #f) ((1) #t))
  642. (interaction-environment)))))
  643. (with-test-prefix "top-level define"
  644. (pass-if "redefinition"
  645. (let ((m (make-module)))
  646. (beautify-user-module! m)
  647. ;; The previous value of `round' must still be visible at the time the
  648. ;; new `round' is defined. According to R5RS (Section 5.2.1), `define'
  649. ;; should behave like `set!' in this case (except that in the case of
  650. ;; Guile, we respect module boundaries).
  651. (eval '(define round round) m)
  652. (eq? (module-ref m 'round) round)))
  653. (with-test-prefix "missing or extra expressions"
  654. (pass-if-syntax-error "(define)"
  655. exception:generic-syncase-error
  656. (eval '(define)
  657. (interaction-environment))))
  658. (pass-if "module scoping"
  659. (equal?
  660. (eval
  661. '(begin
  662. (define-module (top-level-define/module-scoping-1)
  663. #:export (define-10))
  664. (define-syntax-rule (define-10 name)
  665. (begin
  666. (define t 10)
  667. (define (name) t)))
  668. (define-module (top-level-define/module-scoping-2)
  669. #:use-module (top-level-define/module-scoping-1))
  670. (define-10 foo)
  671. (foo))
  672. (current-module))
  673. 10))
  674. (pass-if "module scoping, same symbolic name"
  675. (equal?
  676. (eval
  677. '(begin
  678. (define-module (top-level-define/module-scoping-3))
  679. (define a 10)
  680. (define-module (top-level-define/module-scoping-4)
  681. #:use-module (top-level-define/module-scoping-3))
  682. (define a (@@ (top-level-define/module-scoping-3) a))
  683. a)
  684. (current-module))
  685. 10))
  686. (pass-if "module scoping, introduced names"
  687. (equal?
  688. (eval
  689. '(begin
  690. (define-module (top-level-define/module-scoping-5)
  691. #:export (define-constant))
  692. (define-syntax-rule (define-constant name val)
  693. (begin
  694. (define t val)
  695. (define (name) t)))
  696. (define-module (top-level-define/module-scoping-6)
  697. #:use-module (top-level-define/module-scoping-5))
  698. (define-constant foo 10)
  699. (define-constant bar 20)
  700. (foo))
  701. (current-module))
  702. 10))
  703. (pass-if "module scoping, duplicate introduced name"
  704. (equal?
  705. (eval
  706. '(begin
  707. (define-module (top-level-define/module-scoping-7)
  708. #:export (define-constant))
  709. (define-syntax-rule (define-constant name val)
  710. (begin
  711. (define t val)
  712. (define (name) t)))
  713. (define-module (top-level-define/module-scoping-8)
  714. #:use-module (top-level-define/module-scoping-7))
  715. (define-constant foo 10)
  716. (define-constant foo 20)
  717. (foo))
  718. (current-module))
  719. 20)))
  720. (with-test-prefix "internal define"
  721. (pass-if "internal defines become letrec"
  722. (eval '(let ((a identity) (b identity) (c identity))
  723. (define (a x) (if (= x 0) 'a (b (- x 1))))
  724. (define (b x) (if (= x 0) 'b (c (- x 1))))
  725. (define (c x) (if (= x 0) 'c (a (- x 1))))
  726. (and (eq? 'a (a 0) (a 3))
  727. (eq? 'b (a 1) (a 4))
  728. (eq? 'c (a 2) (a 5))))
  729. (interaction-environment)))
  730. (pass-if "binding is created before expression is evaluated"
  731. ;; Internal defines are equivalent to `letrec' (R5RS, Section 5.2.2).
  732. (= (eval '(let ()
  733. (define foo
  734. (begin
  735. (set! foo 1)
  736. (+ foo 1)))
  737. foo)
  738. (interaction-environment))
  739. 2))
  740. (pass-if "internal defines with begin"
  741. (false-if-exception
  742. (eval '(let ((a identity) (b identity) (c identity))
  743. (define (a x) (if (= x 0) 'a (b (- x 1))))
  744. (begin
  745. (define (b x) (if (= x 0) 'b (c (- x 1)))))
  746. (define (c x) (if (= x 0) 'c (a (- x 1))))
  747. (and (eq? 'a (a 0) (a 3))
  748. (eq? 'b (a 1) (a 4))
  749. (eq? 'c (a 2) (a 5))))
  750. (interaction-environment))))
  751. (pass-if "internal defines with empty begin"
  752. (false-if-exception
  753. (eval '(let ((a identity) (b identity) (c identity))
  754. (define (a x) (if (= x 0) 'a (b (- x 1))))
  755. (begin)
  756. (define (b x) (if (= x 0) 'b (c (- x 1))))
  757. (define (c x) (if (= x 0) 'c (a (- x 1))))
  758. (and (eq? 'a (a 0) (a 3))
  759. (eq? 'b (a 1) (a 4))
  760. (eq? 'c (a 2) (a 5))))
  761. (interaction-environment))))
  762. (pass-if "internal defines with macro application"
  763. (false-if-exception
  764. (eval '(begin
  765. (defmacro my-define forms
  766. (cons 'define forms))
  767. (let ((a identity) (b identity) (c identity))
  768. (define (a x) (if (= x 0) 'a (b (- x 1))))
  769. (my-define (b x) (if (= x 0) 'b (c (- x 1))))
  770. (define (c x) (if (= x 0) 'c (a (- x 1))))
  771. (and (eq? 'a (a 0) (a 3))
  772. (eq? 'b (a 1) (a 4))
  773. (eq? 'c (a 2) (a 5)))))
  774. (interaction-environment))))
  775. (pass-if-syntax-error "missing body expression"
  776. exception:missing-body-expr
  777. (eval '(let () (define x #t))
  778. (interaction-environment))))
  779. (with-test-prefix "set!"
  780. (with-test-prefix "missing or extra expressions"
  781. (pass-if-syntax-error "(set!)"
  782. exception:bad-set!
  783. (eval '(set!)
  784. (interaction-environment)))
  785. (pass-if-syntax-error "(set! 1)"
  786. exception:bad-set!
  787. (eval '(set! 1)
  788. (interaction-environment)))
  789. (pass-if-syntax-error "(set! 1 2 3)"
  790. exception:bad-set!
  791. (eval '(set! 1 2 3)
  792. (interaction-environment))))
  793. (with-test-prefix "bad variable"
  794. (pass-if-syntax-error "(set! \"\" #t)"
  795. exception:bad-set!
  796. (eval '(set! "" #t)
  797. (interaction-environment)))
  798. (pass-if-syntax-error "(set! 1 #t)"
  799. exception:bad-set!
  800. (eval '(set! 1 #t)
  801. (interaction-environment)))
  802. (pass-if-syntax-error "(set! #t #f)"
  803. exception:bad-set!
  804. (eval '(set! #t #f)
  805. (interaction-environment)))
  806. (pass-if-syntax-error "(set! #f #t)"
  807. exception:bad-set!
  808. (eval '(set! #f #t)
  809. (interaction-environment)))
  810. (pass-if-syntax-error "(set! #\\space #f)"
  811. exception:bad-set!
  812. (eval '(set! #\space #f)
  813. (interaction-environment)))))
  814. (with-test-prefix "quote"
  815. (with-test-prefix "missing or extra expression"
  816. (pass-if-syntax-error "(quote)"
  817. exception:bad-quote
  818. (eval '(quote)
  819. (interaction-environment)))
  820. (pass-if-syntax-error "(quote a b)"
  821. exception:bad-quote
  822. (eval '(quote a b)
  823. (interaction-environment)))))
  824. (with-test-prefix "while"
  825. (define (unreachable)
  826. (error "unreachable code has been reached!"))
  827. ;; Return a new procedure COND which when called (COND) will return #t the
  828. ;; first N times, then #f, then any further call is an error. N=0 is
  829. ;; allowed, in which case #f is returned by the first call.
  830. (define (make-iterations-cond n)
  831. (lambda ()
  832. (cond ((not n)
  833. (error "oops, condition re-tested after giving false"))
  834. ((= 0 n)
  835. (set! n #f)
  836. #f)
  837. (else
  838. (set! n (1- n))
  839. #t))))
  840. (pass-if-syntax-error "too few args" exception:generic-syncase-error
  841. (eval '(while) (interaction-environment)))
  842. (with-test-prefix "empty body"
  843. (do ((n 0 (1+ n)))
  844. ((> n 5))
  845. (pass-if n
  846. (eval `(letrec ((make-iterations-cond
  847. (lambda (n)
  848. (lambda ()
  849. (cond ((not n)
  850. (error "oops, condition re-tested after giving false"))
  851. ((= 0 n)
  852. (set! n #f)
  853. #f)
  854. (else
  855. (set! n (1- n))
  856. #t))))))
  857. (let ((cond (make-iterations-cond ,n)))
  858. (while (cond))
  859. #t))
  860. (interaction-environment)))))
  861. (pass-if "initially false"
  862. (while #f
  863. (unreachable))
  864. #t)
  865. (with-test-prefix "iterations"
  866. (do ((n 0 (1+ n)))
  867. ((> n 5))
  868. (pass-if n
  869. (let ((cond (make-iterations-cond n))
  870. (i 0))
  871. (while (cond)
  872. (set! i (1+ i)))
  873. (= i n)))))
  874. (with-test-prefix "break"
  875. (pass-if "normal return"
  876. (not (while #f (error "not reached"))))
  877. (pass-if "no args"
  878. (while #t (break)))
  879. (pass-if "multiple values"
  880. (equal? '(1 2 3)
  881. (call-with-values
  882. (lambda () (while #t (break 1 2 3)))
  883. list)))
  884. (with-test-prefix "from cond"
  885. (pass-if "first"
  886. (while (begin
  887. (break)
  888. (unreachable))
  889. (unreachable))
  890. #t)
  891. (do ((n 0 (1+ n)))
  892. ((> n 5))
  893. (pass-if n
  894. (let ((cond (make-iterations-cond n))
  895. (i 0))
  896. (while (if (cond)
  897. #t
  898. (begin
  899. (break)
  900. (unreachable)))
  901. (set! i (1+ i)))
  902. (= i n)))))
  903. (with-test-prefix "from body"
  904. (pass-if "first"
  905. (while #t
  906. (break)
  907. (unreachable))
  908. #t)
  909. (do ((n 0 (1+ n)))
  910. ((> n 5))
  911. (pass-if n
  912. (let ((cond (make-iterations-cond n))
  913. (i 0))
  914. (while #t
  915. (if (not (cond))
  916. (begin
  917. (break)
  918. (unreachable)))
  919. (set! i (1+ i)))
  920. (= i n)))))
  921. (pass-if "from nested"
  922. (while #t
  923. (let ((outer-break break))
  924. (while #t
  925. (outer-break)
  926. (unreachable)))
  927. (unreachable))
  928. #t)
  929. (pass-if "from recursive"
  930. (let ((outer-break #f))
  931. (define (r n)
  932. (while #t
  933. (if (eq? n 'outer)
  934. (begin
  935. (set! outer-break break)
  936. (r 'inner))
  937. (begin
  938. (outer-break)
  939. (unreachable))))
  940. (if (eq? n 'inner)
  941. (error "broke only from inner loop")))
  942. (r 'outer))
  943. #t))
  944. (with-test-prefix "continue"
  945. (pass-if-syntax-error "too many args" exception:too-many-args
  946. (eval '(while #t
  947. (continue 1))
  948. (interaction-environment)))
  949. (with-test-prefix "from cond"
  950. (do ((n 0 (1+ n)))
  951. ((> n 5))
  952. (pass-if n
  953. (let ((cond (make-iterations-cond n))
  954. (i 0))
  955. (while (if (cond)
  956. (begin
  957. (set! i (1+ i))
  958. (continue)
  959. (unreachable))
  960. #f)
  961. (unreachable))
  962. (= i n)))))
  963. (with-test-prefix "from body"
  964. (do ((n 0 (1+ n)))
  965. ((> n 5))
  966. (pass-if n
  967. (let ((cond (make-iterations-cond n))
  968. (i 0))
  969. (while (cond)
  970. (set! i (1+ i))
  971. (continue)
  972. (unreachable))
  973. (= i n)))))
  974. (pass-if "from nested"
  975. (let ((cond (make-iterations-cond 3)))
  976. (while (cond)
  977. (let ((outer-continue continue))
  978. (while #t
  979. (outer-continue)
  980. (unreachable)))))
  981. #t)
  982. (pass-if "from recursive"
  983. (let ((outer-continue #f))
  984. (define (r n)
  985. (let ((cond (make-iterations-cond 3))
  986. (first #t))
  987. (while (begin
  988. (if (and (not first)
  989. (eq? n 'inner))
  990. (error "continued only to inner loop"))
  991. (cond))
  992. (set! first #f)
  993. (if (eq? n 'outer)
  994. (begin
  995. (set! outer-continue continue)
  996. (r 'inner))
  997. (begin
  998. (outer-continue)
  999. (unreachable))))))
  1000. (r 'outer))
  1001. #t)))