Skip to content

Commit

Permalink
more support for unicode input and output
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed Oct 14, 2016
1 parent 85ecaaf commit adda58a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/AST_translation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,21 @@ rewrite_division(ex::Expr) = Expr(:call, :*, ex.args[2], Expr(:call,:^,ex.args[3
# also, '<:' is the head in v0.5. We don't yet handle this
rewrite_to_comparison(ex::Expr) = Expr(:comparison, ex.args[2], ex.args[1], ex.args[3])

# These symbols are unconditionally translated on input
# This is almost exactly the Dict unicode_translation. Maybe we can refactor.
const INSYMTRANS = Dict( : => :(=>),
=> :Pi,
: => :(>=),
: => :(<=),
=> :EulerGamma,
=> :Gamma,
:𝕖 => :E,
:𝕚 => :I,
:∞ => :Infinity,
: => :!=,
: => :Element
)

# There is no binary minus, no division, and no sqrt in Mxpr's.
# Concrete example: a - b --> a + -b.
# We definitely need to dispatch on a hash query, or types somehow
Expand All @@ -302,6 +317,9 @@ function rewrite_expr(ex::Expr)
if is_type(x,Expr) && x.head == :macrocall
ex.args[i] = eval(x)
end
if haskey(INSYMTRANS,x)
ex.args[i] = INSYMTRANS[x]
end
end
if is_call(ex) && length(ex.args) > 0 && is_comparison_symbol(ex.args[1])
ex = rewrite_to_comparison(ex)
Expand Down
6 changes: 4 additions & 2 deletions src/AST_translation_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ const unicode_translation = Dict{Symbol,Symbol}(:π => :Pi,
:𝕚 => :I,
: => :>=,
: => :<=,
: => :!= )
: => :!=,
:𝕖 => :E,
: => :(=>) )

# Output. Reverse dict for printing if unicode printing is enabled
const unicode_output = Dict{Symbol,Symbol}()
Expand Down Expand Up @@ -121,7 +123,7 @@ const OPTYPE = Dict{Symbol,Symbol}()

for op in (:(=), :(:=), :(=>), :Rule , :RuleDelayed, :Power, :(.>),
:Set, :SetDelayed, :UpSet, :(*=), :(+=),
:TimesBy, :AddTo ) # need :Set here
:TimesBy, :AddTo) # need :Set here
OPTYPE[op] = :binary
end

Expand Down
12 changes: 7 additions & 5 deletions src/output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end

function binaryopspc(ss)
s = string(ss)
(s == "=>" || s == "->" || s == ":>" || s == "^:=") && return " "
(s == "=>" || s == "->" || s == ":>" || s == "^:=" || s == "") && return " "
return ""
end

Expand All @@ -42,7 +42,9 @@ Istring() = getkerneloptions(:unicode_output) ? "𝕚" : "I"
# It depends on which positions unicode symbols may occur.
function outsym(s)
haskey(unicode_output, s) && getkerneloptions(:unicode_output) && return unicode_output[s]
mtojsym(s)
os = mtojsym(s)
haskey(unicode_output, os) && getkerneloptions(:unicode_output) && return unicode_output[os]
os
end

function fullform(io::IO, mx::Mxpr)
Expand Down Expand Up @@ -165,7 +167,7 @@ Base.show(io::IO, x::WORational) = show_rational(io,x.x)
function show_rational(io::IO, x::Rational)
show(io, num(x))
print(io, "/")
show(io, den(x))
show(io, den(x))
end

# I don't want to create a table of latex to unicode conversions.
Expand Down Expand Up @@ -217,7 +219,7 @@ function show_complexrational{T<:Integer}(io::IO, z::Complex{Rational{T}})
if imag(z) == 1
print(io,Istring())
else
print(io,Istring(), " * ")
print(io,Istring(), " * ")
show_rational(io,imag(z))
end
end
Expand Down Expand Up @@ -296,7 +298,7 @@ function show_binary(io::IO, mx::Mxpr)
if length(mx) != 2
show_prefix_function(io,mx)
else
opstr = mtojsym(mhead(mx))
opstr = outsym(mhead(mx))
lop = mx[1]
if needsparen(lop)
print(io,"(")
Expand Down

0 comments on commit adda58a

Please sign in to comment.