Skip to content

Instantly share code, notes, and snippets.

@takeshik
Created April 25, 2012 08:49
Show Gist options
  • Save takeshik/2488281 to your computer and use it in GitHub Desktop.
Save takeshik/2488281 to your computer and use it in GitHub Desktop.
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