Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64

Merged
pkulev merged 40 commits from static-dispatch into main 2026-10-19 17:07:45 +02:00
8 changed files with 72 additions and 25 deletions
Showing only changes of commit 95f39e4b44 - Show all commits

View File

@@ -123,7 +123,8 @@ Hello, Alex!
* Features * Features
** Full C interoperability ** Full C interoperability
Just ~(include "Your/Favourite/Library.h")~ and use it as you would Just ~(include "Your/Favourite/Library.h")~ and use it as you would
have is C. have is C. One ~include~ takes as many headers as you care to name:
~(include stdio.h stdlib.h string.h)~.
*** Auto kebabification *** Auto kebabification
For hardcore fans of traditional Lisp naming convention, For hardcore fans of traditional Lisp naming convention,

View File

@@ -36,7 +36,7 @@
(define (line-directive src) (define (line-directive src)
;; `%line' is fmt-c's #line directive. cpp-line concatenates its ;; `%line' is fmt-c's #line directive. cpp-line concatenates its
;; second argument verbatim, so the file name arrives already quoted. ;; second argument verbatim, so the file name arrives already quoted.
`(%line ,(cdr src) ,(fmt #f #\" (car src) #\"))) `(%line ,(cadr src) ,(fmt #f #\" (car src) #\")))
(define (walk-body stmts) (define (walk-body stmts)
"Walk a statement list, re-anchoring each statement that has a known "Walk a statement list, re-anchoring each statement that has a known
@@ -498,14 +498,32 @@ forms, and what remains."
(('define name . rest) `(%define ,(atom-to-fmt-c name) ,@rest)) (('define name . rest) `(%define ,(atom-to-fmt-c name) ,@rest))
(else (walk-expr form)))) (else (walk-expr form))))
;;; Two toplevel forms are separated by a blank line only if they were
;;; written that way: the prelude's typedefs come out packed, and so do
;;; the three includes at the top of a program that wrote them as three
;;; adjacent lines. Forms from different files -- the prelude, then an
;;; imported module, then the unit itself -- always get one.
(define (blank-line-between? prev next)
(if (equal? (form-file prev) (form-file next))
(let ((end (form-end-line prev))
(start (form-line next)))
(and end start (> (- start end) 1)))
#t))
(define (emit-c sex-forms) (define (emit-c sex-forms)
(for-each (lambda (form) (let emit ((forms (pack-comments sex-forms)) (prev #f))
;; Forms the reader did not produce -- the prelude, and (unless (null? forms)
;; anything a macro built that we could not attribute -- (let ((form (car forms)))
;; have no location and get no directive. (when (and prev (blank-line-between? prev form))
(let ((src (and (not (eq? (sex-line-directives) 'none)) (fmt #t nl))
(form-source form)))) ;; Forms the reader did not produce -- the prelude, and
(when src ;; anything a macro built that we could not attribute --
(fmt #t (c-expr (line-directive src))))) ;; have no location and get no directive.
(fmt #t (c-expr (process-toplevel-form form)) nl)) (let ((src (and (not (eq? (sex-line-directives) 'none))
(pack-comments sex-forms))) (form-source form))))
(when src
(fmt #t (c-expr (line-directive src)))))
;; `fl', not `nl': a comment leaves the cursor at the end of
;; its line while everything else has already broken it
(fmt #t (c-expr (process-toplevel-form form)) fl)
(emit (cdr forms) form)))))

View File

@@ -38,11 +38,13 @@
(define (peek port) (define (peek port)
(peek-char port)) (peek-char port))
;;; Record where a form started. Called with the line of the opening ;;; Record where a form started and where it ended. Called with the
;;; delimiter, sampled before it is consumed ;;; line of the opening delimiter, sampled before it is consumed, once
;;; the form itself has been read -- so the current line is the one its
;;; closing delimiter sits on
(define (stamp form line) (define (stamp form line)
(when (pair? form) (when (pair? form)
(set-form-source! form (current-source-file) line)) (set-form-source! form (current-source-file) line (current-line)))
form) form)
(define (delimiter? c) (define (delimiter? c)

View File

@@ -254,7 +254,7 @@
;; emits the comment immediately before the declaration ;; emits the comment immediately before the declaration
(cons form (cons form
(if doc (if doc
(cons (list 'comment doc) acc) (cons (copy-form-source! form (list 'comment doc)) acc)
acc))) acc)))
(define (strip-fn-header-comments fn-form) (define (strip-fn-header-comments fn-form)
@@ -277,7 +277,11 @@
;; the parameters are the body's outermost scope ;; the parameters are the body's outermost scope
(header (map resolve-closure-types (take expanded boundary))) (header (map resolve-closure-types (take expanded boundary)))
(body (walk-body (drop expanded boundary) env)) (body (walk-body (drop expanded boundary) env))
(processed (append header body))) ;; A function is header and body stitched together, so the
;; result is fresh structure: without this the definition
;; has no location, and nothing downstream can say where
;; the function was written
(processed (copy-form-source! sex-fn-raw (append header body))))
(with-docstring doc processed (with-docstring doc processed
(append (hash-table-ref env :lambda-aux-code) acc)))))) (append (hash-table-ref env :lambda-aux-code) acc))))))

