syntax.test 46 KB

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