forked from alex-eg/sex
Compare commits
22 Commits
refactor-t
...
sdl-exampl
| Author | SHA1 | Date | |
|---|---|---|---|
| c709c69176 | |||
| 1e435f7fdf | |||
| c976378363 | |||
| 5af2e99f5f | |||
| bb1614a123 | |||
| c1fbb02c50 | |||
| 5f9f90ef37 | |||
| 7e6e32488b | |||
| fa4ad5acee | |||
| 36932fb480 | |||
| d609a0c591 | |||
| af6e778185 | |||
| 0db4a11a2b | |||
| d83ee32f12 | |||
| 528f26b7ef | |||
| 9903cae3f7 | |||
| f6ec07a4b5 | |||
| 543a333165 | |||
| 1ca77aaf59 | |||
| 8ae2346e41 | |||
| 878d415e22 | |||
| c678df7978 |
8
.github/workflows/build.yaml
vendored
8
.github/workflows/build.yaml
vendored
@@ -15,11 +15,11 @@ jobs:
|
|||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- name: Install chicken
|
- name: Install chicken
|
||||||
run: |
|
run: |
|
||||||
wget -N https://code.call-cc.org/releases/5.4.0/chicken-5.4.0.tar.gz
|
wget -N https://code.call-cc.org/releases/6.0.0/chicken-6.0.0.tar.gz
|
||||||
tar zxf chicken-5.4.0.tar.gz
|
tar zxf chicken-6.0.0.tar.gz
|
||||||
sudo apt install -y make
|
sudo apt install -y make
|
||||||
make -C chicken-5.4.0 PLATFORM=linux
|
make -C chicken-6.0.0 PLATFORM=linux
|
||||||
sudo make -C chicken-5.4.0 PLATFORM=linux install
|
sudo make -C chicken-6.0.0 PLATFORM=linux install
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
# FIXME: [project-local deps]: use venv or something
|
# FIXME: [project-local deps]: use venv or something
|
||||||
# run: make deps
|
# run: make deps
|
||||||
|
|||||||
38
Makefile
38
Makefile
@@ -15,7 +15,7 @@ CSC_FLAGS += -K prefix -static
|
|||||||
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
||||||
|
|
||||||
# Order matters, since module check correctness on compilation
|
# Order matters, since module check correctness on compilation
|
||||||
MODULES = utils sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
|
MODULES = utils types sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
|
||||||
OBJ = $(MODULES:%=%.o)
|
OBJ = $(MODULES:%=%.o)
|
||||||
|
|
||||||
sexc: $(OBJ) main.scm
|
sexc: $(OBJ) main.scm
|
||||||
@@ -28,17 +28,20 @@ sexc: $(OBJ) main.scm
|
|||||||
utils.o: utils.module.scm utils.scm
|
utils.o: utils.module.scm utils.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
|
||||||
|
|
||||||
|
types.o: types.module.scm types.scm
|
||||||
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types
|
||||||
|
|
||||||
sex-macros.o: sex-macros.module.scm sex-macros.scm
|
sex-macros.o: sex-macros.module.scm sex-macros.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
|
||||||
|
|
||||||
reader.o: reader.module.scm reader.scm
|
reader.o: reader.module.scm reader.scm utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
|
||||||
|
|
||||||
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o utils.o
|
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,utils
|
||||||
|
|
||||||
semen.o: semen.module.scm semen.scm sex-macros.o sex-modules.o utils.o
|
semen.o: semen.module.scm semen.scm sex-macros.o sex-modules.o types.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,types,utils
|
||||||
|
|
||||||
sex-fmt-c.o: sex-fmt-c.scm
|
sex-fmt-c.o: sex-fmt-c.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
|
||||||
@@ -46,15 +49,32 @@ sex-fmt-c.o: sex-fmt-c.scm
|
|||||||
fmt-c-writer.o: fmt-c-writer.module.scm fmt-c-writer.scm sex-fmt-c.o utils.o
|
fmt-c-writer.o: fmt-c-writer.module.scm fmt-c-writer.scm sex-fmt-c.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,utils
|
||||||
|
|
||||||
sexc.o: sexc.module.scm sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
|
sexc.o: sexc.module.scm types.o sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,types,utils
|
||||||
|
|
||||||
|
# Unit testing
|
||||||
sex-tests:
|
sex-tests:
|
||||||
$(MAKE) -C tests sex-tests
|
$(MAKE) -C ./tests sex-tests
|
||||||
cp ./tests/sex-tests ./
|
cp ./tests/sex-tests ./
|
||||||
|
|
||||||
|
sextest:
|
||||||
|
$(MAKE) -C ./tools/sextest sextest
|
||||||
|
cp ./tools/sextest/sextest .
|
||||||
|
|
||||||
|
SEX_TEST_PROGRAMS = hello-world lists comments unicode serialize
|
||||||
|
|
||||||
|
# Multi-module linking is checked end to end; see tests/modules/Makefile.
|
||||||
|
check-modules: sexc
|
||||||
|
@$(MAKE) --no-print-directory -C ./tests/modules check SEXC=../../sexc
|
||||||
|
|
||||||
|
run-tests: sexc sex-tests sextest
|
||||||
|
./sex-tests && ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex) && $(MAKE) check-modules
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJ) main.o
|
rm -f $(OBJ) main.o
|
||||||
rm -f *.import.scm
|
rm -f *.import.scm
|
||||||
rm -f *.link
|
rm -f *.link
|
||||||
rm -f sexc sex-tests
|
rm -f sexc sex-tests sextest
|
||||||
|
$(MAKE) -C ./tests/modules clean
|
||||||
|
|
||||||
|
.PHONY: clean run-tests sex-tests sextest check-modules
|
||||||
|
|||||||
20
Readme.org
20
Readme.org
@@ -4,8 +4,8 @@
|
|||||||
#+ATTR_HTML: :width 300px
|
#+ATTR_HTML: :width 300px
|
||||||
[[sex.png][file:./sex.png]]
|
[[sex.png][file:./sex.png]]
|
||||||
|
|
||||||
Sex is a S-expressions language. Sex is written in Chicken, which is a
|
Sex is a S-expressions language. Sex is written in Chicken, which is an
|
||||||
[[https://call-cc.org][R5RS Scheme]].
|
[[https://call-cc.org][R7RS Scheme]].
|
||||||
Sex is statically typed, compiled general purpose language.
|
Sex is statically typed, compiled general purpose language.
|
||||||
|
|
||||||
* Compilation
|
* Compilation
|
||||||
@@ -49,6 +49,12 @@ 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
|
but you can pass any using ~--c-compiler~ option, or by setting
|
||||||
~SEX_CC~ environment variable.
|
~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
|
** Example
|
||||||
An example of Sex source:
|
An example of Sex source:
|
||||||
#+begin_src scheme
|
#+begin_src scheme
|
||||||
@@ -122,7 +128,7 @@ return Sex code.
|
|||||||
#+begin_src scheme
|
#+begin_src scheme
|
||||||
(pub defmacro (check-sdl-return call message ret-code)
|
(pub defmacro (check-sdl-return call message ret-code)
|
||||||
`(if (< 0 ,call)
|
`(if (< 0 ,call)
|
||||||
(begin
|
(do
|
||||||
(puts ,message)
|
(puts ,message)
|
||||||
(return ,ret-code))))
|
(return ,ret-code))))
|
||||||
|
|
||||||
@@ -135,10 +141,16 @@ return Sex code.
|
|||||||
#+begin_src scheme
|
#+begin_src scheme
|
||||||
(pub fn init () int
|
(pub fn init () int
|
||||||
(if (< 0 (SDL_Init SDL_INIT_VIDEO))
|
(if (< 0 (SDL_Init SDL_INIT_VIDEO))
|
||||||
(begin (puts "Failed to initialize SDL") (return 1)))
|
(do (puts "Failed to initialize SDL") (return 1)))
|
||||||
...)
|
...)
|
||||||
#+end_src
|
#+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
|
** Use an established environment for development
|
||||||
As Sex is S-expressions, you always have Emacs with paredit as your
|
As Sex is S-expressions, you always have Emacs with paredit as your
|
||||||
best option.
|
best option.
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
fmt getopt-long brev-separate test tree srfi-1 srfi-13 srfi-69 matchable
|
fmt getopt-long brev-separate test srfi-1 srfi-13 srfi-69 matchable
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
|
|
||||||
(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)))
|
||||||
`(begin
|
`(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)
|
||||||
|
|||||||
246
example/sdl3-triangle.sex
Normal file
246
example/sdl3-triangle.sex
Normal file
@@ -0,0 +1,246 @@
|
|||||||
|
;;; A triangle that follows the mouse pointer, spins while the left
|
||||||
|
;;; mouse button is held down, and quits on Escape (or on closing the
|
||||||
|
;;; window).
|
||||||
|
;;;
|
||||||
|
;;; SDL3 supplies the window, the GL context and the events. Everything
|
||||||
|
;;; drawn goes through an OpenGL 3.3 core-profile pipeline: a vertex and
|
||||||
|
;;; fragment shader, and one VAO/VBO holding a unit triangle. Where the
|
||||||
|
;;; triangle is and how far it has spun are passed in as uniforms, so
|
||||||
|
;;; the geometry itself is uploaded once and never touched again.
|
||||||
|
;;;
|
||||||
|
;;; Build:
|
||||||
|
;;; ./sexc example/sdl3-triangle.sex -o sdl3-triangle -- `pkg-config --cflags --libs sdl3` -framework OpenGL
|
||||||
|
|
||||||
|
(define GL-SILENCE-DEPRECATION 1) ; OpenGL is deprecated on macOS
|
||||||
|
|
||||||
|
(include SDL3/SDL.h)
|
||||||
|
(include OpenGL/gl3.h)
|
||||||
|
|
||||||
|
(define WINDOW-WIDTH 800)
|
||||||
|
(define WINDOW-HEIGHT 600)
|
||||||
|
|
||||||
|
(define TRIANGLE-RADIUS 70.0) ; in window units
|
||||||
|
(define SPIN-SPEED 3.0) ; radians per second
|
||||||
|
|
||||||
|
;;; The vertex shader does the whole transform: spin the unit triangle
|
||||||
|
;;; by u_angle, scale it to u_radius, move it to u_center, then convert
|
||||||
|
;;; from window coordinates to clip space. That way a frame only has to
|
||||||
|
;;; push four uniforms rather than rebuild any geometry.
|
||||||
|
(var vertex-shader-src (* const char) "
|
||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec2 a_pos;
|
||||||
|
layout (location = 1) in vec3 a_color;
|
||||||
|
|
||||||
|
uniform vec2 u_center; // triangle centre, in window units
|
||||||
|
uniform vec2 u_viewport; // window size, same units as u_center
|
||||||
|
uniform float u_angle; // current spin, in radians
|
||||||
|
uniform float u_radius; // triangle size, in window units
|
||||||
|
|
||||||
|
out vec3 v_color;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
float s = sin(u_angle);
|
||||||
|
float c = cos(u_angle);
|
||||||
|
vec2 spun = vec2(a_pos.x * c - a_pos.y * s,
|
||||||
|
a_pos.x * s + a_pos.y * c);
|
||||||
|
vec2 p = spun * u_radius + u_center;
|
||||||
|
|
||||||
|
// Window coordinates have their origin top-left with y growing
|
||||||
|
// downwards; clip space is centred with y growing upwards.
|
||||||
|
gl_Position = vec4(p.x / u_viewport.x * 2.0 - 1.0,
|
||||||
|
1.0 - p.y / u_viewport.y * 2.0,
|
||||||
|
0.0,
|
||||||
|
1.0);
|
||||||
|
v_color = a_color;
|
||||||
|
}
|
||||||
|
")
|
||||||
|
|
||||||
|
(var fragment-shader-src (* const char) "
|
||||||
|
#version 330 core
|
||||||
|
|
||||||
|
in vec3 v_color;
|
||||||
|
out vec4 frag_color;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
frag_color = vec4(v_color, 1.0);
|
||||||
|
}
|
||||||
|
")
|
||||||
|
|
||||||
|
(fn compile-shader ((kind GLenum) (src (* const char))) GLuint
|
||||||
|
(var shader GLuint (glCreateShader kind))
|
||||||
|
(glShaderSource shader 1 (& src) NULL)
|
||||||
|
(glCompileShader shader)
|
||||||
|
|
||||||
|
(var ok GLint 0)
|
||||||
|
(glGetShaderiv shader GL-COMPILE-STATUS (& ok))
|
||||||
|
(if (== ok 0)
|
||||||
|
(do
|
||||||
|
(var info [char 1024])
|
||||||
|
(glGetShaderInfoLog shader 1024 NULL info)
|
||||||
|
(SDL-Log "shader compilation failed: %s" info)
|
||||||
|
(glDeleteShader shader)
|
||||||
|
(return 0)))
|
||||||
|
(return shader))
|
||||||
|
|
||||||
|
(fn make-program () GLuint
|
||||||
|
(var vertex-shader GLuint (compile-shader GL-VERTEX-SHADER vertex-shader-src))
|
||||||
|
(var fragment-shader GLuint (compile-shader GL-FRAGMENT-SHADER fragment-shader-src))
|
||||||
|
(if (c-or (== vertex-shader 0) (== fragment-shader 0))
|
||||||
|
(do
|
||||||
|
(glDeleteShader vertex-shader)
|
||||||
|
(glDeleteShader fragment-shader)
|
||||||
|
(return 0)))
|
||||||
|
|
||||||
|
(var program GLuint (glCreateProgram))
|
||||||
|
(glAttachShader program vertex-shader)
|
||||||
|
(glAttachShader program fragment-shader)
|
||||||
|
(glLinkProgram program)
|
||||||
|
|
||||||
|
;; The shaders are only needed until the program is linked; the
|
||||||
|
;; program holds its own reference until then.
|
||||||
|
(glDeleteShader vertex-shader)
|
||||||
|
(glDeleteShader fragment-shader)
|
||||||
|
|
||||||
|
(var ok GLint 0)
|
||||||
|
(glGetProgramiv program GL-LINK-STATUS (& ok))
|
||||||
|
(if (== ok 0)
|
||||||
|
(do
|
||||||
|
(var info [char 1024])
|
||||||
|
(glGetProgramInfoLog program 1024 NULL info)
|
||||||
|
(SDL-Log "program linking failed: %s" info)
|
||||||
|
(glDeleteProgram program)
|
||||||
|
(return 0)))
|
||||||
|
(return program))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(if (! (SDL-Init SDL-INIT-VIDEO))
|
||||||
|
(do
|
||||||
|
(SDL-Log "SDL_Init failed: %s" (SDL-GetError))
|
||||||
|
(return 1)))
|
||||||
|
|
||||||
|
;; Ask for core profile 3.3 before the window exists: these attributes
|
||||||
|
;; are read when the context is created.
|
||||||
|
(SDL-GL-SetAttribute SDL-GL-CONTEXT-MAJOR-VERSION 3)
|
||||||
|
(SDL-GL-SetAttribute SDL-GL-CONTEXT-MINOR-VERSION 3)
|
||||||
|
(SDL-GL-SetAttribute SDL-GL-CONTEXT-PROFILE-MASK SDL-GL-CONTEXT-PROFILE-CORE)
|
||||||
|
(SDL-GL-SetAttribute SDL-GL-DOUBLEBUFFER 1)
|
||||||
|
|
||||||
|
(var window (* SDL-Window)
|
||||||
|
(SDL-CreateWindow "Sex + SDL3 + OpenGL"
|
||||||
|
WINDOW-WIDTH WINDOW-HEIGHT
|
||||||
|
SDL-WINDOW-OPENGL))
|
||||||
|
(if (== window NULL)
|
||||||
|
(do
|
||||||
|
(SDL-Log "SDL_CreateWindow failed: %s" (SDL-GetError))
|
||||||
|
(SDL-Quit)
|
||||||
|
(return 1)))
|
||||||
|
|
||||||
|
(var gl-context SDL-GLContext (SDL-GL-CreateContext window))
|
||||||
|
(if (== gl-context NULL)
|
||||||
|
(do
|
||||||
|
(SDL-Log "SDL_GL_CreateContext failed: %s" (SDL-GetError))
|
||||||
|
(SDL-DestroyWindow window)
|
||||||
|
(SDL-Quit)
|
||||||
|
(return 1)))
|
||||||
|
|
||||||
|
(SDL-GL-MakeCurrent window gl-context)
|
||||||
|
(SDL-GL-SetSwapInterval 1)
|
||||||
|
|
||||||
|
(var program GLuint (make-program))
|
||||||
|
(if (== program 0)
|
||||||
|
(do
|
||||||
|
(SDL-GL-DestroyContext gl-context)
|
||||||
|
(SDL-DestroyWindow window)
|
||||||
|
(SDL-Quit)
|
||||||
|
(return 1)))
|
||||||
|
|
||||||
|
;; A unit triangle, two position components then three colour
|
||||||
|
;; components per vertex. The vertices sit on the unit circle at 90,
|
||||||
|
;; 210 and 330 degrees; the shader spins, scales and moves it.
|
||||||
|
(var verts [GLfloat 15]
|
||||||
|
#( 0.000 1.000 1.00 0.35 0.35
|
||||||
|
-0.866 -0.500 0.35 1.00 0.45
|
||||||
|
0.866 -0.500 0.40 0.50 1.00))
|
||||||
|
|
||||||
|
(var vao GLuint 0)
|
||||||
|
(var vbo GLuint 0)
|
||||||
|
(glGenVertexArrays 1 (& vao))
|
||||||
|
(glBindVertexArray vao)
|
||||||
|
(glGenBuffers 1 (& vbo))
|
||||||
|
(glBindBuffer GL-ARRAY-BUFFER vbo)
|
||||||
|
(glBufferData GL-ARRAY-BUFFER (sizeof verts) verts GL-STATIC-DRAW)
|
||||||
|
|
||||||
|
(var stride GLsizei (cast (* 5 (sizeof GLfloat)) GLsizei))
|
||||||
|
(glVertexAttribPointer 0 2 GL-FLOAT GL-FALSE stride (cast 0 (* void)))
|
||||||
|
(glEnableVertexAttribArray 0)
|
||||||
|
(glVertexAttribPointer 1 3 GL-FLOAT GL-FALSE stride
|
||||||
|
(cast (* 2 (sizeof GLfloat)) (* void)))
|
||||||
|
(glEnableVertexAttribArray 1)
|
||||||
|
|
||||||
|
(var u-center GLint (glGetUniformLocation program "u_center"))
|
||||||
|
(var u-viewport GLint (glGetUniformLocation program "u_viewport"))
|
||||||
|
(var u-angle GLint (glGetUniformLocation program "u_angle"))
|
||||||
|
(var u-radius GLint (glGetUniformLocation program "u_radius"))
|
||||||
|
|
||||||
|
(var running bool true)
|
||||||
|
(var angle float 0.0)
|
||||||
|
(var last-ticks Uint64 (SDL-GetTicks))
|
||||||
|
(var event SDL-Event)
|
||||||
|
|
||||||
|
(while running
|
||||||
|
(while (SDL-PollEvent (& event))
|
||||||
|
(switch (. event type)
|
||||||
|
(case SDL-EVENT-QUIT
|
||||||
|
(= running false))
|
||||||
|
(case SDL-EVENT-KEY-DOWN
|
||||||
|
(if (== (. event key key) SDLK-ESCAPE)
|
||||||
|
(= running false)))))
|
||||||
|
|
||||||
|
;; Seconds since the previous frame, so the spin rate does not
|
||||||
|
;; depend on how fast we happen to be rendering.
|
||||||
|
(var now Uint64 (SDL-GetTicks))
|
||||||
|
(var dt float (/ (cast (- now last-ticks) float) 1000.0))
|
||||||
|
(= last-ticks now)
|
||||||
|
|
||||||
|
(var mouse-x float 0.0)
|
||||||
|
(var mouse-y float 0.0)
|
||||||
|
(var buttons SDL-MouseButtonFlags (SDL-GetMouseState (& mouse-x) (& mouse-y)))
|
||||||
|
(if (!= 0 (& buttons SDL-BUTTON-LMASK))
|
||||||
|
(= angle (+ angle (* SPIN-SPEED dt))))
|
||||||
|
|
||||||
|
;; The viewport is in pixels, which is not the same as window units
|
||||||
|
;; on a HiDPI display; the mouse position is in window units, so the
|
||||||
|
;; shader needs that size rather than the pixel one.
|
||||||
|
(var pixel-width int 0)
|
||||||
|
(var pixel-height int 0)
|
||||||
|
(SDL-GetWindowSizeInPixels window (& pixel-width) (& pixel-height))
|
||||||
|
(glViewport 0 0 pixel-width pixel-height)
|
||||||
|
|
||||||
|
(var window-width int 0)
|
||||||
|
(var window-height int 0)
|
||||||
|
(SDL-GetWindowSize window (& window-width) (& window-height))
|
||||||
|
|
||||||
|
(glClearColor 0.06 0.06 0.09 1.0)
|
||||||
|
(glClear GL-COLOR-BUFFER-BIT)
|
||||||
|
|
||||||
|
(glUseProgram program)
|
||||||
|
(glUniform2f u-center mouse-x mouse-y)
|
||||||
|
(glUniform2f u-viewport (cast window-width float) (cast window-height float))
|
||||||
|
(glUniform1f u-angle angle)
|
||||||
|
(glUniform1f u-radius TRIANGLE-RADIUS)
|
||||||
|
|
||||||
|
(glBindVertexArray vao)
|
||||||
|
(glDrawArrays GL-TRIANGLES 0 3)
|
||||||
|
|
||||||
|
(SDL-GL-SwapWindow window))
|
||||||
|
|
||||||
|
(glDeleteVertexArrays 1 (& vao))
|
||||||
|
(glDeleteBuffers 1 (& vbo))
|
||||||
|
(glDeleteProgram program)
|
||||||
|
(SDL-GL-DestroyContext gl-context)
|
||||||
|
(SDL-DestroyWindow window)
|
||||||
|
(SDL-Quit)
|
||||||
|
(return 0))
|
||||||
64
example/serialize.sex
Normal file
64
example/serialize.sex
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
;;; Generating code from a type's own definition.
|
||||||
|
;;;
|
||||||
|
;;; `serialize-struct' is handed nothing but a struct's name. It asks
|
||||||
|
;;; the compiler's type database what fields that struct has and what
|
||||||
|
;;; type each one is, and writes a printer to match. Add a field to the
|
||||||
|
;;; struct and the printer grows with it, with no other edit.
|
||||||
|
;;;
|
||||||
|
;;; The database is filled in as toplevel forms are processed, in order,
|
||||||
|
;;; so a struct has to be declared before the macro call that asks about
|
||||||
|
;;; it -- the same rule C has.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(defmacro (serialize-struct type-name)
|
||||||
|
;; map-fields walks the declaration; type-match picks a printf
|
||||||
|
;; conversion per field. Both come from the compiler's type database,
|
||||||
|
;; so the macro never takes a type apart itself.
|
||||||
|
(let ((printers
|
||||||
|
(map-fields type-name
|
||||||
|
(lambda (name type)
|
||||||
|
`(fprintf out
|
||||||
|
,(string-append
|
||||||
|
" " (symbol->string name) "="
|
||||||
|
(type-match type
|
||||||
|
(int "%d")
|
||||||
|
(char "%c")
|
||||||
|
(long "%ld")
|
||||||
|
(unsigned "%u")
|
||||||
|
(float "%g")
|
||||||
|
(double "%g")
|
||||||
|
((* const char) "%s")
|
||||||
|
((* char) "%s")
|
||||||
|
(else (error "serialize-struct: unsupported field type"
|
||||||
|
type-name name type))))
|
||||||
|
(-> v ,name))))))
|
||||||
|
(if (not printers)
|
||||||
|
(error "serialize-struct: no such struct" type-name)
|
||||||
|
`(pub fn ,(cat 'serialize- type-name)
|
||||||
|
((v (* const struct ,type-name)) (out (* FILE)))
|
||||||
|
void
|
||||||
|
(fprintf out ,(string-append (symbol->string type-name) " {"))
|
||||||
|
,@printers
|
||||||
|
(fprintf out " }\n")))))
|
||||||
|
|
||||||
|
(struct point ((x int) (y int)))
|
||||||
|
|
||||||
|
(struct person
|
||||||
|
((name (* const char))
|
||||||
|
(age int)
|
||||||
|
(height float)))
|
||||||
|
|
||||||
|
;;; Two printers, written by the compiler from the declarations above.
|
||||||
|
(serialize-struct point)
|
||||||
|
(serialize-struct person)
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var origin (struct point) #(0 0))
|
||||||
|
(var corner (struct point) #(640 -480))
|
||||||
|
(var alex (struct person) #("Alex" 34 1.82))
|
||||||
|
|
||||||
|
(serialize-point (& origin) stdout)
|
||||||
|
(serialize-point (& corner) stdout)
|
||||||
|
(serialize-person (& alex) stdout)
|
||||||
|
(return 0))
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
(module fmt-c-writer (emit-c
|
(module fmt-c-writer (emit-c
|
||||||
sex-fmt-current-file)
|
sex-line-directives)
|
||||||
"fmt-c-writer.scm")
|
"fmt-c-writer.scm")
|
||||||
|
|||||||
276
fmt-c-writer.scm
276
fmt-c-writer.scm
@@ -2,20 +2,97 @@
|
|||||||
|
|
||||||
(import
|
(import
|
||||||
scheme
|
scheme
|
||||||
|
(scheme base) ; make-parameter
|
||||||
(chicken base)
|
(chicken base)
|
||||||
(chicken string)
|
(chicken string)
|
||||||
(chicken syntax)
|
(chicken syntax)
|
||||||
brev-separate
|
brev-separate ; fn, flatten
|
||||||
fmt
|
fmt
|
||||||
sex-fmt-c
|
sex-fmt-c
|
||||||
matchable
|
matchable
|
||||||
regex
|
(chicken irregex) ; unkebabify
|
||||||
srfi-1 ; lists
|
srfi-1 ; lists
|
||||||
srfi-13 ; strings
|
srfi-13 ; strings
|
||||||
srfi-39 ; parameters
|
|
||||||
tree
|
|
||||||
utils)
|
utils)
|
||||||
|
|
||||||
|
;;; egg `tree' not ported to CHICKEN 6 yet
|
||||||
|
(define (tree-map f tree)
|
||||||
|
(cond ((null? tree) (list))
|
||||||
|
((pair? tree) (cons (tree-map f (car tree))
|
||||||
|
(tree-map f (cdr tree))))
|
||||||
|
(else (f tree))))
|
||||||
|
|
||||||
|
;;; How much #line information to emit:
|
||||||
|
;;;
|
||||||
|
;;; statement -- before every statement.
|
||||||
|
;;; toplevel -- one directive per toplevel form.
|
||||||
|
;;; none -- none at all, for reading -C output by eye.
|
||||||
|
(define sex-line-directives (make-parameter 'statement))
|
||||||
|
|
||||||
|
(define (anchor-statements?)
|
||||||
|
(eq? (sex-line-directives) 'statement))
|
||||||
|
|
||||||
|
(define (line-directive src)
|
||||||
|
;; `%line' is fmt-c's #line directive. cpp-line concatenates its
|
||||||
|
;; second argument verbatim, so the file name arrives already quoted.
|
||||||
|
`(%line ,(cdr src) ,(fmt #f #\" (car src) #\")))
|
||||||
|
|
||||||
|
(define (walk-body stmts)
|
||||||
|
"Walk a statement list, re-anchoring each statement that has a known
|
||||||
|
source location. Only statement positions may be walked this way: a
|
||||||
|
#line inside an expression is a C syntax error."
|
||||||
|
(if (anchor-statements?)
|
||||||
|
(append-map (lambda (s)
|
||||||
|
(let ((src (form-source s)))
|
||||||
|
(if src
|
||||||
|
(list (line-directive src) (walk-expr s))
|
||||||
|
(list (walk-expr s)))))
|
||||||
|
(pack-comments stmts))
|
||||||
|
(map walk-expr (pack-comments stmts))))
|
||||||
|
|
||||||
|
(define (walk-stmt s)
|
||||||
|
"A statement in a slot that holds exactly one form -- an `if' arm.
|
||||||
|
Splicing is not possible there, since c-if reads anything past the arm
|
||||||
|
as an `else if' chain, so the anchor and the statement are wrapped in
|
||||||
|
`%begin': a statement sequence that emits no braces of its own (the
|
||||||
|
surrounding c-block supplies them)."
|
||||||
|
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
||||||
|
(if src
|
||||||
|
`(%begin ,(line-directive src) ,(walk-expr s))
|
||||||
|
(walk-expr s))))
|
||||||
|
|
||||||
|
;;; A `;' comment reads as a form, so one written inside a construct
|
||||||
|
;;; with positional slots lands in a slot and shifts everything after
|
||||||
|
;;; it. So take the positional slots by skipping comments, and hand
|
||||||
|
;;; the comments back to be emitted just before the statement
|
||||||
|
(define (take-slots forms n)
|
||||||
|
"Three values: the comment forms skipped over, the next N non-comment
|
||||||
|
forms, and what remains."
|
||||||
|
(let loop ((fs forms) (n n) (comments (list)) (slots (list)))
|
||||||
|
(cond ((or (= n 0) (null? fs))
|
||||||
|
(values (reverse comments) (reverse slots) fs))
|
||||||
|
((comment-form? (car fs))
|
||||||
|
(loop (cdr fs) n (cons (car fs) comments) slots))
|
||||||
|
(else
|
||||||
|
(loop (cdr fs) (- n 1) comments (cons (car fs) slots))))))
|
||||||
|
|
||||||
|
;;; `%begin' is a statement sequence that emits no braces of its own, so
|
||||||
|
;;; the comments simply precede the statement.
|
||||||
|
(define (with-comments comments form)
|
||||||
|
(if (null? comments)
|
||||||
|
form
|
||||||
|
`(%begin ,@(map walk-expr comments) ,form)))
|
||||||
|
|
||||||
|
(define (walk-if-clauses clauses)
|
||||||
|
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
|
||||||
|
(let loop ((cs clauses) (acc (list)))
|
||||||
|
(cond ((null? cs) (reverse acc))
|
||||||
|
((null? (cdr cs)) ; trailing else statement
|
||||||
|
(reverse (cons (walk-stmt (car cs)) acc)))
|
||||||
|
(else (loop (cddr cs)
|
||||||
|
(cons (walk-stmt (cadr cs))
|
||||||
|
(cons (walk-expr (car cs)) acc)))))))
|
||||||
|
|
||||||
(define (unkebabify sym)
|
(define (unkebabify sym)
|
||||||
(case sym
|
(case sym
|
||||||
((-) sym)
|
((-) sym)
|
||||||
@@ -24,20 +101,25 @@
|
|||||||
((-=) sym)
|
((-=) sym)
|
||||||
(else
|
(else
|
||||||
(string->symbol
|
(string->symbol
|
||||||
(string-substitute "-(?!>)" "_"
|
(irregex-replace/all "-(?!>)" (symbol->string sym) "_")))))
|
||||||
(symbol->string sym) #t)))))
|
|
||||||
|
|
||||||
(define (atom-to-fmt-c atom)
|
(define (atom-to-fmt-c atom)
|
||||||
(case atom
|
(case atom
|
||||||
((fn) '%fun)
|
((fn) '%fun)
|
||||||
((prototype) '%prototype)
|
((prototype) '%prototype)
|
||||||
((begin) '%block-begin)
|
((do) '%block-begin)
|
||||||
((define) '%define)
|
((define) '%define)
|
||||||
((pointer) '%pointer)
|
((pointer) '%pointer)
|
||||||
((array) '%array)
|
((array) '%array)
|
||||||
((attribute) '%attribute)
|
((attribute) '%attribute)
|
||||||
((¤) 'vector-ref)
|
((¤) 'vector-ref)
|
||||||
((include) '%include)
|
((include) '%include)
|
||||||
|
;; a `|' inside a symbol has to be escaped to be written in
|
||||||
|
;; a Scheme source, so we just rename it in fmt-c compatible
|
||||||
|
;; way
|
||||||
|
((|\||) 'bit-or)
|
||||||
|
((|\|\||) '%or)
|
||||||
|
((|\|=|) 'bit-or=)
|
||||||
;; uh things we do for c89 compatibility
|
;; uh things we do for c89 compatibility
|
||||||
((bool) 'int)
|
((bool) 'int)
|
||||||
((true) 1)
|
((true) 1)
|
||||||
@@ -53,21 +135,47 @@
|
|||||||
(car type)
|
(car type)
|
||||||
type))
|
type))
|
||||||
|
|
||||||
(define (make-field-access form)
|
(define (comment-form? f)
|
||||||
(assert
|
(and (pair? f) (eq? (car f) 'comment)))
|
||||||
(= 2 (length form)) "Wrong field access format")
|
|
||||||
(unkebabify
|
;;; Strip ;s so ;;; Foo becomes /* Foo */ and not /* ;; Foo */
|
||||||
(string->symbol
|
(define (strip-comment-marker text)
|
||||||
(fmt #f (cadr form) (car form)))))
|
(string-trim-both (string-trim text #\;)))
|
||||||
|
|
||||||
|
(define (walk-comment texts)
|
||||||
|
(list '%comment
|
||||||
|
(string-append " "
|
||||||
|
(string-intersperse (map strip-comment-marker texts)
|
||||||
|
"\n ")
|
||||||
|
" ")))
|
||||||
|
|
||||||
|
;;; Merge multiple lines of /* */ into single block
|
||||||
|
(define (pack-comments forms)
|
||||||
|
(let loop ((fs forms) (acc (list)))
|
||||||
|
(cond
|
||||||
|
((null? fs) (reverse acc))
|
||||||
|
((comment-form? (car fs))
|
||||||
|
(let ((first (car fs)))
|
||||||
|
(let gather ((rest (cdr fs)) (texts (cdr first)) (line (form-line first)))
|
||||||
|
(if (and (pair? rest)
|
||||||
|
(comment-form? (car rest))
|
||||||
|
line
|
||||||
|
(equal? (form-file first) (form-file (car rest)))
|
||||||
|
(eqv? (form-line (car rest)) (+ line 1)))
|
||||||
|
(gather (cdr rest)
|
||||||
|
(append texts (cdr (car rest)))
|
||||||
|
(form-line (car rest)))
|
||||||
|
(loop rest
|
||||||
|
(cons (if (eq? texts (cdr first))
|
||||||
|
first ; a run of one, left alone
|
||||||
|
(copy-form-source! first (cons 'comment texts)))
|
||||||
|
acc))))))
|
||||||
|
(else (loop (cdr fs) (cons (car fs) acc))))))
|
||||||
|
|
||||||
(define (walk-generic-toplevel form)
|
(define (walk-generic-toplevel form)
|
||||||
(cond ((atom? form) (atom-to-fmt-c form))
|
(cond ((atom? form) (atom-to-fmt-c form))
|
||||||
((list? form) (map walk-generic-toplevel form))
|
((list? form) (map walk-generic-toplevel form))
|
||||||
(else (error "Malformed form " form))))
|
(else (sex-error form "malformed form" form))))
|
||||||
|
|
||||||
(define (field-access-form? form)
|
|
||||||
(and (symbol? (car form))
|
|
||||||
(char=? #\. (string-ref (symbol->string (car form)) 0))))
|
|
||||||
|
|
||||||
(define (walk-expr form)
|
(define (walk-expr form)
|
||||||
(match form
|
(match form
|
||||||
@@ -76,19 +184,62 @@
|
|||||||
(walk-expr (vector->list form))))
|
(walk-expr (vector->list form))))
|
||||||
((? atom?)
|
((? atom?)
|
||||||
(atom-to-fmt-c form))
|
(atom-to-fmt-c form))
|
||||||
((? field-access-form?)
|
;; (comment "text") -> /* text */. `%comment' is the fmt-c directive.
|
||||||
(make-field-access form))
|
(('comment . text) (walk-comment text))
|
||||||
|
;; (dot-access obj field ...) -> obj.field... member access. `%.'
|
||||||
|
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
|
||||||
|
(('dot-access . rest) (cons '%. (map walk-expr rest)))
|
||||||
(('var . _) (walk-var form))
|
(('var . _) (walk-var form))
|
||||||
(('cast expr type) (list '%cast
|
;; An expression has no room for a statement, so a comment in a
|
||||||
(walk-type type)
|
;; cast is dropped rather than relocated.
|
||||||
(walk-expr expr)))
|
(('cast . rest)
|
||||||
|
(let-values (((comments slots _) (take-slots rest 2)))
|
||||||
|
(list '%cast (walk-type (cadr slots)) (walk-expr (car slots)))))
|
||||||
(('enum . _) (walk-enum form))
|
(('enum . _) (walk-enum form))
|
||||||
;; | is problematic... And c-or/bit-or/etc are actually
|
|
||||||
;; procedures, so we have to call the procedure itself
|
|
||||||
(('c-or . rest) (apply c-or (map walk-expr rest)))
|
(('c-or . rest) (apply c-or (map walk-expr rest)))
|
||||||
(('c-bit-or . rest) (apply c-bit-or (map walk-expr rest)))
|
(('c-bit-or . rest) (apply c-bit-or (map walk-expr rest)))
|
||||||
(('c-bit-or= . rest) (apply c-bit-or= (map walk-expr rest)))
|
(('c-bit-or= . rest) (apply c-bit-or= (map walk-expr rest)))
|
||||||
(else (map walk-expr form))))
|
|
||||||
|
;; Statement positions. These are the only places a #line may go,
|
||||||
|
;; and each is spliced or wrapped according to what the
|
||||||
|
;; corresponding fmt-c procedure accepts.
|
||||||
|
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
||||||
|
(('if . clauses)
|
||||||
|
(with-comments (filter comment-form? clauses)
|
||||||
|
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
||||||
|
(('while . rest)
|
||||||
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
|
(with-comments comments
|
||||||
|
(cons* 'while (walk-expr (car slots)) (walk-body body)))))
|
||||||
|
(('for . rest)
|
||||||
|
(let-values (((comments slots body) (take-slots rest 3)))
|
||||||
|
(with-comments comments
|
||||||
|
(cons* 'for (walk-expr (car slots)) (walk-expr (cadr slots))
|
||||||
|
(walk-expr (caddr slots))
|
||||||
|
(walk-body body)))))
|
||||||
|
;; No anchor *between* switch clauses: c-switch requires every clause
|
||||||
|
;; to be a case/default form and errors on anything else. The clause
|
||||||
|
;; bodies are anchored from inside, which is what a debugger steps
|
||||||
|
;; onto -- a `case' label is not a statement.
|
||||||
|
;; A comment between clauses has to go too: c-switch requires every
|
||||||
|
;; clause to be a case/default form and errors on anything else.
|
||||||
|
(('switch . rest)
|
||||||
|
(let-values (((comments slots clauses) (take-slots rest 1)))
|
||||||
|
(with-comments (append comments (filter comment-form? clauses))
|
||||||
|
(cons* 'switch (walk-expr (car slots))
|
||||||
|
(map walk-expr (remove comment-form? clauses))))))
|
||||||
|
(('case . rest)
|
||||||
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
|
(with-comments comments
|
||||||
|
(cons* 'case (walk-expr (car slots)) (walk-body body)))))
|
||||||
|
(('case/fallthrough . rest)
|
||||||
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
|
(with-comments comments
|
||||||
|
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-body body)))))
|
||||||
|
(('default . body) (cons 'default (walk-body body)))
|
||||||
|
|
||||||
|
;; Drop comments so they will not generate additional comma
|
||||||
|
(else (map walk-expr (remove comment-form? form)))))
|
||||||
|
|
||||||
(define (walk-var form)
|
(define (walk-var form)
|
||||||
;; (var a int) -> (%var int a)
|
;; (var a int) -> (%var int a)
|
||||||
@@ -96,14 +247,16 @@
|
|||||||
;; (var b [const char 512]) -> (%var (%array (const char) 512) b)
|
;; (var b [const char 512]) -> (%var (%array (const char) 512) b)
|
||||||
;; note: [...] is actually (¤ ...) after reading
|
;; note: [...] is actually (¤ ...) after reading
|
||||||
;; (var c (fn ((int) (float)) void)) -> (%var (%fun void ((int) (float))) c)
|
;; (var c (fn ((int) (float)) void)) -> (%var (%fun void ((int) (float))) c)
|
||||||
`(%var
|
;; Likewise a declaration: drop any comment rather than shift the
|
||||||
,(walk-type (third form))
|
;; name and type apart.
|
||||||
,(atom-to-fmt-c (second form))
|
(let ((form (cons (car form) (remove comment-form? (cdr form)))))
|
||||||
.
|
`(%var
|
||||||
,(if (null? (drop form 3))
|
,(walk-type (third form))
|
||||||
(list)
|
,(atom-to-fmt-c (second form))
|
||||||
(walk-expr (drop form 3))) ; optional init expression
|
.
|
||||||
))
|
,(if (null? (drop form 3))
|
||||||
|
(list)
|
||||||
|
(walk-expr (drop form 3)))))) ; optional init expression
|
||||||
|
|
||||||
(define (walk-type form)
|
(define (walk-type form)
|
||||||
;; int -> int
|
;; int -> int
|
||||||
@@ -128,7 +281,7 @@
|
|||||||
(('fn arglist ret-type)
|
(('fn arglist ret-type)
|
||||||
`(%fun ,(walk-type ret-type) ,(walk-arglist arglist)))
|
`(%fun ,(walk-type ret-type) ,(walk-arglist arglist)))
|
||||||
(('fn . _)
|
(('fn . _)
|
||||||
(assert #f "Malformed function type form"))
|
(sex-error form "malformed function type" form))
|
||||||
|
|
||||||
;; Special case: nested structs/unions
|
;; Special case: nested structs/unions
|
||||||
((or ('struct . _)
|
((or ('struct . _)
|
||||||
@@ -138,11 +291,26 @@
|
|||||||
(else
|
(else
|
||||||
(type-convert-to-c form))))
|
(type-convert-to-c form))))
|
||||||
|
|
||||||
|
(define (has-pointer-star? form)
|
||||||
|
(and (pair? form)
|
||||||
|
(or (memq '* form)
|
||||||
|
(any has-pointer-star? (filter pair? form)))))
|
||||||
|
|
||||||
|
;;; A `*' inside a sublist. Pointer chains are written flat -- (* * T),
|
||||||
|
;;; never (* (* T))
|
||||||
|
;;; Sublists that merely group, like (* (const struct suc)), contain
|
||||||
|
;;; no `*' and are fine.
|
||||||
|
(define (nested-pointer? type)
|
||||||
|
(and (pair? type)
|
||||||
|
(any has-pointer-star? (filter pair? type))))
|
||||||
|
|
||||||
(define (type-convert-to-c type)
|
(define (type-convert-to-c type)
|
||||||
;; Our pointers to C pointers
|
;; Our pointers to C pointers
|
||||||
;; int -> int
|
;; int -> int
|
||||||
;; * const char -> const char *
|
;; * const char -> const char *
|
||||||
;; const * const char -> const char * const
|
;; const * const char -> const char * const
|
||||||
|
(when (nested-pointer? type)
|
||||||
|
(sex-error type "pointer chains are written flat, as (* * T), not nested" type))
|
||||||
(if (atom? type) (atom-to-fmt-c type)
|
(if (atom? type) (atom-to-fmt-c type)
|
||||||
(flatten
|
(flatten
|
||||||
(tree-map atom-to-fmt-c
|
(tree-map atom-to-fmt-c
|
||||||
@@ -158,7 +326,7 @@
|
|||||||
,(atom-to-fmt-c name)
|
,(atom-to-fmt-c name)
|
||||||
,(walk-arglist args)
|
,(walk-arglist args)
|
||||||
.
|
.
|
||||||
,(walk-expr maybe-body)))))
|
,(walk-body maybe-body)))))
|
||||||
|
|
||||||
;;; TODO: isn't there a better way?
|
;;; TODO: isn't there a better way?
|
||||||
(define (is-probably-type form)
|
(define (is-probably-type form)
|
||||||
@@ -184,7 +352,7 @@
|
|||||||
|
|
||||||
((var . type) (append (list (walk-type (maybe-unwrap-type type)))
|
((var . type) (append (list (walk-type (maybe-unwrap-type type)))
|
||||||
(list (walk-type var))))))
|
(list (walk-type var))))))
|
||||||
form))
|
(remove comment-form? form)))
|
||||||
|
|
||||||
(define (walk-function form)
|
(define (walk-function form)
|
||||||
;; (fn ret-type name arglist body) -> normal function
|
;; (fn ret-type name arglist body) -> normal function
|
||||||
@@ -197,7 +365,7 @@
|
|||||||
(map (fn
|
(map (fn
|
||||||
(let ((type (walk-type (last x))))
|
(let ((type (walk-type (last x))))
|
||||||
(cons type (map atom-to-fmt-c (drop-right x 1)))))
|
(cons type (map atom-to-fmt-c (drop-right x 1)))))
|
||||||
fields))
|
(remove comment-form? fields)))
|
||||||
|
|
||||||
(define (walk-struct form)
|
(define (walk-struct form)
|
||||||
(match form
|
(match form
|
||||||
@@ -210,7 +378,7 @@
|
|||||||
`(,type ,(atom-to-fmt-c name)
|
`(,type ,(atom-to-fmt-c name)
|
||||||
,(process-struct-fields fields)
|
,(process-struct-fields fields)
|
||||||
. ,(tree-map atom-to-fmt-c attrs)))
|
. ,(tree-map atom-to-fmt-c attrs)))
|
||||||
(else (error "Malformed aggregate definition " form))))
|
(else (sex-error form "malformed aggregate definition" form))))
|
||||||
|
|
||||||
(define (walk-enum form)
|
(define (walk-enum form)
|
||||||
(match form
|
(match form
|
||||||
@@ -226,7 +394,7 @@
|
|||||||
(list 'extern (walk-function form)))
|
(list 'extern (walk-function form)))
|
||||||
(('var . _)
|
(('var . _)
|
||||||
(list 'extern (walk-var form)))
|
(list 'extern (walk-var form)))
|
||||||
(else (error "Extern what?"))))
|
(else (sex-error form "extern must be followed by fn or var" form))))
|
||||||
|
|
||||||
(define (walk-public form)
|
(define (walk-public form)
|
||||||
(match form
|
(match form
|
||||||
@@ -247,10 +415,11 @@
|
|||||||
;; ignore here, used in generating public interface
|
;; ignore here, used in generating public interface
|
||||||
(process-toplevel-form form))
|
(process-toplevel-form form))
|
||||||
(else
|
(else
|
||||||
(error "Pub what?" (cadr form)))))
|
(sex-error form "pub must be followed by a definition" form))))
|
||||||
|
|
||||||
(define (process-toplevel-form form)
|
(define (process-toplevel-form form)
|
||||||
(match form
|
(match form
|
||||||
|
(('comment . text) (walk-comment text))
|
||||||
(('fn . _) (list 'static (walk-function form)))
|
(('fn . _) (list 'static (walk-function form)))
|
||||||
(('var . _) (list 'static (walk-var form)))
|
(('var . _) (list 'static (walk-var form)))
|
||||||
(('extern . rest) (walk-extern rest))
|
(('extern . rest) (walk-extern rest))
|
||||||
@@ -261,23 +430,14 @@
|
|||||||
(('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))))
|
||||||
|
|
||||||
(define (get-line-num form)
|
|
||||||
(let ((num (get-line-number form)))
|
|
||||||
(if (string? num)
|
|
||||||
(last (string-split num ":"))
|
|
||||||
#f)))
|
|
||||||
|
|
||||||
(define sex-fmt-current-file (make-parameter "/dev/null"))
|
|
||||||
(define sex-fmt-line-num (make-parameter 0))
|
|
||||||
|
|
||||||
(define (line-directive-string)
|
|
||||||
(fmt #f #\# "line " (sex-fmt-line-num) " " #\" (sex-fmt-current-file) #\"))
|
|
||||||
|
|
||||||
(define (emit-c sex-forms)
|
(define (emit-c sex-forms)
|
||||||
(for-each (lambda (form)
|
(for-each (lambda (form)
|
||||||
(let ((start-line (get-line-num form)))
|
;; Forms the reader did not produce -- the prelude, and
|
||||||
(when start-line
|
;; anything a macro built that we could not attribute --
|
||||||
(sex-fmt-line-num start-line)
|
;; have no location and get no directive.
|
||||||
(fmt #t (line-directive-string) nl)))
|
(let ((src (and (not (eq? (sex-line-directives) 'none))
|
||||||
|
(form-source form))))
|
||||||
|
(when src
|
||||||
|
(fmt #t (c-expr (line-directive src)))))
|
||||||
(fmt #t (c-expr (process-toplevel-form form)) nl))
|
(fmt #t (c-expr (process-toplevel-form form)) nl))
|
||||||
sex-forms))
|
(pack-comments sex-forms)))
|
||||||
|
|||||||
322
reader.scm
322
reader.scm
@@ -1,62 +1,284 @@
|
|||||||
|
;;; Sex reader
|
||||||
|
;;;
|
||||||
|
;;; A hand-written tokenizer + recursive-descent parser that replaces
|
||||||
|
;;; CHICKEN's built-in `read'. We need our own reader because the
|
||||||
|
;;; features Sex requires cannot be expressed on top of `read':
|
||||||
|
;;; - [ ... ] array/pointer sugar, read as (¤ ...)
|
||||||
|
;;; - a leading `.' rewritten to the symbol `dot-access'
|
||||||
|
;;; - `;' comments preserved as (comment "...") forms, so they can be
|
||||||
|
;;; re-emitted into the generated C (keeping the source mapping)
|
||||||
|
;;; It also records the source location of every form it reads (see
|
||||||
|
;;; utils' form-source), so the C writer can emit #line directives.
|
||||||
|
|
||||||
(import
|
(import
|
||||||
scheme
|
scheme
|
||||||
|
(scheme base) ; make-parameter
|
||||||
(chicken base)
|
(chicken base)
|
||||||
(chicken io)
|
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
(chicken port)
|
utils)
|
||||||
(chicken read-syntax)
|
|
||||||
(chicken string)
|
|
||||||
(chicken syntax)
|
|
||||||
brev-separate
|
|
||||||
fmt)
|
|
||||||
|
|
||||||
(import-syntax utils)
|
;;; Sentinels for structural tokens
|
||||||
|
(define close-paren (list '%close-paren))
|
||||||
|
(define close-bracket (list '%close-bracket))
|
||||||
|
(define dot-token (list '%dot))
|
||||||
|
|
||||||
(define (read-forms acc)
|
;;; Current source line. Tracked as characters are consumed
|
||||||
(let ((r (read-with-source-info (current-input-port))))
|
(define current-line (make-parameter 1))
|
||||||
(if (eof-object? r) (reverse acc)
|
|
||||||
(read-forms (cons r acc)))))
|
|
||||||
|
|
||||||
|
(define (get-ch port)
|
||||||
|
(let ((c (read-char port)))
|
||||||
|
(when (and (char? c) (char=? c #\newline))
|
||||||
|
(current-line (+ 1 (current-line))))
|
||||||
|
c))
|
||||||
|
|
||||||
|
(define (peek port)
|
||||||
|
(peek-char port))
|
||||||
|
|
||||||
|
;;; Record where a form started. Called with the line of the opening
|
||||||
|
;;; delimiter, sampled before it is consumed
|
||||||
|
(define (stamp form line)
|
||||||
|
(when (pair? form)
|
||||||
|
(set-form-source! form (current-source-file) line))
|
||||||
|
form)
|
||||||
|
|
||||||
|
(define (delimiter? c)
|
||||||
|
(or (eof-object? c)
|
||||||
|
(char-whitespace? c)
|
||||||
|
(memv c '(#\( #\) #\[ #\] #\" #\; #\' #\` #\,))))
|
||||||
|
|
||||||
|
;;; Skip whitespace. `;' comments are NOT skipped here: they are read
|
||||||
|
;;; as (comment "...") forms by the tokenizer. Block comments (#| |#)
|
||||||
|
;;; and datum comments (#;) are discarded in the tokenizer's `#'
|
||||||
|
;;; dispatch, since `#' also introduces real data (#t, #f, #\c, #(...))
|
||||||
|
(define (skip-whitespace port)
|
||||||
|
(let ((c (peek port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c) #t)
|
||||||
|
((char-whitespace? c) (get-ch port) (skip-whitespace port))
|
||||||
|
(else #t))))
|
||||||
|
|
||||||
|
;;; A `;' comment, read as (comment "<rest of line>"). The leading `;'
|
||||||
|
;;; is consumed; the newline is left in the stream so line tracking and
|
||||||
|
;;; the surrounding parser see it normally
|
||||||
|
(define (read-comment port)
|
||||||
|
(get-ch port) ; consume the leading ;
|
||||||
|
(let loop ((chars (list)))
|
||||||
|
(let ((c (peek port)))
|
||||||
|
(if (or (eof-object? c) (char=? c #\newline))
|
||||||
|
(list 'comment (list->string (reverse chars)))
|
||||||
|
(begin (get-ch port)
|
||||||
|
(loop (cons c chars)))))))
|
||||||
|
|
||||||
|
;;; Read the next token: a datum, one of the structural sentinels
|
||||||
|
;;; (close-paren / close-bracket / dot-token), or the eof-object
|
||||||
|
(define (next-token port)
|
||||||
|
(skip-whitespace port)
|
||||||
|
(let ((c (peek port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c) c)
|
||||||
|
((char=? c #\()
|
||||||
|
(let ((line (current-line)))
|
||||||
|
(get-ch port)
|
||||||
|
(stamp (read-list port close-paren) line)))
|
||||||
|
((char=? c #\[)
|
||||||
|
(let ((line (current-line)))
|
||||||
|
(get-ch port)
|
||||||
|
(stamp (cons '¤ (read-list port close-bracket)) line)))
|
||||||
|
((char=? c #\)) (get-ch port) close-paren)
|
||||||
|
((char=? c #\]) (get-ch port) close-bracket)
|
||||||
|
((char=? c #\;)
|
||||||
|
(let ((line (current-line)))
|
||||||
|
(stamp (read-comment port) line)))
|
||||||
|
((char=? c #\") (read-string-lit port))
|
||||||
|
((char=? c #\') (get-ch port) (list 'quote (read-datum port)))
|
||||||
|
((char=? c #\`) (get-ch port) (list 'quasiquote (read-datum port)))
|
||||||
|
((char=? c #\,)
|
||||||
|
(get-ch port)
|
||||||
|
(if (eqv? (peek port) #\@)
|
||||||
|
(begin (get-ch port) (list 'unquote-splicing (read-datum port)))
|
||||||
|
(list 'unquote (read-datum port))))
|
||||||
|
((char=? c #\#) (get-ch port) (read-hash port))
|
||||||
|
(else (read-atom port)))))
|
||||||
|
|
||||||
|
;;; Like next-token, but a full datum is required: the structural
|
||||||
|
;;; sentinels and eof are errors here (e.g. after a quote or `.')
|
||||||
|
(define (read-datum port)
|
||||||
|
(let ((tok (next-token port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? tok) (error "Unexpected end of input"))
|
||||||
|
((eq? tok close-paren) (error "Unexpected )"))
|
||||||
|
((eq? tok close-bracket) (error "Unexpected ]"))
|
||||||
|
((eq? tok dot-token) (error "Unexpected ."))
|
||||||
|
(else tok))))
|
||||||
|
|
||||||
|
;;; Read list elements up to close-paren or close-bracket,
|
||||||
|
;;; honoring dotted-pair notation (a b . c)
|
||||||
|
(define (read-list port closer)
|
||||||
|
(let loop ((acc (list)))
|
||||||
|
(let ((tok (next-token port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? tok) (error "Unexpected end of input inside list"))
|
||||||
|
((eq? tok close-paren)
|
||||||
|
(if (eq? closer close-paren)
|
||||||
|
(reverse acc)
|
||||||
|
(error "Unmatched closing bracket")))
|
||||||
|
((eq? tok close-bracket)
|
||||||
|
(if (eq? closer close-bracket)
|
||||||
|
(reverse acc)
|
||||||
|
(error "Unmatched closing bracket")))
|
||||||
|
((eq? tok dot-token)
|
||||||
|
(if (null? acc)
|
||||||
|
;; Leading `.': the member/method access operator. It reads
|
||||||
|
;; as an ordinary `dot-access' symbol in first position.
|
||||||
|
(loop (cons 'dot-access acc))
|
||||||
|
;; Otherwise: ordinary dotted-pair notation (a b . c).
|
||||||
|
(let ((tail (read-datum port))
|
||||||
|
(end (next-token port)))
|
||||||
|
(unless (eq? end closer)
|
||||||
|
(error "Malformed dotted list"))
|
||||||
|
(append-reverse acc tail))))
|
||||||
|
(else (loop (cons tok acc)))))))
|
||||||
|
|
||||||
|
;;; Append the reversed list `rev' in front of `tail', producing a
|
||||||
|
;;; possibly-improper list (used for dotted pairs)
|
||||||
|
(define (append-reverse rev tail)
|
||||||
|
(if (null? rev)
|
||||||
|
tail
|
||||||
|
(append-reverse (cdr rev) (cons (car rev) tail))))
|
||||||
|
|
||||||
|
;;; A bare atom: symbol or number, or the dot token when it is exactly "."
|
||||||
|
(define (read-atom port)
|
||||||
|
(let loop ((chars (list)))
|
||||||
|
(let ((c (peek port)))
|
||||||
|
(if (delimiter? c)
|
||||||
|
(finish-atom (list->string (reverse chars)))
|
||||||
|
(begin (get-ch port) (loop (cons c chars)))))))
|
||||||
|
|
||||||
|
(define (finish-atom s)
|
||||||
|
(cond
|
||||||
|
((string=? s ".") dot-token)
|
||||||
|
((string->number s) => identity)
|
||||||
|
(else (string->symbol s))))
|
||||||
|
|
||||||
|
;;; #-dispatch: booleans, characters, vectors, block/datum comments
|
||||||
|
(define (read-hash port)
|
||||||
|
(let ((c (get-ch port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c) (error "Unexpected end of input after #"))
|
||||||
|
((or (char=? c #\t) (char=? c #\T)) (read-bool port #t))
|
||||||
|
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
||||||
|
((char=? c #\\) (read-char-lit port))
|
||||||
|
((char=? c #\() (list->vector (read-list port close-paren)))
|
||||||
|
((char=? c #\|) (skip-block-comment port 1) (next-token port))
|
||||||
|
((char=? c #\;) (read-datum port) (next-token port)) ; datum comment
|
||||||
|
(else (error "Unsupported # syntax" c)))))
|
||||||
|
|
||||||
|
;;; #t / #true / #f / #false. `val' is the boolean; consume any
|
||||||
|
;;; trailing name characters and validate
|
||||||
|
(define (read-bool port val)
|
||||||
|
(let ((rest (read-atom-string port)))
|
||||||
|
(cond
|
||||||
|
((string=? rest "") val)
|
||||||
|
((and val (string=? rest "rue")) val)
|
||||||
|
((and (not val) (string=? rest "alse")) val)
|
||||||
|
(else (error "Malformed boolean literal" rest)))))
|
||||||
|
|
||||||
|
(define (read-atom-string port)
|
||||||
|
(let loop ((chars (list)))
|
||||||
|
(let ((c (peek port)))
|
||||||
|
(if (delimiter? c)
|
||||||
|
(list->string (reverse chars))
|
||||||
|
(begin (get-ch port) (loop (cons c chars)))))))
|
||||||
|
|
||||||
|
(define named-chars
|
||||||
|
'(("space" . #\space) ("newline" . #\newline) ("tab" . #\tab)
|
||||||
|
("return" . #\return) ("nul" . #\nul) ("null" . #\nul)
|
||||||
|
("delete" . #\delete) ("escape" . #\escape) ("alarm" . #\alarm)
|
||||||
|
("backspace" . #\backspace)))
|
||||||
|
|
||||||
|
(define (read-char-lit port)
|
||||||
|
(let ((first (get-ch port)))
|
||||||
|
(when (eof-object? first)
|
||||||
|
(error "Unexpected end of input in character literal"))
|
||||||
|
(if (char-alphabetic? first)
|
||||||
|
(let ((rest (read-atom-string port)))
|
||||||
|
(if (string=? rest "")
|
||||||
|
first
|
||||||
|
(let ((name (string-append (string first) rest)))
|
||||||
|
(cond
|
||||||
|
((assoc name named-chars) => cdr)
|
||||||
|
(else (error "Unknown character name" name))))))
|
||||||
|
first)))
|
||||||
|
|
||||||
|
;;; String literal with escape processing, matching the common escapes
|
||||||
|
;;; the previous reader (CHICKEN `read') interpreted
|
||||||
|
(define (read-string-lit port)
|
||||||
|
(get-ch port) ; consume opening quote
|
||||||
|
(let loop ((chars (list)))
|
||||||
|
(let ((c (get-ch port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c) (error "Unterminated string literal"))
|
||||||
|
((char=? c #\") (list->string (reverse chars)))
|
||||||
|
((char=? c #\\) (loop (cons (read-escape port) chars)))
|
||||||
|
(else (loop (cons c chars)))))))
|
||||||
|
|
||||||
|
(define (read-escape port)
|
||||||
|
(let ((c (get-ch port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c) (error "Unterminated string literal"))
|
||||||
|
((char=? c #\n) #\newline)
|
||||||
|
((char=? c #\t) #\tab)
|
||||||
|
((char=? c #\r) #\return)
|
||||||
|
((char=? c #\a) #\alarm)
|
||||||
|
((char=? c #\b) #\backspace)
|
||||||
|
((char=? c #\f) (integer->char 12))
|
||||||
|
((char=? c #\v) (integer->char 11))
|
||||||
|
((char=? c #\0) #\nul)
|
||||||
|
(else c)))) ; \" \\ and anything else: literal
|
||||||
|
|
||||||
|
(define (skip-block-comment port depth)
|
||||||
|
(if (= depth 0)
|
||||||
|
#t
|
||||||
|
(let ((c (get-ch port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? c) (error "Unterminated block comment"))
|
||||||
|
((and (char=? c #\|) (eqv? (peek port) #\#))
|
||||||
|
(get-ch port) (skip-block-comment port (- depth 1)))
|
||||||
|
((and (char=? c #\#) (eqv? (peek port) #\|))
|
||||||
|
(get-ch port) (skip-block-comment port (+ depth 1)))
|
||||||
|
(else (skip-block-comment port depth))))))
|
||||||
|
|
||||||
|
;;; Read every top-level form from `port'. Locations are recorded by
|
||||||
|
;;; next-token, for every form rather than only these
|
||||||
|
(define (parse-all port)
|
||||||
|
(parameterize ((current-line 1))
|
||||||
|
(let loop ((acc (list)))
|
||||||
|
(skip-whitespace port)
|
||||||
|
(let ((tok (next-token port)))
|
||||||
|
(cond
|
||||||
|
((eof-object? tok) (reverse acc))
|
||||||
|
((or (eq? tok close-paren)
|
||||||
|
(eq? tok close-bracket))
|
||||||
|
(error "Unmatched closing bracket at top level"))
|
||||||
|
((eq? tok dot-token)
|
||||||
|
(error "Unexpected . at top level"))
|
||||||
|
(else
|
||||||
|
(loop (cons tok acc))))))))
|
||||||
|
|
||||||
|
;;; Entry point: read all forms from a file, or from the current input
|
||||||
|
;;; port when the source is 'stdin
|
||||||
(define (read-from-file file)
|
(define (read-from-file file)
|
||||||
(with-directory file
|
;; Resolve the name before with-directory moves us, so an imported
|
||||||
(with-input-from-file (pathname-strip-directory file)
|
;; module's forms carry that module's path rather than the importer's
|
||||||
(fn (read-forms (list))))))
|
(let ((source-file (to-absolute-pathname file)))
|
||||||
|
(with-directory file
|
||||||
(define (read-bracket port)
|
(with-input-from-file (pathname-strip-directory file)
|
||||||
(let loop ((c (read-char port))
|
(lambda ()
|
||||||
(str (string)))
|
(parameterize ((current-source-file source-file))
|
||||||
(cond ((char=? c #\])
|
(parse-all (current-input-port))))))))
|
||||||
(cons '¤
|
|
||||||
(with-input-from-string str
|
|
||||||
(fn (port-map identity read)))))
|
|
||||||
((char=? c #\[)
|
|
||||||
(loop port (conc )))
|
|
||||||
(else
|
|
||||||
(loop (read-char port)
|
|
||||||
(conc str c))))))
|
|
||||||
|
|
||||||
(define open-bracket-counter (make-parameter 0))
|
|
||||||
|
|
||||||
(define (read-raw-forms input-source)
|
(define (read-raw-forms input-source)
|
||||||
(let ((bracket-end (gensym)))
|
|
||||||
(set-read-syntax!
|
|
||||||
#\]
|
|
||||||
(lambda (port)
|
|
||||||
(when (= 0 (open-bracket-counter))
|
|
||||||
(error "Unmatched closing bracket"))
|
|
||||||
(open-bracket-counter (- (open-bracket-counter) 1))
|
|
||||||
bracket-end))
|
|
||||||
|
|
||||||
(set-read-syntax!
|
|
||||||
#\[
|
|
||||||
(lambda (port)
|
|
||||||
(open-bracket-counter (+ (open-bracket-counter) 1))
|
|
||||||
(let loop ((r (read port))
|
|
||||||
(acc (list)))
|
|
||||||
(if (eq? r bracket-end)
|
|
||||||
(cons '¤ (reverse acc))
|
|
||||||
(loop (read port)
|
|
||||||
(cons r acc)))))))
|
|
||||||
(if (eq? input-source 'stdin)
|
(if (eq? input-source 'stdin)
|
||||||
(read-forms (list))
|
(parameterize ((current-source-file "stdin"))
|
||||||
|
(parse-all (current-input-port)))
|
||||||
(read-from-file input-source)))
|
(read-from-file input-source)))
|
||||||
|
|||||||
70
semen.scm
70
semen.scm
@@ -9,6 +9,7 @@
|
|||||||
fmt
|
fmt
|
||||||
sex-macros
|
sex-macros
|
||||||
sex-modules
|
sex-modules
|
||||||
|
types
|
||||||
matchable ; pattern matching
|
matchable ; pattern matching
|
||||||
srfi-1 ; list routines
|
srfi-1 ; list routines
|
||||||
srfi-69 ; hash tables
|
srfi-69 ; hash tables
|
||||||
@@ -47,10 +48,15 @@
|
|||||||
;;
|
;;
|
||||||
;; Single form we just cons to the top of rest-forms, but multiple
|
;; Single form we just cons to the top of rest-forms, but multiple
|
||||||
;; forms have to be appended to the rest-forms.
|
;; forms have to be appended to the rest-forms.
|
||||||
(let ((res (apply-macro macro-form)))
|
(let ((res (apply-macro macro-form))
|
||||||
|
(src (form-source macro-form)))
|
||||||
|
;; An expansion is fresh structure with no location of its own. Give
|
||||||
|
;; it the call site's, the way cpp attributes a macro body to where
|
||||||
|
;; the macro was used
|
||||||
(if (list? (car res))
|
(if (list? (car res))
|
||||||
(append res rest-forms)
|
(append (map (lambda (f) (stamp-form-source! f src)) res)
|
||||||
(cons res rest-forms))))
|
rest-forms)
|
||||||
|
(cons (stamp-form-source! res src) rest-forms))))
|
||||||
|
|
||||||
(define (match-sex-form sex-form acc)
|
(define (match-sex-form sex-form acc)
|
||||||
(match sex-form
|
(match sex-form
|
||||||
@@ -67,7 +73,11 @@
|
|||||||
('pub 'var . _)
|
('pub 'var . _)
|
||||||
('extern 'var . _)) (process-global-var sex-form acc))
|
('extern 'var . _)) (process-global-var sex-form acc))
|
||||||
(('include _) (cons sex-form acc))
|
(('include _) (cons sex-form acc))
|
||||||
(('define . _) (cons sex-form acc))
|
((or ('define name . _)
|
||||||
|
('pub 'define name . _))
|
||||||
|
(add-define name sex-form)
|
||||||
|
(cons sex-form acc))
|
||||||
|
(('comment . _) (cons sex-form acc))
|
||||||
|
|
||||||
(('import . modules)
|
(('import . modules)
|
||||||
(process-imports (get-modules-public-forms modules) acc))
|
(process-imports (get-modules-public-forms modules) acc))
|
||||||
@@ -77,9 +87,10 @@
|
|||||||
|
|
||||||
((or ('typedef new-type target)
|
((or ('typedef new-type target)
|
||||||
('pub 'typedef new-type target))
|
('pub 'typedef new-type target))
|
||||||
(process-typedef new-type target acc))
|
(add-typedef new-type sex-form)
|
||||||
|
(process-typedef sex-form new-type target acc))
|
||||||
|
|
||||||
(else (assert #f (fmt #f "Unknown top level form " sex-form)))))
|
(else (sex-error sex-form "unknown top level form" sex-form))))
|
||||||
|
|
||||||
(define (process-imports module-public-forms acc)
|
(define (process-imports module-public-forms acc)
|
||||||
;; Recursively process imports: register public macros, cons all
|
;; Recursively process imports: register public macros, cons all
|
||||||
@@ -133,13 +144,34 @@
|
|||||||
|
|
||||||
;;; Typdef
|
;;; Typdef
|
||||||
|
|
||||||
(define (process-typedef new-type target acc)
|
(define (process-typedef form new-type target acc)
|
||||||
(cons `(typedef ,target ,new-type) acc))
|
(cons (copy-form-source! form `(typedef ,target ,new-type)) acc))
|
||||||
|
|
||||||
;;; Fn processing
|
;;; Fn processing
|
||||||
|
|
||||||
(define (process-fn sex-fn acc)
|
(define (comment-form? f)
|
||||||
(let* ((expanded (macro-expand sex-fn))
|
(and (pair? f) (eq? (car f) 'comment)))
|
||||||
|
|
||||||
|
(define (strip-fn-header-comments fn-form)
|
||||||
|
;; Remove comment forms from the function header
|
||||||
|
;; ([pub|extern] fn name arglist rettype) so the positional accessors
|
||||||
|
;; below are not shifted. Comments in the body are left in place as
|
||||||
|
;; ordinary statements and preserved into the generated C.
|
||||||
|
(let ((header-count (if (memq (car fn-form) '(pub extern)) 5 4)))
|
||||||
|
;; This always rebuilds the list, so the location has to be carried
|
||||||
|
;; over explicitly -- otherwise every function loses it
|
||||||
|
(copy-form-source!
|
||||||
|
fn-form
|
||||||
|
(let loop ((form fn-form) (kept 0) (acc (list)))
|
||||||
|
(cond
|
||||||
|
((null? form) (reverse acc))
|
||||||
|
((= kept header-count) (append (reverse acc) form))
|
||||||
|
((comment-form? (car form)) (loop (cdr form) kept acc))
|
||||||
|
(else (loop (cdr form) (+ kept 1) (cons (car form) acc))))))))
|
||||||
|
|
||||||
|
(define (process-fn sex-fn-raw acc)
|
||||||
|
(let* ((sex-fn (strip-fn-header-comments sex-fn-raw))
|
||||||
|
(expanded (macro-expand sex-fn))
|
||||||
(env (make-hash-table))
|
(env (make-hash-table))
|
||||||
(processed
|
(processed
|
||||||
(walk-form
|
(walk-form
|
||||||
@@ -175,14 +207,28 @@
|
|||||||
(('lambda ret-type arglist captures . body)
|
(('lambda ret-type arglist captures . body)
|
||||||
;; Captures are ignored for now, but
|
;; Captures are ignored for now, but
|
||||||
;; we'll need them for TODO: closures support
|
;; we'll need them for TODO: closures support
|
||||||
(process-fn `(fn ,ret-type ,name ,arglist ,@body) (list)))
|
(process-fn (copy-form-source! form `(fn ,ret-type ,name ,arglist ,@body))
|
||||||
(else (assert #f (fmt #f "Malformed lambda " form)))))
|
(list)))
|
||||||
|
(else (sex-error form "malformed lambda" form))))
|
||||||
|
|
||||||
;;; Structs
|
;;; Structs
|
||||||
|
|
||||||
|
;;; Record the named structs, unions and enums in the type database
|
||||||
(define (process-struct sex-struct acc)
|
(define (process-struct sex-struct acc)
|
||||||
|
(register-aggregate! sex-struct)
|
||||||
(cons sex-struct acc))
|
(cons sex-struct acc))
|
||||||
|
|
||||||
|
(define (register-aggregate! form)
|
||||||
|
(let* ((f (if (eq? (car form) 'pub) (cdr form) form))
|
||||||
|
(name (and (pair? (cdr f)) (symbol? (cadr f)) (cadr f))))
|
||||||
|
;; An anonymous aggregate has a field list where the name would be,
|
||||||
|
;; and nothing can refer to it by name anyway
|
||||||
|
(when name
|
||||||
|
(case (car f)
|
||||||
|
((struct) (add-struct name form))
|
||||||
|
((union) (add-union name form))
|
||||||
|
((enum) (add-enum name form))))))
|
||||||
|
|
||||||
(define (process-global-var sex-var acc)
|
(define (process-global-var sex-var acc)
|
||||||
(cons sex-var acc))
|
(cons sex-var acc))
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
|
|
||||||
(import
|
(import
|
||||||
scheme
|
scheme
|
||||||
|
(scheme base) ; string->utf8, bytevector accessors
|
||||||
(chicken base)
|
(chicken base)
|
||||||
fmt
|
fmt
|
||||||
srfi-1
|
srfi-1
|
||||||
@@ -138,7 +139,31 @@
|
|||||||
(case n
|
(case n
|
||||||
((7) "\\a") ((8) "\\b") ((9) "\\t") ((10) "\\n")
|
((7) "\\a") ((8) "\\b") ((9) "\\t") ((10) "\\n")
|
||||||
((11) "\\v") ((12) "\\f") ((13) "\\r")
|
((11) "\\v") ((12) "\\f") ((13) "\\r")
|
||||||
(else (string-append "\\x" (number->string (char->integer c) 16)))))))
|
(else (c-octal-escapes c))))))
|
||||||
|
|
||||||
|
;; A character outside printable ASCII, as one three-digit octal escape
|
||||||
|
;; per UTF-8 byte.
|
||||||
|
;;
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c, which emits "\x" plus the hex of the
|
||||||
|
;; character. C's \x escape consumes *every* following hex digit, so
|
||||||
|
;; "\xc3\xa91" is a single out-of-range value rather than two bytes
|
||||||
|
;; followed by a '1'. Under CHICKEN 6 a character is a codepoint rather
|
||||||
|
;; than a byte, so the hex form additionally emits "\xe9" for a lone
|
||||||
|
;; "e-acute" -- not valid UTF-8 -- and "\x65e5" for anything above the
|
||||||
|
;; Latin-1 range, which does not compile at all. Octal escapes are
|
||||||
|
;; capped at three digits, so they always self-terminate.
|
||||||
|
(define (c-octal-escapes c)
|
||||||
|
(let* ((bytes (string->utf8 (string c)))
|
||||||
|
(len (bytevector-length bytes)))
|
||||||
|
(let loop ((i 0) (acc '()))
|
||||||
|
(if (= i len)
|
||||||
|
(apply string-append (reverse acc))
|
||||||
|
(loop (+ i 1)
|
||||||
|
(cons (let ((oct (number->string (bytevector-u8-ref bytes i) 8)))
|
||||||
|
(string-append "\\"
|
||||||
|
(make-string (- 3 (string-length oct)) #\0)
|
||||||
|
oct))
|
||||||
|
acc))))))
|
||||||
|
|
||||||
(define (c-format-number x)
|
(define (c-format-number x)
|
||||||
(if (and (integer? x) (exact? x))
|
(if (and (integer? x) (exact? x))
|
||||||
@@ -762,8 +787,32 @@
|
|||||||
(fmt-join c-expr name ", ")
|
(fmt-join c-expr name ", ")
|
||||||
(c-expr name))))))
|
(c-expr name))))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: parenthesise a binary operand.
|
||||||
|
;;
|
||||||
|
;; A cast binds tighter than every binary operator, so upstream's
|
||||||
|
;; unparenthesised (cat "(" type ")" expr) silently casts only the
|
||||||
|
;; first operand: (cast (* 2 (sizeof GLfloat)) (* void)) came out as
|
||||||
|
;; `(void *)2 * sizeof(GLfloat)'. Unary operands -- &x, *p, -n,
|
||||||
|
;; sizeof, calls, identifiers, literals -- are already
|
||||||
|
;; unary-expressions and are left alone, so this changes nothing that
|
||||||
|
;; was previously correct.
|
||||||
|
(define c-binary-operators
|
||||||
|
'(+ - * / % << >> < > <= >= == != & ^ && %and %or
|
||||||
|
= *= /= %= &= ^= <<= >>=))
|
||||||
|
|
||||||
|
(define (c-binary-expression? expr)
|
||||||
|
(and (pair? expr)
|
||||||
|
(memq (car expr) c-binary-operators)
|
||||||
|
(pair? (cdr expr))
|
||||||
|
(pair? (cddr expr)))) ; two or more operands
|
||||||
|
|
||||||
(define (c-cast type expr)
|
(define (c-cast type expr)
|
||||||
(cat "(" (c-type type) ")" (c-expr expr)))
|
(cat "(" (c-type type) ")"
|
||||||
|
(if (c-binary-expression? expr)
|
||||||
|
;; c-with-op stops the operand from parenthesising itself
|
||||||
|
;; a second time inside the parens we just added.
|
||||||
|
(cat "(" (c-with-op 'paren (c-expr expr)) ")")
|
||||||
|
(c-expr expr))))
|
||||||
|
|
||||||
(define (c-typedef type alias . o)
|
(define (c-typedef type alias . o)
|
||||||
(c-wrap-stmt
|
(c-wrap-stmt
|
||||||
@@ -820,8 +869,7 @@
|
|||||||
|
|
||||||
(define (c-label name)
|
(define (c-label name)
|
||||||
(lambda (st)
|
(lambda (st)
|
||||||
(let ((indent (make-space (max 0 (- (fmt-col st) 2)))))
|
((cat name ":" fl) st)))
|
||||||
((cat fl indent name ":" fl) st))))
|
|
||||||
|
|
||||||
(define c-break
|
(define c-break
|
||||||
(c-wrap-stmt (dsp "break")))
|
(c-wrap-stmt (dsp "break")))
|
||||||
@@ -837,7 +885,12 @@
|
|||||||
(define (c-switch val . clauses)
|
(define (c-switch val . clauses)
|
||||||
(c-reset-newline
|
(c-reset-newline
|
||||||
(lambda (st)
|
(lambda (st)
|
||||||
((cat "switch (" (c-in-expr val) ")" (c-open-brace st)
|
;; MODIFIED FROM UPSTREAM fmt-c: c-expr on the scrutinee.
|
||||||
|
;; Upstream passes `val' straight to cat, which only works when
|
||||||
|
;; it is an atom -- anything else (a member access, a call) is
|
||||||
|
;; displayed as a raw s-expression instead of being rendered.
|
||||||
|
;; c-while and c-for already do (c-in-test (c-expr check)).
|
||||||
|
((cat "switch (" (c-in-expr (c-expr val)) ")" (c-open-brace st)
|
||||||
(c-indent/switch st)
|
(c-indent/switch st)
|
||||||
(c-in-stmt (apply c-begin/aux #t (map c-switch-clause clauses))) fl
|
(c-in-stmt (apply c-begin/aux #t (map c-switch-clause clauses))) fl
|
||||||
(c-current-indent-string st) (c-close-brace st) fl)
|
(c-current-indent-string st) (c-close-brace st) fl)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
(module sex-macros
|
(module sex-macros
|
||||||
(register-macro
|
(register-macro
|
||||||
cat
|
cat
|
||||||
|
comment
|
||||||
get-macro
|
get-macro
|
||||||
macro?
|
macro?
|
||||||
apply-macro
|
apply-macro
|
||||||
|
|||||||
@@ -11,11 +11,23 @@
|
|||||||
(define (cat sym-1 sym-2)
|
(define (cat sym-1 sym-2)
|
||||||
(string->symbol (cat-syms sym-1 sym-2)))
|
(string->symbol (cat-syms sym-1 sym-2)))
|
||||||
|
|
||||||
|
;;; The reader keeps `;' comments as (comment "...") forms so they can
|
||||||
|
;;; be re-emitted into the generated C. In a macro body a comment
|
||||||
|
;;; should be a call which does nothing, hence this one
|
||||||
|
(define (comment . _)
|
||||||
|
(void))
|
||||||
|
|
||||||
(define (register-macro name arglist body)
|
(define (register-macro name arglist body)
|
||||||
(put! name 'sex-macro
|
(put! name 'sex-macro
|
||||||
`(lambda ,arglist
|
`(lambda ,arglist
|
||||||
|
;; A macro body is ordinary Scheme, evaluated at compile
|
||||||
|
;; time. It gets `cat' for building names, and read access to
|
||||||
|
;; the type database
|
||||||
(import scheme
|
(import scheme
|
||||||
(only sex-macros cat))
|
(scheme base)
|
||||||
|
(only sex-macros cat comment)
|
||||||
|
(only types get-type-info get-fields get-underlying-type
|
||||||
|
type-match map-fields))
|
||||||
,@body)))
|
,@body)))
|
||||||
|
|
||||||
(define (get-macro name)
|
(define (get-macro name)
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
|
|||||||
;; Keywords
|
;; Keywords
|
||||||
(list (concat "("
|
(list (concat "("
|
||||||
(regexp-opt '(
|
(regexp-opt '(
|
||||||
"begin"
|
"do"
|
||||||
"case"
|
"case"
|
||||||
"default"
|
"default"
|
||||||
"do"
|
"do"
|
||||||
|
|||||||
@@ -68,13 +68,16 @@
|
|||||||
(case (car form)
|
(case (car form)
|
||||||
((pub)
|
((pub)
|
||||||
(case (cadr form)
|
(case (cadr form)
|
||||||
((fn) ; replace with prototype
|
;; A function is reduced to a prototype and keeps its `pub', so
|
||||||
;; fn type name (arg-list) (body)
|
;; the importing unit declares it with external linkage
|
||||||
;; 1 2 3 4 - we need first 4
|
((fn)
|
||||||
(cons (take (cdr form) 4) acc))
|
(cons (copy-form-source! form (take form 5)) acc))
|
||||||
((define defmacro import include struct typedef union var)
|
;; A variable becomes an `extern' declaration
|
||||||
(cons (cdr form) acc))
|
((var)
|
||||||
(else (error "Pub what? " (cadr form)))))
|
(cons (copy-form-source! form (cons 'extern (take (cdr form) 3))) acc))
|
||||||
|
((define defmacro import include struct typedef union)
|
||||||
|
(cons (copy-form-source! form (cdr form)) acc))
|
||||||
|
(else (sex-error form "pub must be followed by a definition" form))))
|
||||||
(else acc)))
|
(else acc)))
|
||||||
|
|
||||||
(define (load-persistent-module-paths)
|
(define (load-persistent-module-paths)
|
||||||
|
|||||||
80
sexc.scm
80
sexc.scm
@@ -1,4 +1,5 @@
|
|||||||
(import scheme
|
(import scheme
|
||||||
|
(scheme base) ; call/cc
|
||||||
brev-separate
|
brev-separate
|
||||||
(chicken base)
|
(chicken base)
|
||||||
(chicken file)
|
(chicken file)
|
||||||
@@ -15,8 +16,6 @@
|
|||||||
reader
|
reader
|
||||||
semen
|
semen
|
||||||
srfi-1 ; list routines
|
srfi-1 ; list routines
|
||||||
srfi-13
|
|
||||||
tree
|
|
||||||
utils)
|
utils)
|
||||||
|
|
||||||
;;; Main function facilities
|
;;; Main function facilities
|
||||||
@@ -50,7 +49,14 @@
|
|||||||
(pad padding) "If -E or -m options are provided, defaults to stdout")
|
(pad padding) "If -E or -m options are provided, defaults to stdout")
|
||||||
(required #f)
|
(required #f)
|
||||||
(value #t)
|
(value #t)
|
||||||
(single-char #\o)))))
|
(single-char #\o))
|
||||||
|
(line-directives
|
||||||
|
,(fmt #f "How much #line information to emit: statement (default)," nl
|
||||||
|
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
|
||||||
|
(pad padding) "land on the right source line; `none' is for reading -C" nl
|
||||||
|
(pad padding) "output by eye")
|
||||||
|
(required #f)
|
||||||
|
(value #t)))))
|
||||||
|
|
||||||
(define (print-help)
|
(define (print-help)
|
||||||
(fmt #t "Usage: sexc [options] filename [-- options-for-c-compiler]\n")
|
(fmt #t "Usage: sexc [options] filename [-- options-for-c-compiler]\n")
|
||||||
@@ -66,11 +72,30 @@
|
|||||||
(if arg (cdr arg)
|
(if arg (cdr arg)
|
||||||
default)))
|
default)))
|
||||||
|
|
||||||
|
;;; Everything before the first `--' is ours to parse, everything after
|
||||||
|
;;; is handed to the C compiler verbatim
|
||||||
|
|
||||||
|
(define (separator? a)
|
||||||
|
(string=? a "--"))
|
||||||
|
|
||||||
|
(define (args-before-separator argv)
|
||||||
|
(take-while (complement separator?) argv))
|
||||||
|
|
||||||
|
(define (args-after-separator argv)
|
||||||
|
(let ((tail (drop-while (complement separator?) argv)))
|
||||||
|
(if (null? tail)
|
||||||
|
(list)
|
||||||
|
(cdr tail))))
|
||||||
|
|
||||||
(define (get-rest-args args)
|
(define (get-rest-args args)
|
||||||
(cdr (assoc '@ args)))
|
(cdr (assoc '@ args)))
|
||||||
|
|
||||||
(define (get-c-compiler-args args)
|
(define (line-directives-arg args)
|
||||||
(filter (fn (string-prefix? "-" x)) (get-rest-args args)))
|
(let ((v (get-arg args 'line-directives "statement")))
|
||||||
|
(cond ((equal? v "statement") 'statement)
|
||||||
|
((equal? v "toplevel") 'toplevel)
|
||||||
|
((equal? v "none") 'none)
|
||||||
|
(else (error "--line-directives must be statement, toplevel or none, got" v)))))
|
||||||
|
|
||||||
(define (get-input-file args)
|
(define (get-input-file args)
|
||||||
(let ((rest-args (get-rest-args args)))
|
(let ((rest-args (get-rest-args args)))
|
||||||
@@ -91,26 +116,28 @@
|
|||||||
(map pp sex-forms)
|
(map pp sex-forms)
|
||||||
(emit-c sex-forms)))))
|
(emit-c sex-forms)))))
|
||||||
|
|
||||||
(define (compile-to-file sex-forms output args)
|
(define (compile-to-file sex-forms output args cc-args)
|
||||||
(let ((compiler (or (get-arg args 'c-compiler #f)
|
(let ((compiler (or (get-arg args 'c-compiler #f)
|
||||||
(get-env-var "SEX_CC")
|
(get-env-var "SEX_CC")
|
||||||
"cc"))
|
"cc"))
|
||||||
(out-file (if (eq? output 'default)
|
(out-file (if (eq? output 'default)
|
||||||
"a.out"
|
"a.out"
|
||||||
output)))
|
output)))
|
||||||
(call-with-values
|
;; The generated C goes to a temporary .c file rather than the
|
||||||
(lambda ()
|
;; compiler's stdin
|
||||||
(process compiler (append (list "-o" out-file "-x" "c")
|
(let ((c-file (create-temporary-file "c")))
|
||||||
(if (get-arg args 'compile-object #f)
|
(with-output-to-file c-file
|
||||||
(list "-c")
|
(lambda () (emit-c sex-forms)))
|
||||||
(list))
|
(let ((proc (process compiler (append (list "-o" out-file)
|
||||||
(list "-") ; read stdin
|
(if (get-arg args 'compile-object #f)
|
||||||
(get-c-compiler-args args))))
|
(list "-c")
|
||||||
(lambda (out-port in-port pid)
|
(list))
|
||||||
(with-output-to-port in-port
|
(list c-file)
|
||||||
(lambda () (emit-c sex-forms)))
|
cc-args))))
|
||||||
(close-output-port in-port)
|
(call-with-values (lambda () (process-wait proc))
|
||||||
(process-wait pid)))))
|
(lambda status
|
||||||
|
(delete-file* c-file)
|
||||||
|
(apply values status)))))))
|
||||||
|
|
||||||
(define (semantic-process-forms raw-forms input-source)
|
(define (semantic-process-forms raw-forms input-source)
|
||||||
(if (eq? input-source 'stdin)
|
(if (eq? input-source 'stdin)
|
||||||
@@ -131,7 +158,9 @@
|
|||||||
(typedef i64 int64-t)))
|
(typedef i64 int64-t)))
|
||||||
|
|
||||||
(define (main)
|
(define (main)
|
||||||
(let* ((raw-args (command-line-arguments))
|
(let* ((argv (command-line-arguments))
|
||||||
|
(raw-args (args-before-separator argv))
|
||||||
|
(cc-args (args-after-separator argv))
|
||||||
(args (getopt-long raw-args
|
(args (getopt-long raw-args
|
||||||
opts-grammar))
|
opts-grammar))
|
||||||
(output (get-arg args 'output 'default))
|
(output (get-arg args 'output 'default))
|
||||||
@@ -155,7 +184,14 @@
|
|||||||
(return #f))
|
(return #f))
|
||||||
(load-persistent-module-paths)
|
(load-persistent-module-paths)
|
||||||
|
|
||||||
(sex-fmt-current-file (to-absolute-pathname input))
|
;; The file name in a #line directive now comes from the form's
|
||||||
|
;; own recorded location, so imported modules report themselves
|
||||||
|
;; rather than the unit that imported them
|
||||||
|
(sex-line-directives (line-directives-arg args))
|
||||||
|
(when (and (get-arg args 'emit-c #f)
|
||||||
|
(not (get-arg args 'line-directives #f)))
|
||||||
|
(sex-line-directives 'none))
|
||||||
|
|
||||||
(let* ((raw-forms (append prelude (read-raw-forms input)))
|
(let* ((raw-forms (append prelude (read-raw-forms input)))
|
||||||
(sex-forms (semantic-process-forms raw-forms input)))
|
(sex-forms (semantic-process-forms raw-forms input)))
|
||||||
(if (or (get-arg args 'macro-expand #f)
|
(if (or (get-arg args 'macro-expand #f)
|
||||||
@@ -163,4 +199,4 @@
|
|||||||
;; Emit processed and macro-expanded sex code, or emit C code
|
;; Emit processed and macro-expanded sex code, or emit C code
|
||||||
(emit-c-or-sex sex-forms output args)
|
(emit-c-or-sex sex-forms output args)
|
||||||
;; Compile file!
|
;; Compile file!
|
||||||
(compile-to-file sex-forms output args)))))))
|
(compile-to-file sex-forms output args cc-args)))))))
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ CHICKEN_C = csc
|
|||||||
CSC_FLAGS += -K prefix -static
|
CSC_FLAGS += -K prefix -static
|
||||||
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
||||||
|
|
||||||
MODULES = utils sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
|
MODULES = utils types sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
|
||||||
SEX_OBJ = $(MODULES:%=%.o)
|
SEX_OBJ = $(MODULES:%=%.o)
|
||||||
|
|
||||||
TESTS = basic semen reader fmt-c-writer utils
|
TESTS = basic semen reader fmt-c-writer utils line-directives codegen args types
|
||||||
TEST_SRCS = $(TESTS:%=%.scm)
|
TEST_SRCS = $(TESTS:%=%.scm)
|
||||||
|
|
||||||
sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ)
|
sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ)
|
||||||
@@ -17,17 +17,20 @@ sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ)
|
|||||||
utils.o: utils.module.scm ../utils.scm
|
utils.o: utils.module.scm ../utils.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
|
||||||
|
|
||||||
|
types.o: types.module.scm ../types.scm
|
||||||
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types
|
||||||
|
|
||||||
sex-macros.o: sex-macros.module.scm ../sex-macros.scm
|
sex-macros.o: sex-macros.module.scm ../sex-macros.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
|
||||||
|
|
||||||
reader.o: reader.module.scm ../reader.scm
|
reader.o: reader.module.scm ../reader.scm utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
|
||||||
|
|
||||||
sex-modules.o: sex-modules.module.scm ../sex-modules.scm reader.o utils.o
|
sex-modules.o: sex-modules.module.scm ../sex-modules.scm reader.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,utils
|
||||||
|
|
||||||
semen.o: semen.module.scm ../semen.scm sex-macros.o sex-modules.o utils.o
|
semen.o: semen.module.scm ../semen.scm sex-macros.o sex-modules.o types.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,types,utils
|
||||||
|
|
||||||
sex-fmt-c.o: ../sex-fmt-c.scm
|
sex-fmt-c.o: ../sex-fmt-c.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) ../sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) ../sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
|
||||||
@@ -35,11 +38,11 @@ sex-fmt-c.o: ../sex-fmt-c.scm
|
|||||||
fmt-c-writer.o: fmt-c-writer.module.scm ../fmt-c-writer.scm sex-fmt-c.o utils.o
|
fmt-c-writer.o: fmt-c-writer.module.scm ../fmt-c-writer.scm sex-fmt-c.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,utils
|
||||||
|
|
||||||
sexc.o: sexc.module.scm ../sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
|
sexc.o: sexc.module.scm types.o ../sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,utils
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,types,utils
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJ)
|
rm -f $(SEX_OBJ)
|
||||||
rm -f *.import.scm
|
rm -f *.import.scm
|
||||||
rm -f *.link
|
rm -f *.link
|
||||||
rm -f sex-tests
|
rm -f sex-tests
|
||||||
|
|||||||
55
tests/args.scm
Normal file
55
tests/args.scm
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
;;; Splitting the command line at `--'.
|
||||||
|
;;;
|
||||||
|
;;; getopt-long cannot do this: it consumes the separator and merges
|
||||||
|
;;; everything after it into `@' alongside the input file. So sexc
|
||||||
|
;;; splits the raw argv first, and only the head is parsed as options.
|
||||||
|
;;; Everything else reaches the C compiler exactly as written --
|
||||||
|
;;; including the words that do not start with a dash, which a previous
|
||||||
|
;;; leading-dash heuristic used to drop.
|
||||||
|
|
||||||
|
(import sexc)
|
||||||
|
|
||||||
|
(define full '("foo.sex" "-o" "bar" "--" "-framework" "OpenGL" "-Wall"))
|
||||||
|
|
||||||
|
(test-group "argument separator"
|
||||||
|
|
||||||
|
(test "options and input file stay with sexc"
|
||||||
|
'("foo.sex" "-o" "bar")
|
||||||
|
(args-before-separator full))
|
||||||
|
|
||||||
|
(test "the tail reaches the compiler verbatim"
|
||||||
|
'("-framework" "OpenGL" "-Wall")
|
||||||
|
(args-after-separator full))
|
||||||
|
|
||||||
|
;; The case that motivated this: `OpenGL' has no leading dash and was
|
||||||
|
;; silently dropped, leaving `-framework' to swallow whatever flag
|
||||||
|
;; came next.
|
||||||
|
(test "a word without a dash survives"
|
||||||
|
'("-framework" "OpenGL")
|
||||||
|
(args-after-separator '("x.sex" "--" "-framework" "OpenGL")))
|
||||||
|
|
||||||
|
(test "no separator means nothing for the compiler"
|
||||||
|
'()
|
||||||
|
(args-after-separator '("foo.sex" "-o" "bar")))
|
||||||
|
|
||||||
|
(test "no separator leaves every argument with sexc"
|
||||||
|
'("foo.sex" "-o" "bar")
|
||||||
|
(args-before-separator '("foo.sex" "-o" "bar")))
|
||||||
|
|
||||||
|
(test "a trailing separator is allowed"
|
||||||
|
'()
|
||||||
|
(args-after-separator '("foo.sex" "--")))
|
||||||
|
|
||||||
|
(test "a leading separator leaves no input file"
|
||||||
|
'()
|
||||||
|
(args-before-separator '("--" "-lm")))
|
||||||
|
|
||||||
|
;; Only the first `--' separates; a later one is an ordinary compiler
|
||||||
|
;; argument (ld takes several).
|
||||||
|
(test "only the first separator counts"
|
||||||
|
'("-Wl,--as-needed" "--" "-lm")
|
||||||
|
(args-after-separator '("x.sex" "--" "-Wl,--as-needed" "--" "-lm")))
|
||||||
|
|
||||||
|
(test "an empty command line is handled"
|
||||||
|
'()
|
||||||
|
(args-before-separator '())))
|
||||||
@@ -13,10 +13,18 @@
|
|||||||
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
||||||
(test '__->>> (unkebabify '--->>>))
|
(test '__->>> (unkebabify '--->>>))
|
||||||
|
|
||||||
|
;; Non-ASCII identifiers must survive intact. The `regex' egg's
|
||||||
|
;; string-substitute drops one trailing character per multi-byte
|
||||||
|
;; character, which renames things silently -- the C still compiles,
|
||||||
|
;; just under a different name than was written.
|
||||||
|
(test 'naïve_count (unkebabify 'naïve-count))
|
||||||
|
(test 'aï_b (unkebabify 'aï-b))
|
||||||
|
(test 'ïï (unkebabify 'ïï))
|
||||||
|
|
||||||
;; atom-to-fmt-c
|
;; atom-to-fmt-c
|
||||||
(test '%fun (atom-to-fmt-c 'fn))
|
(test '%fun (atom-to-fmt-c 'fn))
|
||||||
(test '%prototype (atom-to-fmt-c 'prototype))
|
(test '%prototype (atom-to-fmt-c 'prototype))
|
||||||
(test '%block-begin (atom-to-fmt-c 'begin))
|
(test '%block-begin (atom-to-fmt-c 'do))
|
||||||
(test '%define (atom-to-fmt-c 'define))
|
(test '%define (atom-to-fmt-c 'define))
|
||||||
(test '%pointer (atom-to-fmt-c 'pointer))
|
(test '%pointer (atom-to-fmt-c 'pointer))
|
||||||
(test '%array (atom-to-fmt-c 'array))
|
(test '%array (atom-to-fmt-c 'array))
|
||||||
@@ -28,6 +36,16 @@
|
|||||||
(test 1 (atom-to-fmt-c 'true))
|
(test 1 (atom-to-fmt-c 'true))
|
||||||
(test 0 (atom-to-fmt-c 'false))
|
(test 0 (atom-to-fmt-c 'false))
|
||||||
|
|
||||||
;; make-field-access
|
;; dot-access -> %. member-access directive (kebab-converted operands)
|
||||||
(test 'a.b (make-field-access '(.b a)))
|
(test '(%. a b) (walk-expr '(dot-access a b)))
|
||||||
(test 'a.b.c (make-field-access '(.c a.b))))
|
(test '(%. a b c) (walk-expr '(dot-access a b c)))
|
||||||
|
(test '(%. a_b c) (walk-expr '(dot-access a-b c)))
|
||||||
|
|
||||||
|
;; comment -> %comment directive (rendered as /* ... */)
|
||||||
|
;; The reader eats only the `;' that introduced the line, so ";;; Foo"
|
||||||
|
;; arrives as ";; Foo"; and c-comment puts nothing between /* */ and
|
||||||
|
;; the text. Both are handled on the way out.
|
||||||
|
(test '(%comment " hi ") (walk-expr '(comment " hi")))
|
||||||
|
(test '(%comment " hi ") (process-toplevel-form '(comment " hi")))
|
||||||
|
(test '(%comment " Foo ") (walk-expr '(comment ";; Foo")))
|
||||||
|
(test '(%comment " Foo ") (walk-expr '(comment ";;; Foo "))))
|
||||||
|
|||||||
211
tests/codegen.scm
Normal file
211
tests/codegen.scm
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
;;; Codegen details that are easy to get subtly wrong, and that the
|
||||||
|
;;; walk-* unit tests cannot see: they check the intermediate form we
|
||||||
|
;;; hand to fmt-c, not the C that fmt-c renders from it.
|
||||||
|
;;;
|
||||||
|
;;; Both cases below were found by writing an OpenGL example, not by
|
||||||
|
;;; the existing suite, because both need an operand shape that no
|
||||||
|
;;; earlier test program happened to use.
|
||||||
|
|
||||||
|
(import (chicken condition)
|
||||||
|
(chicken port)
|
||||||
|
(chicken string)
|
||||||
|
srfi-13
|
||||||
|
fmt-c-writer
|
||||||
|
reader
|
||||||
|
semen
|
||||||
|
utils)
|
||||||
|
|
||||||
|
(define (sex->c source)
|
||||||
|
"Compile SOURCE, a string of Sex, and return the generated C."
|
||||||
|
(let ((forms (with-input-from-string source
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((current-source-file "codegen.sex"))
|
||||||
|
(parse-all (current-input-port)))))))
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((sex-line-directives 'none))
|
||||||
|
(emit-c (semen-process forms)))))))
|
||||||
|
|
||||||
|
(define (emits? source fragment)
|
||||||
|
(and (string-contains (sex->c source) fragment) #t))
|
||||||
|
|
||||||
|
(define (error-message source)
|
||||||
|
"Compile SOURCE and return the error text as the user sees it --
|
||||||
|
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE
|
||||||
|
compiles."
|
||||||
|
(handle-exceptions e
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda ()
|
||||||
|
(display ((condition-property-accessor 'exn 'message) e))
|
||||||
|
(for-each (lambda (a) (display " ") (write a))
|
||||||
|
((condition-property-accessor 'exn 'arguments) e))))
|
||||||
|
(begin (sex->c source) #f)))
|
||||||
|
|
||||||
|
(define (reports? source fragment)
|
||||||
|
(let ((m (error-message source)))
|
||||||
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
|
(define (in-fn body)
|
||||||
|
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
||||||
|
|
||||||
|
(test-group "codegen"
|
||||||
|
|
||||||
|
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||||
|
;; when it is an atom. Anything else was displayed as a raw
|
||||||
|
;; s-expression: `switch ((%. e type))'.
|
||||||
|
(test-group "switch scrutinee"
|
||||||
|
(test-assert "member access"
|
||||||
|
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
|
||||||
|
"switch (e.type)"))
|
||||||
|
(test-assert "call"
|
||||||
|
(emits? (in-fn "(switch (g a) (case 1 (h)))")
|
||||||
|
"switch (g(a))"))
|
||||||
|
(test-assert "arithmetic"
|
||||||
|
(emits? (in-fn "(switch (+ a b) (case 1 (h)))")
|
||||||
|
"switch (a + b)")))
|
||||||
|
|
||||||
|
;; A cast binds tighter than every binary operator, so an operand
|
||||||
|
;; that is itself a binary expression has to be parenthesised --
|
||||||
|
;; otherwise the cast silently applies to the first operand only.
|
||||||
|
(test-group "cast precedence"
|
||||||
|
(test-assert "binary operand is parenthesised"
|
||||||
|
(emits? (in-fn "(var p (* void) (cast (* 2 (sizeof int)) (* void)))")
|
||||||
|
"(void *)(2 * sizeof(int))"))
|
||||||
|
(test-assert "subtraction operand is parenthesised"
|
||||||
|
(emits? (in-fn "(var f float (cast (- a b) float))")
|
||||||
|
"(float)(a - b)"))
|
||||||
|
;; ...but exactly once. The operand used to parenthesise itself
|
||||||
|
;; again inside the parens the cast had just added.
|
||||||
|
(test-assert "and not parenthesised twice"
|
||||||
|
(not (emits? (in-fn "(var f float (cast (- a b) float))")
|
||||||
|
"(float)((a - b))")))
|
||||||
|
;; Unary operands are already unary-expressions and must be left
|
||||||
|
;; alone, or every existing cast in the tree gains noise.
|
||||||
|
(test-assert "identifier is left bare"
|
||||||
|
(emits? (in-fn "(var f float (cast a float))")
|
||||||
|
"(float)a"))
|
||||||
|
(test-assert "address-of is left bare"
|
||||||
|
(emits? (in-fn "(var p (* int) (cast (& a) (* int)))")
|
||||||
|
"(int *)&a"))
|
||||||
|
(test-assert "sizeof is left bare"
|
||||||
|
(emits? (in-fn "(var n int (cast (sizeof int) int))")
|
||||||
|
"(int)sizeof(int)")))
|
||||||
|
|
||||||
|
;; A comment among a call's arguments used to become an argument,
|
||||||
|
;; and c-apply put a comma on each side of it -- which does not
|
||||||
|
;; compile. It is dropped, as in any other expression context.
|
||||||
|
(test-group "comments among arguments"
|
||||||
|
(test-assert "no stray comma"
|
||||||
|
(not (emits? (in-fn "(g 1 ;; c\n 2)") "*/,")))
|
||||||
|
(test-assert "the arguments survive"
|
||||||
|
(emits? (in-fn "(g 1 ;; c\n 2)") "g(1, 2)")))
|
||||||
|
|
||||||
|
;; The reader leaves the `;'s that introduced each line, and a run of
|
||||||
|
;; comment lines arrives as one form per line.
|
||||||
|
(test-group "comment rendering"
|
||||||
|
(test-assert "the markers are stripped"
|
||||||
|
(emits? "(fn f () void ;;; Foo\n (g))" "/* Foo */"))
|
||||||
|
(test-assert "so none survive into the C"
|
||||||
|
(not (emits? "(fn f () void ;;; Foo\n (g))" ";;")))
|
||||||
|
(test-assert "consecutive lines are packed into one comment"
|
||||||
|
(emits? "(fn f () void\n ;; first\n ;; second\n (g))"
|
||||||
|
"/* first\n second */"))
|
||||||
|
;; Packing compares locations rather than just looking for adjacent
|
||||||
|
;; comment forms, so a blank line still separates them.
|
||||||
|
(test-assert "a blank line keeps them apart"
|
||||||
|
(emits? "(fn f () void\n ;; first\n\n ;; second\n (g))" "/* first */")))
|
||||||
|
|
||||||
|
;; A `;' comment is a form, so one written inside a construct with
|
||||||
|
;; positional slots used to land in a slot and shift everything after
|
||||||
|
;; it -- silently. In an `if' the comment became the then-arm and the
|
||||||
|
;; then-arm became an `else if' condition, and it still compiled.
|
||||||
|
;; Comments are now taken out of the slots and emitted just before the
|
||||||
|
;; statement; comments in a body stay where they were written.
|
||||||
|
(test-group "comments in positional slots"
|
||||||
|
(test-assert "an if arm is not shifted"
|
||||||
|
(not (emits? (in-fn "(if 1 ;; c\n (g 1) (g 2))") "else if")))
|
||||||
|
(test-assert "and both arms survive"
|
||||||
|
(emits? (in-fn "(if 1 ;; c\n (g 1) (g 2))") "g(1)"))
|
||||||
|
(test-assert "the comment survives too"
|
||||||
|
(emits? (in-fn "(if 1 ;; kept here\n (g 1) (g 2))") "kept here"))
|
||||||
|
(test-assert "a for header is not shifted"
|
||||||
|
(emits? (in-fn "(for ;; c\n (var i int 0) (< i 2) (++ i) (g i))")
|
||||||
|
"for (int i = 0; i < 2; ++i)"))
|
||||||
|
(test-assert "a while condition is not shifted"
|
||||||
|
(emits? (in-fn "(while ;; c\n (< a b) (g 1))") "while (a < b)"))
|
||||||
|
(test-assert "a var is not shifted"
|
||||||
|
(emits? (in-fn "(var ;; c\n x int 5)") "int x = 5"))
|
||||||
|
(test-assert "a cast is not shifted"
|
||||||
|
(emits? (in-fn "(var y int (cast ;; c\n a int))") "(int)a"))
|
||||||
|
;; Bodies are a statement sequence, so comments there stay put.
|
||||||
|
(test-assert "a comment in a body stays in the body"
|
||||||
|
(emits? (in-fn "(while (< a b) ;; inside\n (g 1))")
|
||||||
|
"while (a < b) {")))
|
||||||
|
;; `|', `||' and `|=' read as ordinary symbols -- our own reader has
|
||||||
|
;; no |symbol| syntax for them to collide with -- but fmt-c cannot
|
||||||
|
;; dispatch on a symbol whose name it cannot write in Scheme source,
|
||||||
|
;; so they used to fall through to the function-call path and emit
|
||||||
|
;; `|\||(a, b)'. The writer renames them to heads fmt-c spells with
|
||||||
|
;; a string.
|
||||||
|
(test-group "bitwise and logical operators"
|
||||||
|
(test-assert "bit-or"
|
||||||
|
(emits? (in-fn "(var x int (| a b))") "int x = a | b"))
|
||||||
|
(test-assert "logical or"
|
||||||
|
(emits? (in-fn "(var x int (|| a b))") "int x = a || b"))
|
||||||
|
(test-assert "or-assign"
|
||||||
|
(emits? (in-fn "(|= a b)") "a |= b"))
|
||||||
|
(test-assert "bit-and"
|
||||||
|
(emits? (in-fn "(var x int (& a b))") "int x = a & b"))
|
||||||
|
(test-assert "logical and"
|
||||||
|
(emits? (in-fn "(var x int (&& a b))") "int x = a && b"))
|
||||||
|
|
||||||
|
;; Precedence too: the operator reaches fmt-c as a string it looks
|
||||||
|
;; up, not as a symbol in its table
|
||||||
|
(test-assert "parenthesised where C needs it"
|
||||||
|
(emits? (in-fn "(var x int (& (| a b) a))") "(a | b) & a"))
|
||||||
|
(test-assert "and left alone where it does not"
|
||||||
|
(emits? (in-fn "(var x int (| a (& a b)))") "int x = a | a & b"))
|
||||||
|
|
||||||
|
;; The spelling from before they could be written directly
|
||||||
|
(test-assert "c-or is still accepted"
|
||||||
|
(emits? (in-fn "(var x int (c-or a b))") "a || b"))
|
||||||
|
(test-assert "c-bit-or is still accepted"
|
||||||
|
(emits? (in-fn "(var x int (c-bit-or a b))") "a | b")))
|
||||||
|
|
||||||
|
;; A `;' comment is a form. In a macro body `comment' is a no-op
|
||||||
|
;; that swallows the comment itself. Inside a quasiquoted payload
|
||||||
|
;; the same form is data, never evaluated, and reaches the writer
|
||||||
|
;; intact
|
||||||
|
(test-group "comments in macros"
|
||||||
|
(test-assert "a comment in the payload reaches the C"
|
||||||
|
(emits? "(defmacro (m-kept) `(fn f () void\n ;; this survives\n (g)))\n(m-kept)"
|
||||||
|
"this survives"))
|
||||||
|
(test-assert "a comment about the macro does not"
|
||||||
|
(not (emits? "(defmacro (m-dropped)\n ;; this vanishes\n `(fn f () void (g)))\n(m-dropped)"
|
||||||
|
"this vanishes")))
|
||||||
|
(test-assert "and the macro still expands"
|
||||||
|
(emits? "(defmacro (m-both)\n ;; about the macro\n `(fn f () void (g)))\n(m-both)"
|
||||||
|
"void f (void)")))
|
||||||
|
|
||||||
|
;; Diagnostics name also the place. Every form carries a (file
|
||||||
|
;; . line), so an error can cite it
|
||||||
|
(test-group "errors cite the source location"
|
||||||
|
(test-assert "unknown toplevel form"
|
||||||
|
(reports? "(include stdio.h)\n(wat 1 2)" "codegen.sex:2: unknown top level form"))
|
||||||
|
(test-assert "the offending form is shown too"
|
||||||
|
(reports? "(include stdio.h)\n(wat 1 2)" "(wat 1 2)"))
|
||||||
|
(test-assert "pub with nothing to define"
|
||||||
|
(reports? "(pub 1)" "codegen.sex:1:")))
|
||||||
|
|
||||||
|
;; A nested pointer chain used to silently lose a level:
|
||||||
|
;; (var p (* (* char))) emitted `char *p'
|
||||||
|
(test-group "malformed types are rejected"
|
||||||
|
(test-assert "nested pointer chain"
|
||||||
|
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))
|
||||||
|
(test-assert "and names the line"
|
||||||
|
(reports? "(pub fn f () void\n (var p (* (* char))))" "codegen.sex:2:"))
|
||||||
|
;; A sublist that only groups has no `*' in it and must still work.
|
||||||
|
(test-assert "grouping sublist still accepted"
|
||||||
|
(emits? (in-fn "(var s (* (const struct suc)) 0)") "const struct suc * s"))
|
||||||
|
(test-assert "flat chain still accepted"
|
||||||
|
(emits? (in-fn "(var q (* * const char) 0)") "const char * * q"))))
|
||||||
242
tests/line-directives.scm
Normal file
242
tests/line-directives.scm
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
;;; Source-line mapping.
|
||||||
|
;;;
|
||||||
|
;;; Every construct in the generated C must be attributed, through
|
||||||
|
;;; #line directives, to the source line of the Sex form it came from.
|
||||||
|
;;; That mapping is the whole basis of source-level debugging.
|
||||||
|
;;;
|
||||||
|
;;; It cannot be left to the C compiler's implicit line counting,
|
||||||
|
;;; because a Sex form and its C rendering may or may not occupy the
|
||||||
|
;;; same number of lines. A call written across four lines renders as
|
||||||
|
;;; one C line; a one-line `for' renders as a braced block of
|
||||||
|
;;; four. Either way every following line drifts, and the drift
|
||||||
|
;;; accumulates over a function body.
|
||||||
|
;;;
|
||||||
|
;;; The tests below pin one construct per statement kind. Each carries
|
||||||
|
;;; a unique numeric marker chosen so that it lands on the first C line
|
||||||
|
;;; that construct emits; the marker is then located in the fixture (to
|
||||||
|
;;; get the true source line) and in the C output (to get the line the
|
||||||
|
;;; directives claim). The two must agree.
|
||||||
|
|
||||||
|
(import (chicken port)
|
||||||
|
(chicken string)
|
||||||
|
srfi-1
|
||||||
|
srfi-13
|
||||||
|
fmt-c-writer
|
||||||
|
reader
|
||||||
|
semen
|
||||||
|
utils)
|
||||||
|
|
||||||
|
;;; ------------------------------------------------------------------
|
||||||
|
;;; Fixture. Line numbers are in the trailing comments; keep them
|
||||||
|
;;; correct when editing. Markers are distinct 3-digit integers, and no
|
||||||
|
;;; other literal in the fixture contains one as a substring.
|
||||||
|
|
||||||
|
(define fixture-lines
|
||||||
|
'("(include stdio.h)" ; 1
|
||||||
|
"" ; 2
|
||||||
|
"(struct pt" ; 3 multi-line toplevel
|
||||||
|
" ((x int)" ; 4
|
||||||
|
" (y int)))" ; 5
|
||||||
|
"" ; 6
|
||||||
|
"(enum color (red green blue))" ; 7
|
||||||
|
"" ; 8
|
||||||
|
"(typedef byte u8)" ; 9
|
||||||
|
"" ; 10
|
||||||
|
"(define MAXN 101)" ; 11
|
||||||
|
"" ; 12
|
||||||
|
"(var gvar int 102)" ; 13
|
||||||
|
"" ; 14
|
||||||
|
"(extern var evar int)" ; 15
|
||||||
|
"" ; 16
|
||||||
|
"(fn helper ((a int)) int)" ; 17 prototype
|
||||||
|
"" ; 18
|
||||||
|
"(pub fn main () int" ; 19
|
||||||
|
" (var mvar int 103)" ; 20
|
||||||
|
" (var p (struct pt))" ; 21
|
||||||
|
" (var arr [int 4])" ; 22
|
||||||
|
" (= (. p x) 104)" ; 23
|
||||||
|
" (+= mvar 105)" ; 24
|
||||||
|
" (++ mvar)" ; 25
|
||||||
|
" (= [arr 0] 106)" ; 26
|
||||||
|
" (putchar (+ 107" ; 27 form spanning 3 lines,
|
||||||
|
" mvar" ; 28 emitted as one C line:
|
||||||
|
" 0))" ; 29 everything after drifts
|
||||||
|
" (var aftercall int 108)" ; 30
|
||||||
|
" (if (< mvar 109)" ; 31
|
||||||
|
" (putchar 110))" ; 32
|
||||||
|
" (if (< mvar 111)" ; 33
|
||||||
|
" (putchar 112)" ; 34
|
||||||
|
" (putchar 113))" ; 35
|
||||||
|
" (while (< mvar 114)" ; 36
|
||||||
|
" (++ mvar))" ; 37
|
||||||
|
" (for (var i int 115)" ; 38
|
||||||
|
" (< i 116)" ; 39
|
||||||
|
" (++ i)" ; 40
|
||||||
|
" (continue))" ; 41
|
||||||
|
" (switch 117" ; 42
|
||||||
|
" (case 118" ; 43
|
||||||
|
" (putchar 119)" ; 44
|
||||||
|
" (break))" ; 45
|
||||||
|
" (default" ; 46
|
||||||
|
" (putchar 120)))" ; 47
|
||||||
|
" (do" ; 48
|
||||||
|
" (var bvar int 121)" ; 49
|
||||||
|
" (putchar bvar))" ; 50
|
||||||
|
" (goto done)" ; 51
|
||||||
|
" (: done)" ; 52
|
||||||
|
" (var svar u64 (sizeof (struct pt)))" ; 53
|
||||||
|
" (var cvar int (cast mvar int))" ; 54
|
||||||
|
" (return 122))" ; 55
|
||||||
|
))
|
||||||
|
|
||||||
|
(define fixture (string-intersperse fixture-lines "\n"))
|
||||||
|
|
||||||
|
;;; ------------------------------------------------------------------
|
||||||
|
;;; Pipeline and #line accounting
|
||||||
|
|
||||||
|
(define (compile-to-c source)
|
||||||
|
"Run reader -> semen -> writer on SOURCE, returning the generated C.
|
||||||
|
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
|
||||||
|
file is what makes an imported module report itself rather than the unit
|
||||||
|
that imported it."
|
||||||
|
(let ((forms (with-input-from-string source
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((current-source-file "fixture.sex"))
|
||||||
|
(parse-all (current-input-port)))))))
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda () (emit-c (semen-process forms))))))
|
||||||
|
|
||||||
|
(define (split-lines s)
|
||||||
|
(let loop ((i 0) (start 0) (acc (list)))
|
||||||
|
(cond
|
||||||
|
((= i (string-length s))
|
||||||
|
(reverse (if (> i start) (cons (substring s start i) acc) acc)))
|
||||||
|
((char=? (string-ref s i) #\newline)
|
||||||
|
(loop (+ i 1) (+ i 1) (cons (substring s start i) acc)))
|
||||||
|
(else (loop (+ i 1) start acc)))))
|
||||||
|
|
||||||
|
(define (directive-line text)
|
||||||
|
"The N of a `#line N \"file\"' directive, or #f if TEXT is not one."
|
||||||
|
(let ((t (string-trim text)))
|
||||||
|
(and (string-prefix? "#line " t)
|
||||||
|
(string->number (car (string-split (substring t 6) " "))))))
|
||||||
|
|
||||||
|
(define (directive-file text)
|
||||||
|
"The file name of a `#line N \"file\"' directive, or #f."
|
||||||
|
(let ((t (string-trim text)))
|
||||||
|
(and (string-prefix? "#line " t)
|
||||||
|
(let ((parts (string-split (substring t 6) " ")))
|
||||||
|
(and (pair? (cdr parts)) (cadr parts))))))
|
||||||
|
|
||||||
|
(define (attributed-lines c-source)
|
||||||
|
"Pair every non-directive C line with the source line it is
|
||||||
|
attributed to. `#line N' says the *next* physical line is N; each line
|
||||||
|
after that is one more. Lines before the first directive get #f."
|
||||||
|
(let loop ((lines (split-lines c-source)) (cur #f) (acc (list)))
|
||||||
|
(if (null? lines)
|
||||||
|
(reverse acc)
|
||||||
|
(cond
|
||||||
|
((directive-line (car lines))
|
||||||
|
=> (lambda (n) (loop (cdr lines) n acc)))
|
||||||
|
(else
|
||||||
|
(loop (cdr lines)
|
||||||
|
(and cur (+ cur 1))
|
||||||
|
(cons (cons (car lines) cur) acc)))))))
|
||||||
|
|
||||||
|
(define (sex-line token)
|
||||||
|
"1-based fixture line containing TOKEN."
|
||||||
|
(let loop ((lines fixture-lines) (n 1))
|
||||||
|
(cond ((null? lines) #f)
|
||||||
|
((string-contains (car lines) token) n)
|
||||||
|
(else (loop (cdr lines) (+ n 1))))))
|
||||||
|
|
||||||
|
(define (claimed-line attributed token)
|
||||||
|
"The source line the generated C attributes TOKEN to."
|
||||||
|
(let ((hit (find (lambda (p) (string-contains (car p) token)) attributed)))
|
||||||
|
(and hit (cdr hit))))
|
||||||
|
|
||||||
|
;;; ------------------------------------------------------------------
|
||||||
|
|
||||||
|
(define c-out (compile-to-c fixture))
|
||||||
|
(define attributed (attributed-lines c-out))
|
||||||
|
|
||||||
|
;;; MAPS checks a construct found under the same token on both sides.
|
||||||
|
;;; MAPS/TOKENS is for constructs that are spelled differently in Sex
|
||||||
|
;;; and in C (`(break)' -> `break;', `(: done)' -> `done:').
|
||||||
|
(define-syntax maps
|
||||||
|
(syntax-rules ()
|
||||||
|
((maps name token)
|
||||||
|
(test name (sex-line token) (claimed-line attributed token)))))
|
||||||
|
|
||||||
|
(define-syntax maps/tokens
|
||||||
|
(syntax-rules ()
|
||||||
|
((maps/tokens name sex-token c-token)
|
||||||
|
(test name (sex-line sex-token) (claimed-line attributed c-token)))))
|
||||||
|
|
||||||
|
(test-group "line-directives"
|
||||||
|
|
||||||
|
;; Toplevel forms
|
||||||
|
(test-group "toplevel"
|
||||||
|
(maps/tokens "include" "(include stdio.h)" "#include")
|
||||||
|
(maps/tokens "struct" "(struct pt" "struct pt {")
|
||||||
|
(maps/tokens "enum" "(enum color" "enum color {")
|
||||||
|
(maps/tokens "typedef" "(typedef byte u8)" "typedef u8 byte;")
|
||||||
|
(maps "define" "101")
|
||||||
|
(maps "global var" "102")
|
||||||
|
(maps/tokens "extern var" "(extern var evar" "extern int evar")
|
||||||
|
(maps/tokens "prototype" "(fn helper" "int helper (int a)")
|
||||||
|
(maps/tokens "function" "(pub fn main" "int main (void)"))
|
||||||
|
|
||||||
|
;; Declarations and expression statements
|
||||||
|
(test-group "statements"
|
||||||
|
(maps "var decl" "103")
|
||||||
|
(maps "member assignment" "104")
|
||||||
|
(maps "compound assignment" "105")
|
||||||
|
(maps/tokens "increment" "(++ mvar)" "++mvar;")
|
||||||
|
(maps "array assignment" "106")
|
||||||
|
|
||||||
|
;; The point of the whole exercise: a call spread over three source
|
||||||
|
;; lines collapses to one C line, so the statement after it must be
|
||||||
|
;; re-anchored or it is reported two lines too early. The call
|
||||||
|
;; itself anchors to the line it *starts* on, which is where a
|
||||||
|
;; debugger should report it.
|
||||||
|
(maps "multi-line call" "107")
|
||||||
|
(maps "statement after it" "108"))
|
||||||
|
|
||||||
|
;; Control flow
|
||||||
|
(test-group "control flow"
|
||||||
|
(maps "if" "109")
|
||||||
|
(maps "if body" "110")
|
||||||
|
(maps "if/else" "111")
|
||||||
|
(maps "then branch" "112")
|
||||||
|
(maps "else branch" "113")
|
||||||
|
(maps "while" "114")
|
||||||
|
(maps "for" "115")
|
||||||
|
(maps/tokens "continue" "(continue)" "continue;")
|
||||||
|
(maps "switch" "117")
|
||||||
|
;; The `case 118:' label line itself is deliberately not pinned.
|
||||||
|
;; c-switch requires every clause to be a case/default form and
|
||||||
|
;; rejects anything else, so no anchor can be placed between
|
||||||
|
;; clauses. The clause *bodies* are anchored from inside, which is
|
||||||
|
;; what matters -- a label is not a statement a debugger stops on.
|
||||||
|
(maps "case body" "119")
|
||||||
|
(maps/tokens "break" "(break)" "break;")
|
||||||
|
(maps "default body" "120")
|
||||||
|
(maps "block" "121")
|
||||||
|
(maps/tokens "goto" "(goto done)" "goto done;")
|
||||||
|
(maps/tokens "label" "(: done)" "done:")
|
||||||
|
(maps "return" "122"))
|
||||||
|
|
||||||
|
;; Expressions that are their own statement
|
||||||
|
(test-group "expressions"
|
||||||
|
(maps/tokens "sizeof" "(var svar" "sizeof")
|
||||||
|
(maps/tokens "cast" "(var cvar" "(int)mvar"))
|
||||||
|
|
||||||
|
;; A location is (file . line); every directive must name the file the
|
||||||
|
;; form was read from.
|
||||||
|
(test-group "file name"
|
||||||
|
(test "every directive names the fixture"
|
||||||
|
(list "\"fixture.sex\"")
|
||||||
|
(delete-duplicates
|
||||||
|
(filter values (map directive-file (split-lines c-out)))))))
|
||||||
28
tests/modules/Makefile
Normal file
28
tests/modules/Makefile
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Multi-module linking.
|
||||||
|
#
|
||||||
|
# Modules are only testable end to end, and nothing else in the suite
|
||||||
|
# links more than one translation unit. Three things have to hold at
|
||||||
|
# once: an imported `pub fn' comes out as a prototype with external
|
||||||
|
# linkage, an imported `pub var' as an extern declaration, and the
|
||||||
|
# module's object survives being passed after `--'. Get any of them
|
||||||
|
# wrong and this fails to link -- or, in the `pub var' case, links and
|
||||||
|
# quietly counts into a private copy.
|
||||||
|
|
||||||
|
SEXC ?= ../../sexc
|
||||||
|
|
||||||
|
EXPECTED = hello, world\nhello, sex\n2 greetings
|
||||||
|
|
||||||
|
check:
|
||||||
|
@$(SEXC) greet.sex -c -o greet.o
|
||||||
|
@$(SEXC) greet-app.sex -o greet-app -- greet.o
|
||||||
|
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
|
||||||
|
echo "modules ok"; \
|
||||||
|
else \
|
||||||
|
echo "modules FAILED, got:"; ./greet-app; $(MAKE) clean; exit 1; \
|
||||||
|
fi
|
||||||
|
@$(MAKE) --no-print-directory clean
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f greet.o greet-app
|
||||||
|
|
||||||
|
.PHONY: check clean
|
||||||
19
tests/modules/greet-app.sex
Normal file
19
tests/modules/greet-app.sex
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
;;; Uses the greet module. Build both, then link them:
|
||||||
|
;;;
|
||||||
|
;;; ./sexc example/greet.sex -c -o greet.o
|
||||||
|
;;; ./sexc example/greet-app.sex -o greet-app -- greet.o
|
||||||
|
;;;
|
||||||
|
;;; `(import greet)' pastes greet's public declarations here: `greet'
|
||||||
|
;;; as a prototype and `greet-count' as an extern. Both keep external
|
||||||
|
;;; linkage, so they refer to the one definition in greet.o rather than
|
||||||
|
;;; to private copies.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(import greet)
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(greet "world")
|
||||||
|
(greet "sex")
|
||||||
|
(printf "%d greetings\n" greet-count)
|
||||||
|
(return 0))
|
||||||
18
tests/modules/greet.sex
Normal file
18
tests/modules/greet.sex
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
;;; A module. Everything marked `pub' forms its public interface;
|
||||||
|
;;; everything else is private to this file.
|
||||||
|
;;;
|
||||||
|
;;; Importing a module does not link it: it pastes the declarations, so
|
||||||
|
;;; the compiled object still has to be handed to the C compiler. See
|
||||||
|
;;; greet-app.sex.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(pub var greet-count int 0)
|
||||||
|
|
||||||
|
(pub fn greet ((name (* const char))) void
|
||||||
|
(++ greet-count)
|
||||||
|
(printf "hello, %s\n" name))
|
||||||
|
|
||||||
|
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||||
|
(fn unused-helper () void
|
||||||
|
(printf "private\n"))
|
||||||
@@ -15,4 +15,27 @@
|
|||||||
(reader-test '((¤)) "[]")
|
(reader-test '((¤)) "[]")
|
||||||
(reader-test '((¤ (¤))) "[[]]")
|
(reader-test '((¤ (¤))) "[[]]")
|
||||||
(reader-test '((¤ (¤ const char))) "[[const char]]")
|
(reader-test '((¤ (¤ const char))) "[[const char]]")
|
||||||
|
|
||||||
|
;; leading `.' becomes the dot-access operator
|
||||||
|
(reader-test '((dot-access obj field)) "(. obj field)")
|
||||||
|
(reader-test '((dot-access obj method a b)) "(. obj method a b)")
|
||||||
|
(reader-test '((dot-access a b)) "(. a b)")
|
||||||
|
;; nested leading dot
|
||||||
|
(reader-test '((foo (dot-access a b))) "(foo (. a b))")
|
||||||
|
|
||||||
|
;; dotted pairs are preserved (only a *leading* dot is special)
|
||||||
|
(reader-test '((a . b)) "(a . b)")
|
||||||
|
(reader-test '((a b . c)) "(a b . c)")
|
||||||
|
(reader-test '((quote (a . b))) "'(a . b)")
|
||||||
|
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
|
||||||
|
(reader-test '((.field obj)) "(.field obj)")
|
||||||
|
|
||||||
|
;; `;' comments are preserved as (comment "...") forms
|
||||||
|
(reader-test '((comment " hi")) "; hi")
|
||||||
|
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
||||||
|
(reader-test '((foo (comment " c") bar)) "(foo ; c\n bar)")
|
||||||
|
;; a trailing top-level comment is its own form
|
||||||
|
(reader-test '((a b) (comment " t")) "(a b) ; t")
|
||||||
|
;; a `;' inside a string is not a comment
|
||||||
|
(reader-test '("a;b") "\"a;b\"")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,6 +6,10 @@
|
|||||||
(include "reader.scm")
|
(include "reader.scm")
|
||||||
(include "fmt-c-writer.scm")
|
(include "fmt-c-writer.scm")
|
||||||
(include "utils.scm")
|
(include "utils.scm")
|
||||||
|
(include "line-directives.scm")
|
||||||
|
(include "codegen.scm")
|
||||||
|
(include "args.scm")
|
||||||
|
(include "types.scm")
|
||||||
|
|
||||||
;;; Should be the last in the test suite
|
;;; Should be the last in the test suite
|
||||||
(test-exit)
|
(test-exit)
|
||||||
|
|||||||
13
tests/sex-programs/comments.sex
Normal file
13
tests/sex-programs/comments.sex
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
(input)
|
||||||
|
(output "start" "end")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; A top-level comment, preserved into the generated C.
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
;; Another top-level comment, right before the function.
|
||||||
|
(pub fn main () int
|
||||||
|
;; a comment in statement position
|
||||||
|
(puts "start")
|
||||||
|
(puts "end") ; a trailing comment after a statement
|
||||||
|
(return 0))
|
||||||
13
tests/sex-programs/hello-world.sex
Normal file
13
tests/sex-programs/hello-world.sex
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
(input "Sextest")
|
||||||
|
(output "Hello from Sex!" "What is your name?" "Hello, Sextest!")
|
||||||
|
(return 255)
|
||||||
|
|
||||||
|
(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 255))
|
||||||
48
tests/sex-programs/list-macros.sex
Normal file
48
tests/sex-programs/list-macros.sex
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
(pub defmacro (list-T type)
|
||||||
|
(let ((list-type (cat 'list- type)))
|
||||||
|
`(struct ,list-type
|
||||||
|
((value ,type)
|
||||||
|
(next (* struct ,list-type))))))
|
||||||
|
|
||||||
|
(pub defmacro (make-list-T type is-public?)
|
||||||
|
(let ((list-type (list 'struct (cat 'list- type)))
|
||||||
|
(fn-name (cat 'make-list- type)))
|
||||||
|
`(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type)
|
||||||
|
(var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type)))
|
||||||
|
(= (-> list next) NULL)
|
||||||
|
(return list))))
|
||||||
|
|
||||||
|
(pub defmacro (add-value-list-T type is-public?)
|
||||||
|
(let ((list-type (list 'struct (cat 'list- type)))
|
||||||
|
(fn-name (cat 'add-value-list- type)))
|
||||||
|
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void
|
||||||
|
(while (!= (-> list next) NULL)
|
||||||
|
(= list (-> list next)))
|
||||||
|
(= (-> list next) (,(cat 'make-list- type)))
|
||||||
|
(= (-> list value) value))))
|
||||||
|
|
||||||
|
(pub defmacro (length-list-T type is-public?)
|
||||||
|
(let ((fn-name (cat 'length-list- type))
|
||||||
|
(list-type (list 'struct (cat 'list- type))))
|
||||||
|
`(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t
|
||||||
|
(var n size-t 0)
|
||||||
|
(while (!= (-> list next) NULL)
|
||||||
|
(= list (-> list next))
|
||||||
|
(++ n))
|
||||||
|
(return n))))
|
||||||
|
|
||||||
|
(pub defmacro (is-empty-list-T type is-public?)
|
||||||
|
`(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type)
|
||||||
|
((list ,(list '* 'struct (cat 'list- type))))
|
||||||
|
bool
|
||||||
|
(return (== (-> list next) NULL))))
|
||||||
|
|
||||||
|
(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do)
|
||||||
|
(let ((list-var-2 (cat list-var '-2)))
|
||||||
|
`(do
|
||||||
|
(var ,list-var-2 (* ,list-type) ,list-var)
|
||||||
|
(var ,elt-var ,elt-type (-> ,list-var-2 value))
|
||||||
|
(while (!= (-> ,list-var-2 next) NULL)
|
||||||
|
,what-do
|
||||||
|
(= ,list-var-2 (-> ,list-var-2 next))
|
||||||
|
(= ,elt-var (-> ,list-var-2 value))))))
|
||||||
50
tests/sex-programs/lists.sex
Normal file
50
tests/sex-programs/lists.sex
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
(input)
|
||||||
|
(output "Size of the list: 0"
|
||||||
|
"Size of the list: 2"
|
||||||
|
"3 4 "
|
||||||
|
"Size of the list: 2")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
(include stdlib.h)
|
||||||
|
(include stddef.h)
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(import list-macros)
|
||||||
|
|
||||||
|
(struct foo
|
||||||
|
((a-field float)
|
||||||
|
(b int)
|
||||||
|
(c (* const char))
|
||||||
|
(not (fn ((val bool)) bool))))
|
||||||
|
|
||||||
|
(var f (struct foo))
|
||||||
|
|
||||||
|
(list-T int)
|
||||||
|
(make-list-T int #f)
|
||||||
|
(add-value-list-T int #f)
|
||||||
|
(length-list-T int #f)
|
||||||
|
(is-empty-list-T int #f)
|
||||||
|
|
||||||
|
(extern fn puk ((a int) (b float)) void)
|
||||||
|
(pub fn baz () bool
|
||||||
|
(return true))
|
||||||
|
|
||||||
|
(extern var i int)
|
||||||
|
(var j int)
|
||||||
|
(pub var k int)
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var l (* struct list-int) (make-list-int))
|
||||||
|
(printf "Size of the list: %lu\n" (length-list-int l))
|
||||||
|
(add-value-list-int l 3)
|
||||||
|
(add-value-list-int l 4)
|
||||||
|
(printf "Size of the list: %lu\n" (length-list-int l))
|
||||||
|
(list-for-each (struct list-int) l int v
|
||||||
|
(printf "%d " v))
|
||||||
|
(printf "\n")
|
||||||
|
(printf "Size of the list: %lu\n" (length-list-int l))
|
||||||
|
(return 0))
|
||||||
|
|
||||||
|
(pub fn print-list ((l (* const struct list-int))) void
|
||||||
|
(list-for-each (const struct list-int) l int v (printf "%d " v))
|
||||||
|
(printf "\n"))
|
||||||
37
tests/sex-programs/serialize.sex
Normal file
37
tests/sex-programs/serialize.sex
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
(input)
|
||||||
|
(output "box { w=3 h=4 label=wide }")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; A macro generating code from the type database: it is handed a
|
||||||
|
;;; struct name, walks its fields with map-fields, and picks a printf
|
||||||
|
;;; conversion per field with type-match. Exercises semen registering
|
||||||
|
;;; the struct and the macro reading it back at expansion time.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(defmacro (print-struct type-name)
|
||||||
|
(let ((printers
|
||||||
|
(map-fields type-name
|
||||||
|
(lambda (name type)
|
||||||
|
`(printf ,(string-append " " (symbol->string name) "="
|
||||||
|
(type-match type
|
||||||
|
(int "%d")
|
||||||
|
((* const char) "%s")
|
||||||
|
(else (error "print-struct: unsupported field type"
|
||||||
|
name type))))
|
||||||
|
(-> v ,name))))))
|
||||||
|
(if (not printers)
|
||||||
|
(error "print-struct: no such struct" type-name)
|
||||||
|
`(fn ,(cat 'print- type-name) ((v (* const struct ,type-name))) void
|
||||||
|
(printf ,(string-append (symbol->string type-name) " {"))
|
||||||
|
,@printers
|
||||||
|
(printf " }\n")))))
|
||||||
|
|
||||||
|
(struct box ((w int) (h int) (label (* const char))))
|
||||||
|
|
||||||
|
(print-struct box)
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var b (struct box) #(3 4 "wide"))
|
||||||
|
(print-box (& b))
|
||||||
|
(return 0))
|
||||||
24
tests/sex-programs/unicode.sex
Normal file
24
tests/sex-programs/unicode.sex
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
(input)
|
||||||
|
(output "café 日本語 🍺" "café1" "naïve: 3")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; Non-ASCII string literals and identifiers.
|
||||||
|
;;;
|
||||||
|
;;; Two things are pinned here. First, a character outside printable
|
||||||
|
;;; ASCII must reach the C compiler as the UTF-8 bytes it was written
|
||||||
|
;;; as. Octal escapes are used because C's \x escape swallows every
|
||||||
|
;;; following hex digit: "café1" is the case that catches it, since a
|
||||||
|
;;; hex escape would run "\xc3\xa9" into the "1" and produce a value out
|
||||||
|
;;; of range for a char. Second, a kebab-case identifier containing
|
||||||
|
;;; non-ASCII characters must survive unkebabify intact -- getting this
|
||||||
|
;;; wrong truncates the name silently, and the program still compiles
|
||||||
|
;;; and runs, just under a different name than the one written.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(puts "café 日本語 🍺")
|
||||||
|
(puts "café1")
|
||||||
|
(var naïve-count int 3)
|
||||||
|
(printf "naïve: %d\n" naïve-count)
|
||||||
|
(return 0))
|
||||||
@@ -1 +1,3 @@
|
|||||||
(module sexc (main) "../sexc.scm")
|
(module sexc
|
||||||
|
*
|
||||||
|
"../sexc.scm")
|
||||||
|
|||||||
3
tests/types.module.scm
Normal file
3
tests/types.module.scm
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
(module types
|
||||||
|
*
|
||||||
|
"../types.scm")
|
||||||
106
tests/types.scm
Normal file
106
tests/types.scm
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
;;; The type database.
|
||||||
|
;;;
|
||||||
|
;;; Names here are prefixed so they cannot collide with the types the
|
||||||
|
;;; other suites register: the database is one table in the linked
|
||||||
|
;;; binary, and semen fills it in whenever a suite compiles a struct.
|
||||||
|
|
||||||
|
(import types)
|
||||||
|
|
||||||
|
(test-group "types"
|
||||||
|
|
||||||
|
(add-struct 't-point '(struct t-point ((x int) (y int))))
|
||||||
|
(test "fields come back as (name type)"
|
||||||
|
'((x int) (y int))
|
||||||
|
(get-fields 't-point))
|
||||||
|
(test "and the whole entry is tagged"
|
||||||
|
'(struct t-point ((x int) (y int)))
|
||||||
|
(get-type-info 't-point))
|
||||||
|
|
||||||
|
;; Fields are written with the type last, so one entry can declare
|
||||||
|
;; several names. They come back as one field each.
|
||||||
|
(add-struct 't-settings
|
||||||
|
'(pub struct t-settings ((x y w h u32) (title (* const char)))))
|
||||||
|
(test "names sharing a type are split apart"
|
||||||
|
'((x u32) (y u32) (w u32) (h u32) (title (* const char)))
|
||||||
|
(get-fields 't-settings))
|
||||||
|
|
||||||
|
(add-struct 't-commented '(struct t-commented ((comment " hi") (a int))))
|
||||||
|
(test "a comment among the fields is not a field"
|
||||||
|
'((a int))
|
||||||
|
(get-fields 't-commented))
|
||||||
|
|
||||||
|
(add-union 't-value '(union t-value ((i int) (f float))))
|
||||||
|
(test "unions have fields too"
|
||||||
|
'((i int) (f float))
|
||||||
|
(get-fields 't-value))
|
||||||
|
|
||||||
|
(add-enum 't-color '(enum t-color (red green blue)))
|
||||||
|
(test "enums keep their values"
|
||||||
|
'(enum t-color (red green blue))
|
||||||
|
(get-type-info 't-color))
|
||||||
|
(test "but have no fields"
|
||||||
|
#f
|
||||||
|
(get-fields 't-color))
|
||||||
|
|
||||||
|
(add-typedef 't-u8 '(typedef t-u8 uint8-t))
|
||||||
|
(test "a typedef resolves to its target"
|
||||||
|
'uint8-t
|
||||||
|
(get-underlying-type 't-u8))
|
||||||
|
(add-typedef 't-byte '(typedef t-byte t-u8))
|
||||||
|
(test "and chains are followed to the end"
|
||||||
|
'uint8-t
|
||||||
|
(get-underlying-type 't-byte))
|
||||||
|
(test "a struct is not a typedef"
|
||||||
|
#f
|
||||||
|
(get-underlying-type 't-point))
|
||||||
|
|
||||||
|
;; A #define keeps its value forms -- there can be more than one.
|
||||||
|
(add-define 't-maxn '(define t-maxn 8))
|
||||||
|
(test "defines are recorded"
|
||||||
|
'(define t-maxn (8))
|
||||||
|
(get-type-info 't-maxn))
|
||||||
|
|
||||||
|
;; map-fields walks an aggregate, handing each field to a function.
|
||||||
|
(test "map-fields visits every field"
|
||||||
|
'((x int) (y int))
|
||||||
|
(map-fields 't-point (lambda (name type) (list name type))))
|
||||||
|
(test "and splits shared names apart too"
|
||||||
|
'(x y w h title)
|
||||||
|
(map-fields 't-settings (lambda (name type) name)))
|
||||||
|
;; An enumerator's type is the enum itself.
|
||||||
|
(test "enum values are fields whose type is the enum"
|
||||||
|
'((red (enum t-color)) (green (enum t-color)) (blue (enum t-color)))
|
||||||
|
(map-fields 't-color (lambda (name type) (list name type))))
|
||||||
|
(test "a typedef has no fields to map"
|
||||||
|
#f
|
||||||
|
(map-fields 't-u8 (lambda (name type) name)))
|
||||||
|
(test "nor does an undeclared name"
|
||||||
|
#f
|
||||||
|
(map-fields 't-nothing (lambda (name type) name)))
|
||||||
|
|
||||||
|
;; type-match compares whole types, since a type is a form.
|
||||||
|
(test "a bare type matches" 'yes (type-match 'int (int 'yes) (else 'no)))
|
||||||
|
(test "so does a compound one" 'yes (type-match '(* const char)
|
||||||
|
(int 'no)
|
||||||
|
((* const char) 'yes)
|
||||||
|
(else 'no)))
|
||||||
|
;; The [int 10] of the docstring is Sex notation: the Sex reader turns
|
||||||
|
;; brackets into a ¤ form, while CHICKEN reads them as plain parens.
|
||||||
|
;; In a .scm file the array type has to be written out.
|
||||||
|
(test "and an array" 'yes (type-match '(¤ int 10)
|
||||||
|
((¤ int 10) 'yes)
|
||||||
|
(else 'no)))
|
||||||
|
(test "else catches the rest" 'no (type-match '(* void) (int 'yes) (else 'no)))
|
||||||
|
(test "a near miss does not match" 'no (type-match '(¤ int 20)
|
||||||
|
((¤ int 10) 'yes)
|
||||||
|
(else 'no)))
|
||||||
|
(test "no clause matching and no else is #f"
|
||||||
|
#f
|
||||||
|
(type-match 'float (int 'yes)))
|
||||||
|
|
||||||
|
(test "an undeclared name has no entry"
|
||||||
|
#f
|
||||||
|
(get-type-info 't-never-declared))
|
||||||
|
(test "and no fields"
|
||||||
|
#f
|
||||||
|
(get-fields 't-never-declared)))
|
||||||
23
tools/sextest/Makefile
Normal file
23
tools/sextest/Makefile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
CHICKEN_C = csc
|
||||||
|
CSC_FLAGS += -K prefix -static
|
||||||
|
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
||||||
|
|
||||||
|
ROOT = ../..
|
||||||
|
|
||||||
|
# sextest reuses sexc's reader (and its utils dependency) instead of
|
||||||
|
# duplicating the S-expression reader. The module wrappers here include
|
||||||
|
# the shared sources from the project root.
|
||||||
|
|
||||||
|
sextest: sextest.scm reader.o utils.o
|
||||||
|
$(CHICKEN_C) $(CSC_FLAGS) sextest.scm -o sextest -link reader,utils
|
||||||
|
|
||||||
|
utils.o: utils.module.scm $(ROOT)/utils.scm
|
||||||
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
|
||||||
|
|
||||||
|
reader.o: reader.module.scm $(ROOT)/reader.scm utils.o
|
||||||
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o *.import.scm *.link sextest
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
26
tools/sextest/README.org
Normal file
26
tools/sextest/README.org
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
* Sextest
|
||||||
|
A tool for testing Sex compiler by using test programs.
|
||||||
|
The tools compiles test programs, then runs with provided
|
||||||
|
input, checking the output and return code.
|
||||||
|
|
||||||
|
* Test format
|
||||||
|
The test program is just a regular Sex program, which may contain
|
||||||
|
additional toplevel forms, to define compilation parameters, input to
|
||||||
|
the program, and expected output and return code. Default value for
|
||||||
|
compilation, input and output is an empty strings. For the return code
|
||||||
|
it is 0.
|
||||||
|
|
||||||
|
* Example
|
||||||
|
some-test.sex:
|
||||||
|
#+begin_src
|
||||||
|
(compilation "-- -O2")
|
||||||
|
(input "")
|
||||||
|
(output "Hello world!")
|
||||||
|
(return 123)
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(puts "Hello World!")
|
||||||
|
(return 123))
|
||||||
|
#+end_src
|
||||||
13
tools/sextest/hello-sextest.sex
Normal file
13
tools/sextest/hello-sextest.sex
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
(input "Sextest")
|
||||||
|
(output "Hello from Sex!" "What is your name?" "Hello, Sextest!")
|
||||||
|
(return 255)
|
||||||
|
|
||||||
|
(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 255))
|
||||||
3
tools/sextest/reader.module.scm
Normal file
3
tools/sextest/reader.module.scm
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
(module reader (read-from-file
|
||||||
|
read-raw-forms)
|
||||||
|
"../../reader.scm")
|
||||||
148
tools/sextest/sextest.scm
Normal file
148
tools/sextest/sextest.scm
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
(import scheme
|
||||||
|
brev-separate
|
||||||
|
(chicken base)
|
||||||
|
(chicken file)
|
||||||
|
(chicken io)
|
||||||
|
(chicken pathname)
|
||||||
|
(chicken port)
|
||||||
|
(chicken process)
|
||||||
|
(chicken process-context)
|
||||||
|
fmt
|
||||||
|
getopt-long
|
||||||
|
reader ; read-raw-forms, shared with sexc
|
||||||
|
srfi-1)
|
||||||
|
|
||||||
|
(define (print-help)
|
||||||
|
(fmt #t "Usage: sextest [options] filename" nl
|
||||||
|
"Options:" nl
|
||||||
|
(usage opts-grammar) nl))
|
||||||
|
|
||||||
|
(define (process-file target-path)
|
||||||
|
(let ((contents (read-raw-forms target-path)))
|
||||||
|
(foldl (lambda (acc elt)
|
||||||
|
(case (car elt)
|
||||||
|
((compilation input output return)
|
||||||
|
(cons
|
||||||
|
(append (car acc) (list elt))
|
||||||
|
(cdr acc)))
|
||||||
|
(else
|
||||||
|
(cons
|
||||||
|
(car acc)
|
||||||
|
(append (cdr acc) (list elt))))))
|
||||||
|
(cons (list) (list))
|
||||||
|
contents)))
|
||||||
|
|
||||||
|
(define (compile src flags sexc)
|
||||||
|
(let ((compiler (or
|
||||||
|
(and sexc (cdr sexc))
|
||||||
|
(get-environment-variable "SEXC")
|
||||||
|
"sexc"))
|
||||||
|
(compiled-file (create-temporary-file)))
|
||||||
|
;; `process' returns one record; `process-input-port' is named from
|
||||||
|
;; the child's side, so it is the port we write to.
|
||||||
|
(let* ((proc (process compiler (append (list "-o" compiled-file) flags)))
|
||||||
|
(sexc-stdin (process-input-port proc)))
|
||||||
|
(with-output-to-port sexc-stdin
|
||||||
|
(fn (map (fn (fmt #t x)) src)))
|
||||||
|
(close-output-port sexc-stdin)
|
||||||
|
(call-with-values
|
||||||
|
(fn (process-wait proc))
|
||||||
|
(lambda (pid exited retcode)
|
||||||
|
(if (= 0 retcode)
|
||||||
|
compiled-file
|
||||||
|
#f))))))
|
||||||
|
|
||||||
|
(define (run-and-check file in out ret)
|
||||||
|
(let* ((proc (process file))
|
||||||
|
(out-port (process-output-port proc)) ; the program's stdout
|
||||||
|
(in-port (process-input-port proc))) ; the program's stdin
|
||||||
|
(let ()
|
||||||
|
(when in
|
||||||
|
(with-output-to-port in-port
|
||||||
|
(fn (map (fn (fmt #t x))
|
||||||
|
(cdr in))))
|
||||||
|
(close-output-port in-port))
|
||||||
|
(let ((out-lines
|
||||||
|
(with-input-from-port out-port
|
||||||
|
(fn
|
||||||
|
(let loop ((line (read-line))
|
||||||
|
(lines (list)))
|
||||||
|
(if (eof-object? line)
|
||||||
|
(reverse lines)
|
||||||
|
(loop (read-line)
|
||||||
|
(cons line lines)))))))
|
||||||
|
(ret-code
|
||||||
|
(call-with-values
|
||||||
|
;; TODO: what if the program hangs
|
||||||
|
;; we need some kind of timeout mechanism
|
||||||
|
(fn
|
||||||
|
(process-wait proc))
|
||||||
|
(lambda (pid exited retcode)
|
||||||
|
retcode))))
|
||||||
|
(and
|
||||||
|
(if (not (= ret-code (cadr ret)))
|
||||||
|
(begin
|
||||||
|
(fmt #t "Retcode differs. Got " ret-code ", expected " (cadr ret) nl)
|
||||||
|
#f)
|
||||||
|
#t)
|
||||||
|
|
||||||
|
(if (not (equal? out-lines (cdr out)))
|
||||||
|
(begin
|
||||||
|
(fmt #t "Output differs. Got " out-lines " expected " (cdr out) nl)
|
||||||
|
#f)
|
||||||
|
#t))))))
|
||||||
|
|
||||||
|
(define opts-grammar
|
||||||
|
`((sexc ,(fmt #f "Path to sexc compiler. Defaults to value of SEXC" nl
|
||||||
|
(pad 26) "environment variable, ot if it's empty, to sexc" nl )
|
||||||
|
(required #f)
|
||||||
|
(value #t))
|
||||||
|
(help "Show this help"
|
||||||
|
(required #f)
|
||||||
|
(value #f)
|
||||||
|
(single-char #\h))))
|
||||||
|
|
||||||
|
(define (process-test-file sexc path)
|
||||||
|
(set-environment-variable! "SEX_MODULE_PATH"
|
||||||
|
(normalize-pathname (make-absolute-pathname
|
||||||
|
(current-directory)
|
||||||
|
(pathname-directory path))))
|
||||||
|
(let* ((settings-and-src (process-file path))
|
||||||
|
(settings (car settings-and-src))
|
||||||
|
(src (cdr settings-and-src))
|
||||||
|
(compiled-file (compile src (assoc 'compile settings) sexc)))
|
||||||
|
(if (not compiled-file)
|
||||||
|
(begin (fmt #t "Failed to compile " path nl)
|
||||||
|
#f)
|
||||||
|
(if (run-and-check
|
||||||
|
compiled-file
|
||||||
|
(assoc 'input settings)
|
||||||
|
(assoc 'output settings)
|
||||||
|
(assoc 'return settings))
|
||||||
|
(begin (fmt #t ".")
|
||||||
|
#t)
|
||||||
|
(begin (fmt #t ",")
|
||||||
|
#f)))))
|
||||||
|
|
||||||
|
(define (main)
|
||||||
|
(let ((args (getopt-long (command-line-arguments)
|
||||||
|
opts-grammar)))
|
||||||
|
(when (assoc 'help args)
|
||||||
|
(print-help)
|
||||||
|
(exit 0))
|
||||||
|
(when (null? (cdr (assoc '@ args)))
|
||||||
|
(fmt #t "Missing target file" nl)
|
||||||
|
(print-help)
|
||||||
|
(exit 1))
|
||||||
|
(unless
|
||||||
|
(foldl (lambda (a b) (and a b))
|
||||||
|
#t
|
||||||
|
(map (fn (process-test-file (assoc 'sexc args) x))
|
||||||
|
(cdr (assoc '@ args))))
|
||||||
|
;; TODO: add more verbose and human readable output and reporting
|
||||||
|
(fmt #t nl)
|
||||||
|
(exit 2))
|
||||||
|
(fmt #t nl)
|
||||||
|
(exit 0)))
|
||||||
|
|
||||||
|
(main)
|
||||||
19
tools/sextest/utils.module.scm
Normal file
19
tools/sextest/utils.module.scm
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
(module utils
|
||||||
|
(get-env-var
|
||||||
|
set-working-directory
|
||||||
|
to-absolute-pathname
|
||||||
|
list-split
|
||||||
|
list-join
|
||||||
|
recons
|
||||||
|
current-source-file
|
||||||
|
set-form-source!
|
||||||
|
form-source
|
||||||
|
form-file
|
||||||
|
form-line
|
||||||
|
copy-form-source!
|
||||||
|
stamp-form-source!
|
||||||
|
form-location
|
||||||
|
sex-error
|
||||||
|
with-directory
|
||||||
|
)
|
||||||
|
"../../utils.scm")
|
||||||
14
types.module.scm
Normal file
14
types.module.scm
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
(module types
|
||||||
|
(add-struct
|
||||||
|
add-union
|
||||||
|
add-enum
|
||||||
|
add-typedef
|
||||||
|
add-define
|
||||||
|
|
||||||
|
type-match
|
||||||
|
map-fields
|
||||||
|
|
||||||
|
get-type-info
|
||||||
|
get-fields
|
||||||
|
get-underlying-type)
|
||||||
|
"types.scm")
|
||||||
133
types.scm
Normal file
133
types.scm
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
;;; The type database.
|
||||||
|
;;;
|
||||||
|
;;; Every named aggregate, typedef and define the semantic engine
|
||||||
|
;;; walks past is recorded here, so that macros (or other forms) can
|
||||||
|
;;; ask what a type is made of. That is what lets a macro generate
|
||||||
|
;;; code from a struct's fields given nothing but its name.
|
||||||
|
;;;
|
||||||
|
;;; Entries are filled in as toplevel forms are processed, in order, so
|
||||||
|
;;; a type has to be declared before the macro that asks about it.
|
||||||
|
|
||||||
|
(import
|
||||||
|
scheme
|
||||||
|
(scheme base)
|
||||||
|
(chicken base)
|
||||||
|
srfi-1
|
||||||
|
srfi-69)
|
||||||
|
|
||||||
|
(define +type-db+ (make-hash-table))
|
||||||
|
|
||||||
|
(define (strip-pub form)
|
||||||
|
(if (eq? (car form) 'pub) (cdr form) form))
|
||||||
|
|
||||||
|
(define (comment-form? f)
|
||||||
|
(and (pair? f) (eq? (car f) 'comment)))
|
||||||
|
|
||||||
|
;;; Fields are written with the type last and one or more names before
|
||||||
|
;;; it, so ((x y int) (p (* char))) describes three fields. Flatten that
|
||||||
|
;;; into one (name type) per field, which is what a caller wants.
|
||||||
|
(define (normalize-fields fields)
|
||||||
|
(append-map
|
||||||
|
(lambda (field)
|
||||||
|
(if (comment-form? field)
|
||||||
|
(list)
|
||||||
|
(let ((type (last field))
|
||||||
|
(names (drop-right field 1)))
|
||||||
|
(map (lambda (name) (list name type)) names))))
|
||||||
|
(remove comment-form? fields)))
|
||||||
|
|
||||||
|
;;; ([pub] struct name (fields ...) . attrs)
|
||||||
|
(define (aggregate-fields form)
|
||||||
|
(let ((f (strip-pub form)))
|
||||||
|
(if (and (pair? (cddr f)) (list? (caddr f)))
|
||||||
|
(caddr f)
|
||||||
|
(list))))
|
||||||
|
|
||||||
|
(define (add-struct name form)
|
||||||
|
(hash-table-set! +type-db+ name
|
||||||
|
(list 'struct name (normalize-fields (aggregate-fields form)))))
|
||||||
|
|
||||||
|
(define (add-union name form)
|
||||||
|
(hash-table-set! +type-db+ name
|
||||||
|
(list 'union name (normalize-fields (aggregate-fields form)))))
|
||||||
|
|
||||||
|
;;; ([pub] enum name (value ...))
|
||||||
|
(define (add-enum name form)
|
||||||
|
(let ((f (strip-pub form)))
|
||||||
|
(hash-table-set! +type-db+ name
|
||||||
|
(list 'enum name (if (and (pair? (cddr f)) (list? (caddr f)))
|
||||||
|
(caddr f)
|
||||||
|
(list))))))
|
||||||
|
|
||||||
|
;;; ([pub] typedef new-name target)
|
||||||
|
(define (add-typedef name form)
|
||||||
|
(hash-table-set! +type-db+ name
|
||||||
|
(list 'typedef name (last (strip-pub form)))))
|
||||||
|
|
||||||
|
;;; (define name value ...) -- a C #define, kept so a macro can read a
|
||||||
|
;;; compile-time constant rather than re-parse the source.
|
||||||
|
(define (add-define name form)
|
||||||
|
(hash-table-set! +type-db+ name
|
||||||
|
(list 'define name (cddr (strip-pub form)))))
|
||||||
|
|
||||||
|
(define (get-type-info name)
|
||||||
|
(hash-table-ref/default +type-db+ name #f))
|
||||||
|
|
||||||
|
;;; ((name type) ...) for a struct or union, #f for anything else --
|
||||||
|
;;; including a name that was never declared. Callers give the better
|
||||||
|
;;; error, since they know what they wanted it for.
|
||||||
|
(define (get-fields name)
|
||||||
|
(let ((info (get-type-info name)))
|
||||||
|
(and info
|
||||||
|
(memq (car info) '(struct union))
|
||||||
|
(caddr info))))
|
||||||
|
|
||||||
|
;;; Follow a typedef chain to the name it ultimately stands for. #f if
|
||||||
|
;;; NAME is not a typedef.
|
||||||
|
(define (get-underlying-type name)
|
||||||
|
(let ((info (get-type-info name)))
|
||||||
|
(and info
|
||||||
|
(eq? (car info) 'typedef)
|
||||||
|
(let ((target (caddr info)))
|
||||||
|
(or (and (symbol? target) (get-underlying-type target))
|
||||||
|
target)))))
|
||||||
|
|
||||||
|
;;; Type matcher macro
|
||||||
|
;;; (type-match type
|
||||||
|
;;; (int ...)
|
||||||
|
;;; ((* const char) ...)
|
||||||
|
;;; ([int 10] ...)
|
||||||
|
;;; (else ...))
|
||||||
|
;;;
|
||||||
|
;;; A type is a form, not an atom, so this compares with equal? rather
|
||||||
|
;;; than dispatching like `case'. Patterns are literal types and are not
|
||||||
|
;;; evaluated; `else' is optional and the whole thing is #f when nothing
|
||||||
|
;;; matches and there is no else.
|
||||||
|
(define-syntax type-match
|
||||||
|
(syntax-rules (else)
|
||||||
|
((_ type) #f)
|
||||||
|
((_ type (else body ...)) (begin body ...))
|
||||||
|
((_ type (pattern body ...) clause ...)
|
||||||
|
(if (equal? type 'pattern)
|
||||||
|
(begin body ...)
|
||||||
|
(type-match type clause ...)))))
|
||||||
|
|
||||||
|
;;; Map function to each field/value of a structure/union/enum
|
||||||
|
;;; For enums, field-type is the type of the enum (since C 23)
|
||||||
|
;;; (map-fields type-name
|
||||||
|
;;; (lambda (field-name field-type) ...))
|
||||||
|
;;;
|
||||||
|
;;; Returns #f if nothing of that name was declared
|
||||||
|
(define (map-fields struct-union-enum fn)
|
||||||
|
(let ((info (get-type-info struct-union-enum)))
|
||||||
|
(and info
|
||||||
|
(case (car info)
|
||||||
|
((struct union)
|
||||||
|
(map (lambda (field) (fn (car field) (cadr field)))
|
||||||
|
(caddr info)))
|
||||||
|
;; An enumerator's type is the enum itself.
|
||||||
|
((enum)
|
||||||
|
(let ((type (list 'enum struct-union-enum)))
|
||||||
|
(map (lambda (value) (fn value type))
|
||||||
|
(caddr info))))
|
||||||
|
(else #f)))))
|
||||||
@@ -5,6 +5,15 @@
|
|||||||
list-split
|
list-split
|
||||||
list-join
|
list-join
|
||||||
recons
|
recons
|
||||||
|
current-source-file
|
||||||
|
set-form-source!
|
||||||
|
form-source
|
||||||
|
form-file
|
||||||
|
form-line
|
||||||
|
copy-form-source!
|
||||||
|
stamp-form-source!
|
||||||
|
form-location
|
||||||
|
sex-error
|
||||||
with-directory
|
with-directory
|
||||||
)
|
)
|
||||||
"utils.scm")
|
"utils.scm")
|
||||||
|
|||||||
70
utils.scm
70
utils.scm
@@ -1,9 +1,11 @@
|
|||||||
(import
|
(import
|
||||||
scheme
|
scheme
|
||||||
|
(scheme base) ; make-parameter
|
||||||
(chicken base)
|
(chicken base)
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
(chicken process-context)
|
(chicken process-context)
|
||||||
srfi-1)
|
srfi-1
|
||||||
|
srfi-69)
|
||||||
|
|
||||||
(define-syntax prog1
|
(define-syntax prog1
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
@@ -63,4 +65,68 @@
|
|||||||
(if (and (eq? new-car (car old-cons))
|
(if (and (eq? new-car (car old-cons))
|
||||||
(eq? new-cdr (cdr old-cons)))
|
(eq? new-cdr (cdr old-cons)))
|
||||||
old-cons
|
old-cons
|
||||||
(cons new-car new-cdr)))
|
;; A rebuilt cell is still the same source form, so it keeps the
|
||||||
|
;; same location
|
||||||
|
(copy-form-source! old-cons (cons new-car new-cdr))))
|
||||||
|
|
||||||
|
;;; Source-location map.
|
||||||
|
;;; Our hand-written reader records the source location of each form
|
||||||
|
;;; here, keyed by the form's cons cell (eq?). This replaces CHICKEN's
|
||||||
|
;;; read-with-source-info / get-line-number, which only works when forms
|
||||||
|
;;; are produced by the built-in `read'.
|
||||||
|
;;;
|
||||||
|
;;; A location is (file . line). The file matters because imported
|
||||||
|
;;; modules paste their public forms into the current unit: those forms
|
||||||
|
;;; originate in another file and must be reported as such
|
||||||
|
(define +form-sources+ (make-hash-table eq?))
|
||||||
|
|
||||||
|
;;; The file `parse-all' is currently reading. Bound by the reader
|
||||||
|
(define current-source-file (make-parameter "<unknown>"))
|
||||||
|
|
||||||
|
(define (set-form-source! form file line)
|
||||||
|
(hash-table-set! +form-sources+ form (cons file line)))
|
||||||
|
|
||||||
|
(define (form-source form)
|
||||||
|
(hash-table-ref/default +form-sources+ form #f))
|
||||||
|
|
||||||
|
(define (form-file form)
|
||||||
|
(let ((src (form-source form)))
|
||||||
|
(and src (car src))))
|
||||||
|
|
||||||
|
(define (form-line form)
|
||||||
|
(let ((src (form-source form)))
|
||||||
|
(and src (cdr src))))
|
||||||
|
|
||||||
|
(define (copy-form-source! from to)
|
||||||
|
"Give TO the location of FROM, if FROM has one. Returns TO, so it can
|
||||||
|
wrap a form-building expression."
|
||||||
|
(let ((src (form-source from)))
|
||||||
|
(when (and src (pair? to))
|
||||||
|
(hash-table-set! +form-sources+ to src)))
|
||||||
|
to)
|
||||||
|
|
||||||
|
;;; Diagnostics
|
||||||
|
;;;
|
||||||
|
;;; Every form carries a location now, so an error can say where the
|
||||||
|
;;; wrong code was written
|
||||||
|
(define (form-location form)
|
||||||
|
"\"file:line: \" for FORM, or \"\" when it has none"
|
||||||
|
(let ((src (form-source form)))
|
||||||
|
(if src
|
||||||
|
(string-append (car src) ":" (number->string (cdr src)) ": ")
|
||||||
|
"")))
|
||||||
|
|
||||||
|
(define (sex-error form message . args)
|
||||||
|
"Signal an error about FORM, prefixed with where it was written."
|
||||||
|
(apply error (string-append (form-location form) message) args))
|
||||||
|
|
||||||
|
(define (stamp-form-source! form src)
|
||||||
|
"Give FORM and every subform that has none the location SRC. Used for
|
||||||
|
macro expansions, which inherit the location of the call site the way a
|
||||||
|
cpp macro does. Forms that already have a location keep it."
|
||||||
|
(when (and src (pair? form))
|
||||||
|
(unless (form-source form)
|
||||||
|
(hash-table-set! +form-sources+ form src))
|
||||||
|
(stamp-form-source! (car form) src)
|
||||||
|
(stamp-form-source! (cdr form) src))
|
||||||
|
form)
|
||||||
|
|||||||
Reference in New Issue
Block a user