syntax.test 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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, 2013, 2014 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 (ice-9 local-eval)
  22. #:use-module (test-suite lib))
  23. (define exception:generic-syncase-error
  24. "source expression failed to match")
  25. (define exception:unexpected-syntax
  26. "unexpected syntax")
  27. (define exception:bad-expression
  28. "Bad expression")
  29. (define exception:missing/extra-expr
  30. "Missing or extra expression")
  31. (define exception:missing-expr
  32. "Missing expression")
  33. (define exception:missing-body-expr
  34. "no expressions in body")
  35. (define exception:extra-expr
  36. "Extra expression")
  37. (define exception:illegal-empty-combination
  38. "Illegal empty combination")
  39. (define exception:bad-lambda
  40. "bad lambda")
  41. (define exception:bad-let
  42. "bad let$")
  43. (define exception:bad-letrec
  44. "bad letrec$")
  45. (define exception:bad-letrec*
  46. "bad letrec\\*$")
  47. (define exception:bad-set!
  48. "bad set!")
  49. (define exception:bad-quote
  50. '(quote . "bad syntax"))
  51. (define exception:bad-bindings
  52. "Bad bindings")
  53. (define exception:bad-binding
  54. "Bad binding")
  55. (define exception:duplicate-binding
  56. "duplicate bound variable")
  57. (define exception:bad-body
  58. "^bad body")
  59. (define exception:bad-formals
  60. "invalid argument list")
  61. (define exception:bad-formal
  62. "Bad formal")
  63. (define exception:duplicate-formals
  64. "duplicate identifier in argument list")
  65. (define exception:missing-clauses
  66. "Missing clauses")
  67. (define exception:misplaced-else-clause
  68. "Misplaced else clause")
  69. (define exception:bad-case-clause
  70. "Bad case clause")
  71. (define exception:bad-case-labels
  72. "Bad case labels")
  73. (define exception:bad-cond-clause
  74. "Bad cond clause")
  75. (define exception:too-many-args
  76. "too many arguments")
  77. (define exception:wrong-number-of-values
  78. '(wrong-number-of-args . "number of (values)|(arguments)"))
  79. (define exception:zero-expression-sequence
  80. "sequence of zero expressions")
  81. (define exception:variable-ref
  82. '(misc-error . "Unbound variable"))
  83. ;; (put 'pass-if-syntax-error 'scheme-indent-function 1)
  84. (define-syntax pass-if-syntax-error
  85. (syntax-rules ()
  86. ((_ name pat exp)
  87. (pass-if name
  88. (catch 'syntax-error
  89. (lambda () exp (error "expected syntax-error exception"))
  90. (lambda (k who what where form . maybe-subform)
  91. (if (if (pair? pat)
  92. (and (eq? who (car pat))
  93. (string-match (cdr pat) what))
  94. (string-match pat what))
  95. #t
  96. (error "unexpected syntax-error exception" what pat))))))))
  97. (with-test-prefix "expressions"
  98. (with-test-prefix "Bad argument list"
  99. (pass-if-syntax-error "improper argument list of length 1"
  100. exception:generic-syncase-error
  101. (eval '(let ((foo (lambda (x y) #t)))
  102. (foo . 1))
  103. (interaction-environment)))
  104. (pass-if-syntax-error "improper argument list of length 2"
  105. exception:generic-syncase-error
  106. (eval '(let ((foo (lambda (x y) #t)))
  107. (foo 1 . 2))
  108. (interaction-environment))))
  109. (with-test-prefix "missing or extra expression"
  110. ;; R5RS says:
  111. ;; *Note:* In many dialects of Lisp, the empty combination, (),
  112. ;; is a legitimate expression. In Scheme, combinations must
  113. ;; have at least one subexpression, so () is not a syntactically
  114. ;; valid expression.
  115. ;; Fixed on 2001-3-3
  116. (pass-if-syntax-error "empty parentheses \"()\""
  117. exception:unexpected-syntax
  118. (eval '()
  119. (interaction-environment)))))
  120. (with-test-prefix "quote"
  121. #t)
  122. (with-test-prefix "quasiquote"
  123. (with-test-prefix "unquote"
  124. (pass-if "repeated execution"
  125. (let ((foo (let ((i 0)) (lambda () (set! i (+ i 1)) `(,i)))))
  126. (and (equal? (foo) '(1)) (equal? (foo) '(2))))))
  127. (with-test-prefix "unquote-splicing"
  128. (pass-if "extra arguments"
  129. (equal? (eval '(quasiquote ((unquote-splicing (list 1 2) (list 3 4))))
  130. (interaction-environment))
  131. '(1 2 3 4)))))
  132. (with-test-prefix "begin"
  133. (pass-if "valid (begin)"
  134. (eval '(begin (begin) #t) (interaction-environment)))
  135. (if (not (include-deprecated-features))
  136. (pass-if-syntax-error "invalid (begin)"
  137. exception:zero-expression-sequence
  138. (eval '(begin (if #t (begin)) #t) (interaction-environment)))))
  139. (define-syntax matches?
  140. (syntax-rules (<>)
  141. ((_ (op arg ...) pat) (let ((x (op arg ...)))
  142. (matches? x pat)))
  143. ((_ x ()) (null? x))
  144. ((_ x (a . b)) (and (pair? x)
  145. (matches? (car x) a)
  146. (matches? (cdr x) b)))
  147. ((_ x <>) #t)
  148. ((_ x pat) (equal? x 'pat))))
  149. (with-test-prefix "lambda"
  150. (with-test-prefix "bad formals"
  151. (pass-if-syntax-error "(lambda)"
  152. exception:bad-lambda
  153. (eval '(lambda)
  154. (interaction-environment)))
  155. (pass-if-syntax-error "(lambda . \"foo\")"
  156. exception:bad-lambda
  157. (eval '(lambda . "foo")
  158. (interaction-environment)))
  159. (pass-if-syntax-error "(lambda \"foo\")"
  160. exception:bad-lambda
  161. (eval '(lambda "foo")
  162. (interaction-environment)))
  163. (pass-if-syntax-error "(lambda \"foo\" #f)"
  164. exception:bad-formals
  165. (eval '(lambda "foo" #f)
  166. (interaction-environment)))
  167. (pass-if-syntax-error "(lambda (x 1) 2)"
  168. exception:bad-formals
  169. (eval '(lambda (x 1) 2)
  170. (interaction-environment)))
  171. (pass-if-syntax-error "(lambda (1 x) 2)"
  172. exception:bad-formals
  173. (eval '(lambda (1 x) 2)
  174. (interaction-environment)))
  175. (pass-if-syntax-error "(lambda (x \"a\") 2)"
  176. exception:bad-formals
  177. (eval '(lambda (x "a") 2)
  178. (interaction-environment)))
  179. (pass-if-syntax-error "(lambda (\"a\" x) 2)"
  180. exception:bad-formals
  181. (eval '(lambda ("a" x) 2)
  182. (interaction-environment))))
  183. (with-test-prefix "duplicate formals"
  184. ;; Fixed on 2001-3-3
  185. (pass-if-syntax-error "(lambda (x x) 1)"
  186. exception:duplicate-formals
  187. (eval '(lambda (x x) 1)
  188. (interaction-environment)))
  189. ;; Fixed on 2001-3-3
  190. (pass-if-syntax-error "(lambda (x x x) 1)"
  191. exception:duplicate-formals
  192. (eval '(lambda (x x x) 1)
  193. (interaction-environment))))
  194. (with-test-prefix "bad body"
  195. (pass-if-syntax-error "(lambda ())"
  196. exception:bad-lambda
  197. (eval '(lambda ())
  198. (interaction-environment)))))
  199. (with-test-prefix "let"
  200. (with-test-prefix "bindings"
  201. (pass-if-exception "late binding"
  202. exception:unbound-var
  203. (let ((x 1) (y x)) y)))
  204. (with-test-prefix "bad bindings"
  205. (pass-if-syntax-error "(let)"
  206. exception:bad-let
  207. (eval '(let)
  208. (interaction-environment)))
  209. (pass-if-syntax-error "(let 1)"
  210. exception:bad-let
  211. (eval '(let 1)
  212. (interaction-environment)))
  213. (pass-if-syntax-error "(let (x))"
  214. exception:bad-let
  215. (eval '(let (x))
  216. (interaction-environment)))
  217. (pass-if-syntax-error "(let ((x)))"
  218. exception:bad-let
  219. (eval '(let ((x)))
  220. (interaction-environment)))
  221. (pass-if-syntax-error "(let (x) 1)"
  222. exception:bad-let
  223. (eval '(let (x) 1)
  224. (interaction-environment)))
  225. (pass-if-syntax-error "(let ((x)) 3)"
  226. exception:bad-let
  227. (eval '(let ((x)) 3)
  228. (interaction-environment)))
  229. (pass-if-syntax-error "(let ((x 1) y) x)"
  230. exception:bad-let
  231. (eval '(let ((x 1) y) x)
  232. (interaction-environment)))
  233. (pass-if-syntax-error "(let ((1 2)) 3)"
  234. exception:bad-let
  235. (eval '(let ((1 2)) 3)
  236. (interaction-environment))))
  237. (with-test-prefix "duplicate bindings"
  238. (pass-if-syntax-error "(let ((x 1) (x 2)) x)"
  239. exception:duplicate-binding
  240. (eval '(let ((x 1) (x 2)) x)
  241. (interaction-environment))))
  242. (with-test-prefix "bad body"
  243. (pass-if-syntax-error "(let ())"
  244. exception:bad-let
  245. (eval '(let ())
  246. (interaction-environment)))
  247. (pass-if-syntax-error "(let ((x 1)))"
  248. exception:bad-let
  249. (eval '(let ((x 1)))
  250. (interaction-environment)))))
  251. (with-test-prefix "named let"
  252. (with-test-prefix "initializers"
  253. (pass-if "evaluated in outer environment"
  254. (let ((f -))
  255. (eqv? (let f ((n (f 1))) n) -1))))
  256. (with-test-prefix "bad bindings"
  257. (pass-if-syntax-error "(let x (y))"
  258. exception:bad-let
  259. (eval '(let x (y))
  260. (interaction-environment))))
  261. (with-test-prefix "bad body"
  262. (pass-if-syntax-error "(let x ())"
  263. exception:bad-let
  264. (eval '(let x ())
  265. (interaction-environment)))
  266. (pass-if-syntax-error "(let x ((y 1)))"
  267. exception:bad-let
  268. (eval '(let x ((y 1)))
  269. (interaction-environment)))))
  270. (with-test-prefix "let*"
  271. (with-test-prefix "bindings"
  272. (pass-if "(let* ((x 1) (x 2)) ...)"
  273. (let* ((x 1) (x 2))
  274. (= x 2)))
  275. (pass-if "(let* ((x 1) (x x)) ...)"
  276. (let* ((x 1) (x x))
  277. (= x 1)))
  278. (pass-if "(let ((x 1) (y 2)) (let* () ...))"
  279. (let ((x 1) (y 2))
  280. (let* ()
  281. (and (= x 1) (= y 2))))))
  282. (with-test-prefix "bad bindings"
  283. (pass-if-syntax-error "(let*)"
  284. exception:generic-syncase-error
  285. (eval '(let*)
  286. (interaction-environment)))
  287. (pass-if-syntax-error "(let* 1)"
  288. exception:generic-syncase-error
  289. (eval '(let* 1)
  290. (interaction-environment)))
  291. (pass-if-syntax-error "(let* (x))"
  292. exception:generic-syncase-error
  293. (eval '(let* (x))
  294. (interaction-environment)))
  295. (pass-if-syntax-error "(let* (x) 1)"
  296. exception:generic-syncase-error
  297. (eval '(let* (x) 1)
  298. (interaction-environment)))
  299. (pass-if-syntax-error "(let* ((x)) 3)"
  300. exception:generic-syncase-error
  301. (eval '(let* ((x)) 3)
  302. (interaction-environment)))
  303. (pass-if-syntax-error "(let* ((x 1) y) x)"
  304. exception:generic-syncase-error
  305. (eval '(let* ((x 1) y) x)
  306. (interaction-environment)))
  307. (pass-if-syntax-error "(let* x ())"
  308. exception:generic-syncase-error
  309. (eval '(let* x ())
  310. (interaction-environment)))
  311. (pass-if-syntax-error "(let* x (y))"
  312. exception:generic-syncase-error
  313. (eval '(let* x (y))
  314. (interaction-environment)))
  315. (pass-if-syntax-error "(let* ((1 2)) 3)"
  316. exception:generic-syncase-error
  317. (eval '(let* ((1 2)) 3)
  318. (interaction-environment))))
  319. (with-test-prefix "bad body"
  320. (pass-if-syntax-error "(let* ())"
  321. exception:generic-syncase-error
  322. (eval '(let* ())
  323. (interaction-environment)))
  324. (pass-if-syntax-error "(let* ((x 1)))"
  325. exception:generic-syncase-error
  326. (eval '(let* ((x 1)))
  327. (interaction-environment)))))
  328. (with-test-prefix "letrec"
  329. (with-test-prefix "bindings"
  330. (pass-if-exception "initial bindings are undefined"
  331. exception:variable-ref
  332. (eval '(let ((x 1))
  333. (letrec ((x 1) (y x)) y))
  334. (interaction-environment))))
  335. (with-test-prefix "bad bindings"
  336. (pass-if-syntax-error "(letrec)"
  337. exception:bad-letrec
  338. (eval '(letrec)
  339. (interaction-environment)))
  340. (pass-if-syntax-error "(letrec 1)"
  341. exception:bad-letrec
  342. (eval '(letrec 1)
  343. (interaction-environment)))
  344. (pass-if-syntax-error "(letrec (x))"
  345. exception:bad-letrec
  346. (eval '(letrec (x))
  347. (interaction-environment)))
  348. (pass-if-syntax-error "(letrec (x) 1)"
  349. exception:bad-letrec
  350. (eval '(letrec (x) 1)
  351. (interaction-environment)))
  352. (pass-if-syntax-error "(letrec ((x)) 3)"
  353. exception:bad-letrec
  354. (eval '(letrec ((x)) 3)
  355. (interaction-environment)))
  356. (pass-if-syntax-error "(letrec ((x 1) y) x)"
  357. exception:bad-letrec
  358. (eval '(letrec ((x 1) y) x)
  359. (interaction-environment)))
  360. (pass-if-syntax-error "(letrec x ())"
  361. exception:bad-letrec
  362. (eval '(letrec x ())
  363. (interaction-environment)))
  364. (pass-if-syntax-error "(letrec x (y))"
  365. exception:bad-letrec
  366. (eval '(letrec x (y))
  367. (interaction-environment)))
  368. (pass-if-syntax-error "(letrec ((1 2)) 3)"
  369. exception:bad-letrec
  370. (eval '(letrec ((1 2)) 3)
  371. (interaction-environment))))
  372. (with-test-prefix "duplicate bindings"
  373. (pass-if-syntax-error "(letrec ((x 1) (x 2)) x)"
  374. exception:duplicate-binding
  375. (eval '(letrec ((x 1) (x 2)) x)
  376. (interaction-environment))))
  377. (with-test-prefix "bad body"
  378. (pass-if-syntax-error "(letrec ())"
  379. exception:bad-letrec
  380. (eval '(letrec ())
  381. (interaction-environment)))
  382. (pass-if-syntax-error "(letrec ((x 1)))"
  383. exception:bad-letrec
  384. (eval '(letrec ((x 1)))
  385. (interaction-environment)))))
  386. (with-test-prefix "letrec*"
  387. (with-test-prefix "bindings"
  388. (pass-if-exception "initial bindings are undefined"
  389. exception:variable-ref
  390. (eval '(letrec* ((x y) (y 1)) y)
  391. (interaction-environment))))
  392. (with-test-prefix "bad bindings"
  393. (pass-if-syntax-error "(letrec*)"
  394. exception:bad-letrec*
  395. (eval '(letrec*)
  396. (interaction-environment)))
  397. (pass-if-syntax-error "(letrec* 1)"
  398. exception:bad-letrec*
  399. (eval '(letrec* 1)
  400. (interaction-environment)))
  401. (pass-if-syntax-error "(letrec* (x))"
  402. exception:bad-letrec*
  403. (eval '(letrec* (x))
  404. (interaction-environment)))
  405. (pass-if-syntax-error "(letrec* (x) 1)"
  406. exception:bad-letrec*
  407. (eval '(letrec* (x) 1)
  408. (interaction-environment)))
  409. (pass-if-syntax-error "(letrec* ((x)) 3)"
  410. exception:bad-letrec*
  411. (eval '(letrec* ((x)) 3)
  412. (interaction-environment)))
  413. (pass-if-syntax-error "(letrec* ((x 1) y) x)"
  414. exception:bad-letrec*
  415. (eval '(letrec* ((x 1) y) x)
  416. (interaction-environment)))
  417. (pass-if-syntax-error "(letrec* x ())"
  418. exception:bad-letrec*
  419. (eval '(letrec* x ())
  420. (interaction-environment)))
  421. (pass-if-syntax-error "(letrec* x (y))"
  422. exception:bad-letrec*
  423. (eval '(letrec* x (y))
  424. (interaction-environment)))
  425. (pass-if-syntax-error "(letrec* ((1 2)) 3)"
  426. exception:bad-letrec*
  427. (eval '(letrec* ((1 2)) 3)
  428. (interaction-environment))))
  429. (with-test-prefix "duplicate bindings"
  430. (pass-if-syntax-error "(letrec* ((x 1) (x 2)) x)"
  431. exception:duplicate-binding
  432. (eval '(letrec* ((x 1) (x 2)) x)
  433. (interaction-environment))))
  434. (with-test-prefix "bad body"
  435. (pass-if-syntax-error "(letrec* ())"
  436. exception:bad-letrec*
  437. (eval '(letrec* ())
  438. (interaction-environment)))
  439. (pass-if-syntax-error "(letrec* ((x 1)))"
  440. exception:bad-letrec*
  441. (eval '(letrec* ((x 1)))
  442. (interaction-environment))))
  443. (with-test-prefix "referencing previous values"
  444. (pass-if (equal? (letrec* ((a (cons 'foo 'bar))
  445. (b a))
  446. b)
  447. '(foo . bar)))
  448. (pass-if (equal? (let ()
  449. (define a (cons 'foo 'bar))
  450. (define b a)
  451. b)
  452. '(foo . bar)))))
  453. (with-test-prefix "if"
  454. (with-test-prefix "missing or extra expressions"
  455. (pass-if-syntax-error "(if)"
  456. exception:generic-syncase-error
  457. (eval '(if)
  458. (interaction-environment)))
  459. (pass-if-syntax-error "(if 1 2 3 4)"
  460. exception:generic-syncase-error
  461. (eval '(if 1 2 3 4)
  462. (interaction-environment)))))
  463. (with-test-prefix "cond"
  464. (with-test-prefix "cond is hygienic"
  465. (pass-if "bound 'else is handled correctly"
  466. (eq? (let ((else 'ok)) (cond (else))) 'ok))
  467. (with-test-prefix "bound '=> is handled correctly"
  468. (pass-if "#t => 'ok"
  469. (let ((=> 'foo))
  470. (eq? (cond (#t => 'ok)) 'ok)))
  471. (pass-if "else =>"
  472. (let ((=> 'foo))
  473. (eq? (cond (else =>)) 'foo)))
  474. (pass-if "else => identity"
  475. (let ((=> 'foo))
  476. (eq? (cond (else => identity)) identity)))))
  477. (with-test-prefix "SRFI-61"
  478. (pass-if "always available"
  479. (cond-expand (srfi-61 #t) (else #f)))
  480. (pass-if "single value consequent"
  481. (eq? 'ok (cond (#t identity => (lambda (x) 'ok)) (else #f))))
  482. (pass-if "single value alternate"
  483. (eq? 'ok (cond (#t not => (lambda (x) #f)) (else 'ok))))
  484. (pass-if-exception "doesn't affect standard =>"
  485. exception:wrong-num-args
  486. (cond ((values 1 2) => (lambda (x y) #t))))
  487. (pass-if "multiple values consequent"
  488. (equal? '(2 1) (cond ((values 1 2)
  489. (lambda (one two)
  490. (and (= 1 one) (= 2 two))) =>
  491. (lambda (one two) (list two one)))
  492. (else #f))))
  493. (pass-if "multiple values alternate"
  494. (eq? 'ok (cond ((values 2 3 4)
  495. (lambda args (equal? '(1 2 3) args)) =>
  496. (lambda (x y z) #f))
  497. (else 'ok))))
  498. (pass-if "zero values"
  499. (eq? 'ok (cond ((values) (lambda () #t) => (lambda () 'ok))
  500. (else #f))))
  501. (pass-if "bound => is handled correctly"
  502. (let ((=> 'ok))
  503. (eq? 'ok (cond (#t identity =>) (else #f)))))
  504. (pass-if-syntax-error "missing recipient"
  505. '(cond . "wrong number of receiver expressions")
  506. (eval '(cond (#t identity =>))
  507. (interaction-environment)))
  508. (pass-if-syntax-error "extra recipient"
  509. '(cond . "wrong number of receiver expressions")
  510. (eval '(cond (#t identity => identity identity))
  511. (interaction-environment))))
  512. (with-test-prefix "bad or missing clauses"
  513. (pass-if-syntax-error "(cond)"
  514. exception:generic-syncase-error
  515. (eval '(cond)
  516. (interaction-environment)))
  517. (pass-if-syntax-error "(cond #t)"
  518. '(cond . "invalid clause")
  519. (eval '(cond #t)
  520. (interaction-environment)))
  521. (pass-if-syntax-error "(cond 1)"
  522. '(cond . "invalid clause")
  523. (eval '(cond 1)
  524. (interaction-environment)))
  525. (pass-if-syntax-error "(cond 1 2)"
  526. '(cond . "invalid clause")
  527. (eval '(cond 1 2)
  528. (interaction-environment)))
  529. (pass-if-syntax-error "(cond 1 2 3)"
  530. '(cond . "invalid clause")
  531. (eval '(cond 1 2 3)
  532. (interaction-environment)))
  533. (pass-if-syntax-error "(cond 1 2 3 4)"
  534. '(cond . "invalid clause")
  535. (eval '(cond 1 2 3 4)
  536. (interaction-environment)))
  537. (pass-if-syntax-error "(cond ())"
  538. '(cond . "invalid clause")
  539. (eval '(cond ())
  540. (interaction-environment)))
  541. (pass-if-syntax-error "(cond () 1)"
  542. '(cond . "invalid clause")
  543. (eval '(cond () 1)
  544. (interaction-environment)))
  545. (pass-if-syntax-error "(cond (1) 1)"
  546. '(cond . "invalid clause")
  547. (eval '(cond (1) 1)
  548. (interaction-environment)))
  549. (pass-if-syntax-error "(cond (else #f) (#t #t))"
  550. '(cond . "else must be the last clause")
  551. (eval '(cond (else #f) (#t #t))
  552. (interaction-environment))))
  553. (with-test-prefix "wrong number of arguments"
  554. (pass-if-exception "=> (lambda (x y) #t)"
  555. exception:wrong-num-args
  556. (cond (1 => (lambda (x y) #t))))))
  557. (with-test-prefix "case"
  558. (pass-if "clause with empty labels list"
  559. (case 1 (() #f) (else #t)))
  560. (with-test-prefix "case handles '=> correctly"
  561. (pass-if "(1 2 3) => list"
  562. (equal? (case 1 ((1 2 3) => list))
  563. '(1)))
  564. (pass-if "else => list"
  565. (equal? (case 6
  566. ((1 2 3) 'wrong)
  567. (else => list))
  568. '(6)))
  569. (with-test-prefix "bound '=> is handled correctly"
  570. (pass-if "(1) => 'ok"
  571. (let ((=> 'foo))
  572. (eq? (case 1 ((1) => 'ok)) 'ok)))
  573. (pass-if "else =>"
  574. (let ((=> 'foo))
  575. (eq? (case 1 (else =>)) 'foo)))
  576. (pass-if "else => list"
  577. (let ((=> 'foo))
  578. (eq? (case 1 (else => identity)) identity))))
  579. (pass-if-syntax-error "missing recipient"
  580. '(case . "wrong number of receiver expressions")
  581. (eval '(case 1 ((1) =>))
  582. (interaction-environment)))
  583. (pass-if-syntax-error "extra recipient"
  584. '(case . "wrong number of receiver expressions")
  585. (eval '(case 1 ((1) => identity identity))
  586. (interaction-environment))))
  587. (with-test-prefix "case is hygienic"
  588. (pass-if-syntax-error "bound 'else is handled correctly"
  589. '(case . "invalid clause")
  590. (eval '(let ((else #f)) (case 1 (else #f)))
  591. (interaction-environment))))
  592. (with-test-prefix "bad or missing clauses"
  593. (pass-if-syntax-error "(case)"
  594. exception:generic-syncase-error
  595. (eval '(case)
  596. (interaction-environment)))
  597. (pass-if-syntax-error "(case . \"foo\")"
  598. exception:generic-syncase-error
  599. (eval '(case . "foo")
  600. (interaction-environment)))
  601. (pass-if-syntax-error "(case 1)"
  602. exception:generic-syncase-error
  603. (eval '(case 1)
  604. (interaction-environment)))
  605. (pass-if-syntax-error "(case 1 . \"foo\")"
  606. exception:generic-syncase-error
  607. (eval '(case 1 . "foo")
  608. (interaction-environment)))
  609. (pass-if-syntax-error "(case 1 \"foo\")"
  610. '(case . "invalid clause")
  611. (eval '(case 1 "foo")
  612. (interaction-environment)))
  613. (pass-if-syntax-error "(case 1 ())"
  614. '(case . "invalid clause")
  615. (eval '(case 1 ())
  616. (interaction-environment)))
  617. (pass-if-syntax-error "(case 1 (\"foo\"))"
  618. '(case . "invalid clause")
  619. (eval '(case 1 ("foo"))
  620. (interaction-environment)))
  621. (pass-if-syntax-error "(case 1 (\"foo\" \"bar\"))"
  622. '(case . "invalid clause")
  623. (eval '(case 1 ("foo" "bar"))
  624. (interaction-environment)))
  625. (pass-if-syntax-error "(case 1 ((2) \"bar\") . \"foo\")"
  626. exception:generic-syncase-error
  627. (eval '(case 1 ((2) "bar") . "foo")
  628. (interaction-environment)))
  629. (pass-if-syntax-error "(case 1 ((2) \"bar\") (else))"
  630. '(case . "invalid clause")
  631. (eval '(case 1 ((2) "bar") (else))
  632. (interaction-environment)))
  633. (pass-if-syntax-error "(case 1 (else #f) . \"foo\")"
  634. exception:generic-syncase-error
  635. (eval '(case 1 (else #f) . "foo")
  636. (interaction-environment)))
  637. (pass-if-syntax-error "(case 1 (else #f) ((1) #t))"
  638. '(case . "else must be the last clause")
  639. (eval '(case 1 (else #f) ((1) #t))
  640. (interaction-environment)))))
  641. (with-test-prefix "top-level define"
  642. (pass-if "redefinition"
  643. (let ((m (make-module)))
  644. (beautify-user-module! m)
  645. ;; The previous value of `round' must still be visible at the time the
  646. ;; new `round' is defined. According to R5RS (Section 5.2.1), `define'
  647. ;; should behave like `set!' in this case (except that in the case of
  648. ;; Guile, we respect module boundaries).
  649. (eval '(define round round) m)
  650. (eq? (module-ref m 'round) round)))
  651. (with-test-prefix "missing or extra expressions"
  652. (pass-if-syntax-error "(define)"
  653. exception:generic-syncase-error
  654. (eval '(define)
  655. (interaction-environment))))
  656. (pass-if "module scoping"
  657. (equal?
  658. (eval
  659. '(begin
  660. (define-module (top-level-define/module-scoping-1)
  661. #:export (define-10))
  662. (define-syntax-rule (define-10 name)
  663. (begin
  664. (define t 10)
  665. (define (name) t)))
  666. (define-module (top-level-define/module-scoping-2)
  667. #:use-module (top-level-define/module-scoping-1))
  668. (define-10 foo)
  669. (foo))
  670. (current-module))
  671. 10))
  672. (pass-if "module scoping, same symbolic name"
  673. (equal?
  674. (eval
  675. '(begin
  676. (define-module (top-level-define/module-scoping-3))
  677. (define a 10)
  678. (define-module (top-level-define/module-scoping-4)
  679. #:use-module (top-level-define/module-scoping-3))
  680. (define a (@@ (top-level-define/module-scoping-3) a))
  681. a)
  682. (current-module))
  683. 10))
  684. (pass-if "module scoping, introduced names"
  685. (equal?
  686. (eval
  687. '(begin
  688. (define-module (top-level-define/module-scoping-5)
  689. #:export (define-constant))
  690. (define-syntax-rule (define-constant name val)
  691. (begin
  692. (define t val)
  693. (define (name) t)))
  694. (define-module (top-level-define/module-scoping-6)
  695. #:use-module (top-level-define/module-scoping-5))
  696. (define-constant foo 10)
  697. (define-constant bar 20)
  698. (foo))
  699. (current-module))
  700. 10))
  701. (pass-if "module scoping, duplicate introduced name"
  702. (equal?
  703. (eval
  704. '(begin
  705. (define-module (top-level-define/module-scoping-7)
  706. #:export (define-constant))
  707. (define-syntax-rule (define-constant name val)
  708. (begin
  709. (define t val)
  710. (define (name) t)))
  711. (define-module (top-level-define/module-scoping-8)
  712. #:use-module (top-level-define/module-scoping-7))
  713. (define-constant foo 10)
  714. (define-constant foo 20)
  715. (foo))
  716. (current-module))
  717. 20)))
  718. (with-test-prefix "internal define"
  719. (pass-if "internal defines become letrec"
  720. (eval '(let ((a identity) (b identity) (c identity))
  721. (define (a x) (if (= x 0) 'a (b (- x 1))))
  722. (define (b x) (if (= x 0) 'b (c (- x 1))))
  723. (define (c x) (if (= x 0) 'c (a (- x 1))))
  724. (and (eq? 'a (a 0) (a 3))
  725. (eq? 'b (a 1) (a 4))
  726. (eq? 'c (a 2) (a 5))))
  727. (interaction-environment)))
  728. (pass-if "binding is created before expression is evaluated"
  729. ;; Internal defines are equivalent to `letrec' (R5RS, Section 5.2.2).
  730. (= (eval '(let ()
  731. (define foo
  732. (begin
  733. (set! foo 1)
  734. (+ foo 1)))
  735. foo)
  736. (interaction-environment))
  737. 2))
  738. (pass-if "internal defines with begin"
  739. (false-if-exception
  740. (eval '(let ((a identity) (b identity) (c identity))
  741. (define (a x) (if (= x 0) 'a (b (- x 1))))
  742. (begin
  743. (define (b x) (if (= x 0) 'b (c (- x 1)))))
  744. (define (c x) (if (= x 0) 'c (a (- x 1))))
  745. (and (eq? 'a (a 0) (a 3))
  746. (eq? 'b (a 1) (a 4))
  747. (eq? 'c (a 2) (a 5))))
  748. (interaction-environment))))
  749. (pass-if "internal defines with empty begin"
  750. (false-if-exception
  751. (eval '(let ((a identity) (b identity) (c identity))
  752. (define (a x) (if (= x 0) 'a (b (- x 1))))
  753. (begin)
  754. (define (b x) (if (= x 0) 'b (c (- x 1))))
  755. (define (c x) (if (= x 0) 'c (a (- x 1))))
  756. (and (eq? 'a (a 0) (a 3))
  757. (eq? 'b (a 1) (a 4))
  758. (eq? 'c (a 2) (a 5))))
  759. (interaction-environment))))
  760. (pass-if "internal defines with macro application"
  761. (false-if-exception
  762. (eval '(begin
  763. (defmacro my-define forms
  764. (cons 'define forms))
  765. (let ((a identity) (b identity) (c identity))
  766. (define (a x) (if (= x 0) 'a (b (- x 1))))
  767. (my-define (b x) (if (= x 0) 'b (c (- x 1))))
  768. (define (c x) (if (= x 0) 'c (a (- x 1))))
  769. (and (eq? 'a (a 0) (a 3))
  770. (eq? 'b (a 1) (a 4))
  771. (eq? 'c (a 2) (a 5)))))
  772. (interaction-environment))))
  773. (pass-if-syntax-error "missing body expression"
  774. exception:missing-body-expr
  775. (eval '(let () (define x #t))
  776. (interaction-environment))))
  777. (with-test-prefix "top-level define-values"
  778. (pass-if "zero values"
  779. (eval '(begin (define-values () (values))
  780. #t)
  781. (interaction-environment)))
  782. (pass-if-equal "one value"
  783. 1
  784. (eval '(begin (define-values (x) 1)
  785. x)
  786. (interaction-environment)))
  787. (pass-if-equal "two values"
  788. '(2 3)
  789. (eval '(begin (define-values (x y) (values 2 3))
  790. (list x y))
  791. (interaction-environment)))
  792. (pass-if-equal "three values"
  793. '(4 5 6)
  794. (eval '(begin (define-values (x y z) (values 4 5 6))
  795. (list x y z))
  796. (interaction-environment)))
  797. (pass-if-equal "one value with tail"
  798. '(a (b c d))
  799. (eval '(begin (define-values (x . y) (values 'a 'b 'c 'd))
  800. (list x y))
  801. (interaction-environment)))
  802. (pass-if-equal "two values with tail"
  803. '(x y (z w))
  804. (eval '(begin (define-values (x y . z) (values 'x 'y 'z 'w))
  805. (list x y z))
  806. (interaction-environment)))
  807. (pass-if-equal "just tail"
  808. '(1 2 3)
  809. (eval '(begin (define-values x (values 1 2 3))
  810. x)
  811. (interaction-environment)))
  812. (pass-if-exception "expected 0 values, got 1"
  813. exception:wrong-number-of-values
  814. (eval '(define-values () 1)
  815. (interaction-environment)))
  816. (pass-if-exception "expected 1 value, got 0"
  817. exception:wrong-number-of-values
  818. (eval '(define-values (x) (values))
  819. (interaction-environment)))
  820. (pass-if-exception "expected 1 value, got 2"
  821. exception:wrong-number-of-values
  822. (eval '(define-values (x) (values 1 2))
  823. (interaction-environment)))
  824. (pass-if-exception "expected 1 value with tail, got 0"
  825. exception:wrong-number-of-values
  826. (eval '(define-values (x . y) (values))
  827. (interaction-environment)))
  828. (pass-if-exception "expected 2 value with tail, got 1"
  829. exception:wrong-number-of-values
  830. (eval '(define-values (x y . z) 1)
  831. (interaction-environment)))
  832. (pass-if "redefinition"
  833. (let ((m (make-module)))
  834. (beautify-user-module! m)
  835. ;; The previous values of `floor' and `round' must still be
  836. ;; visible at the time the new `floor' and `round' are defined.
  837. (eval '(define-values (floor round) (values floor round)) m)
  838. (and (eq? (module-ref m 'floor) floor)
  839. (eq? (module-ref m 'round) round))))
  840. (with-test-prefix "missing expression"
  841. (pass-if-syntax-error "(define-values)"
  842. exception:generic-syncase-error
  843. (eval '(define-values)
  844. (interaction-environment)))))
  845. (with-test-prefix "internal define-values"
  846. (pass-if "zero values"
  847. (let ()
  848. (define-values () (values))
  849. #t))
  850. (pass-if-equal "one value"
  851. 1
  852. (let ()
  853. (define-values (x) 1)
  854. x))
  855. (pass-if-equal "two values"
  856. '(2 3)
  857. (let ()
  858. (define-values (x y) (values 2 3))
  859. (list x y)))
  860. (pass-if-equal "three values"
  861. '(4 5 6)
  862. (let ()
  863. (define-values (x y z) (values 4 5 6))
  864. (list x y z)))
  865. (pass-if-equal "one value with tail"
  866. '(a (b c d))
  867. (let ()
  868. (define-values (x . y) (values 'a 'b 'c 'd))
  869. (list x y)))
  870. (pass-if-equal "two values with tail"
  871. '(x y (z w))
  872. (let ()
  873. (define-values (x y . z) (values 'x 'y 'z 'w))
  874. (list x y z)))
  875. (pass-if-equal "just tail"
  876. '(1 2 3)
  877. (let ()
  878. (define-values x (values 1 2 3))
  879. x))
  880. (pass-if-exception "expected 0 values, got 1"
  881. exception:wrong-number-of-values
  882. (eval '(let ()
  883. (define-values () 1)
  884. #f)
  885. (interaction-environment)))
  886. (pass-if-exception "expected 1 value, got 0"
  887. exception:wrong-number-of-values
  888. (eval '(let ()
  889. (define-values (x) (values))
  890. #f)
  891. (interaction-environment)))
  892. (pass-if-exception "expected 1 value, got 2"
  893. exception:wrong-number-of-values
  894. (eval '(let ()
  895. (define-values (x) (values 1 2))
  896. #f)
  897. (interaction-environment)))
  898. (pass-if-exception "expected 1 value with tail, got 0"
  899. exception:wrong-number-of-values
  900. (eval '(let ()
  901. (define-values (x . y) (values))
  902. #f)
  903. (interaction-environment)))
  904. (pass-if-exception "expected 2 value with tail, got 1"
  905. exception:wrong-number-of-values
  906. (eval '(let ()
  907. (define-values (x y . z) 1)
  908. #f)
  909. (interaction-environment)))
  910. (with-test-prefix "missing expression"
  911. (pass-if-syntax-error "(define-values)"
  912. exception:generic-syncase-error
  913. (eval '(let ()
  914. (define-values)
  915. #f)
  916. (interaction-environment)))))
  917. (with-test-prefix "set!"
  918. (with-test-prefix "missing or extra expressions"
  919. (pass-if-syntax-error "(set!)"
  920. exception:bad-set!
  921. (eval '(set!)
  922. (interaction-environment)))
  923. (pass-if-syntax-error "(set! 1)"
  924. exception:bad-set!
  925. (eval '(set! 1)
  926. (interaction-environment)))
  927. (pass-if-syntax-error "(set! 1 2 3)"
  928. exception:bad-set!
  929. (eval '(set! 1 2 3)
  930. (interaction-environment))))
  931. (with-test-prefix "bad variable"
  932. (pass-if-syntax-error "(set! \"\" #t)"
  933. exception:bad-set!
  934. (eval '(set! "" #t)
  935. (interaction-environment)))
  936. (pass-if-syntax-error "(set! 1 #t)"
  937. exception:bad-set!
  938. (eval '(set! 1 #t)
  939. (interaction-environment)))
  940. (pass-if-syntax-error "(set! #t #f)"
  941. exception:bad-set!
  942. (eval '(set! #t #f)
  943. (interaction-environment)))
  944. (pass-if-syntax-error "(set! #f #t)"
  945. exception:bad-set!
  946. (eval '(set! #f #t)
  947. (interaction-environment)))
  948. (pass-if-syntax-error "(set! #\\space #f)"
  949. exception:bad-set!
  950. (eval '(set! #\space #f)
  951. (interaction-environment)))))
  952. (with-test-prefix "quote"
  953. (with-test-prefix "missing or extra expression"
  954. (pass-if-syntax-error "(quote)"
  955. exception:bad-quote
  956. (eval '(quote)
  957. (interaction-environment)))
  958. (pass-if-syntax-error "(quote a b)"
  959. exception:bad-quote
  960. (eval '(quote a b)
  961. (interaction-environment)))))
  962. (with-test-prefix "while"
  963. (define (unreachable)
  964. (error "unreachable code has been reached!"))
  965. ;; Return a new procedure COND which when called (COND) will return #t the
  966. ;; first N times, then #f, then any further call is an error. N=0 is
  967. ;; allowed, in which case #f is returned by the first call.
  968. (define (make-iterations-cond n)
  969. (lambda ()
  970. (cond ((not n)
  971. (error "oops, condition re-tested after giving false"))
  972. ((= 0 n)
  973. (set! n #f)
  974. #f)
  975. (else
  976. (set! n (1- n))
  977. #t))))
  978. (pass-if-syntax-error "too few args" exception:generic-syncase-error
  979. (eval '(while) (interaction-environment)))
  980. (with-test-prefix "empty body"
  981. (do ((n 0 (1+ n)))
  982. ((> n 5))
  983. (pass-if n
  984. (eval `(letrec ((make-iterations-cond
  985. (lambda (n)
  986. (lambda ()
  987. (cond ((not n)
  988. (error "oops, condition re-tested after giving false"))
  989. ((= 0 n)
  990. (set! n #f)
  991. #f)
  992. (else
  993. (set! n (1- n))
  994. #t))))))
  995. (let ((cond (make-iterations-cond ,n)))
  996. (while (cond))
  997. #t))
  998. (interaction-environment)))))
  999. (pass-if "initially false"
  1000. (while #f
  1001. (unreachable))
  1002. #t)
  1003. (with-test-prefix "iterations"
  1004. (do ((n 0 (1+ n)))
  1005. ((> n 5))
  1006. (pass-if n
  1007. (let ((cond (make-iterations-cond n))
  1008. (i 0))
  1009. (while (cond)
  1010. (set! i (1+ i)))
  1011. (= i n)))))
  1012. (with-test-prefix "break"
  1013. (pass-if "normal return"
  1014. (not (while #f (error "not reached"))))
  1015. (pass-if "no args"
  1016. (while #t (break)))
  1017. (pass-if "multiple values"
  1018. (equal? '(1 2 3)
  1019. (call-with-values
  1020. (lambda () (while #t (break 1 2 3)))
  1021. list)))
  1022. (with-test-prefix "from cond"
  1023. (pass-if "first"
  1024. (while (begin
  1025. (break)
  1026. (unreachable))
  1027. (unreachable))
  1028. #t)
  1029. (do ((n 0 (1+ n)))
  1030. ((> n 5))
  1031. (pass-if n
  1032. (let ((cond (make-iterations-cond n))
  1033. (i 0))
  1034. (while (if (cond)
  1035. #t
  1036. (begin
  1037. (break)
  1038. (unreachable)))
  1039. (set! i (1+ i)))
  1040. (= i n)))))
  1041. (with-test-prefix "from body"
  1042. (pass-if "first"
  1043. (while #t
  1044. (break)
  1045. (unreachable))
  1046. #t)
  1047. (do ((n 0 (1+ n)))
  1048. ((> n 5))
  1049. (pass-if n
  1050. (let ((cond (make-iterations-cond n))
  1051. (i 0))
  1052. (while #t
  1053. (if (not (cond))
  1054. (begin
  1055. (break)
  1056. (unreachable)))
  1057. (set! i (1+ i)))
  1058. (= i n)))))
  1059. (pass-if "from nested"
  1060. (while #t
  1061. (let ((outer-break break))
  1062. (while #t
  1063. (outer-break)
  1064. (unreachable)))
  1065. (unreachable))
  1066. #t)
  1067. (pass-if "from recursive"
  1068. (let ((outer-break #f))
  1069. (define (r n)
  1070. (while #t
  1071. (if (eq? n 'outer)
  1072. (begin
  1073. (set! outer-break break)
  1074. (r 'inner))
  1075. (begin
  1076. (outer-break)
  1077. (unreachable))))
  1078. (if (eq? n 'inner)
  1079. (error "broke only from inner loop")))
  1080. (r 'outer))
  1081. #t))
  1082. (with-test-prefix "continue"
  1083. (pass-if-syntax-error "too many args" exception:too-many-args
  1084. (eval '(while #t
  1085. (continue 1))
  1086. (interaction-environment)))
  1087. (with-test-prefix "from cond"
  1088. (do ((n 0 (1+ n)))
  1089. ((> n 5))
  1090. (pass-if n
  1091. (let ((cond (make-iterations-cond n))
  1092. (i 0))
  1093. (while (if (cond)
  1094. (begin
  1095. (set! i (1+ i))
  1096. (continue)
  1097. (unreachable))
  1098. #f)
  1099. (unreachable))
  1100. (= i n)))))
  1101. (with-test-prefix "from body"
  1102. (do ((n 0 (1+ n)))
  1103. ((> n 5))
  1104. (pass-if n
  1105. (let ((cond (make-iterations-cond n))
  1106. (i 0))
  1107. (while (cond)
  1108. (set! i (1+ i))
  1109. (continue)
  1110. (unreachable))
  1111. (= i n)))))
  1112. (pass-if "from nested"
  1113. (let ((cond (make-iterations-cond 3)))
  1114. (while (cond)
  1115. (let ((outer-continue continue))
  1116. (while #t
  1117. (outer-continue)
  1118. (unreachable)))))
  1119. #t)
  1120. (pass-if "from recursive"
  1121. (let ((outer-continue #f))
  1122. (define (r n)
  1123. (let ((cond (make-iterations-cond 3))
  1124. (first #t))
  1125. (while (begin
  1126. (if (and (not first)
  1127. (eq? n 'inner))
  1128. (error "continued only to inner loop"))
  1129. (cond))
  1130. (set! first #f)
  1131. (if (eq? n 'outer)
  1132. (begin
  1133. (set! outer-continue continue)
  1134. (r 'inner))
  1135. (begin
  1136. (outer-continue)
  1137. (unreachable))))))
  1138. (r 'outer))
  1139. #t)))
  1140. (with-test-prefix "syntax-rules"
  1141. (pass-if-equal "custom ellipsis within normal ellipsis"
  1142. '((((a x) (a y) (a …))
  1143. ((b x) (b y) (b …))
  1144. ((c x) (c y) (c …)))
  1145. (((a x) (b x) (c x))
  1146. ((a y) (b y) (c y))
  1147. ((a …) (b …) (c …))))
  1148. (let ()
  1149. (define-syntax foo
  1150. (syntax-rules ()
  1151. ((_ y ...)
  1152. (syntax-rules … ()
  1153. ((_ x …)
  1154. '((((x y) ...) …)
  1155. (((x y) …) ...)))))))
  1156. (define-syntax bar (foo x y …))
  1157. (bar a b c)))
  1158. (pass-if-equal "normal ellipsis within custom ellipsis"
  1159. '((((a x) (a y) (a z))
  1160. ((b x) (b y) (b z))
  1161. ((c x) (c y) (c z)))
  1162. (((a x) (b x) (c x))
  1163. ((a y) (b y) (c y))
  1164. ((a z) (b z) (c z))))
  1165. (let ()
  1166. (define-syntax foo
  1167. (syntax-rules … ()
  1168. ((_ y …)
  1169. (syntax-rules ()
  1170. ((_ x ...)
  1171. '((((x y) …) ...)
  1172. (((x y) ...) …)))))))
  1173. (define-syntax bar (foo x y z))
  1174. (bar a b c)))
  1175. ;; This test is given in SRFI-46.
  1176. (pass-if-equal "custom ellipsis is handled hygienically"
  1177. '((1) 2 (3) (4))
  1178. (let-syntax
  1179. ((f (syntax-rules ()
  1180. ((f ?e)
  1181. (let-syntax
  1182. ((g (syntax-rules --- ()
  1183. ((g (??x ?e) (??y ---))
  1184. '((??x) ?e (??y) ---)))))
  1185. (g (1 2) (3 4)))))))
  1186. (f ---))))
  1187. (with-test-prefix "syntax-error"
  1188. (pass-if-syntax-error "outside of macro without args"
  1189. "test error"
  1190. (eval '(syntax-error "test error")
  1191. (interaction-environment)))
  1192. (pass-if-syntax-error "outside of macro with args"
  1193. "test error x \\(y z\\)"
  1194. (eval '(syntax-error "test error" x (y z))
  1195. (interaction-environment)))
  1196. (pass-if-equal "within macro"
  1197. '(simple-let
  1198. "expected an identifier but got (z1 z2)"
  1199. (simple-let ((y (* x x))
  1200. ((z1 z2) (values x x)))
  1201. (+ y 1)))
  1202. (catch 'syntax-error
  1203. (lambda ()
  1204. (eval '(let ()
  1205. (define-syntax simple-let
  1206. (syntax-rules ()
  1207. ((_ (head ... ((x . y) val) . tail)
  1208. body1 body2 ...)
  1209. (syntax-error
  1210. "expected an identifier but got"
  1211. (x . y)))
  1212. ((_ ((name val) ...) body1 body2 ...)
  1213. ((lambda (name ...) body1 body2 ...)
  1214. val ...))))
  1215. (define (foo x)
  1216. (simple-let ((y (* x x))
  1217. ((z1 z2) (values x x)))
  1218. (+ y 1)))
  1219. foo)
  1220. (interaction-environment))
  1221. (error "expected syntax-error exception"))
  1222. (lambda (k who what where form . maybe-subform)
  1223. (list who what form)))))
  1224. (with-test-prefix "syntax-case"
  1225. (pass-if-syntax-error "duplicate pattern variable"
  1226. '(syntax-case . "duplicate pattern variable")
  1227. (eval '(lambda (e)
  1228. (syntax-case e ()
  1229. ((a b c d e d f) #f)))
  1230. (interaction-environment)))
  1231. (with-test-prefix "misplaced ellipses"
  1232. (pass-if-syntax-error "bare ellipsis"
  1233. '(syntax-case . "misplaced ellipsis")
  1234. (eval '(lambda (e)
  1235. (syntax-case e ()
  1236. (... #f)))
  1237. (interaction-environment)))
  1238. (pass-if-syntax-error "ellipsis singleton"
  1239. '(syntax-case . "misplaced ellipsis")
  1240. (eval '(lambda (e)
  1241. (syntax-case e ()
  1242. ((...) #f)))
  1243. (interaction-environment)))
  1244. (pass-if-syntax-error "ellipsis in car"
  1245. '(syntax-case . "misplaced ellipsis")
  1246. (eval '(lambda (e)
  1247. (syntax-case e ()
  1248. ((... . _) #f)))
  1249. (interaction-environment)))
  1250. (pass-if-syntax-error "ellipsis in cdr"
  1251. '(syntax-case . "misplaced ellipsis")
  1252. (eval '(lambda (e)
  1253. (syntax-case e ()
  1254. ((_ . ...) #f)))
  1255. (interaction-environment)))
  1256. (pass-if-syntax-error "two ellipses in the same list"
  1257. '(syntax-case . "misplaced ellipsis")
  1258. (eval '(lambda (e)
  1259. (syntax-case e ()
  1260. ((x ... y ...) #f)))
  1261. (interaction-environment)))
  1262. (pass-if-syntax-error "three ellipses in the same list"
  1263. '(syntax-case . "misplaced ellipsis")
  1264. (eval '(lambda (e)
  1265. (syntax-case e ()
  1266. ((x ... y ... z ...) #f)))
  1267. (interaction-environment)))))
  1268. (with-test-prefix "with-ellipsis"
  1269. (pass-if-equal "simple"
  1270. '(a 1 2 3)
  1271. (let ()
  1272. (define-syntax define-quotation-macros
  1273. (lambda (x)
  1274. (syntax-case x ()
  1275. ((_ (macro-name head-symbol) ...)
  1276. #'(begin (define-syntax macro-name
  1277. (lambda (x)
  1278. (with-ellipsis …
  1279. (syntax-case x ()
  1280. ((_ x …)
  1281. #'(quote (head-symbol x …)))))))
  1282. ...)))))
  1283. (define-quotation-macros (quote-a a) (quote-b b))
  1284. (quote-a 1 2 3)))
  1285. (pass-if-equal "disables normal ellipsis"
  1286. '(a ...)
  1287. (let ()
  1288. (define-syntax foo
  1289. (lambda (x)
  1290. (with-ellipsis …
  1291. (syntax-case x ()
  1292. ((_)
  1293. #'(quote (a ...)))))))
  1294. (foo)))
  1295. (pass-if-equal "doesn't affect ellipsis for generated code"
  1296. '(a b c)
  1297. (let ()
  1298. (define-syntax quotation-macro
  1299. (lambda (x)
  1300. (with-ellipsis …
  1301. (syntax-case x ()
  1302. ((_)
  1303. #'(lambda (x)
  1304. (syntax-case x ()
  1305. ((_ x ...)
  1306. #'(quote (x ...))))))))))
  1307. (define-syntax kwote (quotation-macro))
  1308. (kwote a b c)))
  1309. (pass-if-equal "propagates into syntax binders"
  1310. '(a b c)
  1311. (let ()
  1312. (with-ellipsis …
  1313. (define-syntax kwote
  1314. (lambda (x)
  1315. (syntax-case x ()
  1316. ((_ x …)
  1317. #'(quote (x …))))))
  1318. (kwote a b c))))
  1319. (pass-if-equal "works with local-eval"
  1320. 5
  1321. (let ((env (with-ellipsis … (the-environment))))
  1322. (local-eval '(syntax-case #'(a b c d e) ()
  1323. ((x …)
  1324. (length #'(x …))))
  1325. env))))
  1326. ;;; Local Variables:
  1327. ;;; eval: (put 'pass-if-syntax-error 'scheme-indent-function 1)
  1328. ;;; eval: (put 'with-ellipsis 'scheme-indent-function 1)
  1329. ;;; End: