Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tact as library #314

Open
byakuren-hijiri opened this issue May 1, 2024 · 6 comments
Open

Tact as library #314

byakuren-hijiri opened this issue May 1, 2024 · 6 comments
Labels
kind: refactoring Improve code readability misc: ecosystem Blueprint, Misti, Verifier, Web IDE, VSCode plugin scope: library-api Tact's TypeScript API for third-party tools (src/index.ts)

Comments

@byakuren-hijiri
Copy link
Contributor

byakuren-hijiri commented May 1, 2024

Third-party TypeScript API

@byakuren-hijiri byakuren-hijiri added kind: discussion Ideas that are not fully formed and require discussion kind: refactoring Improve code readability scope: tooling Language tooling scope: library-api Tact's TypeScript API for third-party tools (src/index.ts) labels May 1, 2024
@byakuren-hijiri byakuren-hijiri changed the title Tact AST API: Improvements for tooling Tact frontend API: Improvements for tooling May 1, 2024
@novusnota
Copy link
Member

Save types to AST or improve an access API to type information.

This would be insanely useful in tools like language server and such!

In general, I agree with all those points, but implementation of some may just require us to make our own lexer→parser→semantic analysis pipeline, suitable for compiler AND external tooling depending on various steps in it. I'd say this depends on #286, but I may be wrong and we could pull off such feat without compromising the Ohm's toolkit.

@byakuren-hijiri
Copy link
Contributor Author

I would also suggest creating named union types for AST entries used in fields. For example:

export type ASTContract = {
    kind: 'def_contract';
    origin: TypeOrigin;
    id: number;
    name: string;
    traits: ASTString[];
    attributes: ASTContractAttribute[];
    declarations: (ASTField | ASTFunction | ASTInitFunction | ASTReceive | ASTConstant)[];
    ref: ASTRef;
};

Could be rewritten as:

export type ASTContractDeclaration = (ASTField | ASTFunction | ASTInitFunction | ASTReceive | ASTConstant);
export type ASTContract = {
    kind: 'def_contract';
    origin: TypeOrigin;
    id: number;
    name: string;
    traits: ASTString[];
    attributes: ASTContractAttribute[];
    declarations: ASTContractDeclaration[];
    ref: ASTRef;
};

This will simplify the life of tooling developers by enabling them to reuse these type definitions from the compiler. Otherwise, I find myself copy-pasting these entries in my projects. Here is an example of a function with such a signature implemented in the static analyzer internals:

function getMethodInfo(
    decl: ASTField | ASTFunction | ASTInitFunction | ASTReceive | ASTConstant,
  ): [string | undefined, FunctionKind | undefined] {

byakuren-hijiri added a commit to byakuren-hijiri/tact that referenced this issue May 6, 2024
byakuren-hijiri added a commit to byakuren-hijiri/tact that referenced this issue May 6, 2024
anton-trunov pushed a commit that referenced this issue May 7, 2024
…326)

Refers to #314

* feat(store): Remove `openMockContext`

API users can simply create an `ASTProgram` containing the desired
top-level entries and use `createContext` instead.

* feat(store): Change the default behavior of `openContext`
@byakuren-hijiri
Copy link
Contributor Author

byakuren-hijiri commented May 23, 2024

Added three more points:

Add more context to every internal Error to be thrown in compiler internals. This is crucial for debugging third-party tools.
Add AST iterators that perform functional map and fold over nested nodes of the AST. This will enable API users to inspect the AST in a more convenient way, for example, sorting nodes of a certain type.
Add an API that provides equivalence checks between AST nodes of the same type. This is needed, for example, in #335 to implement unit tests.

byakuren-hijiri added a commit to nowarp/misti that referenced this issue May 29, 2024
This should be introduced in the compiler's API: tact-lang/tact#314
byakuren-hijiri added a commit to byakuren-hijiri/tact that referenced this issue May 30, 2024
This improves compiler's API providing an interface to traverse the AST.
It should be a part of the compiler, since it is be useful for devtools
using the API and might be useful internally.

Refers to tact-lang#314
anton-trunov pushed a commit that referenced this issue Jun 3, 2024
This improves compiler's API providing an interface to traverse the AST.
It should be a part of the compiler, since it is useful for devtools
using the API and might be useful internally.

Refers to #314
@anton-trunov anton-trunov pinned this issue Jun 4, 2024
@anton-trunov anton-trunov added the scope: ast AST (src/ast/*) label Jun 16, 2024
@byakuren-hijiri
Copy link
Contributor Author

byakuren-hijiri commented Jul 18, 2024

Added while working on tests for #559:

  • Refactoring: Extract methods from the build function (src/pipeline/build.ts) to make it more modular. We need to separate the build functionality from CLI parsing and use different methods to create context, compile, and precompile. This is important to implement in order to enable third-party tools to hook into the compilation pipeline in the most flexible way. Perhaps, the best way to achieve this functionality is to create the Builder class with public methods defining the pipeline.

@verytactical
Copy link
Contributor

Every point here makes a lot of sense, except for

Optionally, we should consider introducing mappings from ASTNode ID to ASTNode

In fact, we should remove id from AST nodes. They are just disguised references, and there is neither GC nor type safety to ensure AST ids stored elsewhere won't become dangling references.

While we could patch createNode to ensure all the ids would be in that Map, we wouldn't ever know they should have been removed from that map. Even though it would be possible to find any node by its id, we'd end up performing some actions on nodes that aren't even relevant anymore.

@jubnzv
Copy link
Contributor

jubnzv commented Jan 2, 2025

They are just disguised references, and there is neither GC nor type safety to ensure AST ids stored elsewhere won't become dangling references.

I would argue against any idea of mutating the AST. If we need to transform AST, we should create a second AST to ensure a clean design for both the compiler and API users. Otherwise, the AST should be available at all stages of compilation since we need to access that information from different places; therefore, it will never be GCed.

Additionally, having unique IDs is essential for maintaining symbol tables that are useful at different stages of compilation, particularly for analysis and accessing the AST from other IRs what is used in Misti.

@anton-trunov anton-trunov unpinned this issue Jan 16, 2025
@anton-trunov anton-trunov added misc: ecosystem Blueprint, Misti, Verifier, Web IDE, VSCode plugin and removed scope: tooling Language tooling labels Jan 20, 2025
@verytactical verytactical removed tracking-issue kind: discussion Ideas that are not fully formed and require discussion scope: ast AST (src/ast/*) labels Jan 20, 2025
@verytactical verytactical changed the title Tact frontend API: Improvements for tooling Tooling for developers Jan 23, 2025
@verytactical verytactical changed the title Tooling for developers Tact as library Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: refactoring Improve code readability misc: ecosystem Blueprint, Misti, Verifier, Web IDE, VSCode plugin scope: library-api Tact's TypeScript API for third-party tools (src/index.ts)
Projects
None yet
Development

No branches or pull requests

5 participants