From 02ec06609fff3efe3bd84ab66db5f83f480a7bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madeleine=20Sydney=20=C5=9Alaga?= Date: Wed, 23 Sep 2026 15:33:24 -0600 Subject: [PATCH] expand test --- resources/scm/scheme/base.scm | 10 ++++++++-- src/expand.clj | 37 ++++++++++++++++++++++------------- test/expand_test.clj | 16 ++++++++++++++- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/resources/scm/scheme/base.scm b/resources/scm/scheme/base.scm index b0f0755..fb9938e 100644 --- a/resources/scm/scheme/base.scm +++ b/resources/scm/scheme/base.scm @@ -1,3 +1,9 @@ (define-library (scheme base) - (export lambda) - (import (scheme primitives))) + (export lambda begin define define-syntax + syntax-rules if when) + (import (primitives+builtins)) + (begin + (define-syntax when + (syntax-rules () + ((_ test body) + (if test (begin body))))))) diff --git a/src/expand.clj b/src/expand.clj index 7c91693..e2d7202 100644 --- a/src/expand.clj +++ b/src/expand.clj @@ -215,7 +215,7 @@ (for [x (cons f xs)] (expand (scm/rescope x stx) env store-atom)))))) -(defn import-primitives+builtins [scopeholder env-atom store-atom] +(defn- import-primitives+builtins [scopeholder env-atom store-atom] (let [scopes (scm/syntax-context scopeholder) additions (concat (for [p primitives] [p @@ -252,24 +252,33 @@ (cons :begin) vec))))))) +(defn library->datum [lib] + (cons 'define-library + (cons (:library/name lib) + (for [d (:library/declarations lib)] + (match d + [:begin & xs] (cons 'begin xs) + [:import & xs] (cons 'import xs) + [:export & xs] (cons 'export xs)))))) + +(defn library-syntax->datum [lib] + (update lib :library/declarations + (fn [decls] + (for [decl decls] + (match decl + [:begin & xs] (->> xs + (map scm/syntax->datum) + (cons :begin) + vec) + :else decl))))) + (comment (-> "t.scm" slurp (scm/read-syntax :library? true) expand-library - ;; :library/declarations - ;; second - ;; second - ;; scm/syntax->datum - (update :library/declarations - (fn [decls] - (for [decl decls] - (match decl - [:begin & xs] (->> xs - (map scm/syntax->datum) - (cons :begin) - vec) - :else decl))))) + library-syntax->datum + library->datum) (let [scopeholder (scm/new-scopeholder) env {[:global 'lambda] [:primitive 'lambda]} store-atom (atom {['lambda #{}] [:global 'lambda]})] diff --git a/test/expand_test.clj b/test/expand_test.clj index e1312a0..d743027 100644 --- a/test/expand_test.clj +++ b/test/expand_test.clj @@ -34,7 +34,21 @@ '{[x #{a b}] [:lexical x A] [x #{a}] [:lexical x B]})))) -(t/deftest expand-test) +(defn- expand [stx] + (-> #:library {:name '(user) + :declarations [[:import '(primitives+builtins)] + [:begin stx]]} + sut/expand-library + :library/declarations + second + second + scm/syntax->datum)) + +(t/deftest expand-test + (t/is (= '(lambda (x) x) + (expand #scm/stx (#scm/stx lambda + #scm/stx (#scm/stx x) + #scm/stx x))))) ;;; specs