npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

macros.coffee

v0.0.1

Published

Down-and-dirty, 100-line Lisp-style macros in CoffeeScript

Downloads

8

Readme

macros.coffee

Down-and-dirty, 100-line (cake loc shows 87, actually) implementation of Lisp-style macros for CoffeeScript. Annotated Source

You can install it like so:

npm install macros.coffee

And then require() it before requiring any file(s) that use macros:

require "macros.coffee"
app = require "my_application_that_uses_macros" 

app.start()

Or, to compile to javascript files from the command line (currently very flaky):

$ macros.coffee app/*.coffee

Examples

Some examples can be seen in the /test directory. Use of quote, backquote, macro-defining-macros, call-with-current-callback (cc()) ...

Writing Macros

Macros are functions that operate on nodes of a program's Abstract Syntax Tree (AST). Here's a basic macro.

mac foo (nodes)->
  CoffeeScript.nodes "x = 2"

foo() becomes x = 2;.

More complex or messy macros may want access to two other arguments that are passed in every macro: the parent node, and the global Macros object containing all of the macro definitions.

Basic Macro Tools: Quote, Backquote and Gensyms

A macro that replaces occurrences of x with a 2:

mac two_for_x (n)->
  backquote (x:2), n

two_for_x( y = x ) # produces y = 2

Backquote is a function that performs the implied replacement on an AST. Quote, on the other hand, is a macro, not a function because it needs to do this:

node1 = quote -> 2 + 2
node2 = CoffeeScript.nodes "2+2"
node1.compile() is node2.compile()

Here's a useless example that assigns x, y, and z to a value:

mac assign_xyz ({args:[val,rest...]})->
  backquote {val:val}, quote ->
    x = y = z = val

assign_xyz 12

... why???

I wrote this because I really, REALLY wanted to write macros in CoffeeScript. In Ruby, the buy-in to message-passing goes deep, and you can override most of the language, but in JS/CS metaprogramming is limited to tricks on this.

If you want to fork this and make it better, or build a substantially more 'heavy-duty' implementation, I'll probably end up using yours.