Skip to content

Commit

Permalink
extended df() & int() dispatch + show() method
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Sep 30, 2017
1 parent 4ce1030 commit e958ae9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Interface for applying symbolic manipulation on [Julia expressions](https://docs
* reduce expressions are `RExpr` objects that can `parse` into julia `Expr` objects and vice versa;
* interface link communicates and interprets via various reduce output modes using `rcall` method;
* high-level reduce-julia syntax parser-generator walks arbitrary expression to rewrite mathematical code;
* import operators from REDUCE using code generation to apply to arbitrary computational expressions.
* import operators from REDUCE using code generation to apply to arbitrary computational expressions;
* interactive `reduce>` REPL within the Julia terminal window activated by `}` key.

## Setup

Expand All @@ -49,7 +50,8 @@ Reduce expressions encapsulated into `RExpr` objects can be manipulated within j
Sequences of reduce statements are automatically parsed into julia `quote` blocks using the `RExpr` constructor, which can `parse` back into a julia expression.
```Julia
julia> :((x+1+π)^2; int(1/(1+x^3),x)) |> RExpr
"**(+(x,1,pi),2); int(/(1,+(1,**(x,3))),x)"
**(+(x,1,pi),2);
int(/(1,+(1,**(x,3))),x);

julia> rcall(ans,:expand) |> parse
quote
Expand Down
3 changes: 2 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Interface for applying symbolic manipulation on [Julia expressions](https://docs
* reduce expressions are `RExpr` objects that can `parse` into julia `Expr` objects and vice versa;
* interface link communicates and interprets via various reduce output modes using `rcall` method;
* high-level reduce-julia syntax parser-generator walks arbitrary expression to rewrite mathematical code;
* import operators from REDUCE using code generation to apply to arbitrary computational expressions.
* import operators from REDUCE using code generation to apply to arbitrary computational expressions;
* interactive `reduce>` REPL within the Julia terminal window activated by `}` key.

## Setup

Expand Down
8 changes: 4 additions & 4 deletions src/calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ end

for fun in calculus
quote
function $fun(expr::Compat.String,s::Compat.String;be=0)
convert(Compat.String, $fun(RExpr(expr),RExpr(s);be=be))
function $fun(expr::Compat.String,s...;be=0)
convert(Compat.String, $fun(RExpr(expr),s...;be=be))
end
function $fun(expr::Expr,s;be=0)
convert(Expr, $fun(RExpr(expr),RExpr(s);be=be))
function $fun(expr::Expr,s...;be=0)
convert(Expr, $fun(RExpr(expr),s...;be=be))
end
end |> eval
end
26 changes: 9 additions & 17 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function parsegen(fun::Symbol,mode::Symbol)
elseif mode == :switch
:(rcall("$js" |> RExpr, $(string(fun))))
elseif mode == :calculus
:($(string(fun)) * "($js,$s)" |> RExpr |> rcall)
:($(string(fun)) * "($js,$(join(s,',')))" |> RExpr |> rcall)
end
mode != :calculus ? (args = [:(r::RExpr)]) : (args = [:(r::RExpr),:(s::RExpr)])
mode != :calculus ? (args = [:(r::RExpr)]) : (args = [:(r::RExpr),Expr(:...,:s)])
return quote
function $fun($(args...);be=0)
nsr = $arty[]
Expand All @@ -52,18 +52,13 @@ function parsegen(fun::Symbol,mode::Symbol)
(h,state) = next(iter, state)
y = h
(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))))
elseif mode == :calculus
quote
push!(nsr,Compat.String("procedure "*js))
push!(nsr,$rfun(sexpr[y:h],s;be=be) |> string)
end
:(push!(nsr,$rfun(sexpr[y:h],s;be=be) |> string))
else
quote
push!(nsr,Compat.String("procedure "*js))
push!(nsr,$rfun(sexpr[y:h];be=be) |> string)
end
:(push!(nsr,$rfun(sexpr[y:h];be=be) |> string))
end)
elseif contains(sh[en], "begin")
js = join(split(sexpr[h],"begin")[2:end],"begin")
Expand Down Expand Up @@ -108,16 +103,13 @@ function parsegen(fun::Symbol,mode::Symbol)
y = h
(h,state) = bematch(js,sexpr,h,iter,state)
$(if mode == :expr
quote
rp = $rfun(vcat(js,sexpr[y+1:h]...);be=be)
push!(nsr,Expr(:return,rp))
end
:(rp = $rfun(vcat(js,sexpr[y+1:h]...);be=be))
elseif mode == :calculus
:(rp = $rfun(vcat(js,sexpr[y+1:h]...),s;be=be) |> string)
else
:(rp = $rfun(vcat(js,sexpr[y+1:h]...);be=be) |> string)
end)
$(mode != :expr ? :(push!(nsr,"return "*rp)) : :(nothing))
$(mode != :expr ? :(push!(nsr,"return "*rp)) : :(push!(nsr,Expr(:return,rp))))
elseif contains(sh[en],"for")
throw(ReduceError("for block parsing not supported"))
elseif contains(sh[en],"end")
Expand Down Expand Up @@ -161,8 +153,8 @@ function parsegen(fun::Symbol,mode::Symbol)
end
$(if mode == :calculus
quote
$rfun(r::Array{Compat.String,1},s::RExpr;be=0) = $fun(RExpr(r),s;be=be)
$rfun(r,s;be=0) = $fun(r |> Compat.String |> RExpr,s;be=be)
$rfun(r::Array{Compat.String,1},s;be=0) = $fun(RExpr(r),s...;be=be)
$rfun(r,s;be=0) = $fun(r |> Compat.String |> RExpr,s...;be=be)
end
else
quote
Expand Down
6 changes: 3 additions & 3 deletions src/rexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ end
function split(r::RExpr)
n = Array{Compat.String,1}(0)
for h 1:length(r.str)
p = split(replace(r.str[h],r"\$",";"),r"(?<!\!#[0-9a-fA-F]{4});")
p = split(replace(r.str[h],r"(\$)|(;\n)",";"),r"(?<!\!#[0-9a-fA-F]{4});")
for t 1:length(p)
push!(n,p[t])
end; end
Expand All @@ -75,7 +75,7 @@ string(r::RExpr) = convert(Compat.String,r)
show(io::IO, r::RExpr) = print(io,convert(Compat.String,r))

@compat function show(io::IO, ::MIME"text/plain", r::RExpr)
length(r.str) > 1 && (show(string(r)); return nothing)
length(r.str) > 1 && (print(io,string(r)*";"); return nothing)
print(io,rcall(r;on=[:nat]) |> string |> chomp)
end

Expand Down Expand Up @@ -190,7 +190,7 @@ end

convert(::Type{RExpr}, r::RExpr) = r
convert(::Type{Array{Compat.String,1}}, r::RExpr) = r.str
convert(::Type{Compat.String}, r::RExpr) = join(r.str,"; ")
convert(::Type{Compat.String}, r::RExpr) = join(r.str,";\n")
convert{T}(::Type{T}, r::RExpr) = T <: Number ? eval(parse(r)) : parse(r)

"""
Expand Down

0 comments on commit e958ae9

Please sign in to comment.