Skip to content

Divide the run() function in smaller portions #366

Closed

Description

@Razican

Currently, the run() function is around 500 lines long. This is going to quickly become unmaintainable, so we need to divide this.

The first thing that comes to my mind to improve this is to just create new function for each match arm, but as we saw in the parser, we can probably do this better and easier to understand.

One option is to have an Executable trait, as I proposed in #304. This trait would just be as simple as this:

trait Executable {
	fn run<E>(interpreter: &mut E) -> ResultValue where E: Executor;
}

Then, I think that we should change the Node enum to be just a Box<dyn Executable>. And each Node type should be a struct where we could use Box to box dynamic types, or we could use compile-time generics to have less allocations (but longer compile times).

Then we implement this trait individually for each of the node types, which is easily extensible, and we reduce the run() function to:

fn run(&mut self, node: &Node) -> ResultValue {
	node.run(&mut self)
}

Tracking:

  • Node::ArrayDecl
  • Node::ArrowFunctionDecl
  • Node::Assign
  • Node::BinOp
  • Node::Block
  • Node::Break
  • Node::Call
  • Node::ConditionalOp
  • Node::Const
  • Node::ConstDeclList
  • Node::Continue
  • Node::DoWhileLoop
  • Node::FunctionDecl
  • Node::FunctionExpr
  • Node::GetConstField
  • Node::GetField
  • Node::ForLoop
  • Node::If
  • Node::LetDeclList
  • Node::Identifier
  • Node::New
  • Node::Object
  • Node::Return
  • Node::Switch
  • Node::Spread
  • StatementList
  • Node::Throw
  • Node::Try
  • Node::This
  • Node::UnaryOp
  • Node::VarDeclList
  • Node::WhileLoop

Activity

  1. added
    Help Wanted
    A-Technical Debt
    on May 2, 2020
  2. HalidOdat commented on May 2, 2020

    @HalidOdat
    Member

    Hmmm. I think doing this at compile time is better than having to do it at runtime.
    Using Box<dyn Trait> has to allocate and use dynamic dispatch (vtable) which would worsen the execution time.

  3. Razican commented on May 2, 2020

    @Razican
    MemberAuthor

    Hmmm. I think doing this at compile time is better than having to do it at runtime.
    Using Box<dyn Trait> has to allocate and use dynamic dispatch (vtable) which would worsen the execution time.

    So, Node would be Node<T: Executable>, but that would make it more difficult to have for example a Vec<Node> of different kinds, and we already use Box inside of nodes, this would just make a Box and only one allocation per node.

  4. Razican commented on May 7, 2020

    @Razican
    MemberAuthor

    A different option would be to still have an enum, but each of the enum members would be just a box to a given Executable.

  5. added this to the v0.9.0 milestone on May 14, 2020
  6. linked a pull request that will close this issueExecution and Node modularization #392on May 14, 2020
  7. self-assigned this
    on May 14, 2020
  8. removed their assignment
    on May 26, 2020
  9. Razican commented on May 26, 2020

    @Razican
    MemberAuthor

    Tracking list of nodes added, now that we have the infrastructure in place.

    This could be a very good first issue, since it shows you how the AST nodes work, so feel free to claim it and to ask for help!

  10. Lan2u commented on May 26, 2020

    @Lan2u

    I shall give it a go :)

  11. Razican commented on May 26, 2020

    @Razican
    MemberAuthor

    I shall give it a go :)

    Go ahead!

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

Metadata

Metadata

Assignees

Labels

A-Technical DebtGood First IssueHelp Wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions