<refactor> Refactor Tree to standardised object#29
Merged
Conversation
Node class contains properties: - type - content - leftNode - rightNode - parent - commutative - precedence
Previously, expressionToComponentList wasn't able to use the new Node class, for a few reasons: - The Node class needs to be defined before it is accessed (see the Temporal Dead Zone https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz) - Move Node class to a new file - Add private type and content properties, which are accessed by the Node getter and setters
- Change node type references to the enum, instead of string - Fix spelling errors
- Change references to node type to reference the enum
- Change references to node type to reference the enum
- Replace references to string literals in the function to use the NodeType and Operator enums
Returning in a switch-case statement terminates the statement. Hence, a break after the return is just dead code, and unneeded.
eb59845 to
7ed5fbe
Compare
Previously, a missing break caused the setting of content for any Operator Node to fall into the function case. e.g: This would set its content as '+', instead of OPERATOR.ADDITION. Currently, these two things are the same thing, but this could deviate in the future.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Error objects return their stack trace, unlike a raw string.
Tree object contains the following: DATA: -- list body, contains node objects making up the tree -- int root, pointer to root node in tree FUNCTIONALITY: -- Add(Node, Node parentNode) - adds node to the tree under parentNode -- Remove(Node) - removes node from the tree Constructor and functionality still needs to be implemented
Add function now adds nodes to tree
Still need to test Add and Remove function - after I've done constructor, I should be able to
Tree object can be instantiated with a body (list of node objects)
- Syntax errors fixed - Renamed parentNode to parent - The keyword isn't needed for class methods. The class has been fixed to reflect this.
Make it possible to retrieve the Nodes inside the tree, by calling the Get function. Unfortunately, JS doesn't support function overloading. Hence, we have to settle here for a helper method.
Beginning of migrating all functions over to the Tree object
Functions in script.js now use the Tree object when interacting with a tree.
7ed5fbe to
d573a65
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the expression “tree” representation into standardized Node/Tree classes and updates the existing parsing/normalisation code to use the new API, with the goal of enabling safe node removal without breaking index-based pointers.
Changes:
- Added
pages/index/tree.jsintroducingOperator,NodeType,Node, andTreeabstractions. - Updated
pages/index/script.jsto constructNodeinstances, useNodeType/Operatorconstants, and interact with trees viaTree.Get()/Tree.Find(). - Updated
index.htmlto loadtree.jsbeforescript.js.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 12 comments.
| File | Description |
|---|---|
| pages/index/tree.js | Introduces the new standardized Tree/Node object model and pointer-management logic (incl. removal). |
| pages/index/script.js | Migrates parsing and tree algorithms from plain objects/arrays to the new Node and Tree APIs. |
| index.html | Ensures the new tree module is loaded for browser runtime before dependent code. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
In Add(), the parent did not have to be in the tree to be added to. This is now not possible. In Remove(), the node removed did not have to be in the tree. This is now also not possible. This prevents unintended consequences of phantom trees being created / phantom nodes messing up pointers.
Owner
Author
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Error objects contain the stack trace of the program - throwing a string doesn't.
bbf356a to
4efebcf
Compare
4efebcf to
83fd7a4
Compare
Previously, eTCL used to set these properties on the component objects it created. Now, the Node object computes these values at request. Hence, these locals are dead code and can be removed.
If root index > index of node being removed, we need to -1 from tree.root to ensure it still points towards the correct index.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Tree object contains the following:
DATA:
-- list body, contains node objects making up the tree
-- int root, pointer to root node in tree
-- int length, length of body
FUNCTIONALITY:
-- Get(index) - gets node at specified index
-- Find(Node) - finds index of node in tree
-- Add(Node, Node parentNode) - adds node to the tree under parentNode
-- Remove(Node) - removes node from the tree
The key feature of this change is that nodes can be removed from the tree without ruining the pointers.
Previously, removing a node from the tree would mean that all pointers that pointed to an index greater than the element removed would be off by 1. The Remove(Node) funciton handles modifying the pointers in the tree, so this is no longer an impossibility.