Skip to content

Commit

Permalink
fixed Meta.parse syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Nov 21, 2017
1 parent 0b17651 commit b9c4e2e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function parsegen(fun::Symbol,mode::Symbol)
argfun = Symbol(fun,"_args")
arty = (mode == :expr) ? :Any : :(Compat.String)
exec = if mode == :expr
:(parse(RSymReplace(js)))
:(Meta.parse(RSymReplace(js)))
elseif mode == :unary
:($(string(fun)) * "($js)" |> RExpr |> rcall)
elseif mode == :switch
Expand Down Expand Up @@ -77,7 +77,7 @@ function parsegen(fun::Symbol,mode::Symbol)
(h,state) = bematch(sexpr[h],sexpr,h,iter,state)
$(mode != :expr ? :(push!(nsr,Compat.String("procedure "*js))) : :(nothing))
$(if mode == :expr
:(push!(nsr,Expr(:function,parse(js),rparse(sexpr[y:h];be=be))))
:(push!(nsr,Expr(:function,Meta.parse(js),rparse(sexpr[y:h];be=be))))
elseif mode == :calculus
:(push!(nsr,$rfun(sexpr[y:h],s;be=be) |> string))
else
Expand Down Expand Up @@ -261,7 +261,7 @@ function parsegen(fun::Symbol,mode::Symbol)
:(qr = qr * smp; smp = "")
end))
end
push!(nsr,$((mode == :expr) ? :(u = "("*qr*")" |> parse |> linefilter) : :qr))
push!(nsr,$((mode == :expr) ? :(u = "("*qr*")" |> Meta.parse |> linefilter) : :qr))
end; else; :(nothing); end)
elseif contains(sh[en],"end")
nothing
Expand All @@ -270,7 +270,7 @@ function parsegen(fun::Symbol,mode::Symbol)
elseif contains(sexpr[h], ":=")
sp = split(sexpr[h], ":=")
$(if mode == :expr
:(push!(nsr,Expr(:(=),parse(sp[1]),rparse(sp[2];be=be))))
:(push!(nsr,Expr(:(=),Meta.parse(sp[1]),rparse(sp[2];be=be))))
elseif mode == :calculus
:(push!(nsr, Compat.String(sp[1]) * ":=" * string($rfun(sp[2] |> Compat.String |> RExpr,s;be=be))))
else
Expand Down Expand Up @@ -414,16 +414,16 @@ end
parsegen(:parse,:expr) |> eval

@doc """
parse(r::RExpr)
Reduce.parse(r::RExpr)
Parse a Reduce expression into a Julia expression
# Examples
```julia-repl
julia> parse(R\"sin(i*x)\")
julia> Reduce.parse(R\"sin(i*x)\")
:(sin(im * x))
```
""" parse
""" Reduce.parse

function print_args(io::IO,a::Array{Any,1})
print(io, "(")
Expand Down
2 changes: 1 addition & 1 deletion src/rexpr.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is part of Reduce.jl. It is licensed under the MIT license
# Copyright (C) 2017 Michael Reed

export RExpr, @RExpr, @R_str, parse, rcall, @rcall, convert, ==, getindex, string, show
export RExpr, @RExpr, @R_str, rcall, @rcall, convert, ==, getindex, string, show
import Base: parse, convert, ==, getindex, *, split, string, show

"""
Expand Down
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using Base.Test
@test display(RExpr("(x+i)^3")) == nothing; print('\n')
@test Reduce._syme(Reduce.r_to_jl) |> typeof == String
@test R"x+2" == R"2+x-1+1"
@test :((x+1+π)^2; int(1/(1+x^3),x)) |> RExpr |> parse |> typeof == Expr
@test :((x+1+π)^2; int(1/(1+x^3),x)) |> RExpr |> Reduce.parse |> typeof == Expr
@test !Base.process_exited(Reduce.rs)
@test string(R"x+1") |> typeof == String
@test RExpr(:x) == R"x"
Expand All @@ -22,13 +22,13 @@ using Base.Test
@test "1"*R"1"*"1" == R"1;1;1"
@test (x = :(x^2+2x+1); rcall(x,off=[:factor]) == x)
@test rcall("x + 1","factor") == "x + 1"
@test Expr(:function,:fun,:(return begin; x = 700; y = x; end)) |> RExpr |> parse |> typeof == Expr
@test Expr(:function,:fun,:(return begin; x = 700; y = x; end)) |> RExpr |> Reduce.parse |> typeof == Expr
@test Expr(:for,:(i=2:34),:(product(i))) |> rcall |> eval |> typeof == BigInt
@test try; Expr(:type,false,:x) |> RExpr; false; catch; true; end
@test try; :(@time f(x)) |> RExpr; false; catch; true; end
@test (x = Expr(:function,:fun,:(return y=a^3+3*a^2*b+3*a*b^2+b^3)); x==x |> Reduce.factor |> expand)
@test try; Expr(:for,:(i=2:34),:(product(i))) |> RExpr |> parse; false; catch; true; end
@test R"begin; 1:2; end" |> parse |> RExpr |> string == "1:2 "
@test try; Expr(:for,:(i=2:34),:(product(i))) |> RExpr |> Reduce.parse; false; catch; true; end
@test R"begin; 1:2; end" |> Reduce.parse |> RExpr |> string == "1:2 "
@test latex(:(x+1)) |> typeof == String
@test length(:(x+y)) |> typeof == Int
@test log(:(e^x)) == :x
Expand All @@ -42,11 +42,11 @@ using Base.Test
@test Reduce.parsegen(:switchtest,:switch) |> typeof == Expr
@test Reduce.parsegen(:unarytest,:unary) |> typeof == Expr
!(VERSION < v"0.6.0") && is_linux() && @test Reduce.RSymReplace("!#03a9; *x**2 + !#03a9;") |> typeof == String
@test :((x+im+π)^2; int(1/(1+x^3),x)) |> RExpr |> rcall |> parse |> typeof == Expr
@test :((x+im+π)^2; int(1/(1+x^3),x)) |> RExpr |> rcall |> Reduce.parse |> typeof == Expr
@test :(int(sin(im*x+pi)^2-1,x)) |> rcall |> typeof == Expr
@test int(:(x^2+y),:x) |> RExpr == int("x^2+y","x") |> RExpr
@test R"/(2,begin 2; +(7,4); return +(4,*(2,7))+9 end)" |> parse |> typeof == Expr
@test R"/(2,begin 2; +(7,4); return +(4,*(2,7))+9 end)" |> Reduce.parse |> typeof == Expr
@test df(Expr(:function,:fun,:(return begin; zn = z^2+c; nz = z^3-1; end))|>RExpr,:z) |> typeof == RExpr
@test :([1 2; 3 4]) |> RExpr |> parse |> RExpr == [1 2; 3 4] |> RExpr
@test :([1 2; 3 4]) |> RExpr |> Reduce.parse |> RExpr == [1 2; 3 4] |> RExpr
println()
#@test Reduce.repl_init(Base.active_repl)==nothing

0 comments on commit b9c4e2e

Please sign in to comment.