Skip to content

Instantly share code, notes, and snippets.

@missett
Created November 5, 2014 16:09
Show Gist options
  • Save missett/c965e06950198fc5e245 to your computer and use it in GitHub Desktop.
Save missett/c965e06950198fc5e245 to your computer and use it in GitHub Desktop.
function parse(input) {
var i = 0;
function descend(input) {
var escape = false,
atom = '',
atomSet = [];
for(i; i<input.length; i++) {
switch(input[i]) {
case '(':
i += 1;
atomSet.push( descend( input ) );
break;
case ')':
if(atom.length > 0)
atomSet.push(atom);
escape = true;
break;
case ' ':
if(atom.length > 0)
atomSet.push(atom);
atom = '';
break;
default:
atom += input[i];
break;
}
if(escape)
break;
}
return atomSet;
}
return descend(input);
}
var t = parse("(a (b c))(d e f)");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment