`.' now writes whichever of C's member operators the type calls for, so nothing needs the arrow spelled out and the name is free for the macro Clojure spells the same way: (-> 5 double-it (add 3)) reads in the order it happens rather than inside out. It has no business in the prelude, which every unit pays for whether or not it threads anything, so it opens the standard library instead and is imported like anything else. The compiler finds lib/ by asking the binary where it is -- beside sexc in a work tree, under ../share/sex once installed -- and looks there last, so a module of one's own by that name still wins. The C arrow becomes `%->', and that rename is what makes the rest possible. The closure lifter writes arrows of its own for the capture record, those forms go back through walk-statement, and a macro named `->' threads them: (-> ƛcaptures n) turns into a call to n, with nothing saying so until the C compiler. fmt-c rendered a bare `->' as the arrow, which left the one spelling that cannot be right -- threading with no import -- looking like the operator it stopped being. It goes through c-apply now, the way any other head nothing knows does.
338 lines
11 KiB
Org Mode
338 lines
11 KiB
Org Mode
* The Sex language
|
|
|
|
#+NAME: the Sex logo
|
|
#+ATTR_HTML: :width 300px
|
|
[[sex.png][file:./sex.png]]
|
|
|
|
Sex is a S-expressions language. Sex is written in Chicken, which is an
|
|
[[https://call-cc.org][R7RS Scheme]].
|
|
Sex is statically typed, compiled general purpose language.
|
|
|
|
* Compilation
|
|
First, get yourself a Chicken, then, some Chicken deps. You also will
|
|
need a C compiler.
|
|
|
|
** Development
|
|
#+begin_src sh
|
|
make deps
|
|
make
|
|
#+end_src
|
|
|
|
~make deps~ installs pinned eggs from ~eggs.lock~ into a project-local
|
|
~.eggs/~ repository. ~dependencies.txt~ is the unpinned request list.
|
|
To refresh ~eggs.lock~ after changing it:
|
|
|
|
#+begin_src sh
|
|
make deps-update
|
|
#+end_src
|
|
|
|
** Packaging for system package managers
|
|
- Depend ~sex~ package on Chicken-6 (with ~libchicken.a~) and all eggs from ~dependencies.txt~.
|
|
- Compile and install with ~make~ (usually no other arguments required).
|
|
- Move resulting ~sexc~ binary to the appropriate place.
|
|
|
|
** Static compilation
|
|
The default ~CSC_FLAGS~ include ~-static~, so ~sexc~ does not need
|
|
~.eggs~ at runtime and ~make install~ stays relocatable. Chicken must
|
|
provide ~libchicken.a~ (E.g. for gentoo: ~dev-scheme/chicken~ with
|
|
~static-libs~ use flag).
|
|
|
|
To link dynamically instead (the binary will look for eggs under this
|
|
tree's ~.eggs~ path):
|
|
|
|
#+begin_src sh
|
|
make CSC_FLAGS='-K prefix'
|
|
#+end_src
|
|
|
|
** Installation
|
|
GNU directory variables: ~prefix~, ~exec_prefix~, ~bindir~, ~datadir~,
|
|
~DESTDIR~. The binary goes to ~bindir~ and the standard library to
|
|
~$(datadir)/sex~.
|
|
|
|
#+begin_src sh
|
|
# default installation (/usr/local/bin/)
|
|
make install
|
|
# customize the prefix (installs to ~/.local/bin)
|
|
make prefix=$(HOME)/.local install
|
|
# staged install for packaging
|
|
make DESTDIR=/tmp/stage prefix=/usr install
|
|
#+end_src
|
|
|
|
* Usage
|
|
** Summary
|
|
#+begin_src
|
|
Usage: sexc [options] filename [-- options-for-c-compiler]
|
|
Options:
|
|
--c-compiler=ARG Select C compiler. Defaults to value of SEX_CC
|
|
environment variable, or if it is empty, to cc
|
|
-c, --compile-object Compile object file instead of executable program
|
|
-f, --features=ARG Comma-separated feature names, added to the host's own
|
|
for #+ and #- feature expressions. May be given
|
|
more than once
|
|
--no-platform-features Leave out the host's own features. With --features,
|
|
this reads a file the way another platform would
|
|
-C, --emit-c Emit C code
|
|
--public-interface Get module's public interface
|
|
-h, --help Show this help
|
|
-m, --macro-expand Emit macro-expanded semantically processed Sex code
|
|
-o, --output=ARG Write output to file. Default file name is a.out.
|
|
If -E or -m options are provided, defaults to stdout
|
|
--line-directives=ARG How much #line information to emit: statement (default),
|
|
toplevel, or none. `statement' is what makes a debugger
|
|
land on the right source line; `none' is for reading -C
|
|
output by eye
|
|
#+end_src
|
|
** Compiling Hello World
|
|
#+begin_src shell
|
|
sexc ./examples/hello-world.sex -o hello
|
|
#+end_src
|
|
|
|
That's it. Now you should have executable named ~hello~ in your
|
|
directory. Sex uses C under the hood, the default C compiler is ~cc~,
|
|
but you can pass any using ~--c-compiler~ option, or by setting
|
|
~SEX_CC~ environment variable.
|
|
|
|
Everything after ~--~ is handed to the C compiler exactly as written:
|
|
|
|
#+begin_src shell
|
|
sexc example/sdl3-triangle.sex -o triangle -- `pkg-config --cflags --libs sdl3` -framework OpenGL
|
|
#+end_src
|
|
|
|
** Example
|
|
An example of Sex source:
|
|
#+begin_src scheme
|
|
(include stdio.h)
|
|
|
|
(pub fn main ((argc int) (argv [* const char])) int
|
|
(puts "Hello from Sex!")
|
|
(var name [char 512])
|
|
(puts "What is your name?")
|
|
(scanf "%s" (cast (& name) (* char)))
|
|
(printf "Hello, %s!\n" name)
|
|
(return 0))
|
|
#+end_src
|
|
|
|
Compile and run:
|
|
#+begin_src shell
|
|
~/dev/sex $ ./sexc ./example/hello-world.sex -o hello-world
|
|
~/dev/sex $ ./hello-world
|
|
Hello from Sex!
|
|
What is your name?
|
|
Alex
|
|
Hello, Alex!
|
|
#+end_src
|
|
|
|
* Features
|
|
** Full C interoperability
|
|
Just ~(include "Your/Favourite/Library.h")~ and use it as you would
|
|
have is C. One ~include~ takes as many headers as you care to name:
|
|
~(include stdio.h stdlib.h string.h)~.
|
|
|
|
*** Auto kebabification
|
|
For hardcore fans of traditional Lisp naming convention,
|
|
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
|
|
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
|
|
Each source file is a module. Module can provide public interface and
|
|
be imported by using ~(import path/to/module)~ expression. Module
|
|
search path has three parts: first is relative to the source being
|
|
compiled location, then the ~SEX_MODULE_PATH~ environment variable,
|
|
and last the standard library.
|
|
|
|
Module's public interface consists of everything declared
|
|
~pub~. Structures, function, macros, types, variables can be
|
|
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
|
|
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that
|
|
follows is kept only when the feature expression is true, and otherwise
|
|
is read and thrown away.
|
|
|
|
#+begin_src scheme
|
|
#+macosx (include OpenGL/gl3.h)
|
|
#-macosx (include GL/gl.h)
|
|
|
|
#+(and unix (not macosx)) (define HAVE-EPOLL 1)
|
|
#+end_src
|
|
|
|
An expression is a feature name, or ~and~, ~or~ and ~not~ of them.
|
|
|
|
This is read time, not compile time. What does not apply never reaches macro
|
|
expansion, the type database or the generated C.
|
|
|
|
The features are the host's ~(software-version)~, ~(software-type)~
|
|
and ~(machine-type)~, e.g. ~macosx unix arm64~ or ~linux unix
|
|
x86-64~. ~--features~ adds to them:
|
|
|
|
#+begin_src shell
|
|
sexc prog.sex -f debug,with-sdl
|
|
sexc prog.sex --features=debug --features=with-sdl
|
|
#+end_src
|
|
|
|
A feature is never taken away. The host's features can be disabled,
|
|
e.g. for checking output for other platform:
|
|
|
|
#+begin_src shell
|
|
sexc example/sdl3-triangle.sex -C --no-platform-features --features=linux,unix,x86-64
|
|
#+end_src
|
|
|
|
** Aggregate initializers and compound literals
|
|
~#(...)~ is a brace initializer. On its own it has no type and takes one
|
|
from where it is written:
|
|
|
|
#+begin_src scheme
|
|
(var p (struct point) #(1 2))
|
|
#+end_src
|
|
|
|
A ~:~ inside one ends a type and makes the whole thing a compound
|
|
literal --- an unnamed object of that type, usable anywhere an
|
|
expression is:
|
|
|
|
#+begin_src scheme
|
|
(var q (struct point) #(struct point : 3 4))
|
|
(var a (* int) #([int 3] : 10 20 30))
|
|
(draw-line ui #(struct point : 0 0) end)
|
|
(var p (* struct point) (& #(struct point : 9 9))) ; an lvalue, so `&' works
|
|
#+end_src
|
|
|
|
The type is written as bare words, the way it is everywhere else in the
|
|
language; ~:~ is what ends it.
|
|
|
|
A leading ~.~ names a field, so initializers may be designated, given in
|
|
any order, and mixed with positional ones:
|
|
|
|
#+begin_src scheme
|
|
(var r (struct named) #(struct named : .n 7 .first-name "zoe"))
|
|
#+end_src
|
|
|
|
A compound literal written inside a block lives until the end of that
|
|
block and no longer, so returning its address is a dangling pointer.
|
|
|
|
** Syntactic macros
|
|
Sex has support for syntactic macros. Macro definitions look like
|
|
functions: they have a name, an argument list and a body. Macro should
|
|
return Sex code.
|
|
|
|
A macro returns *one* form. To return several --- a function beside the
|
|
struct it works on, say --- return them under =$=, which splices them in
|
|
where the macro was written:
|
|
|
|
#+begin_src scheme
|
|
(defmacro (pair-of-fns a b)
|
|
`($ (fn ,a () int (return 1))
|
|
(fn ,b () int (return 2))))
|
|
#+end_src
|
|
|
|
=($)= expands to nothing. Everything else is a single form, including
|
|
one whose head is itself a form: =`((make-adder 10) 5)= calls what
|
|
=make-adder= returned, and is not two forms.
|
|
|
|
*** Examples:
|
|
**** Structure with templated value type
|
|
#+begin_src scheme
|
|
(pub defmacro (list-T type)
|
|
(let ((list-type (cat 'list- type)))
|
|
`(struct ,list-type
|
|
((value ,type)
|
|
(next (* ,list-type))))))
|
|
|
|
(list-T int)
|
|
#+end_src
|
|
->
|
|
#+begin_src scheme
|
|
(struct list_int
|
|
((value int)
|
|
(next (* list_int))))
|
|
#+end_src
|
|
|
|
**** Wrapper for checking return codes
|
|
#+begin_src scheme
|
|
(pub defmacro (check-sdl-return call message ret-code)
|
|
`(if (< 0 ,call)
|
|
(do
|
|
(puts ,message)
|
|
(return ,ret-code))))
|
|
|
|
(pub fn init () int
|
|
(check-sdl-return
|
|
(SDL-Init SDL-INIT-VIDEO) "Failed to initialize SDL" 1)
|
|
...)
|
|
#+end_src
|
|
->
|
|
#+begin_src scheme
|
|
(pub fn init () int
|
|
(if (< 0 (SDL_Init SDL_INIT_VIDEO))
|
|
(do (puts "Failed to initialize SDL") (return 1)))
|
|
...)
|
|
#+end_src
|
|
|
|
** Compile-time type information
|
|
Sex has a number of type reflection features, aiming to help with
|
|
macro writing. During the compilation, all type info is collected, and
|
|
is accessible during macro expansion. This allows us to write things
|
|
like providing auto serialization, adding meta information, and so on.
|
|
|
|
** Use an established environment for development
|
|
As Sex is S-expressions, you always have Emacs with paredit as your
|
|
best option.
|
|
|
|
*** sex-mode.el
|
|
To harness the power of sex-mode, add the following lines to your
|
|
~$HOME/.config/emacs/init.el~:
|
|
#+begin_src emacs-lisp
|
|
(use-package sex-mode
|
|
:load-path "/path/to/sex"
|
|
:mode ("\\.sex\\'"))
|
|
#+end_src
|