Elm-to-Gleam transpiler.
WORK-IN-PROGRESS.
Currently supported:
- Naive transpilling of type declarations (e.g.
Maybe
isMaybe
in generated Gleam code). - Sum types
- Typed Records
- Function types
- Tuples
- Unit
Coming up soon:
- Type aliases
type alias A = B
- Untyped records
type alias R = { foo: Int }
Unsupported:
- Function declarations
- Ports
cat samples/Sample1.elm
module Sample1 exposing (Declaration(..), Node)
-- Elm declaration
module Sample1 exposing (Declaration(..), Node)
{-| Elm declaration
-}
type Declaration
= FunctionDeclaration Function
| AliasDeclaration TypeAlias
| CustomTypeDeclaration Type
| PortDeclaration Signature
| InfixDeclaration Infix
| Destructuring
{ pattern : Node Pattern
, expression : Node Expression
}
{-| Provides additional context
-}
type Node a
= Node Range a
gleam run samples/Sample1.elm
[...]
pub type Declaration {
FunctionDeclaration(Function)
AliasDeclaration(TypeAlias)
CustomTypeDeclaration(Type)
PortDeclaration(Signature)
InfixDeclaration(Infix)
Destructuring(pattern: Node(Pattern), expression: Node(Expression))
}
pub opaque type Node(a) {
Node(Range, a)
}
Success!
gleam run # Run the project
gleam test # Run the tests
There is a web app you can use to turn Elm source code into AST compatible with elm-syntax which is an inspiration for Elm AST in this project. The generated JSON isn't prefect but it's close enough to help figuring out some quirkier aspects.
cd tools/elm-syntax
elm reactor
The flow for adding support for more Elm syntax is as follows:
- Add a test case to
test/transpile
by adding two files,xyz.elm
containing the input andxyz.expected
. - If necessary, use
tools/elm-syntax
to inspect the corresponding elm-syntax AST. - If you need to add new tokens, add them in
elm/lexer.gleam
. Don't forget about tests. - Implement the parser in
elm/parser.gleam
, adding tests where necessary. - Update the transpiler in
transpile.gleam
. - Run tests via
gleam test
. Iterate until they pass.