# I would like to use the built in macro(AST) to create a representation for crystal code

**URL:** <https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244>\
**Category:** Help & Support\
**Created:** [January 8, 2023, 4:59pm UTC](https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244 "2023-01-08T16:59:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [January 8, 2023, 4:59pm UTC](https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244/1 "2023-01-08T16:59:23Z")

</div>

Hi, I have looked for resources around how to get to generate an ast tree output as a string.  
I found a GitHub issue about it 8 years ago but that code isn’t compatible with today’s crystal: [What is the interface to get an AST representation for a given crystal code? · Issue #65 · crystal-lang/crystal (github.com)](https://github.com/crystal-lang/crystal/issues/65)

I am a little bit unsure how to get this to work since the ast module is quite undocumented.  
If some one can help me to do a similar script as in the issue but compatible with crystal 1.6.2 so would I be very thankful.

---

<div class="post-metadata">

**Author:** ![beta-ziliani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/beta-ziliani/32/1821_2.png) [@beta-ziliani](https://forum.crystal-lang.org/u/beta-ziliani)\
**Post date:** [January 9, 2023, 7:02pm UTC](https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244/2 "2023-01-09T19:02:28Z")

</div>

Hi @meatball133 , the following works:

```plaintext
require "compiler/crystal/syntax/*"

include Crystal

parser = Parser.new "
  def foo
    1
  end

  def bar
    2
  end
"

nodes = parser.parse

puts "Before:"
puts nodes

```

As you can see, the location of the parser changed at some point. And this could serve as a warning: unlike with the stdlib, the compiler is subject to change without warning. I don’t know what you’re trying to do, but have this into consideration.

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [January 9, 2023, 7:24pm UTC](https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244/3 "2023-01-09T19:24:24Z")

</div>

Hi, thanks for the replay.

I am working on the crystal track on the learning platform [Exercism](https://exercism.org/tracks/crystal).  
I have rebuilt some tooling already thanks to some wonderful people here on the forum.

Exercism has a feature called “representer” which is creating a represented code of users’ solutions to problems.  
The point of having a representer is to be able to give standardized feedback on solutions with particular ways of solving problems.  
To be able to do this standardized feedback we have to make the solutions more standardized.

Therefore a solution that other languages have done on the platform is to use a built-in ast library to convert the code to an ast tree.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [January 9, 2023, 7:41pm UTC](https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244/4 "2023-01-09T19:41:47Z")

</div>

What’s the main difference between source code and the standardized solution? Is it whitespace? Is it comments removal? Variable names?

---

<div class="post-metadata">

**Author:** ![meatball133](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/meatball133/32/1869_2.png) [@meatball133](https://forum.crystal-lang.org/u/meatball133)\
**Post date:** [January 9, 2023, 7:49pm UTC](https://forum.crystal-lang.org/t/i-would-like-to-use-the-built-in-macro-ast-to-create-a-representation-for-crystal-code/5244/5 "2023-01-09T19:49:41Z")

</div>

The representer format, remove comment and variable names.

There is more detail of what the representer does: [Representers](https://exercism.org/docs/building/tooling/representers)
