AST output from a fork of the Elm Compiler

Intro

I’m working on a fork of the Elm compiler, the sole aim of which is to produce output in various forms of the AST created when you run elm make on a file.

The idea is this. First clone and build the fork of the compiler. Locate the binary (see the repo Elm compiler ast fork for more instructions). Then make an alias to it, say elma. Run elma make --help to see the CLI options for viewing the AST. Finally, try an example, e.g., cd example && elma make --ast src/Main.elm. If the example is as listed further below, you get this output:

Output:

Success! Compiled 1 module.
Module: Main

Imports:
  Platform.Sub
  ...
  Html

Values:
[Function] main with patterns:  = [23:5-23:61] [23:5-23:9] text <| [23:13-23:35] "Hello, World! Test: " ++ [23:39-23:60] [23:39-23:53] String.fromInt [23:55-23:60] [23:55-23:58] inc [23:59-23:60] 1

[Function] inc with patterns: [13:5-13:6] x = [14:5-18:14] let [15:9-16:14] delta  = [16:13-16:14] 2 in [18:5-18:14] [18:5-18:6] x + [18:9-18:14] delta

[Function] agent with patterns:  = [9:9-9:20] [9:9-9:11] AI [9:12-9:20] "Claude"

Types:
[Type] [6:6-6:11] Agent
  [6:14-6:19] Human String
  [6:29-6:31] AI String
  [6:41-6:46] Alien

Type Aliases:
Ports:

Note the location data. Thus the Agent type is defined on line 6 starting at column 6 and ending at column 11.

Example program

module Main exposing (main, inc, Agent(..))

import Html exposing (text)

type Agent = Human String | AI String | Alien

{-| The agent who will work with you-}
agent = AI "Claude"

{-| A weird inc function -}
inc : Int -> Int
inc x =
    let
        delta =
            2
    in
    x + delta

{-| Entry point for the program -}
main : Html.Html msg
main =
    text "Hello, World!"

Notes

  • One motivation for this project is produce output to be consumed by suitable AI’s. See, for example this post, especially the comment by @rupert . What AI’s need is suitably chunked source text. The chunks should be meaningful, and the way to get these is to use the Elm compiler’s parser.
  • Try the above example with elma --rag. You will see the documentation strings in the output.
  • This an experiment. I welcome suggestions and comments of all kinds, including issues and pull requests. See repo.
  • If you need output in a specific form, let’s talk.
  • Working on a project like this is a good exercise in trying to understand parts of the Elm compiler. There is new folder Pretty in compiler/src/AST, with files Raw.hs and Json.hs. This is where most of the changes are. There are also a few changes in folder terminal/src: files Make.hs and Main.hs. These are needed to implement the command-line options.
9 Likes

Would you be open to add binary blob serialization with store?

This would take out the dependency on Haskell environment and maybe allow for wider experimentation with formats.

I’d be willing to undertake documenting and visualizing the binary output over the following months. Using Elm, of course.

Using store opens the possibility for de-serialization for free, so one could imagine processing the AST outside of the compiler and reading it back in to the next stage at high speed.