Skip to content

Commit

Permalink
Merge pull request #72 from hyrodium/feature/deparse
Browse files Browse the repository at this point in the history
Add `@deparse` macro
  • Loading branch information
terasakisatoshi authored Oct 20, 2023
2 parents da8c4b1 + b0063c4 commit b13fc47
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ sections = [
options=``,
mode=:asciinema,
),
(
name="deparse_macro",
title="`@deparse` macro",
options=``,
mode=:asciinema,
),
],
"More in REPL" => [
(
Expand Down
22 changes: 22 additions & 0 deletions examples/deparse_macro/app.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Replay

i1 = @deparse println("Hello World")
i2 = @deparse using LinearAlgebra
i3 = @deparse begin
x = [1, 1]
A = [1 0; 0 2]
end
i4 = @deparse @show dot(x, A, x)
i5 = @deparse function f(x)
@comment This is a comment
if x > 0
@comment x is larger than 0
@info "x is larger than 0"
end
@info "compute 2x + 2"
2x + 2
end
i6 = @deparse x = 3
i7 = @deparse f(x)

replay([i1, i2, i3, i4, i5, i6, i7], use_ghostwriter=true)
44 changes: 44 additions & 0 deletions src/Replay.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,50 @@ const LEFT_ARROW = "\e[D"
export CTRL_C, TAB
export UP_ARROW, DOWN_ARROW, RIGHT_ARROW, LEFT_ARROW
export replay
export @deparse

@doc raw"""
@deparse expr
Create a string from `expr`. It tries to output something similar to we type in Julia REPL.
If `expr` contains `@comment <message>`, it is transformed into `# <message>`.
# Examples
```jldoctest
julia> @deparse 1+1
"1 + 1"
julia> @deparse sin(1f0+a)
"sin(1.0f0 + a)"
julia> @deparse f(x) = sin(x)/x # Sometimes not work as expected
"f(x) = begin\n sin(x) / x\n end\n"
```
"""
macro deparse(expr)
Base.remove_linenums!(expr)
lines = map(split(string(expr), "\n")) do w
# check that line `w` contains a macro expression
m = match(r"@", w)
if isnothing(m)
w
else
# Here string object `w` has a form of "<indent>#= ... =# @<macro> ..."
# extract <indent>
indent = replace(
w[begin:match(r"\s?#|$", w).offset],
"#" => "" # remove `#`
)
# remove `#= ... =#`
# If `@<macro>` is equal to `@comment`, we replace `@<macro>` with `#`.
indent * replace(w[m.offset:end], "@comment" => "#")
end
end
if length(lines) > 1
join(lines, "\n") * "\n"
else
join(lines, "\n")
end
end

function clearline(; move_up::Bool=false)
buf = IOBuffer()
Expand Down
90 changes: 90 additions & 0 deletions test/inst.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
@testset "@deparse 1" begin
ref = "1"
tar = @deparse 1
@test tar == ref
end

@testset "@deparse @show x" begin
ref = "@show x"
tar = @deparse @show x
@test tar == ref
end

@testset "@deparse println(\"Hello World\")" begin
ref = "println(\"Hello World\")"
tar = @deparse println("Hello World")
@test tar == ref
end

@testset "using LinearAlgebra: dot" begin
ref = "using LinearAlgebra: dot"
tar = @deparse using LinearAlgebra: dot
@test tar == ref
end

# We don't support the expression yet.
@testset "f(x) = x" begin
ref = "f(x) = x"
tar = @deparse f(x) = x
@test_broken tar == ref
end

@testset "function definition" begin
ref = """
function f(x)
y = 2x + 1
z = y ^ 3
return y
end
"""
tar = @deparse function f(x)
y = 2x + 1
z = y^3
return y
end
@test ref == tar
end

@testset "function definition with comments" begin
ref = """
function f(x)
# linear transformation
y = 2x
# square root of y
z = √y
return y
end
"""
tar = @deparse function f(x)
@comment linear transformation
y = 2x
@comment square root of y
z = y
return y
end
@test ref == tar
end

@testset "@comment" begin
ref = """
function f(x)
# This is a comment
if x > 0
# x is larger than 0
@info "x is larger than 0"
end
@info "compute 2x + 2"
2x + 2
end
"""
tar = @deparse function f(x)
@comment This is a comment
if x > 0
@comment x is larger than 0
@info "x is larger than 0"
end
@info "compute 2x + 2"
2x + 2
end
@test tar == ref
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ end
@test out == ref
end

include("inst.jl")

#=
using Replay, Test
endexamples_dir = joinpath(pkgdir(Replay), "examples")
Expand Down

0 comments on commit b13fc47

Please sign in to comment.