Compare commits
10 Commits
07cf85910a
...
dfafe8becd
| Author | SHA1 | Date | |
|---|---|---|---|
| dfafe8becd | |||
| e9960488fb | |||
| 6c8b38b7e2 | |||
| dd3a4bdac5 | |||
| fb4b8aa1f9 | |||
| 95f39e4b44 | |||
| 7f3f22e853 | |||
| f071684aec | |||
| 737864dadc | |||
| 6181a6c8d4 |
20
Makefile
20
Makefile
@@ -20,8 +20,16 @@ CSC_FLAGS += -K prefix -static
|
|||||||
prefix = /usr/local
|
prefix = /usr/local
|
||||||
exec_prefix = $(prefix)
|
exec_prefix = $(prefix)
|
||||||
bindir = $(exec_prefix)/bin
|
bindir = $(exec_prefix)/bin
|
||||||
|
datarootdir = $(prefix)/share
|
||||||
|
datadir = $(datarootdir)
|
||||||
|
# sexc looks for the standard library beside itself: next to the binary
|
||||||
|
# in a work tree, and bindir/../share/sex once installed
|
||||||
|
sexlibdir = $(datadir)/sex
|
||||||
INSTALL = install
|
INSTALL = install
|
||||||
INSTALL_PROGRAM = $(INSTALL)
|
INSTALL_PROGRAM = $(INSTALL)
|
||||||
|
INSTALL_DATA = $(INSTALL) -m 644
|
||||||
|
|
||||||
|
SEX_LIB = $(wildcard lib/*.sex)
|
||||||
|
|
||||||
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
||||||
|
|
||||||
@@ -96,7 +104,7 @@ sextest:
|
|||||||
$(MAKE) -C ./tools/sextest sextest
|
$(MAKE) -C ./tools/sextest sextest
|
||||||
cp ./tools/sextest/sextest .
|
cp ./tools/sextest/sextest .
|
||||||
|
|
||||||
SEX_TEST_PROGRAMS = c99 \
|
SEX_TEST_PROGRAMS = c11 \
|
||||||
closure-signatures \
|
closure-signatures \
|
||||||
closures \
|
closures \
|
||||||
comments \
|
comments \
|
||||||
@@ -112,11 +120,14 @@ SEX_TEST_PROGRAMS = c99 \
|
|||||||
ops \
|
ops \
|
||||||
records \
|
records \
|
||||||
serialize \
|
serialize \
|
||||||
|
specs \
|
||||||
sums \
|
sums \
|
||||||
|
threading \
|
||||||
type-shapes \
|
type-shapes \
|
||||||
unicode \
|
unicode \
|
||||||
unnamed-params \
|
unnamed-params \
|
||||||
wildcards
|
wildcards \
|
||||||
|
worked-example
|
||||||
|
|
||||||
# Multi-module linking is checked end to end; see tests/modules/Makefile.
|
# Multi-module linking is checked end to end; see tests/modules/Makefile.
|
||||||
check-modules: sexc
|
check-modules: sexc
|
||||||
@@ -131,15 +142,18 @@ check run-tests: sexc sex-tests sextest
|
|||||||
|
|
||||||
install: all installdirs
|
install: all installdirs
|
||||||
$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
|
$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
|
||||||
|
$(INSTALL_DATA) $(SEX_LIB) $(DESTDIR)$(sexlibdir)
|
||||||
|
|
||||||
install-strip:
|
install-strip:
|
||||||
$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install
|
$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install
|
||||||
|
|
||||||
installdirs:
|
installdirs:
|
||||||
$(INSTALL) -d $(DESTDIR)$(bindir)
|
$(INSTALL) -d $(DESTDIR)$(bindir) $(DESTDIR)$(sexlibdir)
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm -f $(DESTDIR)$(bindir)/sexc
|
rm -f $(DESTDIR)$(bindir)/sexc
|
||||||
|
rm -f $(SEX_LIB:lib/%=$(DESTDIR)$(sexlibdir)/%)
|
||||||
|
-rmdir $(DESTDIR)$(sexlibdir)
|
||||||
|
|
||||||
$(EGGS_STAMP) deps-update: export CHICKEN_EGG_CACHE := $(EGGS_DIR)/cache
|
$(EGGS_STAMP) deps-update: export CHICKEN_EGG_CACHE := $(EGGS_DIR)/cache
|
||||||
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_REPOSITORY := $(EGGS_DIR)
|
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_REPOSITORY := $(EGGS_DIR)
|
||||||
|
|||||||
62
Readme.org
62
Readme.org
@@ -45,7 +45,9 @@ tree's ~.eggs~ path):
|
|||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** Installation
|
** Installation
|
||||||
GNU directory variables: ~prefix~, ~exec_prefix~, ~bindir~, ~DESTDIR~.
|
GNU directory variables: ~prefix~, ~exec_prefix~, ~bindir~, ~datadir~,
|
||||||
|
~DESTDIR~. The binary goes to ~bindir~ and the standard library to
|
||||||
|
~$(datadir)/sex~.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
# default installation (/usr/local/bin/)
|
# default installation (/usr/local/bin/)
|
||||||
@@ -123,7 +125,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,
|
||||||
@@ -131,17 +134,66 @@ Sex offers automatic kebabification of all symbols, i.e. no more
|
|||||||
ugly ~GL_ARRAY_BUFFER~ s in your code, they may be written in their
|
ugly ~GL_ARRAY_BUFFER~ s in your code, they may be written in their
|
||||||
proper form: ~GL-ARRAY-BUFFER~.
|
proper form: ~GL-ARRAY-BUFFER~.
|
||||||
|
|
||||||
|
** Member access
|
||||||
|
~(. x field ...)~ reads a path, and writes C's ~.~ or its ~->~ according
|
||||||
|
to what it is reading from. Each step asks the type in hand, so one path
|
||||||
|
may be both:
|
||||||
|
|
||||||
|
#+begin_src scheme
|
||||||
|
(struct point ((x int) (y int)))
|
||||||
|
(struct line ((a (* struct point)) (b (struct point))))
|
||||||
|
|
||||||
|
(. p x) ; p : (struct point) -> p.x
|
||||||
|
(. pp x) ; pp : (* struct point) -> pp->x
|
||||||
|
(. l a x) ; l.a is a pointer -> l.a->x
|
||||||
|
(. lp b y) ; lp is, l.b is not -> lp->b.y
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
A receiver whose type cannot be worked out --- something out of a header
|
||||||
|
nothing has parsed --- is read as a value, with a warning saying so.
|
||||||
|
|
||||||
|
A method is ~(.name receiver args ...)~ and a field is ~(. receiver
|
||||||
|
name)~, so the shape of a form says which of the two it is without
|
||||||
|
knowing any types: ~(. a b c)~ is a path and ~(.b a c)~ is a call.
|
||||||
|
|
||||||
** Modules
|
** Modules
|
||||||
Each source file is a module. Module can provide public interface and
|
Each source file is a module. Module can provide public interface and
|
||||||
be imported by using ~(import path/to/module)~ expression. Module
|
be imported by using ~(import path/to/module)~ expression. Module
|
||||||
search path consists of two parts: first is relative to the source
|
search path has three parts: first is relative to the source being
|
||||||
being compiled location, and the second is ~SEX_MODULE_PATH~
|
compiled location, then the ~SEX_MODULE_PATH~ environment variable,
|
||||||
environment variable.
|
and last the standard library.
|
||||||
|
|
||||||
Module's public interface consists of everything declared
|
Module's public interface consists of everything declared
|
||||||
~pub~. Structures, function, macros, types, variables can be
|
~pub~. Structures, function, macros, types, variables can be
|
||||||
public.
|
public.
|
||||||
|
|
||||||
|
** Standard library
|
||||||
|
~lib/~ holds the modules that come with the compiler. They are imported
|
||||||
|
like any other, and being searched last, a module of your own by the
|
||||||
|
same name wins over one of ours.
|
||||||
|
|
||||||
|
~sexc~ looks for them beside itself, so a work tree needs nothing set
|
||||||
|
up; installed, ~$(bindir)/sexc~ finds them under ~$(prefix)/share/sex~.
|
||||||
|
|
||||||
|
There is one so far.
|
||||||
|
|
||||||
|
*** threading-macros
|
||||||
|
~->~ passes a value through a chain of forms, each step taking what came
|
||||||
|
before it as its first argument, so the chain reads in the order it
|
||||||
|
happens instead of inside out. Clojure spells it this way and this is
|
||||||
|
the same thing:
|
||||||
|
|
||||||
|
#+begin_src scheme
|
||||||
|
(import threading-macros)
|
||||||
|
|
||||||
|
(-> 5 double-it (add 3) double-it) ; (double-it (add (double-it 5) 3))
|
||||||
|
(-> lp (. a x)) ; lp->a->x
|
||||||
|
(-> q .length-sq) ; a method is a step like any other
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
A step that is not a list is a call of one argument: ~(-> x f)~ is
|
||||||
|
~(f x)~.
|
||||||
|
|
||||||
** Read-time feature expressions
|
** Read-time feature expressions
|
||||||
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that
|
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that
|
||||||
follows is kept only when the feature expression is true, and otherwise
|
follows is kept only when the feature expression is true, and otherwise
|
||||||
|
|||||||
@@ -9,25 +9,25 @@
|
|||||||
(fn-name (cat 'make-list- type)))
|
(fn-name (cat 'make-list- type)))
|
||||||
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
||||||
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
||||||
(= (-> list next) NULL)
|
(= (. list next) NULL)
|
||||||
(return list))))
|
(return list))))
|
||||||
|
|
||||||
(pub defmacro (add-value-list-T type is-public?)
|
(pub defmacro (add-value-list-T type is-public?)
|
||||||
(let ((list-type (list 'struct (cat 'list- type)))
|
(let ((list-type (list 'struct (cat 'list- type)))
|
||||||
(fn-name (cat 'add-value-list- type)))
|
(fn-name (cat 'add-value-list- type)))
|
||||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
||||||
(while (!= (-> list next) NULL)
|
(while (!= (. list next) NULL)
|
||||||
(= list (-> list next)))
|
(= list (. list next)))
|
||||||
(= (-> list next) (,(cat 'make-list- type)))
|
(= (. list next) (,(cat 'make-list- type)))
|
||||||
(= (-> list value) value))))
|
(= (. list value) value))))
|
||||||
|
|
||||||
(pub defmacro (length-list-T type is-public?)
|
(pub defmacro (length-list-T type is-public?)
|
||||||
(let ((fn-name (cat 'length-list- type))
|
(let ((fn-name (cat 'length-list- type))
|
||||||
(list-type (list 'struct (cat 'list- type))))
|
(list-type (list 'struct (cat 'list- type))))
|
||||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
||||||
(var n size-t 0)
|
(var n size-t 0)
|
||||||
(while (!= (-> list next) NULL)
|
(while (!= (. list next) NULL)
|
||||||
(= list (-> list next))
|
(= list (. list next))
|
||||||
(++ n))
|
(++ n))
|
||||||
(return n))))
|
(return n))))
|
||||||
|
|
||||||
@@ -35,14 +35,14 @@
|
|||||||
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
||||||
((list ,(list '* 'struct (cat 'list- type))))
|
((list ,(list '* 'struct (cat 'list- type))))
|
||||||
bool
|
bool
|
||||||
(return (== (-> list next) NULL))))
|
(return (== (. list next) NULL))))
|
||||||
|
|
||||||
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
||||||
(let ((list-var-2 (cat list-var '-2)))
|
(let ((list-var-2 (cat list-var '-2)))
|
||||||
`(do
|
`(do
|
||||||
(var ,list-var-2 (* ,list-type) ,list-var)
|
(var ,list-var-2 (* ,list-type) ,list-var)
|
||||||
(var ,elt-var ,elt-type (-> ,list-var-2 value))
|
(var ,elt-var ,elt-type (. ,list-var-2 value))
|
||||||
(while (!= (-> ,list-var-2 next) NULL)
|
(while (!= (. ,list-var-2 next) NULL)
|
||||||
,what-do
|
,what-do
|
||||||
(= ,list-var-2 (-> ,list-var-2 next))
|
(= ,list-var-2 (. ,list-var-2 next))
|
||||||
(= ,elt-var (-> ,list-var-2 value))))))
|
(= ,elt-var (. ,list-var-2 value))))))
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
((* char) "%s")
|
((* char) "%s")
|
||||||
(else (error "serialize-struct: unsupported field type"
|
(else (error "serialize-struct: unsupported field type"
|
||||||
type-name name type))))
|
type-name name type))))
|
||||||
(-> v ,name))))))
|
(. v ,name))))))
|
||||||
(if (not printers)
|
(if (not printers)
|
||||||
(error "serialize-struct: no such struct" type-name)
|
(error "serialize-struct: no such struct" type-name)
|
||||||
`(pub fn ,(cat 'serialize- type-name)
|
`(pub fn ,(cat 'serialize- type-name)
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -190,6 +190,10 @@ forms, and what remains."
|
|||||||
;; (dot-access obj field ...) -> obj.field... member access. `%.'
|
;; (dot-access obj field ...) -> obj.field... member access. `%.'
|
||||||
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
|
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
|
||||||
(('dot-access . rest) (cons '%. (map walk-expr rest)))
|
(('dot-access . rest) (cons '%. (map walk-expr rest)))
|
||||||
|
;; `%->' is the C arrow. Nothing writes it: it is what a member
|
||||||
|
;; path lowers to where the thing it reads from is a pointer, the
|
||||||
|
;; name `->' itself being the threading macro
|
||||||
|
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
||||||
(('var . _) (walk-var form))
|
(('var . _) (walk-var form))
|
||||||
;; An expression has no room for a statement, so a comment in a
|
;; An expression has no room for a statement, so a comment in a
|
||||||
;; cast is dropped rather than relocated.
|
;; cast is dropped rather than relocated.
|
||||||
@@ -492,17 +496,38 @@ forms, and what remains."
|
|||||||
((or ('struct . _)
|
((or ('struct . _)
|
||||||
('union . _)) (walk-struct form))
|
('union . _)) (walk-struct form))
|
||||||
(('enum . _) (walk-enum form))
|
(('enum . _) (walk-enum form))
|
||||||
|
;; A target is a filename and not an identifier: GL/gl.h is a path,
|
||||||
|
;; and unkebabify would spell it GLːgl.h
|
||||||
|
(('include target) (list '%include target))
|
||||||
(('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)))))
|
||||||
|
|||||||
20
lib/threading-macros.sex
Normal file
20
lib/threading-macros.sex
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
;;; Threading macros
|
||||||
|
;;;
|
||||||
|
;;; `->' passes a value through a chain of forms, each step taking what
|
||||||
|
;;; came before it as its first argument, so a chain of calls reads in
|
||||||
|
;;; the order it happens rather than inside out. Clojure spells it this
|
||||||
|
;;; way and this is the same thing:
|
||||||
|
;;;
|
||||||
|
;;; (-> 5 double-it (add 3)) == (add (double-it 5) 3)
|
||||||
|
;;;
|
||||||
|
;;; A step that is not a list is a call of one argument, so `(-> x f)'
|
||||||
|
;;; is `(f x)' and a method is a step like any other.
|
||||||
|
|
||||||
|
(pub defmacro (-> x . steps)
|
||||||
|
(let thread ((acc x) (steps steps))
|
||||||
|
(if (null? steps)
|
||||||
|
acc
|
||||||
|
(thread (if (pair? (car steps))
|
||||||
|
(cons (car (car steps)) (cons acc (cdr (car steps))))
|
||||||
|
(list (car steps) acc))
|
||||||
|
(cdr steps)))))
|
||||||
@@ -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)
|
||||||
|
|||||||
263
semen.scm
263
semen.scm
@@ -83,6 +83,10 @@
|
|||||||
((or ('fn . _)
|
((or ('fn . _)
|
||||||
('pub 'fn . _)
|
('pub 'fn . _)
|
||||||
('extern 'fn . _)) (process-fn sex-form acc))
|
('extern 'fn . _)) (process-fn sex-form acc))
|
||||||
|
((or ('spec . _)
|
||||||
|
('pub 'spec . _)) (process-spec sex-form acc))
|
||||||
|
((or ('realize . _)
|
||||||
|
('pub 'realize . _)) (process-realize sex-form acc))
|
||||||
(('ops . _) (process-ops sex-form acc))
|
(('ops . _) (process-ops sex-form acc))
|
||||||
(('pub 'ops . _)
|
(('pub 'ops . _)
|
||||||
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
|
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
|
||||||
@@ -99,14 +103,19 @@
|
|||||||
((or ('var . _)
|
((or ('var . _)
|
||||||
('pub 'var . _)
|
('pub 'var . _)
|
||||||
('extern 'var . _)) (process-global-var sex-form acc))
|
('extern 'var . _)) (process-global-var sex-form acc))
|
||||||
(('include . includes) (process-includes sex-form includes acc))
|
;; `pub' on either of these is what read-public-interface already
|
||||||
|
;; accepts, and a module whose spec defaults call printf has to be
|
||||||
|
;; able to say so
|
||||||
|
((or ('include . includes)
|
||||||
|
('pub 'include . includes)) (process-includes sex-form includes acc))
|
||||||
((or ('define name . _)
|
((or ('define name . _)
|
||||||
('pub 'define name . _))
|
('pub 'define name . _))
|
||||||
(add-define name sex-form)
|
(add-define name sex-form)
|
||||||
(cons sex-form acc))
|
(cons sex-form acc))
|
||||||
(('comment . _) (cons sex-form acc))
|
(('comment . _) (cons sex-form acc))
|
||||||
|
|
||||||
(('import . modules)
|
((or ('import . modules)
|
||||||
|
('pub 'import . modules))
|
||||||
(process-imports (get-modules-public-forms modules) acc))
|
(process-imports (get-modules-public-forms modules) acc))
|
||||||
|
|
||||||
((or ('defmacro . rest)
|
((or ('defmacro . rest)
|
||||||
@@ -245,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)
|
||||||
@@ -268,10 +277,126 @@
|
|||||||
;; 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))))))
|
||||||
|
|
||||||
|
;;; (spec (printable T) (fn show ((self (* const T))) void))
|
||||||
|
;;;
|
||||||
|
;;; declares a contract and emits nothing: a spec method only exists
|
||||||
|
;;; once some type realizes it, and then once per type.
|
||||||
|
(define (process-spec form acc)
|
||||||
|
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
|
||||||
|
(head (and (pair? (cdr core)) (cadr core)))
|
||||||
|
(methods (remove comment-form? (cddr core))))
|
||||||
|
(unless (and (list? head) (= 2 (length head))
|
||||||
|
(symbol? (car head)) (symbol? (cadr head)))
|
||||||
|
(sex-error form "a spec is (spec (name T) methods ...)" form))
|
||||||
|
(let ((name (car head)))
|
||||||
|
(when (get-spec name)
|
||||||
|
(sex-error form "spec is already declared" name))
|
||||||
|
(for-each (lambda (m)
|
||||||
|
(unless (and (pair? m) (eq? (car m) 'fn)
|
||||||
|
(pair? (cdr m)) (symbol? (second m)))
|
||||||
|
(sex-error form "a spec takes fn forms" m)))
|
||||||
|
methods)
|
||||||
|
(add-spec! name (cadr head) methods)
|
||||||
|
acc)))
|
||||||
|
|
||||||
|
;;; (realize (printable expr) (fn show ((self (* const expr))) void ...))
|
||||||
|
;;;
|
||||||
|
;;; is the spec's methods for one type: the ones written here, plus a
|
||||||
|
;;; copy of every default the spec carries, each with the parameter
|
||||||
|
;;; substituted and the name mangled to exprːprintableːshow. They land
|
||||||
|
;;; in the ops registry beside inherent methods, so (.show e) finds
|
||||||
|
;;; them and a name used twice on one type is caught either way.
|
||||||
|
(define (process-realize form acc)
|
||||||
|
(let* ((pub? (eq? (car form) 'pub))
|
||||||
|
(core (if pub? (cdr form) form))
|
||||||
|
(head (and (pair? (cdr core)) (cadr core)))
|
||||||
|
(given (remove comment-form? (cddr core))))
|
||||||
|
(unless (and (list? head) (= 2 (length head))
|
||||||
|
(symbol? (car head)) (symbol? (cadr head)))
|
||||||
|
(sex-error form "a realization is (realize (spec type) methods ...)" form))
|
||||||
|
(let* ((name (car head))
|
||||||
|
(type (cadr head))
|
||||||
|
(spec (or (get-spec name)
|
||||||
|
(sex-error form "no such spec" name))))
|
||||||
|
(when (realizes? name type)
|
||||||
|
(sex-error form "spec is already realized for" name type))
|
||||||
|
(add-realization! name type)
|
||||||
|
(let ((wanted (spec-methods spec))
|
||||||
|
(parameter (spec-parameter spec)))
|
||||||
|
(check-realization form name type parameter wanted given)
|
||||||
|
(fold (lambda (entry acc)
|
||||||
|
(process-realized-method form name type
|
||||||
|
(and pub? (cdr entry)) (car entry) acc))
|
||||||
|
acc
|
||||||
|
(realization-methods type parameter wanted given))))))
|
||||||
|
|
||||||
|
;;; What is written here, which a pub realization exports, and then
|
||||||
|
;;; every default nobody overrode, which it does not: a default is
|
||||||
|
;;; synthesized again in every unit that imports the realization, so
|
||||||
|
;;; exporting one too would be a second definition of one symbol.
|
||||||
|
(define (realization-methods type parameter wanted given)
|
||||||
|
(append (map (lambda (g) (cons g #t)) given)
|
||||||
|
(filter-map
|
||||||
|
(lambda (w)
|
||||||
|
(and (> (length w) 4) ; carries a body, so a default
|
||||||
|
(not (find (lambda (g) (eq? (second g) (second w))) given))
|
||||||
|
(cons (substitute-parameter parameter type w) #f)))
|
||||||
|
wanted)))
|
||||||
|
|
||||||
|
(define (check-realization form name type parameter wanted given)
|
||||||
|
(for-each
|
||||||
|
(lambda (g)
|
||||||
|
(unless (and (pair? g) (eq? (car g) 'fn) (pair? (cdr g)) (symbol? (second g)))
|
||||||
|
(sex-error form "a realization takes fn forms" g))
|
||||||
|
(let ((w (find (lambda (w) (eq? (second w) (second g))) wanted)))
|
||||||
|
(unless w
|
||||||
|
(sex-error form "spec has no such method" name (second g)))
|
||||||
|
(let ((expected (substitute-parameter parameter type w)))
|
||||||
|
(unless (equal? (arglist-types (sex-fn-arglist expected))
|
||||||
|
(arglist-types (sex-fn-arglist g)))
|
||||||
|
(sex-error form "method's arguments do not match the spec" (second g)))
|
||||||
|
(unless (equal? (sex-fn-return-type expected) (sex-fn-return-type g))
|
||||||
|
(sex-error form "method's return type does not match the spec"
|
||||||
|
(second g))))))
|
||||||
|
given)
|
||||||
|
(let ((missing (filter-map
|
||||||
|
(lambda (w)
|
||||||
|
(and (= (length w) 4) ; no body, so required
|
||||||
|
(not (find (lambda (g) (eq? (second g) (second w))) given))
|
||||||
|
(second w)))
|
||||||
|
wanted)))
|
||||||
|
(when (pair? missing)
|
||||||
|
(apply sex-error form "realization is missing a method of" name missing))))
|
||||||
|
|
||||||
|
;;; A realization is public or private as a unit -- its methods do
|
||||||
|
;;; not carry `pub' of their own, the way an ops block's do, because
|
||||||
|
;;; the spec already says which of them there are.
|
||||||
|
(define (process-realized-method form spec type pub? method acc)
|
||||||
|
(let* ((name (second method))
|
||||||
|
(qualified (qualify-op (qualify-op type spec) name))
|
||||||
|
(renamed (rename-fn method qualified)))
|
||||||
|
(when (get-method type name)
|
||||||
|
(sex-error form "method is already defined on" type name))
|
||||||
|
(add-method! type name qualified)
|
||||||
|
(process-fn (copy-form-source! form (if pub? (cons 'pub renamed) renamed))
|
||||||
|
acc)))
|
||||||
|
|
||||||
|
;;; T in a spec's method stands for whatever realizes it
|
||||||
|
(define (substitute-parameter parameter type form)
|
||||||
|
(cond ((eq? form parameter) type)
|
||||||
|
((pair? form) (recons form
|
||||||
|
(substitute-parameter parameter type (car form))
|
||||||
|
(substitute-parameter parameter type (cdr form))))
|
||||||
|
(else form)))
|
||||||
|
|
||||||
;;; (ops vec2
|
;;; (ops vec2
|
||||||
;;; (pub fn length ((self (* const vec2))) float ...))
|
;;; (pub fn length ((self (* const vec2))) float ...))
|
||||||
;;;
|
;;;
|
||||||
@@ -308,9 +433,17 @@
|
|||||||
(define (make-fn-env fn-form)
|
(define (make-fn-env fn-form)
|
||||||
(let ((env (make-hash-table))
|
(let ((env (make-hash-table))
|
||||||
(parameters (make-hash-table)))
|
(parameters (make-hash-table)))
|
||||||
|
;; `(p (* struct point))' writes the type in one piece and
|
||||||
|
;; `(p * struct point)' spreads it over the rest, so the type is
|
||||||
|
;; the whole tail whenever there is more of it than one element
|
||||||
(for-each (lambda (param)
|
(for-each (lambda (param)
|
||||||
(when (and (pair? param) (pair? (cdr param)))
|
(when (and (pair? param) (pair? (cdr param))
|
||||||
(hash-table-set! parameters (first param) (second param))))
|
(named-arg? param))
|
||||||
|
(hash-table-set! parameters
|
||||||
|
(first param)
|
||||||
|
(if (null? (cddr param))
|
||||||
|
(second param)
|
||||||
|
(cdr param)))))
|
||||||
(sex-fn-arglist fn-form))
|
(sex-fn-arglist fn-form))
|
||||||
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
|
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
|
||||||
(set! (hash-table-ref env :lambda-counter) 0)
|
(set! (hash-table-ref env :lambda-counter) 0)
|
||||||
@@ -478,6 +611,8 @@
|
|||||||
|
|
||||||
((match) (walk-match form env))
|
((match) (walk-match form env))
|
||||||
|
|
||||||
|
((dot-access) (walk-member-path form env))
|
||||||
|
|
||||||
(else
|
(else
|
||||||
(if (method-head? (car form))
|
(if (method-head? (car form))
|
||||||
(walk-method-call form env)
|
(walk-method-call form env)
|
||||||
@@ -642,7 +777,7 @@
|
|||||||
(cond ((symbol? expr) #t)
|
(cond ((symbol? expr) #t)
|
||||||
((not (pair? expr)) #f)
|
((not (pair? expr)) #f)
|
||||||
(else (case (car expr)
|
(else (case (car expr)
|
||||||
((dot-access ->) (simple-path? (second expr)))
|
((dot-access %->) (simple-path? (second expr)))
|
||||||
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
|
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
|
||||||
(else #f)))))
|
(else #f)))))
|
||||||
|
|
||||||
@@ -751,12 +886,38 @@
|
|||||||
method))
|
method))
|
||||||
(else `(& ,receiver)))))
|
(else `(& ,receiver)))))
|
||||||
|
|
||||||
|
;;; `(. p x)' is `p->x' when p is a pointer and `p.x' when it is not:
|
||||||
|
;;; the receiver's type says which, so a path reads the same however
|
||||||
|
;;; what it starts from is held. One step at a time, since only the
|
||||||
|
;;; type in hand can answer for the next field:
|
||||||
|
;;;
|
||||||
|
;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x
|
||||||
|
;;;
|
||||||
|
;;; A type we cannot work out is read as a value, which is what the
|
||||||
|
;;; path would have meant before any of this.
|
||||||
|
(define (walk-member-path form env)
|
||||||
|
(let ((base (second form)))
|
||||||
|
(let step ((expr (walk-statement base env))
|
||||||
|
(type (expression-type base env))
|
||||||
|
(fields (cddr form))
|
||||||
|
(told? #f))
|
||||||
|
(if (null? fields)
|
||||||
|
(copy-form-source! form expr)
|
||||||
|
(let ((target (pointer-target type)))
|
||||||
|
(unless (or type told?)
|
||||||
|
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||||
|
base))
|
||||||
|
(step (list (if target '%-> 'dot-access) expr (car fields))
|
||||||
|
(field-type (or target type) (car fields))
|
||||||
|
(cdr fields)
|
||||||
|
(or told? (not type))))))))
|
||||||
|
|
||||||
(define (lvalue? expr)
|
(define (lvalue? expr)
|
||||||
(cond ((symbol? expr) #t)
|
(cond ((symbol? expr) #t)
|
||||||
((vector? expr) #t) ; a compound literal is one
|
((vector? expr) #t) ; a compound literal is one
|
||||||
((not (pair? expr)) #f)
|
((not (pair? expr)) #f)
|
||||||
(else (case (car expr)
|
(else (case (car expr)
|
||||||
((dot-access -> ¤) #t)
|
((dot-access %-> ¤) #t)
|
||||||
((*) (= 2 (length expr)))
|
((*) (= 2 (length expr)))
|
||||||
(else #f)))))
|
(else #f)))))
|
||||||
|
|
||||||
@@ -830,8 +991,8 @@
|
|||||||
(arithmetic-type expr env)))
|
(arithmetic-type expr env)))
|
||||||
((dot-access) (member-path-type (expression-type (second expr) env)
|
((dot-access) (member-path-type (expression-type (second expr) env)
|
||||||
(cddr expr)))
|
(cddr expr)))
|
||||||
((->) (member-path-type (pointer-target (expression-type (second expr) env))
|
((%->) (member-path-type (pointer-target (expression-type (second expr) env))
|
||||||
(cddr expr)))
|
(cddr expr)))
|
||||||
((cast) (and (= 3 (length expr)) (third expr)))
|
((cast) (and (= 3 (length expr)) (third expr)))
|
||||||
((sizeof) 'size-t)
|
((sizeof) 'size-t)
|
||||||
;; `(closure ((x int)) int () ...)' is a `(closure ((int)) int)',
|
;; `(closure ((x int)) int () ...)' is a `(closure ((int)) int)',
|
||||||
@@ -849,13 +1010,25 @@
|
|||||||
;; ...and an increment is the operand unpromoted:
|
;; ...and an increment is the operand unpromoted:
|
||||||
;; (++ c), c : char -> char
|
;; (++ c), c : char -> char
|
||||||
((++ --) (expression-type (second expr) env))
|
((++ --) (expression-type (second expr) env))
|
||||||
;; otherwise a call: a closure answers with its own return type,
|
;; otherwise a call: a method answers for the name it resolves
|
||||||
;; anything else with what its signature says
|
;; to, a closure with its own return type, anything else with
|
||||||
|
;; what its signature says
|
||||||
(else
|
(else
|
||||||
(let ((closure (receiver-closure-type (car expr) env)))
|
(cond
|
||||||
(if closure
|
((method-head? (car expr)) (method-call-type expr env))
|
||||||
(third closure)
|
((receiver-closure-type (car expr) env) => third)
|
||||||
(and (symbol? (car expr)) (get-return-type (car expr))))))))))
|
(else (and (symbol? (car expr)) (get-return-type (car expr))))))))))
|
||||||
|
|
||||||
|
;;; (.length-sq (.sub a b)) types the inner call to find the outer
|
||||||
|
;;; one's receiver, so a method call has to answer like any other
|
||||||
|
(define (method-call-type expr env)
|
||||||
|
(and (pair? (cdr expr))
|
||||||
|
(let* ((written (expression-type (second expr) env))
|
||||||
|
(target (pointer-target written))
|
||||||
|
(key (ops-key (or target written))))
|
||||||
|
(and key
|
||||||
|
(let ((method (get-method key (method-head-name (car expr)))))
|
||||||
|
(and method (get-return-type method)))))))
|
||||||
|
|
||||||
;;; C's usual arithmetic conversions, far enough to answer `_':
|
;;; C's usual arithmetic conversions, far enough to answer `_':
|
||||||
;;;
|
;;;
|
||||||
@@ -944,22 +1117,36 @@
|
|||||||
(eq? '* (car type))
|
(eq? '* (car type))
|
||||||
(unwrap-type (cdr type))))
|
(unwrap-type (cdr type))))
|
||||||
|
|
||||||
|
;;; A path looks through a pointer wherever it meets one, the way the
|
||||||
|
;;; access itself does: `l.a->x' and `l.b.x' are both (. l a x) and
|
||||||
|
;;; both answer with x's type
|
||||||
(define (member-path-type type fields)
|
(define (member-path-type type fields)
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
type
|
type
|
||||||
(member-path-type (field-type type (car fields)) (cdr fields))))
|
(member-path-type (field-type (or (pointer-target type) type)
|
||||||
|
(car fields))
|
||||||
|
(cdr fields))))
|
||||||
|
|
||||||
(define (field-type type field)
|
(define (field-type type field)
|
||||||
(let ((name (cond ((symbol? type) type)
|
(let* ((fields (type-fields type))
|
||||||
((and (list? type)
|
(entry (and fields (assq field fields))))
|
||||||
(= 2 (length type))
|
(and entry (second entry))))
|
||||||
(memq (car type) '(struct union)))
|
|
||||||
(second type))
|
;;; `(struct point)' names its fields in the type database. A sum's
|
||||||
(else #f))))
|
;;; payload writes its union out in place -- `(union ((circle (struct
|
||||||
(and name
|
;;; shapeːcircle)) ...))' -- and carries them with it, so there is
|
||||||
(let* ((fields (get-fields name))
|
;;; nothing to look up
|
||||||
(entry (and fields (assq field fields))))
|
(define (type-fields type)
|
||||||
(and entry (second entry))))))
|
(cond ((and (list? type)
|
||||||
|
(= 2 (length type))
|
||||||
|
(memq (car type) '(struct union))
|
||||||
|
(list? (second type)))
|
||||||
|
(second type))
|
||||||
|
;; `ops-key' is already the "name of the aggregate" question,
|
||||||
|
;; const and all: (* const expr) derefs to (const expr), which
|
||||||
|
;; has the fields `expr' has
|
||||||
|
((ops-key type) => get-fields)
|
||||||
|
(else #f)))
|
||||||
|
|
||||||
(define (make-lambda-name enclosing-fn-name counter)
|
(define (make-lambda-name enclosing-fn-name counter)
|
||||||
(string->symbol
|
(string->symbol
|
||||||
@@ -995,19 +1182,13 @@
|
|||||||
|
|
||||||
(define +closure-env-bytes+ 16)
|
(define +closure-env-bytes+ 16)
|
||||||
|
|
||||||
;;; +closure-env-bytes+ for capacity, the widest built-ins for
|
;;; +closure-env-bytes+ for capacity, max_align_t for alignment, hence
|
||||||
;;; alignment, hence union -- a union takes the strictest alignment of
|
;;; union -- a union takes the strictest alignment of its members.
|
||||||
;;; its members. `max_align_t' would say the second in one word:
|
|
||||||
;;;
|
|
||||||
;;; sexc hello-world.sex -- -std=c99 unknown type name 'max_align_t'
|
|
||||||
(define +closure-env-type+ 'ƛenv)
|
(define +closure-env-type+ 'ƛenv)
|
||||||
|
|
||||||
(define (closure-env-declaration)
|
(define (closure-env-declaration)
|
||||||
`(union ,+closure-env-type+ ((bytes (¤ char ,+closure-env-bytes+))
|
`(union ,+closure-env-type+ ((align max-align-t)
|
||||||
(align-integer (long long))
|
(bytes (¤ char ,+closure-env-bytes+)))))
|
||||||
(align-real (long double))
|
|
||||||
(align-pointer (* void))
|
|
||||||
(align-code (fn ((* void)) void)))))
|
|
||||||
|
|
||||||
(define +closure-structs+ (make-hash-table))
|
(define +closure-structs+ (make-hash-table))
|
||||||
(define +closure-forwards+ (make-hash-table))
|
(define +closure-forwards+ (make-hash-table))
|
||||||
@@ -1164,7 +1345,7 @@
|
|||||||
(iota (length (second type))))))
|
(iota (length (second type))))))
|
||||||
(unless (hash-table-exists? +closure-conversions+ convert)
|
(unless (hash-table-exists? +closure-conversions+ convert)
|
||||||
(hash-table-set! +closure-conversions+ convert #t)
|
(hash-table-set! +closure-conversions+ convert #t)
|
||||||
(let ((call `((-> ƛcaptures f) ,@(map first params))))
|
(let ((call `((%-> ƛcaptures f) ,@(map first params))))
|
||||||
(for-each
|
(for-each
|
||||||
(lambda (emitted)
|
(lambda (emitted)
|
||||||
(set! *pending-closure-structs*
|
(set! *pending-closure-structs*
|
||||||
@@ -1182,7 +1363,7 @@
|
|||||||
(= (dot-access ƛc code) ,thunk)
|
(= (dot-access ƛc code) ,thunk)
|
||||||
(var ƛcaptures (* (struct ,record))
|
(var ƛcaptures (* (struct ,record))
|
||||||
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
||||||
(= (-> ƛcaptures f) f)
|
(= (%-> ƛcaptures f) f)
|
||||||
(return ƛc))))))
|
(return ƛc))))))
|
||||||
convert))
|
convert))
|
||||||
|
|
||||||
@@ -1300,7 +1481,7 @@
|
|||||||
`((var ƛcaptures (* (struct ,record)) ƛe)
|
`((var ƛcaptures (* (struct ,record)) ƛe)
|
||||||
,@(map (lambda (cap)
|
,@(map (lambda (cap)
|
||||||
`(var ,(first cap) ,(second cap)
|
`(var ,(first cap) ,(second cap)
|
||||||
(-> ƛcaptures ,(first cap))))
|
(%-> ƛcaptures ,(first cap))))
|
||||||
caps)))
|
caps)))
|
||||||
,@body)
|
,@body)
|
||||||
|
|
||||||
@@ -1318,7 +1499,7 @@
|
|||||||
`((var ƛcaptures (* (struct ,record))
|
`((var ƛcaptures (* (struct ,record))
|
||||||
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
(cast (& (dot-access ƛc env)) (* (struct ,record))))
|
||||||
,@(map (lambda (cap)
|
,@(map (lambda (cap)
|
||||||
`(= (-> ƛcaptures ,(first cap)) ,(first cap)))
|
`(= (%-> ƛcaptures ,(first cap)) ,(first cap)))
|
||||||
caps)))
|
caps)))
|
||||||
(return ƛc))))))))
|
(return ƛc))))))))
|
||||||
(else (sex-error form "malformed closure" form))))
|
(else (sex-error form "malformed closure" form))))
|
||||||
|
|||||||
@@ -110,10 +110,19 @@
|
|||||||
|
|
||||||
(define (c-paren x) (cat "(" (c-expr x) ")"))
|
(define (c-paren x) (cat "(" (c-expr x) ")"))
|
||||||
|
|
||||||
|
;; `.' and `->' associate left, and their right operand is always a
|
||||||
|
;; name, so a member access written inside another is the left one
|
||||||
|
;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r'
|
||||||
|
(define (c-member-op? x)
|
||||||
|
(let ((str (display-to-string x)))
|
||||||
|
(or (string=? str ".") (string=? str "->"))))
|
||||||
|
|
||||||
(define (c-maybe-paren op x)
|
(define (c-maybe-paren op x)
|
||||||
(lambda (st)
|
(lambda (st)
|
||||||
((fmt-let 'op op
|
((fmt-let 'op op
|
||||||
(if (and (c-op<= (fmt-op st) op)
|
(if (and (c-op<= (fmt-op st) op)
|
||||||
|
(not (and (c-member-op? (fmt-op st))
|
||||||
|
(c-member-op? op)))
|
||||||
(not (vector? x)))
|
(not (vector? x)))
|
||||||
(c-paren x)
|
(c-paren x)
|
||||||
x))
|
x))
|
||||||
@@ -297,8 +306,6 @@
|
|||||||
(cond
|
(cond
|
||||||
((eq? (car x) (string->symbol "."))
|
((eq? (car x) (string->symbol "."))
|
||||||
((apply c-op "." (cdr x)) st))
|
((apply c-op "." (cdr x)) st))
|
||||||
((eq? (car x) (string->symbol "->"))
|
|
||||||
((apply c-op "->" (cdr x)) st))
|
|
||||||
((eq? (car x) (string->symbol "++"))
|
((eq? (car x) (string->symbol "++"))
|
||||||
((apply c-op "++" (cdr x)) st))
|
((apply c-op "++" (cdr x)) st))
|
||||||
((eq? (car x) (string->symbol "--"))
|
((eq? (car x) (string->symbol "--"))
|
||||||
|
|||||||
@@ -32,7 +32,11 @@
|
|||||||
(only types get-type-info get-tag-info get-fields
|
(only types get-type-info get-tag-info get-fields
|
||||||
get-underlying-type type-match type-pattern-matches?
|
get-underlying-type type-match type-pattern-matches?
|
||||||
map-fields
|
map-fields
|
||||||
get-name-type get-return-type type-of))
|
get-name-type get-return-type type-of
|
||||||
|
;; what a generic asks before it expands:
|
||||||
|
;; (realizes? 'ord type) beats a wall of C
|
||||||
|
;; errors two stages downstream
|
||||||
|
realizes? get-method))
|
||||||
,@body)))
|
,@body)))
|
||||||
|
|
||||||
(define (get-macro name)
|
(define (get-macro name)
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
|
|||||||
"fn"
|
"fn"
|
||||||
"pub"
|
"pub"
|
||||||
"record"
|
"record"
|
||||||
|
"realize"
|
||||||
|
"spec"
|
||||||
"struct"
|
"struct"
|
||||||
"sum"
|
"sum"
|
||||||
"var"
|
"var"
|
||||||
@@ -85,6 +87,9 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
|
|||||||
(put 'defmacro 'lisp-indent-function 'defun)
|
(put 'defmacro 'lisp-indent-function 'defun)
|
||||||
(put 'record 'lisp-indent-function 'defun)
|
(put 'record 'lisp-indent-function 'defun)
|
||||||
(put 'sum 'lisp-indent-function 'defun)
|
(put 'sum 'lisp-indent-function 'defun)
|
||||||
|
(put 'spec 'lisp-indent-function 'defun)
|
||||||
|
(put 'realize 'lisp-indent-function 'defun)
|
||||||
|
(put 'ops 'lisp-indent-function 'defun)
|
||||||
(put 'match 'lisp-indent-function 1)
|
(put 'match 'lisp-indent-function 1)
|
||||||
(put 'struct 'lisp-indent-function 'defun)
|
(put 'struct 'lisp-indent-function 'defun)
|
||||||
(put 'union 'lisp-indent-function 'defun)
|
(put 'union 'lisp-indent-function 'defun)
|
||||||
|
|||||||
@@ -43,7 +43,20 @@
|
|||||||
|
|
||||||
(define (get-module-paths)
|
(define (get-module-paths)
|
||||||
(cons (current-directory)
|
(cons (current-directory)
|
||||||
+persistent-module-paths+))
|
(append +persistent-module-paths+ (standard-library-paths))))
|
||||||
|
|
||||||
|
;;; Where the modules shipped with the compiler live. Asked of the
|
||||||
|
;;; binary rather than baked in when it is built: in a work tree sexc
|
||||||
|
;;; sits beside lib/, and installed it sits in bindir with the modules
|
||||||
|
;;; under ../share/sex. Last in the search order, so a module of one's
|
||||||
|
;;; own wins over one of ours by the same name
|
||||||
|
(define (standard-library-paths)
|
||||||
|
(let ((binary (executable-pathname)))
|
||||||
|
(if binary
|
||||||
|
(let ((bin (pathname-directory binary)))
|
||||||
|
(list (make-pathname bin "lib")
|
||||||
|
(make-pathname (list (pathname-directory bin) "share") "sex")))
|
||||||
|
(list))))
|
||||||
|
|
||||||
(define (locate-module name)
|
(define (locate-module name)
|
||||||
;; Module locations: relative to file being compiled, or in what was
|
;; Module locations: relative to file being compiled, or in what was
|
||||||
@@ -107,6 +120,19 @@
|
|||||||
(copy-form-source! form
|
(copy-form-source! form
|
||||||
`(ops ,type ,@(map public-fn-interface methods))))))
|
`(ops ,type ,@(map public-fn-interface methods))))))
|
||||||
|
|
||||||
|
;;; A realization leaves with the methods written in it, reduced to
|
||||||
|
;;; prototypes. Its defaults do not travel: the importer synthesizes
|
||||||
|
;;; its own static copies from the spec, which is why a default is
|
||||||
|
;;; static even when the realization is pub.
|
||||||
|
(define (realize-public-interface form)
|
||||||
|
(let ((core (cdr form)))
|
||||||
|
(copy-form-source!
|
||||||
|
form
|
||||||
|
`(pub realize ,(second core)
|
||||||
|
;; pub goes back on at the realize, not on its methods
|
||||||
|
,@(map (lambda (m) (cdr (public-fn-interface (cons 'pub m))))
|
||||||
|
(remove comment-form? (cddr core)))))))
|
||||||
|
|
||||||
;;; TODO: use semen facilities to analyze modules
|
;;; TODO: use semen facilities to analyze modules
|
||||||
(define (process-public-interface-form form acc)
|
(define (process-public-interface-form form acc)
|
||||||
(match form
|
(match form
|
||||||
@@ -117,11 +143,13 @@
|
|||||||
(('pub 'var . _)
|
(('pub 'var . _)
|
||||||
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
|
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
|
||||||
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
|
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
|
||||||
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'struct 'sum 'typedef 'union) . _)
|
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'spec 'struct 'sum 'typedef 'union) . _)
|
||||||
(cons (copy-form-source! form (cdr form)) acc))
|
(cons (copy-form-source! form (cdr form)) acc))
|
||||||
(('ops . _)
|
(('ops . _)
|
||||||
(let ((exported (ops-public-interface form)))
|
(let ((exported (ops-public-interface form)))
|
||||||
(if exported (cons exported acc) acc)))
|
(if exported (cons exported acc) acc)))
|
||||||
|
(('pub 'realize . _)
|
||||||
|
(cons (realize-public-interface form) acc))
|
||||||
(('pub . _)
|
(('pub . _)
|
||||||
(sex-error form "pub must be followed by a definition" form))
|
(sex-error form "pub must be followed by a definition" form))
|
||||||
(_ acc)))
|
(_ acc)))
|
||||||
|
|||||||
@@ -26,8 +26,16 @@
|
|||||||
(parameterize ((sex-line-directives 'none))
|
(parameterize ((sex-line-directives 'none))
|
||||||
(emit-c (semen-process forms)))))))
|
(emit-c (semen-process forms)))))))
|
||||||
|
|
||||||
|
;;; Warnings are what `warns?' is for; a test that happens to trip one
|
||||||
|
;;; should not print it
|
||||||
|
(define (silently thunk)
|
||||||
|
"THUNK's value, with whatever it wrote to stderr dropped."
|
||||||
|
(let ((value #f))
|
||||||
|
(with-error-output-to-string (lambda () (set! value (thunk))))
|
||||||
|
value))
|
||||||
|
|
||||||
(define (emits? source fragment)
|
(define (emits? source fragment)
|
||||||
(and (string-contains (sex->c source) fragment) #t))
|
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
||||||
|
|
||||||
(define (error-message source)
|
(define (error-message source)
|
||||||
"Compile SOURCE and return the error text as the user sees it --
|
"Compile SOURCE and return the error text as the user sees it --
|
||||||
@@ -48,6 +56,23 @@ compiles."
|
|||||||
(define (in-fn body)
|
(define (in-fn body)
|
||||||
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
||||||
|
|
||||||
|
;;; A struct, a struct holding a pointer to one, and locals of every
|
||||||
|
;;; shape a member path can start from
|
||||||
|
(define (in-line body)
|
||||||
|
(string-append
|
||||||
|
"(struct point ((x int) (y int)))"
|
||||||
|
"(struct line ((a (* struct point)) (b (struct point))))"
|
||||||
|
"(fn f ((lp (* struct line))) void"
|
||||||
|
" (var p (struct point)) (var pp (* struct point) (& p))"
|
||||||
|
" (var l (struct line)) " body ")"))
|
||||||
|
|
||||||
|
(define (warnings source)
|
||||||
|
"What compiling SOURCE writes to stderr."
|
||||||
|
(with-error-output-to-string (lambda () (sex->c source))))
|
||||||
|
|
||||||
|
(define (warns? source fragment)
|
||||||
|
(and (string-contains (warnings source) fragment) #t))
|
||||||
|
|
||||||
(test-group "codegen"
|
(test-group "codegen"
|
||||||
|
|
||||||
;; c-switch handed its scrutinee straight to `cat', which only works
|
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||||
@@ -394,6 +419,16 @@ compiles."
|
|||||||
(fn f ((c (closure ((int)) int))) int (return (g c)))"
|
(fn f ((c (closure ((int)) int))) int (return (g c)))"
|
||||||
"g(c)")))
|
"g(c)")))
|
||||||
|
|
||||||
|
;; An include target is a path, so none of the identifier mangling
|
||||||
|
;; applies to it: GL/gl.h is a file and GLːgl.h is not.
|
||||||
|
(test-group "include targets are paths"
|
||||||
|
(test-assert "a slash survives"
|
||||||
|
(emits? "(include SDL3/SDL.h)" "#include <SDL3/SDL.h>"))
|
||||||
|
(test-assert "and so does a dash"
|
||||||
|
(emits? "(include my-header.h)" "#include <my-header.h>"))
|
||||||
|
(test-assert "pub include emits the same"
|
||||||
|
(emits? "(pub include GL/gl.h)" "#include <GL/gl.h>")))
|
||||||
|
|
||||||
;; The walker offered WALK-FN the cdr of every form as well as the
|
;; The walker offered WALK-FN the cdr of every form as well as the
|
||||||
;; form, so the tail (t) of (fn g () t) read as a call to a macro
|
;; form, so the tail (t) of (fn g () t) read as a call to a macro
|
||||||
;; named t, and any function returning a type that shared a name
|
;; named t, and any function returning a type that shared a name
|
||||||
@@ -623,4 +658,49 @@ 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[]")))
|
||||||
|
|
||||||
|
;; `.' is C's `.' or its `->' depending on what is being read from,
|
||||||
|
;; so a path reads the same however the thing it starts from is held
|
||||||
|
(test-group "member access"
|
||||||
|
(test-assert "a value reads with a dot"
|
||||||
|
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
||||||
|
(test-assert "a pointer reads with an arrow"
|
||||||
|
(emits? (in-line "(= (. pp x) 1)") "pp->x = 1"))
|
||||||
|
;; each step asks the type in hand, so one path may be both
|
||||||
|
(test-assert "a pointer field inside a value"
|
||||||
|
(emits? (in-line "(= (. l a x) 1)") "l.a->x = 1"))
|
||||||
|
(test-assert "a value field inside a pointer"
|
||||||
|
(emits? (in-line "(= (. lp b y) 1)") "lp->b.y = 1"))
|
||||||
|
(test-assert "and two pointers in a row"
|
||||||
|
(emits? (in-line "(= (. lp a x) 1)") "lp->a->x = 1"))
|
||||||
|
;; a path is left-associative and its right operand is always a
|
||||||
|
;; name, so none of it needs parenthesising
|
||||||
|
(test-assert "a path carries no parens"
|
||||||
|
(not (emits? (in-line "(= (. lp a x) 1)") "(lp->a)")))
|
||||||
|
;; ...which is not to say nothing does
|
||||||
|
(test-assert "though a dereference still does"
|
||||||
|
(emits? (in-line "(= (. (* pp) x) 1)") "(*pp).x = 1"))
|
||||||
|
(test-assert "a type we cannot work out is read as a value"
|
||||||
|
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
||||||
|
(test-assert "and said so"
|
||||||
|
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))
|
||||||
|
;; the arrow is `%->' and nothing else writes one, so a bare `->'
|
||||||
|
;; is an ordinary head -- a threading macro that nobody imported,
|
||||||
|
;; and a call like any other unknown
|
||||||
|
(test-assert "a bare -> is not the arrow"
|
||||||
|
(emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1")))
|
||||||
|
|
||||||
|
;; 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>"))))
|
||||||
|
|||||||
@@ -25,6 +25,16 @@
|
|||||||
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
|
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
|
||||||
(apply string-append body) ")"))
|
(apply string-append body) ")"))
|
||||||
|
|
||||||
|
(define (spec-src . body)
|
||||||
|
(string-append
|
||||||
|
"(record v ((x int)))"
|
||||||
|
"(spec (p T)"
|
||||||
|
" (fn s ((self (* const T))) void)"
|
||||||
|
" (fn both ((self (* const T))) void (.s self)))"
|
||||||
|
"(realize (p v)"
|
||||||
|
" (fn s ((self (* const v))) void (return)))"
|
||||||
|
(apply string-append body)))
|
||||||
|
|
||||||
(test-group "encapsulation"
|
(test-group "encapsulation"
|
||||||
|
|
||||||
;; A record is a struct plus a typedef of its own name, so the bare
|
;; A record is a struct plus a typedef of its own name, so the bare
|
||||||
@@ -42,7 +52,10 @@
|
|||||||
(test-assert "struct keeps C's meaning"
|
(test-assert "struct keeps C's meaning"
|
||||||
(not (emits? "(struct raw ((x int)))" "typedef")))
|
(not (emits? "(struct raw ((x int)))" "typedef")))
|
||||||
(test-assert "a record needs a name"
|
(test-assert "a record needs a name"
|
||||||
(reports? "(record ((x int)))" "record needs a name")))
|
(reports? "(record ((x int)))" "record needs a name"))
|
||||||
|
;; An opaque handle: the declaration is public, the fields are not
|
||||||
|
(test-assert "declared, then defined, is still defined"
|
||||||
|
(emits? "(pub record h)(record h ((x int)))" "struct h {")))
|
||||||
|
|
||||||
;; An ops block is mangled functions plus a registry entry. `pub'
|
;; An ops block is mangled functions plus a registry entry. `pub'
|
||||||
;; inside it means what it means outside.
|
;; inside it means what it means outside.
|
||||||
@@ -99,7 +112,15 @@
|
|||||||
(test-assert "and so is a receiver with no type to dispatch on"
|
(test-assert "and so is a receiver with no type to dispatch on"
|
||||||
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
|
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
|
||||||
(test-assert "an address cannot be taken of a call"
|
(test-assert "an address cannot be taken of a call"
|
||||||
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue")))
|
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
||||||
|
;; the inner call has to be typed to find the outer one's receiver
|
||||||
|
(test-assert "a method call can be another's receiver"
|
||||||
|
(emits? (string-append
|
||||||
|
"(record w ((x int)))"
|
||||||
|
"(ops w (fn twice ((a w)) w (return a))"
|
||||||
|
" (fn get ((a w)) int (return (. a x))))"
|
||||||
|
"(fn f () int (var a w #(w : 1)) (return (.get (.twice a))))")
|
||||||
|
"wːget(wːtwice(a))")))
|
||||||
|
|
||||||
;; A sum is the tag enum, a payload struct per variant that carries
|
;; A sum is the tag enum, a payload struct per variant that carries
|
||||||
;; fields, the record holding both, and a constructor each.
|
;; fields, the record holding both, and a constructor each.
|
||||||
@@ -127,6 +148,79 @@
|
|||||||
(test-assert "a variant is a name and a field list"
|
(test-assert "a variant is a name and a field list"
|
||||||
(reports? "(sum t (a))" "a variant is")))
|
(reports? "(sum t (a))" "a variant is")))
|
||||||
|
|
||||||
|
;; A spec is a contract that emits nothing; a realization is its
|
||||||
|
;; methods for one type. The refusals are what make it a contract.
|
||||||
|
(test-group "spec and realize"
|
||||||
|
(test-assert "a spec on its own emits nothing"
|
||||||
|
(not (emits? "(spec (p T) (fn s ((a (* const T))) void))" "p")))
|
||||||
|
(test-assert "a realization mangles type, spec and method"
|
||||||
|
(emits? (spec-src "") "void vːpːs"))
|
||||||
|
(test-assert "a default is copied per realizing type"
|
||||||
|
(emits? (spec-src "") "vːpːboth"))
|
||||||
|
(test-assert "and its parameter is substituted"
|
||||||
|
(emits? (spec-src "") "void vːpːboth (const v * self)"))
|
||||||
|
(test-assert "a default calls that type's own required method"
|
||||||
|
(emits? (spec-src "") "vːpːs(self)"))
|
||||||
|
(test-assert "a default is static even when the realization is pub"
|
||||||
|
(emits? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||||
|
" (fn d ((a (* const T))) void (.s a)))"
|
||||||
|
"(pub realize (p v)"
|
||||||
|
" (fn s ((a (* const v))) void (return)))")
|
||||||
|
"static void vːpːd"))
|
||||||
|
(test-assert "but a written method is not"
|
||||||
|
(not (emits? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||||
|
"(pub realize (p v)"
|
||||||
|
" (fn s ((a (* const v))) void (return)))")
|
||||||
|
"static void vːpːs")))
|
||||||
|
(test-assert "a spec method answers to .method"
|
||||||
|
(emits? (spec-src "(fn f () void (var a v #(v : 1)) (.s (& a)))") "vːpːs(&a)"))
|
||||||
|
|
||||||
|
(test-assert "a spec is declared once"
|
||||||
|
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
|
||||||
|
"spec is already declared"))
|
||||||
|
(test-assert "a type realizes a spec once"
|
||||||
|
(reports? (string-append (spec-src "") "(realize (p v)"
|
||||||
|
" (fn s ((a (* const v))) void (return)))")
|
||||||
|
"spec is already realized for"))
|
||||||
|
(test-assert "a realization names a spec that exists"
|
||||||
|
(reports? "(record v ((x int)))(realize (nope v))" "no such spec"))
|
||||||
|
(test-assert "every required method is provided"
|
||||||
|
(reports? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||||
|
" (fn q ((a (* const T))) void))"
|
||||||
|
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||||
|
"realization is missing a method of"))
|
||||||
|
(test-assert "and the missing one is named"
|
||||||
|
(reports? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||||
|
" (fn q ((a (* const T))) void))"
|
||||||
|
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||||
|
"q"))
|
||||||
|
(test-assert "a method the spec has not got is refused"
|
||||||
|
(reports? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||||
|
"(realize (p v) (fn s ((a (* const v))) void (return))"
|
||||||
|
" (fn z ((a (* const v))) void (return)))")
|
||||||
|
"spec has no such method"))
|
||||||
|
(test-assert "a return type has to match the spec"
|
||||||
|
(reports? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||||
|
"(realize (p v) (fn s ((a (* const v))) int (return 1)))")
|
||||||
|
"return type does not match"))
|
||||||
|
(test-assert "so do the arguments"
|
||||||
|
(reports? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||||
|
"(realize (p v) (fn s ((a v)) void (return)))")
|
||||||
|
"arguments do not match"))
|
||||||
|
(test-assert "a method name already taken on the type is refused"
|
||||||
|
(reports? (string-append "(record v ((x int)))"
|
||||||
|
"(ops v (fn s ((a (* const v))) void (return)))"
|
||||||
|
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||||
|
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||||
|
"method is already defined on")))
|
||||||
|
|
||||||
;; match is a switch over the tag whose arms bind the payload. What
|
;; match is a switch over the tag whose arms bind the payload. What
|
||||||
;; it refuses is why it exists.
|
;; it refuses is why it exists.
|
||||||
(test-group "match"
|
(test-group "match"
|
||||||
@@ -137,7 +231,7 @@
|
|||||||
(test-assert "an arm binds its payload"
|
(test-assert "an arm binds its payload"
|
||||||
(emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))"
|
(emits? (sum-src "(match s ((circle (r)) (= out r)) ((rect (w h)) (= out w))"
|
||||||
" ((empty ()) (= out 0.0)))")
|
" ((empty ()) (= out 0.0)))")
|
||||||
"float r = ((s.u).circle).r"))
|
"float r = s.u.circle.r"))
|
||||||
(test-assert "_ binds nothing"
|
(test-assert "_ binds nothing"
|
||||||
(not (emits? (sum-src "(match s ((circle (r)) (= out r))"
|
(not (emits? (sum-src "(match s ((circle (r)) (= out r))"
|
||||||
" ((rect (w _)) (= out w))"
|
" ((rect (w _)) (= out w))"
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -13,14 +13,15 @@
|
|||||||
# reads the imported struct's fields; and that a closure type crossing
|
# reads the imported struct's fields; and that a closure type crossing
|
||||||
# the boundary works both ways -- one built in the module and called
|
# the boundary works both ways -- one built in the module and called
|
||||||
# here, one built here and called there, through a code pointer that is
|
# here, one built here and called there, through a code pointer that is
|
||||||
# static in the other object.
|
# static in the other object. And that a record can cross as a handle
|
||||||
|
# alone, its fields left behind and its methods still found.
|
||||||
#
|
#
|
||||||
# The public forms carry comments in their headers, which the reduction
|
# The public forms carry comments in their headers, which the reduction
|
||||||
# to a prototype and to an extern both have to look past.
|
# to a prototype and to an extern both have to look past.
|
||||||
|
|
||||||
SEXC ?= ../../sexc
|
SEXC ?= ../../sexc
|
||||||
|
|
||||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\ncolour 123
|
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\ncolour 123
|
||||||
|
|
||||||
check:
|
check:
|
||||||
@$(SEXC) greet.sex -c -o greet.o
|
@$(SEXC) greet.sex -c -o greet.o
|
||||||
|
|||||||
@@ -38,6 +38,15 @@
|
|||||||
(var v vec2 #(vec2 : 3 4))
|
(var v vec2 #(vec2 : 3 4))
|
||||||
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
||||||
|
|
||||||
|
(.show-line v)
|
||||||
|
|
||||||
|
;; only the handle crossed: (. t n) here is an incomplete type
|
||||||
|
(var t (* tally) (tally/make))
|
||||||
|
(.bump t)
|
||||||
|
(.bump t)
|
||||||
|
(printf "tally %d\n" (.count t))
|
||||||
|
(.drop t)
|
||||||
|
|
||||||
(var c colour (colour/rgb 1 2 3))
|
(var c colour (colour/rgb 1 2 3))
|
||||||
(match c
|
(match c
|
||||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
;;; the compiled object still has to be handed to the C compiler. See
|
;;; the compiled object still has to be handed to the C compiler. See
|
||||||
;;; greet-app.sex.
|
;;; greet-app.sex.
|
||||||
|
|
||||||
(include stdio.h)
|
(pub include stdio.h)
|
||||||
|
(include stdlib.h)
|
||||||
|
|
||||||
(pub var greet-count ;; a comment in the header of a public form is
|
(pub var greet-count ;; a comment in the header of a public form is
|
||||||
;; not part of it: what the importer is given has
|
;; not part of it: what the importer is given has
|
||||||
@@ -58,13 +59,45 @@
|
|||||||
|
|
||||||
(ops vec2
|
(ops vec2
|
||||||
(pub fn manhattan ((self (* const vec2))) int
|
(pub fn manhattan ((self (* const vec2))) int
|
||||||
(return (+ (-> self x) (-> self y)))))
|
(return (+ (. self x) (. self y)))))
|
||||||
|
|
||||||
|
;;; An opaque record: the handle is public and the fields are not, so
|
||||||
|
;;; the importer holds a (* tally) and reaches it only through methods.
|
||||||
|
(pub record tally)
|
||||||
|
|
||||||
|
(record tally ((n int)))
|
||||||
|
|
||||||
|
(ops tally
|
||||||
|
(pub fn make () (* tally)
|
||||||
|
(var t (* tally) (malloc (sizeof tally)))
|
||||||
|
(= (. t n) 0)
|
||||||
|
(return t))
|
||||||
|
(pub fn bump ((self (* tally))) void
|
||||||
|
(++ (. self n)))
|
||||||
|
(pub fn count ((self (* const tally))) int
|
||||||
|
(return (. self n)))
|
||||||
|
(pub fn drop ((self (* tally))) void
|
||||||
|
(free self)))
|
||||||
|
|
||||||
;;; A pub sum is pasted whole, so the importer builds its own copy of
|
;;; A pub sum is pasted whole, so the importer builds its own copy of
|
||||||
;;; the declarations -- and of the constructors, which is why those are
|
;;; the declarations -- and of the constructors, which is why those are
|
||||||
;;; static: two units defining colourːrgb would not link.
|
;;; static: two units defining colourːrgb would not link.
|
||||||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||||||
|
|
||||||
|
;;; A spec and a realization over the boundary. The written method
|
||||||
|
;;; travels as a prototype and keeps external linkage; the default
|
||||||
|
;;; does not travel at all -- the importer synthesizes its own static
|
||||||
|
;;; copy from the spec, which is why a default is static even here.
|
||||||
|
(pub spec (printable T)
|
||||||
|
(fn show ((self (* const T))) void)
|
||||||
|
(fn show-line ((self (* const T))) void
|
||||||
|
(.show self)
|
||||||
|
(printf "\n")))
|
||||||
|
|
||||||
|
(pub realize (printable vec2)
|
||||||
|
(fn show ((self (* const vec2))) void
|
||||||
|
(printf "vec2(%d %d)" (. self x) (. self y))))
|
||||||
|
|
||||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||||
(fn unused-helper () void
|
(fn unused-helper () void
|
||||||
(printf "private\n"))
|
(printf "private\n"))
|
||||||
|
|||||||
39
tests/sex-programs/c11.sex
Normal file
39
tests/sex-programs/c11.sex
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
(compilation "-- -std=c11 -pedantic-errors")
|
||||||
|
(input)
|
||||||
|
(output "c11: 42 43 6")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; C11 is the floor, and this is the program that holds it there: the
|
||||||
|
;;; C every feature emits has to build under it with no extension, so
|
||||||
|
;;; one that reaches past it -- a GNU statement expression, a C23
|
||||||
|
;;; keyword -- fails here rather than on a user's compiler.
|
||||||
|
;;;
|
||||||
|
;;; A capturing closure is the one that leans on C11 itself, through
|
||||||
|
;;; max_align_t in the environment and `_Static_assert' on its size;
|
||||||
|
;;; the record, the method and the match are here for what they emit.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(record point ((x int) (y int)))
|
||||||
|
|
||||||
|
(ops point
|
||||||
|
(fn area ((self (* const point))) int
|
||||||
|
(return (* (. self x) (. self y)))))
|
||||||
|
|
||||||
|
(sum shape ((square ((side int))) (dot ())))
|
||||||
|
|
||||||
|
(fn sides ((s shape)) int
|
||||||
|
(var n int 0)
|
||||||
|
(match s
|
||||||
|
((square (side)) (= n (* 4 side)))
|
||||||
|
((dot ()) (= n 0)))
|
||||||
|
(return n))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var p point #(point : 6 7))
|
||||||
|
(var k int 1)
|
||||||
|
(var add-k (closure ((int)) int)
|
||||||
|
(closure ((v int)) int (k) (return (+ v k))))
|
||||||
|
(printf "c11: %d %d %d\n"
|
||||||
|
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2))
|
||||||
|
(return 0))
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
(compilation "-- -std=c99 -pedantic-errors")
|
|
||||||
(input)
|
|
||||||
(output "c99: 42")
|
|
||||||
(return 0)
|
|
||||||
|
|
||||||
;;; The closure environment is part of the ABI, so its union is declared
|
|
||||||
;;; in every translation unit whether or not one is used. That put
|
|
||||||
;;; whatever it was written with into every program: `max_align_t' named
|
|
||||||
;;; the alignment in one word, and made C11 the floor for a program with
|
|
||||||
;;; no closure in it at all.
|
|
||||||
;;;
|
|
||||||
;;; The widest built-ins say the same thing -- a union is aligned for the
|
|
||||||
;;; strictest of its members -- and say it in C99.
|
|
||||||
;;;
|
|
||||||
;;; Closures themselves still want C11 for the `_Static_assert' that
|
|
||||||
;;; checks the captures fit, so this program keeps clear of them.
|
|
||||||
|
|
||||||
(include stdio.h)
|
|
||||||
|
|
||||||
(struct point ((x int) (y int)))
|
|
||||||
|
|
||||||
(fn area ((p (struct point))) int
|
|
||||||
(return (* (. p x) (. p y))))
|
|
||||||
|
|
||||||
(pub fn main () int
|
|
||||||
(var p (struct point) #((struct point) : 6 7))
|
|
||||||
(printf "c99: %d\n" (area p))
|
|
||||||
(return 0))
|
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
;; a compound literal is an lvalue, so its address can be taken --
|
;; a compound literal is an lvalue, so its address can be taken --
|
||||||
;; until the end of the enclosing block, and no longer
|
;; until the end of the enclosing block, and no longer
|
||||||
(var pp (* struct point) (& #(struct point : 9 9)))
|
(var pp (* struct point) (& #(struct point : 9 9)))
|
||||||
(printf "through a pointer %d\n" (-> pp x))
|
(printf "through a pointer %d\n" (. pp x))
|
||||||
|
|
||||||
;; an array literal decays the way an array does
|
;; an array literal decays the way an array does
|
||||||
(var a (* int) #([int 3] : 10 20 30))
|
(var a (* int) #([int 3] : 10 20 30))
|
||||||
|
|||||||
@@ -29,14 +29,14 @@
|
|||||||
;;; the self-application hidden inside
|
;;; the self-application hidden inside
|
||||||
(fn fix ((step (* (struct rec)))) (closure ((int)) int)
|
(fn fix ((step (* (struct rec)))) (closure ((int)) int)
|
||||||
(return (closure ((n int)) int (step)
|
(return (closure ((n int)) int (step)
|
||||||
(return ((-> step f) step n)))))
|
(return ((. step f) step n)))))
|
||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
(var fac-knot (struct rec))
|
(var fac-knot (struct rec))
|
||||||
(= (. fac-knot f)
|
(= (. fac-knot f)
|
||||||
(closure ((self (* (struct rec))) (n int)) int ()
|
(closure ((self (* (struct rec))) (n int)) int ()
|
||||||
(if (<= n 1) (return 1))
|
(if (<= n 1) (return 1))
|
||||||
(return (* n ((-> self f) self (- n 1))))))
|
(return (* n ((. self f) self (- n 1))))))
|
||||||
|
|
||||||
;; the knot applied to itself directly, without fix
|
;; the knot applied to itself directly, without fix
|
||||||
(printf "direct: %d\n" ((. fac-knot f) (& fac-knot) 5))
|
(printf "direct: %d\n" ((. fac-knot f) (& fac-knot) 5))
|
||||||
@@ -45,8 +45,8 @@
|
|||||||
(= (. fib-knot f)
|
(= (. fib-knot f)
|
||||||
(closure ((self (* (struct rec))) (n int)) int ()
|
(closure ((self (* (struct rec))) (n int)) int ()
|
||||||
(if (< n 2) (return n))
|
(if (< n 2) (return n))
|
||||||
(return (+ ((-> self f) self (- n 1))
|
(return (+ ((. self f) self (- n 1))
|
||||||
((-> self f) self (- n 2))))))
|
((. self f) self (- n 2))))))
|
||||||
|
|
||||||
;; one combinator, two different recursions
|
;; one combinator, two different recursions
|
||||||
(var fac (closure ((int)) int) (fix (& fac-knot)))
|
(var fac (closure ((int)) int) (fix (& fac-knot)))
|
||||||
|
|||||||
@@ -43,8 +43,8 @@
|
|||||||
(var m (struct point))
|
(var m (struct point))
|
||||||
;; No `_' here: `m' has no initializer to infer from. Inference fills
|
;; No `_' here: `m' has no initializer to infer from. Inference fills
|
||||||
;; in a type, it does not invent one.
|
;; in a type, it does not invent one.
|
||||||
(= (. m x) (/ (+ (-> a x) (-> b x)) 2))
|
(= (. m x) (/ (+ (. a x) (. b x)) 2))
|
||||||
(= (. m y) (/ (+ (-> a y) (-> b y)) 2))
|
(= (. m y) (/ (+ (. a y) (. b y)) 2))
|
||||||
(return m))
|
(return m))
|
||||||
|
|
||||||
;;; The closure's type is written once, in the signature; `_' reads it
|
;;; The closure's type is written once, in the signature; `_' reads it
|
||||||
@@ -118,7 +118,7 @@
|
|||||||
(var p (* _) (& origin))
|
(var p (* _) (& origin))
|
||||||
|
|
||||||
;; Member access reads the same type database the macros do.
|
;; Member access reads the same type database the macros do.
|
||||||
(var x _ (-> p x))
|
(var x _ (. p x))
|
||||||
(printf "%d\n" x)
|
(printf "%d\n" x)
|
||||||
|
|
||||||
;; A call into a function Sex has actually parsed: the return type is
|
;; A call into a function Sex has actually parsed: the return type is
|
||||||
|
|||||||
@@ -9,25 +9,25 @@
|
|||||||
(fn-name (cat 'make-list- type)))
|
(fn-name (cat 'make-list- type)))
|
||||||
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
||||||
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
||||||
(= (-> list next) NULL)
|
(= (. list next) NULL)
|
||||||
(return list))))
|
(return list))))
|
||||||
|
|
||||||
(pub defmacro (add-value-list-T type is-public?)
|
(pub defmacro (add-value-list-T type is-public?)
|
||||||
(let ((list-type (list 'struct (cat 'list- type)))
|
(let ((list-type (list 'struct (cat 'list- type)))
|
||||||
(fn-name (cat 'add-value-list- type)))
|
(fn-name (cat 'add-value-list- type)))
|
||||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
||||||
(while (!= (-> list next) NULL)
|
(while (!= (. list next) NULL)
|
||||||
(= list (-> list next)))
|
(= list (. list next)))
|
||||||
(= (-> list next) (,(cat 'make-list- type)))
|
(= (. list next) (,(cat 'make-list- type)))
|
||||||
(= (-> list value) value))))
|
(= (. list value) value))))
|
||||||
|
|
||||||
(pub defmacro (length-list-T type is-public?)
|
(pub defmacro (length-list-T type is-public?)
|
||||||
(let ((fn-name (cat 'length-list- type))
|
(let ((fn-name (cat 'length-list- type))
|
||||||
(list-type (list 'struct (cat 'list- type))))
|
(list-type (list 'struct (cat 'list- type))))
|
||||||
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
||||||
(var n size-t 0)
|
(var n size-t 0)
|
||||||
(while (!= (-> list next) NULL)
|
(while (!= (. list next) NULL)
|
||||||
(= list (-> list next))
|
(= list (. list next))
|
||||||
(++ n))
|
(++ n))
|
||||||
(return n))))
|
(return n))))
|
||||||
|
|
||||||
@@ -35,14 +35,14 @@
|
|||||||
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
||||||
((list ,(list '* 'struct (cat 'list- type))))
|
((list ,(list '* 'struct (cat 'list- type))))
|
||||||
bool
|
bool
|
||||||
(return (== (-> list next) NULL))))
|
(return (== (. list next) NULL))))
|
||||||
|
|
||||||
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
||||||
(let ((list-var-2 (cat list-var '-2)))
|
(let ((list-var-2 (cat list-var '-2)))
|
||||||
`(do
|
`(do
|
||||||
(var ,list-var-2 (* ,list-type) ,list-var)
|
(var ,list-var-2 (* ,list-type) ,list-var)
|
||||||
(var ,elt-var ,elt-type (-> ,list-var-2 value))
|
(var ,elt-var ,elt-type (. ,list-var-2 value))
|
||||||
(while (!= (-> ,list-var-2 next) NULL)
|
(while (!= (. ,list-var-2 next) NULL)
|
||||||
,what-do
|
,what-do
|
||||||
(= ,list-var-2 (-> ,list-var-2 next))
|
(= ,list-var-2 (. ,list-var-2 next))
|
||||||
(= ,elt-var (-> ,list-var-2 value))))))
|
(= ,elt-var (. ,list-var-2 value))))))
|
||||||
|
|||||||
@@ -23,12 +23,12 @@
|
|||||||
(ops vec2
|
(ops vec2
|
||||||
(pub fn length ((self (* const vec2))) float
|
(pub fn length ((self (* const vec2))) float
|
||||||
"Pointer self, so (.length v) has to take the address."
|
"Pointer self, so (.length v) has to take the address."
|
||||||
(return (sqrtf (+ (* (-> self x) (-> self x))
|
(return (sqrtf (+ (* (. self x) (. self x))
|
||||||
(* (-> self y) (-> self y))))))
|
(* (. self y) (. self y))))))
|
||||||
|
|
||||||
(fn scale ((self (* vec2)) (k float)) void
|
(fn scale ((self (* vec2)) (k float)) void
|
||||||
(*= (-> self x) k)
|
(*= (. self x) k)
|
||||||
(*= (-> self y) k))
|
(*= (. self y) k))
|
||||||
|
|
||||||
(fn sum-of ((self vec2)) float
|
(fn sum-of ((self vec2)) float
|
||||||
;; value self, so (.sum-of p) has to dereference
|
;; value self, so (.sum-of p) has to dereference
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
|
|
||||||
(ops raw
|
(ops raw
|
||||||
(fn total ((self (* const (struct raw)))) int
|
(fn total ((self (* const (struct raw)))) int
|
||||||
(return (+ (-> self a) (-> self b)))))
|
(return (+ (. self a) (. self b)))))
|
||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
(var v vec2 #(vec2 : 3.0 4.0))
|
(var v vec2 #(vec2 : 3.0 4.0))
|
||||||
|
|||||||
@@ -18,8 +18,8 @@
|
|||||||
(struct raw ((a int) (b int)))
|
(struct raw ((a int) (b int)))
|
||||||
|
|
||||||
(fn shift ((p (* point)) (dx int) (dy int)) void
|
(fn shift ((p (* point)) (dx int) (dy int)) void
|
||||||
(+= (-> p x) dx)
|
(+= (. p x) dx)
|
||||||
(+= (-> p y) dy))
|
(+= (. p y) dy))
|
||||||
|
|
||||||
(fn area ((p point)) int
|
(fn area ((p point)) int
|
||||||
(return (* (. p x) (. p y))))
|
(return (* (. p x) (. p y))))
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
((* const char) "%s")
|
((* const char) "%s")
|
||||||
(else (error "print-struct: unsupported field type"
|
(else (error "print-struct: unsupported field type"
|
||||||
name type))))
|
name type))))
|
||||||
(-> v ,name))))))
|
(. v ,name))))))
|
||||||
(if (not printers)
|
(if (not printers)
|
||||||
(error "print-struct: no such struct" type-name)
|
(error "print-struct: no such struct" type-name)
|
||||||
`(fn ,(cat 'print- type-name) ((v (* const struct ,type-name))) void
|
`(fn ,(cat 'print- type-name) ((v (* const struct ,type-name))) void
|
||||||
|
|||||||
68
tests/sex-programs/specs.sex
Normal file
68
tests/sex-programs/specs.sex
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
(input)
|
||||||
|
(output "$12.34"
|
||||||
|
"(3, 4)"
|
||||||
|
"max 1234"
|
||||||
|
"realizes ord: money 1, point 0")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; A spec is a contract and emits nothing; a realization is the
|
||||||
|
;;; spec's methods for one type, mangled typeːspecːmethod. A default
|
||||||
|
;;; is copied into every realizing type with the parameter
|
||||||
|
;;; substituted, so the `show' it calls is that type's own.
|
||||||
|
;;;
|
||||||
|
;;; What the layer is for is the last line: a generic asks `realizes?'
|
||||||
|
;;; before it expands, and says so itself rather than leaving a wall
|
||||||
|
;;; of C errors two stages downstream.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(record money ((cents int)))
|
||||||
|
(record point ((x int) (y int)))
|
||||||
|
|
||||||
|
(spec (printable T)
|
||||||
|
(fn show ((self (* const T))) void)
|
||||||
|
(fn show-line ((self (* const T))) void ; default, calls the required one
|
||||||
|
(.show self)
|
||||||
|
(printf "\n")))
|
||||||
|
|
||||||
|
(spec (ord T)
|
||||||
|
(fn less ((a (* const T)) (b (* const T))) bool))
|
||||||
|
|
||||||
|
(realize (printable money)
|
||||||
|
(fn show ((self (* const money))) void
|
||||||
|
(printf "$%d.%02d" (/ (. self cents) 100) (% (. self cents) 100))))
|
||||||
|
|
||||||
|
(realize (printable point)
|
||||||
|
(fn show ((self (* const point))) void
|
||||||
|
(printf "(%d, %d)" (. self x) (. self y))))
|
||||||
|
|
||||||
|
(realize (ord money)
|
||||||
|
(fn less ((a (* const money)) (b (* const money))) bool
|
||||||
|
(return (< (. a cents) (. b cents)))))
|
||||||
|
|
||||||
|
;;; A bounded generic: the macro refuses before it expands
|
||||||
|
(defmacro (max-of type)
|
||||||
|
(unless (realizes? 'ord type)
|
||||||
|
(error "does not realize ord" type))
|
||||||
|
`(fn ,(cat 'max- type) ((a (* const ,type)) (b (* const ,type))) ,type
|
||||||
|
(if (,(cat type '/ord/less) a b)
|
||||||
|
(return (* b))
|
||||||
|
(return (* a)))))
|
||||||
|
|
||||||
|
(max-of money)
|
||||||
|
|
||||||
|
(defmacro (ord? type) (if (realizes? 'ord type) 1 0))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var m money #(money : 1234))
|
||||||
|
(var p point #(point : 3 4))
|
||||||
|
|
||||||
|
(.show-line m) ; the default, through the registry
|
||||||
|
(point/printable/show-line (& p)) ; and spelled out
|
||||||
|
|
||||||
|
(var cheap money #(money : 150))
|
||||||
|
(var big money (max-money (& m) (& cheap)))
|
||||||
|
(printf "max %d\n" (. big cents))
|
||||||
|
|
||||||
|
(printf "realizes ord: money %d, point %d\n" (ord? money) (ord? point))
|
||||||
|
(return 0))
|
||||||
56
tests/sex-programs/threading.sex
Normal file
56
tests/sex-programs/threading.sex
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
(input)
|
||||||
|
(output "arithmetic: 26"
|
||||||
|
"a bare step needs no parens: 20"
|
||||||
|
"through a path: 1 4"
|
||||||
|
"mixed with a method: 25"
|
||||||
|
"one step is just the value: 7")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; `->' used to be C's arrow. The arrow is what `.' writes now when
|
||||||
|
;;; the thing it reads from is a pointer, which left the name free for
|
||||||
|
;;; threading -- the first module of the standard library.
|
||||||
|
;;;
|
||||||
|
;;; What this pins is that a step may be a call, a bare name or a
|
||||||
|
;;; method, that the value is threaded into the *first* argument, and
|
||||||
|
;;; that the compiler's own generated arrows survived the rename --
|
||||||
|
;;; `ops' below lifts a method, which is where the captures live.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
(import threading-macros)
|
||||||
|
|
||||||
|
(struct point ((x int) (y int)))
|
||||||
|
(struct line ((a (* struct point)) (b (struct point))))
|
||||||
|
|
||||||
|
(fn double-it ((n int)) int
|
||||||
|
(return (* n 2)))
|
||||||
|
|
||||||
|
(fn add ((n int) (m int)) int
|
||||||
|
(return (+ n m)))
|
||||||
|
|
||||||
|
(ops point
|
||||||
|
(pub fn length-sq ((self (* const struct point))) int
|
||||||
|
(return (+ (* (. self x) (. self x))
|
||||||
|
(* (. self y) (. self y))))))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
;; (add (double-it 5) 3) doubled: inside out, written outside in
|
||||||
|
(printf "arithmetic: %d\n"
|
||||||
|
(-> 5
|
||||||
|
double-it
|
||||||
|
(add 3)
|
||||||
|
double-it))
|
||||||
|
(printf "a bare step needs no parens: %d\n" (-> 5 double-it double-it))
|
||||||
|
|
||||||
|
(var p (struct point) #(1 2))
|
||||||
|
(var l (struct line) #(struct line : .a (& p) .b #(3 4)))
|
||||||
|
(var lp (* struct line) (& l))
|
||||||
|
;; a member path is an ordinary step, and reads through both pointers
|
||||||
|
(printf "through a path: %d %d\n"
|
||||||
|
(-> lp (. a x))
|
||||||
|
(. lp b y))
|
||||||
|
|
||||||
|
(var q (struct point) #(3 4))
|
||||||
|
(printf "mixed with a method: %d\n" (-> q .length-sq))
|
||||||
|
|
||||||
|
(printf "one step is just the value: %d\n" (-> 7))
|
||||||
|
(return 0))
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
(printf "members: %d %g\n" px py)
|
(printf "members: %d %g\n" px py)
|
||||||
|
|
||||||
(var pp _ (& p))
|
(var pp _ (& p))
|
||||||
(printf "pointers: %d %g\n" (-> pp x) (-> pp y))
|
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
||||||
|
|
||||||
(var table (¤ int 3))
|
(var table (¤ int 3))
|
||||||
(= (¤ table 0) 10)
|
(= (¤ table 0) 10)
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
|
|
||||||
;; a wildcard inside a written type: only it is solved
|
;; a wildcard inside a written type: only it is solved
|
||||||
(var pp2 (* _) (& p))
|
(var pp2 (* _) (& p))
|
||||||
(printf "partial: %d %g\n" (-> pp2 x) (-> pp2 y))
|
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
||||||
|
|
||||||
;; C's usual arithmetic conversions, far enough to answer `_'
|
;; C's usual arithmetic conversions, far enough to answer `_'
|
||||||
(var d double 1.5)
|
(var d double 1.5)
|
||||||
|
|||||||
45
tests/sex-programs/worked-example.sex
Normal file
45
tests/sex-programs/worked-example.sex
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
(input)
|
||||||
|
(output "-(2 + 3)"
|
||||||
|
"-5")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; The worked example from Encapsulation.org, kept here so it stays
|
||||||
|
;;; true: a recursive sum, an inherent method on it, and a spec
|
||||||
|
;;; realized for it, which is every layer that is built at once.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(sum expr
|
||||||
|
((lit ((v float)))
|
||||||
|
(neg ((e (* const expr))))
|
||||||
|
(add ((l (* const expr)) (r (* const expr))))))
|
||||||
|
|
||||||
|
(ops expr
|
||||||
|
(pub fn eval ((self (* const expr))) float
|
||||||
|
(match (* self)
|
||||||
|
((lit (v)) (return v))
|
||||||
|
((neg (e)) (return (- (.eval e))))
|
||||||
|
((add (l r)) (return (+ (.eval l) (.eval r)))))))
|
||||||
|
|
||||||
|
(spec (printable T)
|
||||||
|
(fn show ((self (* const T))) void)
|
||||||
|
(fn show-line ((self (* const T))) void ; default, calls the required one
|
||||||
|
(.show self)
|
||||||
|
(printf "\n")))
|
||||||
|
|
||||||
|
(realize (printable expr)
|
||||||
|
(fn show ((self (* const expr))) void
|
||||||
|
(match (* self)
|
||||||
|
((lit (v)) (printf "%g" v))
|
||||||
|
((neg (e)) (printf "-") (.show e))
|
||||||
|
((add (l r)) (printf "(") (.show l) (printf " + ") (.show r) (printf ")")))))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var two expr (expr/lit 2.0))
|
||||||
|
(var three expr (expr/lit 3.0))
|
||||||
|
(var both expr (expr/add (& two) (& three)))
|
||||||
|
(var e expr (expr/neg (& both)))
|
||||||
|
|
||||||
|
(.show-line e) ; -(2 + 3)
|
||||||
|
(printf "%g\n" (.eval e)) ; -5
|
||||||
|
(return 0))
|
||||||
@@ -13,6 +13,13 @@
|
|||||||
get-sum
|
get-sum
|
||||||
variant-fields
|
variant-fields
|
||||||
reset-declarations!
|
reset-declarations!
|
||||||
|
|
||||||
|
add-spec!
|
||||||
|
get-spec
|
||||||
|
spec-parameter
|
||||||
|
spec-methods
|
||||||
|
add-realization!
|
||||||
|
realizes?
|
||||||
normalize-fields
|
normalize-fields
|
||||||
|
|
||||||
add-method!
|
add-method!
|
||||||
|
|||||||
@@ -13,6 +13,13 @@
|
|||||||
get-sum
|
get-sum
|
||||||
variant-fields
|
variant-fields
|
||||||
reset-declarations!
|
reset-declarations!
|
||||||
|
|
||||||
|
add-spec!
|
||||||
|
get-spec
|
||||||
|
spec-parameter
|
||||||
|
spec-methods
|
||||||
|
add-realization!
|
||||||
|
realizes?
|
||||||
normalize-fields
|
normalize-fields
|
||||||
|
|
||||||
add-method!
|
add-method!
|
||||||
|
|||||||
31
types.scm
31
types.scm
@@ -109,12 +109,41 @@
|
|||||||
(let ((entry (assq name variants)))
|
(let ((entry (assq name variants)))
|
||||||
(and entry (cdr entry))))
|
(and entry (cdr entry))))
|
||||||
|
|
||||||
|
;;; A spec is a parameter and a list of methods, each of which either
|
||||||
|
;;; declares a signature or also carries a default body:
|
||||||
|
;;;
|
||||||
|
;;; (spec (printable T) (fn show ((self (* const T))) void))
|
||||||
|
;;; -> printable -> (T (fn show ((self (* const T))) void))
|
||||||
|
;;;
|
||||||
|
;;; And a realization is only ever asked whether it exists -- the
|
||||||
|
;;; methods it provides land in +ops-db+ like any others.
|
||||||
|
(define +spec-db+ (make-hash-table))
|
||||||
|
(define +realize-db+ (make-hash-table))
|
||||||
|
|
||||||
|
(define (add-spec! name parameter methods)
|
||||||
|
(hash-table-set! +spec-db+ name (cons parameter methods)))
|
||||||
|
|
||||||
|
(define (get-spec name)
|
||||||
|
(hash-table-ref/default +spec-db+ name #f))
|
||||||
|
|
||||||
|
(define (spec-parameter spec) (car spec))
|
||||||
|
(define (spec-methods spec) (cdr spec))
|
||||||
|
|
||||||
|
(define (add-realization! spec type)
|
||||||
|
(hash-table-set! +realize-db+ (cons spec type) #t))
|
||||||
|
|
||||||
|
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
|
||||||
|
(define (realizes? spec type)
|
||||||
|
(hash-table-exists? +realize-db+ (cons spec type)))
|
||||||
|
|
||||||
;;; Both of these refuse a second declaration of the same name, which
|
;;; Both of these refuse a second declaration of the same name, which
|
||||||
;;; is per translation unit rather than per process -- so a process
|
;;; is per translation unit rather than per process -- so a process
|
||||||
;;; that compiles two units has to start the second one clean.
|
;;; that compiles two units has to start the second one clean.
|
||||||
(define (reset-declarations!)
|
(define (reset-declarations!)
|
||||||
(set! +sum-db+ (make-hash-table))
|
(set! +sum-db+ (make-hash-table))
|
||||||
(set! +ops-db+ (make-hash-table)))
|
(set! +ops-db+ (make-hash-table))
|
||||||
|
(set! +spec-db+ (make-hash-table))
|
||||||
|
(set! +realize-db+ (make-hash-table)))
|
||||||
|
|
||||||
;;; Methods of an `ops' block: (type . method) -> the qualified name
|
;;; Methods of an `ops' block: (type . method) -> the qualified name
|
||||||
;;; the call is rewritten to.
|
;;; the call is rewritten to.
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
19
utils.scm
19
utils.scm
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user