Skip to content

Instantly share code, notes, and snippets.

@simonbyrne
Created January 26, 2017 09:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbyrne/c4146dc286fd5387385ca911e8318509 to your computer and use it in GitHub Desktop.
Save simonbyrne/c4146dc286fd5387385ca911e8318509 to your computer and use it in GitHub Desktop.
A julia ccall macro
@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