Another Fun House Language Called Context-Free

September 25th 14:06
by why

This tapestry of circles, believe it or not, was hacked out in just 12 lines of code.

startshape LIMIT
rule LIMIT {
  4*{r 90 h 90} QUAD [r 45 x .5 y .5 h 45 sat 1 b 1 |b 0]
rule SHAPE {
  SQUARE{a -.5}
  CIRCLE{s .8 sat -1 b 1 a -.5 z 1}
rule QUAD {
  SHAPE{} 2*{x -1 y 1}
  QUAD [x .5 y -.5 s .618 x .5 y .5 b .15| h 90]

This is Square Limit I by AlliedEnvy and it’s written using the Context-Free Design Grammar. Which is basically LOGO with recursion and functions. You run your code and you get back a PNG.

This stuff really lights me up. So, there are three functions in this drawing: LIMIT, SHAPE and QUAD. The most basic part is the SHAPE, which is just a circle and square. The QUAD function is a neat little tree-like function which draws a series of SHAPE and then calls itself. The LIMIT rotates 90 degrees and draws four QUAD.

Then, from the commandline:

$ cfdg sqlimit1.cfdg sqlimit1.png

The trickiest part of all this is just how succinct it is. All the shape properties are listed here. But while x and y are obvious, attrs like size can be abbreviated as s and rotate can be abbreviated as r.

What really grabs me is that Context-Free is this tiny, unassuming language of just thirty or so parts. And it stops right there. It’s so tight and minimal. So you can create some rather delightful eyecandy in just ten or so lines. Some other really short examples are Alhambra, Spike, BW Circles, Hex Cloud, Orbido and Youthfulness.



said on September 25th 16:41

Are you going to port any of this to Shoes? It seems like it would be pretty straightforward!

Matt B

said on September 25th 17:52

Logo does have functions and recursion, by the way.


said on September 25th 18:43

I think the key difference to Logo is that ContextFree is entirely declarative (versus Logo’s very procedural approach to drawing), and that it is not turing-complete (assuming no features were added which allow you to write context-dependent grammars).

This would be a cool thing to have in Shoes, really.


said on September 25th 19:03

Oh, here we go: an online interp. With a cheat sheet.


said on September 25th 23:00

I saw cfdg about two years ago…
it blew me away, especially some user-submitted .cfdg files. It was one of my favourite apps :)


said on September 25th 23:00

This is a great pattern for a quilt. Beautiful colours. I bet I can make this quilt out of scraps that I already have.

Oliver Steele

said on September 26th 13:02

I love cfdg. I wrote Ruby and JavaScript interpreters for it a couple of years ago; I’ll clean them up and post them this weekend in case anyone wants to do anything with them.

Travis Reitter

said on September 26th 13:46

This reminds me a lot of the syntax of POV-Ray, except 2D. Either way, it gets even more interesting when you generate this code from other programs :)


said on September 26th 15:21

Oliver: anything that generates SVG, out of curiosity? Something I’ve wanted to see for a long time is a CFDG extension for Inkscape.


said on September 26th 21:47

Logo is basically CFDG but with colors!


said on October 4th 15:38

I came up with this after learning a bit of Nodebox and CFDG.

# Nodebox version of Square Limit I
# Adapted by Zellyn Hunter

sz = 700
g = 1.61803399

translate(sz/2, sz/2)

def circles(hue, iter):
    if (iter < 11):
        fill(hue%1, 1.0, 0.85**iter, 0.5)
        fill(0.0, 0.0, 1.0, 0.5)
        translate(0, g)
        circles(hue+0.25, iter+1)
        circles(hue+0.25, iter+1)

for i in range(4):
    circles(i/4.0 + 0.125, 0)


said on October 7th 22:55

Another interesting doohicky out there for the 3D realm is
(More related to hacketyhack than CFDG, but there it is)

Comments are closed for this entry.