Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
onetonfoot committed May 13, 2020
1 parent cf6c404 commit 6841736
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
25 changes: 6 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
[![Build Status](https://travis-ci.com/onetonfoot/ExprManipulation.jl.svg?branch=master)](https://travis-ci.com/onetonfoot/ExprManipulation.jl)
[![Coverage Status](https://coveralls.io/repos/github/onetonfoot/ExprManipulation.jl/badge.svg?branch=master)](https://coveralls.io/github/onetonfoot/ExprManipulation.jl?branch=master)

ExprManipulation provides tools for manipulating expression based on the Expr syntax.
ExprManipulation provides tools for manipulating expressions based on the Expr syntax.

# Intro

There are 4 constructs to help `MExpr`, `Capture`, `Slurp` and `transform`, it's easier to illustarte with and example so.

The API is small there are only 4 constructs `MExpr`, `Capture`, `Slurp` and `transform`.

## Equality

```julia
using ExprManipulation
using Base.Meta: show_sexpr

expr = :(x + 1)
show_sexpr(expr)
# (:call, :+, :x, 1)
Expand All @@ -36,8 +37,7 @@ match(m_expr, expr)
#(x = :x, n = 1)
```

`Slurp` allows you to capture a variable number of arguments. It can be used anywhere in the expression but
only a single `Slurp` per an `MExpr`.
`Slurp` allows you to capture a variable number of arguments. It can be used anywhere in the expressions args but only a single `Slurp` per an `MExpr`.

```julia
m_expr = MExpr(:tuple, Capture(:first_number), Slurp(:args), Capture(:last_number))
Expand Down Expand Up @@ -69,19 +69,6 @@ match(vec_or_tuple, :((1,"2",3)))

Transform can be used to create a new expression, it applies a function to each node in the Expr tree starting from the leaves. For example to replace the power call with plus.

```julia
input_expr = :( 2 + 2 ^ 5 * 2)
match_power = MExpr(:call, :^, Slurp(:args))
transform(input_expr) do node
matches = match(match_power, node)
!isnothing(matches) ? Expr(:call, :+, matches[:args]...) : node
end
# :(2 + (2 + 5) * 2)

```

Or replace all the numbers with 1

```julia
transform(input_expr) do node
node isa Number ? 1 : node
Expand All @@ -91,7 +78,7 @@ end

# Examples

For more in-depth real examples see the examples folder.
For more in-depth examples see the examples folder.

# Related Packages

Expand Down
1 change: 0 additions & 1 deletion example/slurp.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ExprManipulation
using Base.Meta

match_slurp = MExpr(:..., Capture(:var))
match_assign = MExpr(:(=), MExpr(:tuple, Slurp(:lexprs)), Capture(:rexpr))
Expand Down

0 comments on commit 6841736

Please sign in to comment.