View File

@@ -633,4 +633,18 @@ compiles."
(test-assert "nor is an aggregate tag" (test-assert "nor is an aggregate tag"
(emits? "(struct t ((z int))) (struct s ((a (¤ struct t))))" "struct t a[]")) (emits? "(struct t ((z int))) (struct s ((a (¤ struct t))))" "struct t a[]"))
(test-assert "nor one behind a pointer" (test-assert "nor one behind a pointer"
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))) (emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
;; Toplevel forms used to be separated by a blank line unconditionally,
;; which spread a block of adjacent typedefs over twice the lines its
;; author wrote
(test-group "vertical spacing"
(test-assert "adjacent forms stay adjacent"
(emits? "(include a.h)\n(include b.h)" "#include <a.h>\n#include <b.h>"))
(test-assert "a blank line is kept"
(emits? "(include a.h)\n\n(include b.h)" "#include <a.h>\n\n#include <b.h>"))
;; the gap is measured from where the form ended, not where it began
(test-assert "a multi-line form does not invent one"
(emits? "(struct s\n ((x int)))\n(include b.h)" "};\n#include <b.h>"))
(test-assert "nor swallows one"
(emits? "(struct s\n ((x int)))\n\n(include b.h)" "};\n\n#include <b.h>"))))

View File

@@ -97,7 +97,7 @@
(define (compile-to-c source) (define (compile-to-c source)
"Run reader -> semen -> writer on SOURCE, returning the generated C. "Run reader -> semen -> writer on SOURCE, returning the generated C.
parse-all is called directly rather than through read-raw-forms so the parse-all is called directly rather than through read-raw-forms so the
fixture can be given a file name: a location is (file . line), and the fixture can be given a file name: a location carries one, and the
file is what makes an imported module report itself rather than the unit file is what makes an imported module report itself rather than the unit
that imported it." that imported it."
(let ((forms (with-input-from-string source (let ((forms (with-input-from-string source

View File

@@ -12,6 +12,7 @@
form-source form-source
form-file form-file
form-line form-line
form-end-line
copy-form-source! copy-form-source!
stamp-form-source! stamp-form-source!
form-location form-location

View File

@@ -93,9 +93,12 @@
;;; read-with-source-info / get-line-number, which only works when forms ;;; read-with-source-info / get-line-number, which only works when forms
;;; are produced by the built-in `read'. ;;; are produced by the built-in `read'.
;;; ;;;
;;; A location is (file . line). The file matters because imported ;;; A location is (file line end-line): where the form starts and the
;;; line its closing delimiter is on. The file matters because imported
;;; modules paste their public forms into the current unit: those forms ;;; modules paste their public forms into the current unit: those forms
;;; originate in another file and must be reported as such ;;; originate in another file and must be reported as such. The end line
;;; tells the C writer whether two toplevel forms were written with a
;;; blank line between them
(define +form-sources+ (make-hash-table eq?)) (define +form-sources+ (make-hash-table eq?))
;;; The file `parse-all' is currently reading. Bound by the reader ;;; The file `parse-all' is currently reading. Bound by the reader
@@ -114,8 +117,8 @@
(define (form-type form) (define (form-type form)
(hash-table-ref/default +form-types+ form #f)) (hash-table-ref/default +form-types+ form #f))
(define (set-form-source! form file line) (define (set-form-source! form file line #!optional (end-line line))
(hash-table-set! +form-sources+ form (cons file line))) (hash-table-set! +form-sources+ form (list file line end-line)))
(define (form-source form) (define (form-source form)
(hash-table-ref/default +form-sources+ form #f)) (hash-table-ref/default +form-sources+ form #f))
@@ -126,7 +129,11 @@
(define (form-line form) (define (form-line form)
(let ((src (form-source form))) (let ((src (form-source form)))
(and src (cdr src)))) (and src (cadr src))))
(define (form-end-line form)
(let ((src (form-source form)))
(and src (caddr src))))
(define (copy-form-source! from to) (define (copy-form-source! from to)
"Give TO the location of FROM, if FROM has one. Returns TO, so it can "Give TO the location of FROM, if FROM has one. Returns TO, so it can
@@ -144,7 +151,7 @@ wrap a form-building expression."
"\"file:line: \" for FORM, or \"\" when it has none" "\"file:line: \" for FORM, or \"\" when it has none"
(let ((src (form-source form))) (let ((src (form-source form)))
(if src (if src
(string-append (car src) ":" (number->string (cdr src)) ": ") (string-append (car src) ":" (number->string (cadr src)) ": ")
""))) "")))
(define (sex-error form message . args) (define (sex-error form message . args)