Skip to content

Commit 79e64b1

Browse files
Merge pull request #48 from Keno/kf/mapexpr
Use proper `mapexpr` include feature for rewriting included expressions
2 parents 3f29769 + 60123c4 commit 79e64b1

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/SaferIntTypes.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function changetype(ex::Expr)
6161
elseif isa(ex, Integer)
6262
return Int === Int64 ? SafeInt64(ex) : SafeInt32(ex)
6363
elseif (Meta.isexpr(ex, :call, 2) && isa(ex.args[2], Integer))
64-
return changetype_of_cast_int(ex)
64+
return changetype_of_cast_int(ex)
6565
elseif (Meta.isexpr(ex, :(=), 2) && isa(ex.args[2], Integer) && isa(ex.args[1], Symbol))
6666
ex.args[2] = Int === Int64 ? SafeInt64(ex.args[2]) : SafeInt32(ex.args[2])
6767
return ex
@@ -76,10 +76,25 @@ function changetype(ex::Expr)
7676
end
7777
end
7878

79+
if VERSION < v"1.5"
80+
# calls to include(f) are changed to include_mapexpr_compat(modul, f) so that
81+
# @changetype can apply recursively to included files.
82+
function include_mapexpr_compat(modul, filename::AbstractString)
83+
# use the undocumented parse_input_line function so that we preserve
84+
# the filename and line-number information.
85+
s = string("begin; ", read(filename, String), "\nend\n")
86+
expr = Base.parse_input_line(s, filename=filename)
87+
Core.eval(modul, changetype(expr))
88+
end
89+
end
7990

8091
function changetypes(ex::Expr)
8192
if Meta.isexpr(ex, :call, 2) && ex.args[1] == :include
82-
return :($include(@__MODULE__, $(ex.args[2])))
93+
if VERSION < v"1.5"
94+
return Expr(:call, include_mapexpr_compat, :(@__MODULE__), ex.args[2:end]...)
95+
else
96+
return Expr(ex.head, ex.args[1], changetype, ex.args[2:end]...)
97+
end
8398
elseif Meta.isexpr(ex, :call) && ex.args[1] in changefuncs
8499
return Expr(:call, Core.eval(SaferIntTypes, ex.args[1]), changetype.(ex.args[2:end])...)
85100
elseif Meta.isexpr(ex, :., 2) && ex.args[1] in changefuncs && Meta.isexpr(ex.args[2], :tuple)
@@ -90,7 +105,7 @@ function changetypes(ex::Expr)
90105
end
91106
end
92107

93-
function changetype_of_cast_int(ex::Expr)
108+
function changetype_of_cast_int(ex::Expr)
94109
if @capture(ex, Int64(_))
95110
ex.args[1] = :SafeInt64
96111
elseif @capture(ex, Int32(_))
@@ -115,7 +130,7 @@ function changetype_of_cast_int(ex::Expr)
115130
return ex
116131
end
117132

118-
function changetype_of_assigned_cast_int(ex::Expr)
133+
function changetype_of_assigned_cast_int(ex::Expr)
119134
if @capture(ex, _=Int64(_))
120135
ex.args[2].args[1] = :SafeInt64
121136
elseif @capture(ex, _=Int32(_))
@@ -140,16 +155,6 @@ function changetype_of_assigned_cast_int(ex::Expr)
140155
return ex
141156
end
142157

143-
# calls to include(f) are changed to include(T, modul, f) so that
144-
# @changetype can apply recursively to included files.
145-
function include(modul, filename::AbstractString)
146-
# use the undocumented parse_input_line function so that we preserve
147-
# the filename and line-number information.
148-
s = string("begin; ", read(filename, String), "\nend\n")
149-
expr = Base.parse_input_line(s, filename=filename)
150-
Core.eval(modul, changetype(expr))
151-
end
152-
153158
for F in binaryfuncs
154159
# @eval $F(T, args...) = Base.$F(changetype.(T, args...)...,)
155160
@eval Base.$F(::Type{S}, ::Type{I}, n) where {S<:SFInt, I<:HWInt} = $F(S, n)

0 commit comments

Comments
 (0)