Create QuoteNode in Expr

I’m trying to build this


ccall((:u3,"./u1.so"),Float64,(Float64,),x)

as expression, so i can eval (it’s a longer story of library/code adaptation …)

julia> u1 = "ccall((:u3,\"./u1.so\"),Float64,(Float64,),x)"
"ccall((:u3,\"./u1.so\"),Float64,(Float64,),x)"

julia> e0 = parse(u1)
:(ccall((:u3,"./u1.so"),Float64,(Float64,),x))

julia> dump(e0)
Expr
  head: Symbol ccall
  args: Array{Any}((4,))
    1: Expr
      head: Symbol tuple
      args: Array{Any}((2,))
        1: QuoteNode
          value: Symbol u3
        2: String "./u1.so"
      typ: Any
    2: Symbol Float64
    3: Expr
      head: Symbol tuple
      args: Array{Any}((1,))
        1: Symbol Float64
      typ: Any
    4: Symbol x
  typ: Any

So how do i get the QuoteNode in the first argument to ccall?

Is this what you’re after:

julia> dump(:(:u3))
QuoteNode
  value: Symbol u3

Not fully … i’d like to enter “u3” by string


julia> dump(:(:u3))
QuoteNode
  value: Symbol u3

julia> dump(:(Symbol("u3")))
Expr
  head: Symbol call
  args: Array{Any}((2,))
    1: Symbol Symbol
    2: String "u3"
  typ: Any

Then this should work

julia> Core.QuoteNode(Symbol("u3"))
:(:u3)

Yes.


julia> function f1a()
           e1 = Expr(:tuple)
           e1.args = [Core.QuoteNode(Symbol("u3")), "./u1.so"]

           e2 = :Float64
           
           e3 = Expr(:tuple)
           e3.args = [:Float64]
           
           e4 = :x
           
           e5 = Expr(:ccall)
           e5.args = [e1,e2,e3,e4]
           e5
       end
f1a (generic function with 1 method)

julia> e0 = f1a()
:(ccall((:u3,"./u1.so"),Float64,(Float64,),x))

julia> x = 11.2
11.2

julia> eval(e0)
13.2

Core sound dangerously like ‘serious internal stuff’, is it recommended to use (and stay) or do i have better options constructing Expr?

I believe that you should use Meta.quot(Symbol("u3")) instead. See the discussion at

1 Like

I tried this previously (according to my .julia_history) but somehow got it wrong, but it’s working now…