Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Latest commit

 

History

History
67 lines (50 loc) · 1.97 KB

README-library.md

File metadata and controls

67 lines (50 loc) · 1.97 KB

elm-in-elm

Elm compiler written in Elm!

For more information on the elm-in-elm project in general read the comprehensive documentation.

The entrypoint of this library is the Elm.Compiler module.

The typical flow of functions inside this library looks like this:

Stages of the compiler

Example usage - Elm to Elixir

An example of how the core of Elm to Elixir compiler might look; pay attention to the pipeline inside elmToElixir:

module ElmToElixir exposing (elmToElixir)

import Elm.AST.Typed as Typed
import Elm.AST.Typed.Unwrapped as TypedU
import Elm.Compiler
import Elm.Compiler.Error exposing (Error)
import Elm.Data.FileContents exposing (FileContents)
import Elm.Data.FilePath exposing (FilePath)
import Elm.Data.Module as Module exposing (Module)


{-| Usage: 

    elmToElixir
      { filePath = "src/Foo.elm"
      , sourceCode = 
          """
          module Foo exposing (foo)

          foo : Int -> String
          foo x =
            String.repeat x "FOO! "
          """
      }
-}
elmToElixir : { filePath : FilePath, sourceCode : FileContents} -> Result Error String
elmToElixir file =
    file
        |> Elm.Compiler.parseModule
        |> Result.andThen Elm.Compiler.desugarOnlyModule
        |> Result.andThen Elm.Compiler.inferModule
        |> Result.map Elm.Compiler.optimizeModule
        |> Result.map (Module.map Typed.unwrap)
        |> Result.map emitElixirModule


{-| The important part: emit Elixir source code!  -}
emitElixirModule : Module TypedU.Expr -> FileContents
emitElixirModule m =
    [ emitHeader m.name m.exposing_
    , emitImports m.imports
    , emitDeclarations m.declarations
    ]
        |> String.join "\n\n\n"


emitHeader = Debug.todo "emitHeader"
emitImports = Debug.todo "emitImports"
emitDeclarations = Debug.todo "emitDeclarations"