Created
January 26, 2017 09:57
-
-
Save simonbyrne/c4146dc286fd5387385ca911e8318509 to your computer and use it in GitHub Desktop.
A julia ccall macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@eval macro $(Symbol("ccall"))(expr) | |
expr.head == :(::) && expr.args[1].head == :call || error("Invalid use of @ccall") | |
rettype = expr.args[2] | |
fname = expr.args[1].args[1] | |
cargs = expr.args[1].args[2:end] | |
arglist = [] | |
typlist = [] | |
tupexpr = :(()) | |
ccexpr = :(ccall($(esc(fname)), $(esc(rettype)), $(esc(tupexpr)))) | |
for carg in cargs | |
carg.head == :(::) || error("Invalid use of @ccall") | |
push!(ccexpr.args, :($(esc(carg.args[1])))) | |
push!(tupexpr.args, carg.args[2]) | |
end | |
ccexpr | |
end | |
@ccall (:sin,:libm)(1.0::Float64)::Float64 | |
for (T,suf) in ((Float64,""), (Float32,"f")) | |
@eval xsin(x::$T) = @ccall ($(string(:sin,suf),:libm))(x::$T)::$T | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment