syntax.test 47 KB

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