Created
September 10, 2019 20:47
-
-
Save postspectacular/c61342deb1b4c6652f56ce574fd89bd1 to your computer and use it in GitHub Desktop.
S-expr tokenizer
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 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