Make the AST round-trippable

Open
#170 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
20/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
cpp
Domain
compilers

Research direction

No files, tests, or code entry points are named. Start by reviewing the current LFortran AST and ASR design, then compare the proposed optional nodes and formatting-preservation requirements with the Roslyn references. Done is not defined: the issue leaves the node representation, generation policy, and whether AST or ASR must reproduce source open.

Written by the indexing model from the issue text.

Description

Original issue: https://gitlab.com/lfortran/lfortran/-/issues/42

The general idea is to follow the Roslyn design:

https://github.com/dotnet/roslyn/wiki/Roslyn%20Overview

One can play with the Roslyn AST viewer here:

https://sharplab.io/


The problem is that it seems the Roslyn AST is not as simple as our LFortran AST. For example Roslyn has all the tokens like } and ; in the AST tree. But those are not needed for the rest of the compiler, because they do not convey any semantic information.

So we decided to first design LFortran's AST and ASR to be as simple and abstract as possible. We are pretty close with the design. It is important that the design is not influenced by this round-trip requirement, but rather by the requirement that AST and ASR is simple, because that will simplify the rest of LFortran as well as all the other tools that people will write on top.

Next step is to figure out how to add all the optional nodes like white space, ;, end if and other tokens, so that the original source code can be exactly reproduced from AST, and perhaps even from ASR (that's even more complicated than from AST, because ASR does not have any declaration nodes).

Another open question is whether these optional white space nodes should always be generated, or only when the given (refactoring) tool requires it.

One should note that we can use the current AST to do formatting, and if the source code is always formatted the way LFortran does it, then we can use LFortran to do refactorings. It's just that it would be very nice to have a tool that can refactor a code base that is not already formatted using LFortran, exactly preserving the original formatting.

Dominant language
C++
Stars
1.2k
Forks
271
Avg merge
1d 2h
Merged PRs (30d)
173

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lfortran/lfortran

All issues in lfortran/lfortran

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.