Skip to content

Splitting out AstNode to AstNode, Block, Pipeline, StatementNode, ExpressionNode.#68

Draft
WindSoilder wants to merge 12 commits intonushell:mainfrom
WindSoilder:ast_nodes
Draft

Splitting out AstNode to AstNode, Block, Pipeline, StatementNode, ExpressionNode.#68
WindSoilder wants to merge 12 commits intonushell:mainfrom
WindSoilder:ast_nodes

Conversation

@WindSoilder
Copy link
Contributor

@WindSoilder WindSoilder commented Mar 16, 2026

As title, this pr is a huge refactor of the codebase, it splits a general AstNode to different nodes.

- A BlockNode, it contains
- A list of StatementNode or ExpressionNode

We can look into enum StatementNode and enum ExpressionNode to see what they can be.

It's a little different to another pr #54 , this pr saves all these nodes into Compiler, so we have the following fields:

  • ast_nodes (It contains some smaller set of original AstNode)
  • expression_nodes
  • block_nodes
  • pipeline_nodes
  • name_nodes (explained in next paragraph)
  • string_nodes (explained in next paragraph)
  • variable_nodes (explained in next paragraph)

For Expression Node, I dupliated the storage of NameNode, StringNode and VariableNode to an individual place, because they can be re-used in many places, after that, we can define StatementNode::Let, StatementNode::Def easier.

After we pushing these node, we also put an Indexer into Compiler.indexer, we can still get all nodes sequentially from this field.

For every type of node, they have a new id type, for example:

  • NameNode: NameNodeId
  • StringNode: StringNodeId
  • VariableNode: VariableNodeId
    And here we define NodeIdGetter trait for NodeId and NodePush trait for Node. So we can push node to compiler and get node information from compiler easily.

What can we actheves after this pr:

  • A better runtime performance, because we don't need to check AstNode every time after we get a node, we are more likely to get right node when we get from a dedicated typed id.
  • Less possibility of bugs, because we can make use of Rust's type system to make sure we are using the right node in the right place.

Something might goes bad after this pr:

  • Some spaces overhead because we need to store more vectors to store these nodes.
  • More complicated output in display_state method. Currently it doesn't show useful information.

I only finished changes in compiler.rs and parser.rs, and part of typechecker.rs, before making more progress, I'd like to create this draft pr to see if I am on the right way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant