Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Created September 10, 2019 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postspectacular/c61342deb1b4c6652f56ce574fd89bd1 to your computer and use it in GitHub Desktop.
Save postspectacular/c61342deb1b4c6652f56ce574fd89bd1 to your computer and use it in GitHub Desktop.
S-expr tokenizer
function tokenizeSexpr(src) {
let curr = "";
const scopes = [[]];
const $word = () => {
if (curr) {
scopes[scopes.length - 1].push(curr);
curr = "";
}
};
for(let i = 0; i < src.length; i++) {
const c = src[i];
if(c === "(") {
$word();
scopes.push([]);
} else if (c === ")") {
$word();
const x = scopes.pop();
scopes[scopes.length - 1].push(x);
} else if (/\s/.test(c)) {
$word();
} else {
curr += c;
}
}
$word();
return scopes[0];
}
tokenize("(+ a1 b1 c1 (* (sum c2:e3) 10))")[0]
// [ '+', 'a1', 'b1', 'c1', [ '*', [ 'sum', 'c2:e3' ], '10' ] ]
tokenize("((fn (a b) (+ a b)) 1 2)")[0]
// [ [ 'fn', [ 'a', 'b' ], [ '+', 'a', 'b' ] ], '1', '2' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment