# How to run lowered AST?

**URL:** <https://discourse.julialang.org/t/how-to-run-lowered-ast/10231>\
**Category:** General Usage\
**Created:** [April 8, 2018, 4:36pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231 "2018-04-08T16:36:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 8, 2018, 4:36pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/1 "2018-04-08T16:36:09Z")

</div>

Is there an “official” way to run the `CodeInfo` objects that one gets from `code_typed`?

I’d like to be able to do some transformations on code based the types of intermediate results, and then run the transformed code. (I could work with the surface-syntax `Expr` and write my own machinery to figure out the types, but I’d rather avoid having to do that.) My plan was to get the lowered, typed IR from `code_typed`, transform that, and evaluate the transformed code. For the last step, I would need some kind of `feval(f::Core.CodeInfo, args...)`.

I’ve seen this done in [Cassette.jl](https://github.com/jrevels/Cassette.jl) using some advanced voodoo, but I was thinking there was maybe an easier way. (If not, I’ll probably end up using Cassette - It seems like a brilliant project.)

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 8, 2018, 5:32pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/2 "2018-04-08T17:32:37Z")

</div>

Sugar has a bit of this:  
[https://github.com/SimonDanisch/Sugar.jl/blob/master/test/runtests.jl#L32](https://github.com/SimonDanisch/Sugar.jl/blob/master/test/runtests.jl#L32)  
I started rewriting Sugar to offer those features more as first class use cases.

Right now, it’s a bit of a monster, mainly written to work with Transpiler.jl, without much time invested into a real user facing API.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 8, 2018, 5:41pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/3 "2018-04-08T17:41:48Z")

</div>

I did look at Sugar, but I couldn’t find any documentation. Does `get_func_expr` convert the IR back into an `Expr`? Does this work in general, or only for a subset of Julia?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 8, 2018, 5:57pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/4 "2018-04-08T17:57:48Z")

</div>

it’s just a subset for silly reasons… Btw, it should be relatively straight forward to replace the  
unsupported expressions in the linear ir returned by code\_typed…

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 8, 2018, 6:18pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/5 "2018-04-08T18:18:21Z")

</div>

This seems very useful. I will definitely have a look. Is there any way to learn more, other than reading the source code?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 9, 2018, 10:43am UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/6 "2018-04-09T10:43:46Z")

</div>

Wow! I haven’t quite figured everything out yet, but I think this is exactly what I was looking for.

Are you planning on updating Sugar to support Julia 0.7 ?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 9, 2018, 10:50am UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/7 "2018-04-09T10:50:13Z")

</div>

I am indeed, together with the new, user friendly API 🙂

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 9, 2018, 9:34pm UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/8 "2018-04-09T21:34:23Z")

</div>

[https://github.com/SimonDanisch/Sugar.jl/blob/sd/07/src/lambdas.jl#L268](https://github.com/SimonDanisch/Sugar.jl/blob/sd/07/src/lambdas.jl#L268)

I ported `get_func_expr` to the 0.7 branch… should be more stable now, but doesn’t sugar the ast. Let me know if that’s what you need!

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 10, 2018, 4:35am UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/9 "2018-04-10T04:35:04Z")

</div>

Thanks!

…but I get an error message because `postwalk` (called by `replace_slots`, line 258) is undefined. I’ve switched to branch sd/07 on Sugar and Matcha. Am I missing something?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 10, 2018, 9:05am UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/10 "2018-04-10T09:05:59Z")

</div>

I shouldn’t have announced this before going to bed, especially without a CI^^  
I added some tests and it should be fixed now 😉

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 10, 2018, 10:22am UTC](https://discourse.julialang.org/t/how-to-run-lowered-ast/10231/11 "2018-04-10T10:22:27Z")

</div>

It works. Thanks!
