Skip to content

Instantly share code, notes, and snippets.

@werelax
Last active December 18, 2015 11:39
Show Gist options
  • Select an option

  • Save werelax/5777042 to your computer and use it in GitHub Desktop.

Select an option

Save werelax/5777042 to your computer and use it in GitHub Desktop.
The laziest Lisp lexer in history.
function lexer(code) {
var strings = [],
normalize = function(code) {
return code.replace(/\(/g, " ( ")
.replace(/\)/g, " ) ")
.replace(/\s\s*/g, " ")
.replace(/^\s*/g, "")
.replace(/\s*$/g, "");
};
code = code.replace(/(".*?")/g, function(str) { return "ç" + (strings.push(str) - 1) + "ç"; });
code = normalize(code)
.replace(/([a-zA-Zñ_\-\+\?\*\>\<\/\%=#]\S*)/g, "\"$1\"")
.replace(/ç(\d)ç/g, "strings[$1]")
.replace(/\(\s*/g, "[")
.replace(/\s*\)/g, "]")
.split(" ").join(", ");
return eval(code);
}
@werelax
Copy link
Copy Markdown
Author

werelax commented Jun 13, 2013

Caveats:

  • Can't handle " inside strings

What does this thing do?

It's just a silly lexer. For example:

lexer('((lambda (a b) (+ a b)) 12 "buenos días")')

gives you:

[["lambda",["a","b"],["+","a","b"]],12,"\"buenos días\""]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment