Created
April 25, 2012 08:49
-
-
Save takeshik/2488281 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
private static IEnumerable<Expression> Unquote(Expression expression, SymbolTable symbols, Int32 level) | |
{ | |
return (expression as ListExpression).Null(el => el[0].Id().Let(i => i == "quasiquote" | |
? Unquote(el[1], symbols, ++level) | |
: i == "unquote" | |
? EnumerableEx.Generate( | |
el, | |
_ => _ != null && _[0].Id() == "unquote", | |
_ => _[1] as ListExpression, | |
_ => _[1] | |
) | |
.ToArray() | |
.Let(es => Enumerable.Range(0, level - es.Length) | |
.Aggregate(es.Last(), (e_, _) => Quote(symbols, e_)) | |
) | |
.If(_ => _ is IdentifierExpression && !(((IdentifierExpression) _).Reduce(symbols).Type == typeof(IdentifierExpression)), _ => Function(symbols, "const", _)) | |
.Let(EnumerableEx.Return) | |
: i == "unquote-splicing" | |
? Enumerable.Range(0, 10).Select(x => Constant(x)) | |
: EnumerableEx.Return<Expression>(Function(symbols, "list", el.Elements.SelectMany(e => Unquote(e, symbols, level)))) | |
)) ?? EnumerableEx.Return( | |
(expression as VectorExpression).Null(ev => Vector(symbols, ev.Elements.SelectMany(e => Unquote(e, symbols, level)))) ?? | |
(expression as LambdaListExpression).Null(el => LambdaList(symbols, el.Elements.SelectMany(e => Unquote(e, symbols, level)))) ?? | |
(Expression) Quote(symbols, expression) | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment