How do I extract the rescript AST?

How do I get the datastructure of AST of rescript compiler? We are figuring out how to move from Purescript to Rescript (Both places we are using React as the building block). We are able to get hold of purescript CST as it is written in Haskell. We found the jscomp/ml/… has all the AST info of rescript which is written in Ocaml. But we need to move this to haskell so that we can transpile with one language (Haskell). We also found that Rescript has playgorund library which can compile Rescript code to Javascript. Felt like we can find the Rescript AST in this playground thing which would be lot easy to understand. But we are not able to find the Github repository for the same. Is it like compiler itself is converted to Javascript with js_of_ocaml?

Do suggest if there are better approaches here.

Hey, would you expand on what you’re trying to do? Convert some Purescript code to ReScript?

So we have a large frontend code base which is currently written in Purescript (https://www.purescript.org/). Another very similar project is built in Rescript. We wanted to unify these two projects and wanted to move from two languages to one in the company. Reason for this change are many but right now we have decided to move our Purescript codebase to Rescript. To do this we extracted the CST of Purescript whose compiler is written in Haskell (https://github.com/purescript/purescript/blob/master/src/Language/PureScript/CST/Types.hs). Once we have the AST we will transform it to print Rescript code. Now wanted to understand how to extract AST/CST of Rescript so that we can write code mapping between both programming language.

In short we are writing transpiler which will convert a Purescript React code to Rescript code. We want to transpile and not handwrite the implementation because it is always a moving target for such large code bases. Features get added faster than we can move things and also we want to compare things side by side old and new programming language.

For starters, you can call the compiler directly on a .res file to get the AST:

npx bsc -dparsetree MyFile.res

Sounds crazy what you want to do, but nevertheless, wish you much success!

EDIT: It reminds me of philip2, an Elm to OCaml compiler

3 Likes

I think it’s possible to use the OCaml ast and then convert it to ReScript.

1 Like

It might be easier to generare .res files as strings directly than trying to convert one AST to another kind of AST.
Most of the difficulties will be of a semantic nature though – unrelated to the representation used.

We decided to go ahead with this for now.

1 Like