Creature Comforts and First Light!


So, attached to this post, you should see a screenshot of the first look at the playfield. This is actually being drawn into the canvas every 60th of a second (or 120th, if you're so lucky to have a newer machine). Not *very* efficient, but good for working stuff out. To get here, I wound up adding some bits and pieces to Urlang to make it easier to deal with.

Macros?!?

Urlang is almost magical with how it works, but there are some puzzling omissions -- or at least I haven't found how to turn them on yet. One of the biggies is quoted values, which is how I usually set up data. For instance, I can define the playfield like this:

   (define layout '((0 0 0 1 0 0 0)
                    (0 0 0 1 1 0 0)
                    (0 0 1 1 1 0 0)
                    (0 0 1 1 1 1 0)
                    (0 1 1 1 1 1 0)
                    (0 1 1 1 1 1 1)
                    (1 1 1 1 1 1 1)))

This is pretty straightforward. In JavaScript, it would just be a nested list of lists. Square brackets instead of round. 

However, it seems that Urlang doesn't know how to deal with quoting -- at all. So, it was time to investigate the <tt>define-urlang-macro</tt> mechanism for extending the compiler. This is really nicely done and works completely with all the built-in Racket syntax definition machinery. My initial implementation just converted <tt>'(x y z)</tt> to <tt>(array x y z)</tt>, but after consultation with @BenKenobi and @AlexKnauth on the Racket Discord, I wound up with a much more comprehensive macro:

(define-urlang-macro quote
  (syntax-parser
    [(_ (e ...))   #'(array (quote e) ...)]
    [(_ e:number)  #'e]
    [(_ e:string)  #'e]
    [(_ e:id)      (datum->syntax this-syntax
                                  (symbol->string (syntax->datum #'e)))]
    ))

This macro actually handles pretty much all of the common cases of using quote. And... for a Lisp macro, it's pretty straightforward. Part of that is because syntax-parser is magic and pretty smart. Basically, it's just a list of patterns to look out for (The "_" character is the placeholder for the "quote" name) and what to do with them. Things got a little gnarly with symbols because I wanted to convert them into plain strings, but everything else was really straightforward.

Once it was working, a whole bunch of stuff got a lot easier. Not just the layout, but also specifying polygon point lists for drawing and stuff like that.

Fabulous For Loops

I won't say much about this for now because I'm really tired, but the urlang/for package includes some super-spiffy for loop goodness that takes a lot of the pain out of looping. The author calls them "Racket style", but they have some quirks from what you might be used to. The main one being that the clauses need some keywords to help the compiler figure out what it's supposed to do. 

Reading the Urlang source helped a lot here. There's some extra documentation that's not on the main page.

Get Qube*Cat

Leave a comment

Log in with itch.io to leave a comment.