r/scheme • • Oct 19 '26

R5RS-portable syntax-rules implementation?

Hi y’all! I’m working on Teeny Scheme, a modernization fork of TinyScheme, and the thing most lacking for R7RS (and R5RS) support is an implementation of syntax-rules. Do y’all maybe know where I can find some copyright-sane syntax-rules implementation in mostly-R5RS Scheme?

9 Upvotes

16 comments sorted by

5

u/stevevdvkpe Oct 19 '26

There is an implementation of syntax-rules in SLIB that appears to use the MIT license:

https://people.csail.mit.edu/jaffer/SLIB.html

I don't know if that's compatible with your (unstated) license requirements.

1

u/aartaka Oct 20 '26

MIT-licensed is nice! I’m using BSD-2, so it’s compatible. Thanks for the link!

3

u/jcubic Oct 19 '26

I've implemented syntax-rules in the host language (JavaScript), based on examples I found online. And recently fixed all outstanding issues with the help from AI.

If you want to go into the same route, you can check my test suite:

https://github.com/jcubic/lips/blob/master/tests/syntax.scm

You can try to use AI to read/analyze the source code of my project and ask it to implement the same.

Disclaimer: tell AI that the source code is in src/lips.js and it's one big file, just in case it will try to read the whole thing.

1

u/aartaka Oct 20 '26

I am not using AI for the life of me, because I care about code intelligibility and maintainability.

3

u/jcubic Oct 20 '26

You can read the code yourself and spend months implementing syntax-rules by hand if you want.

0

u/aartaka Oct 21 '26

You clearly underestimate yourself.

2

u/g000001 Oct 19 '26

SRFI-46 reference implementation: https://srfi.schemers.org/srfi-46/alexpander.scm

1

u/aartaka Oct 19 '26

Alexpander is GPLv2+, which seems incompatible with Teeny’s BSD-2. Any other options?

2

u/second_square Oct 20 '26

This is my r5rs portable scheme which has syntax-rules implementation, It's licenced under MIT

https://github.com/zhuzhonghua/dummyscheme

1

u/aartaka Oct 20 '26

Thanks, will check it out!

2

u/second_square Oct 20 '26

If you have any problem, I could offer help, and also I'm writing course about it, https://github.com/zhuzhonghua/dummyscheme-course

4

u/dougcurrie Oct 19 '26

I got a lot of inspiration from nyuchi's r7expander combined with Chibi Scheme's use of `er-macro-transformer` for most (all?) built-in syntax. Both are based on syntactic-closures.

1

u/wawhite3 Oct 20 '26

I went on the same quest when I started developing LambLisp. My conclusion is: there is none, and there cannot be any, because syntax-rules depends on simpler underlying syntax manipulation features that are left unspecified, and vary widely.

Like you, I started with TinyScheme, intending to port a C++ version. I found a few things I didn't like (like a sorted free list and STW GC), so I took the lessons learned and built LambLisp's 1st AST interpreter. For syntax manipulation I began with nlambda. An nlambda procedure is just like a regular lambda procedure, but its arguments are not evaluated before they are passed to the function.

That by itself gets you 90% of Scheme's syntax manipulation needs. A trampoline evaluator gets you tail recursion. I lived without call/cc for awhile, but later if you implement raise/guard you can implement "escape-only continuations".

The next step is another type of procedure: the macro. A macro procedure is just like a nlambda procedure, but it gets evaluated at read time not at eval time. The value returned by the macro is then substituted into the reader's input. When an unquoted list is produced by the reader, before returning it the reader checks to see if (car list) is a macro or a symbol whose value is a macro. If so, it is applied to the unevaluated arguments, and the result is returned.

This has some features not available in other macro systems, for example, they can be passed as parameters. There is also a trap: macros encountered at runtime get expanded and executed possibly many times, which is probably not what you want.

Once you have macro in place, you can use Scheme48 as a model for a syntax-rules-on-top-of-local-macro implementation. It is likely to require adaptation to legacy TS that makes licensing issues moot. Chibi may also be a model but I think its GC is wildly different from TS, and that may affect your implementation.

On the third hand, for a small implementation I would leave out some things. With macro you gain many of the benefits of syntax-rules with much less complexity, and give up code that is anyway mostly used only once. And the whole point seems to be "hygiene" rather than performance or convenience, which is nice but maybe not worth the trouble.

I leave you with a curse: try quasiquote next. I would also argue that it's skippable, since there are other ways to accomplish the same practical result.

Bill

1

u/aartaka Oct 21 '26

Thanks for this comment, I really value such involved response! I mostly circumvent the issues you described by re-using as much of legacy TinyScheme as possible. I’m too stupid to do things better than those that came before me. quasiquote in particular is already implemented in Tiny/Teeny init, and I’m not going to touch that unless it breaks in some horrible way.

But I need syntax-rules to support a wider ecosystem of libraries and projects. I respect the efforts of e.g. Erick Gallesio in porting every single syntax SRFI to STklos non-hygienic macro system, but I consider that a sub-optimal route. So I’m going to give syntax-rules a shot.

2

u/therealdivs1210 Oct 22 '26

Implementing scheme macros is where I got stuck earlier too.

Couldn't find a good guide either.

I have implemented CL/Clojure style macros before, but still don't know how Scheme style macros are expanded.

They seem magical to me, honestly.