Created
November 5, 2014 16:09
-
-
Save missett/c965e06950198fc5e245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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