From c2d09d5b8399c87472cf97f0e82b9e00cd413688 Mon Sep 17 00:00:00 2001 From: Udayshankar Ravikumar Date: Tue, 24 Dec 2024 23:18:27 +0530 Subject: [PATCH] All nodes are records now! --- src/Runtime/Interpreter.cs | 60 ++++++++-------- src/Runtime/Nodes/BaseNodes.cs | 37 ++-------- .../Nodes/CollectionNodes/ArrayLikeNode.cs | 33 ++------- .../Nodes/CollectionNodes/DictionaryNode.cs | 26 ++----- .../Nodes/CollectionNodes/StatementsNode.cs | 22 ++---- .../DefineBlockNode.cs | 29 ++------ .../VariableAccessNode.cs | 29 ++------ .../VariableAssignmentNode.cs | 43 ++---------- .../ControlFlowStructureNodes/CountNode.cs | 50 +++----------- .../ControlFlowStructureNodes/ForEachNode.cs | 29 ++------ .../Nodes/ControlFlowStructureNodes/IfNode.cs | 33 ++------- .../ControlFlowStructureNodes/TryNode.cs | 42 ++--------- .../ControlFlowStructureNodes/WhileNode.cs | 29 ++------ src/Runtime/Nodes/ExecutableNodes/CallNode.cs | 33 ++------- .../ExecutableNodes/ClassDefinitionNode.cs | 54 +++------------ .../ExecutableNodes/FunctionDefinitionNode.cs | 69 +++---------------- .../Nodes/ExecutableNodes/IncludeNode.cs | 43 ++---------- .../Nodes/ExecutableNodes/ReturnNode.cs | 29 ++------ .../OperationNodes/BinaryOperationNode.cs | 36 ++-------- .../OperationNodes/UnaryOperationNode.cs | 29 ++------ src/Runtime/Nodes/SimpleNodes/NoValueNode.cs | 22 ++---- src/Runtime/Nodes/SimpleNodes/ValueNode.cs | 22 ++---- src/Syntax/Parser/Parser.cs | 12 ++-- 23 files changed, 152 insertions(+), 659 deletions(-) diff --git a/src/Runtime/Interpreter.cs b/src/Runtime/Interpreter.cs index 3db859a..bda5935 100644 --- a/src/Runtime/Interpreter.cs +++ b/src/Runtime/Interpreter.cs @@ -236,10 +236,9 @@ private void VisitStatementsNode(StatementsNode node, Context executionContext, private void VisitArrayLikeNodeList(ArrayLikeNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers) { RuntimeEzrObjectList elementsReferences = new(node.Elements.Count); - for (int i = 0; i < node.Elements.Count; i++) + foreach (Node element in node.Elements) { - Node elementNode = node.Elements[i]; - VisitNode(elementNode, executionContext, callingContext, accessibilityModifiers); + VisitNode(element, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; @@ -259,10 +258,11 @@ private void VisitArrayLikeNodeList(ArrayLikeNode node, Context executionContext private void VisitArrayLikeNodeArray(ArrayLikeNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers) { IEzrObject[] elements = new IEzrObject[node.Elements.Count]; - for (int i = 0; i < node.Elements.Count; i++) + using IEnumerator enumerator = node.Elements.GetEnumerator(); + + for (int i = 0; enumerator.MoveNext(); i++) { - Node elementNode = node.Elements[i]; - VisitNode(elementNode, executionContext, callingContext, accessibilityModifiers); + VisitNode(enumerator.Current, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; @@ -282,25 +282,23 @@ private void VisitArrayLikeNodeArray(ArrayLikeNode node, Context executionContex /// The accessibility modifiers for objects that will be assigned from the executing . private void VisitDictionaryNode(DictionaryNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers) { - RuntimeEzrObjectDictionary dictionary = []; - List<(Node Key, Node Value)> pairs = node.KeyValuePairs; - - for (int i = 0; i < pairs.Count; i++) + RuntimeEzrObjectDictionary dictionary = new(node.KeyValuePairs.Count); + foreach ((Node key, Node value) in node.KeyValuePairs) { // Get the key. - VisitNode(pairs[i].Key, executionContext, callingContext, accessibilityModifiers); + VisitNode(key, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; - IEzrObject key = RuntimeResult.Reference.Object; + IEzrObject keyObject = RuntimeResult.Reference.Object; // Get the value. - VisitNode(pairs[i].Value, executionContext, callingContext, accessibilityModifiers); + VisitNode(value, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; // Add it to the dictionary! - dictionary.Update(key, RuntimeResult.Reference.Object, RuntimeResult); + dictionary.Update(keyObject, RuntimeResult.Reference.Object, RuntimeResult); if (RuntimeResult.ShouldReturn) return; } @@ -809,19 +807,19 @@ private void ExecuteBinaryOperation(IEzrObject first, IEzrObject second, Node no /// The accessibility modifiers for objects that will be assigned from the executing . private void VisitIfNode(IfNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers) { - for (int i = 0; i < node.Cases.Count; i++) + foreach ((Node condition, Node body) in node.Cases) { - VisitNode(node.Cases[i].Condition, executionContext, callingContext, accessibilityModifiers); + VisitNode(condition, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; - bool condition = RuntimeResult.Reference.Object.EvaluateBoolean(RuntimeResult); + bool conditionEval = RuntimeResult.Reference.Object.EvaluateBoolean(RuntimeResult); if (RuntimeResult.ShouldReturn) return; - if (condition) + if (conditionEval) { - VisitNode(node.Cases[i].Body, executionContext, callingContext, accessibilityModifiers); + VisitNode(body, executionContext, callingContext, accessibilityModifiers); return; } } @@ -1165,10 +1163,8 @@ private void VisitTryNode(TryNode node, Context executionContext, Context callin IEzrRuntimeError error = RuntimeResult.Error!; RuntimeResult.Reset(); - for (int i = 0; i < node.Cases.Count; i++) + foreach ((Node errorType, Node? errorVariableNode, Node body) in node.Cases) { - (Node errorType, Node? errorVariableNode, Node body) = node.Cases[i]; - VisitNode(errorType, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; @@ -1248,9 +1244,11 @@ private void VisitFunctionDefinitionNode(FunctionDefinitionNode node, Context ex Context newContext = new($"", false, node.StartPosition, executionContext, executionContext.StaticContext); (string Name, Node Node)[] parameters = new (string, Node)[node.Parameters.Count]; - for (int i = 0; i < parameters.Length; i++) + using IEnumerator enumerator = node.Parameters.GetEnumerator(); + + for (int i = 0; enumerator.MoveNext(); i++) { - Node parameter = node.Parameters[i]; + Node parameter = enumerator.Current; VisitNode(parameter, newContext, callingContext, AccessMod.None, true); if (RuntimeResult.ShouldReturn) return; @@ -1262,7 +1260,7 @@ private void VisitFunctionDefinitionNode(FunctionDefinitionNode node, Context ex return; } - parameters[i] = (reference.Name, node.Parameters[i]); + parameters[i] = (reference.Name, parameter); } OptionalExtraArguments keywordArguments = null; @@ -1322,9 +1320,11 @@ private void VisitFunctionDefinitionNode(FunctionDefinitionNode node, Context ex private void VisitClassDefinitionNode(ClassDefinitionNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers) { EzrClass[] parents = new EzrClass[node.Parents.Count]; - for (int i = 0; i < parents.Length; i++) + using IEnumerator enumerator = node.Parents.GetEnumerator(); + + for (int i = 0; enumerator.MoveNext(); i++) { - Node parentNode = node.Parents[i]; + Node parentNode = enumerator.Current; VisitNode(parentNode, executionContext, callingContext, accessibilityModifiers); if (RuntimeResult.ShouldReturn) return; @@ -1397,10 +1397,10 @@ private void VisitCallNode(CallNode node, Context executionContext, Context call Context argumentsContext = new($"<{receiver.TypeName} arguments>", false, receiver.StartPosition, callingContext, callingContext.StaticContext); arguments = new Reference[node.Arguments.Count]; - for (int i = 0; i < node.Arguments.Count; i++) + using IEnumerator enumerator = node.Arguments.GetEnumerator(); + for (int i = 0; enumerator.MoveNext(); i++) { - Node argument = node.Arguments[i]; - VisitNode(argument, argumentsContext, callingContext, accessibilityModifiers & ~AccessMod.LocalScope); + VisitNode(enumerator.Current, argumentsContext, callingContext, accessibilityModifiers & ~AccessMod.LocalScope); if (RuntimeResult.ShouldReturn) return; diff --git a/src/Runtime/Nodes/BaseNodes.cs b/src/Runtime/Nodes/BaseNodes.cs index f80134b..9b816e8 100644 --- a/src/Runtime/Nodes/BaseNodes.cs +++ b/src/Runtime/Nodes/BaseNodes.cs @@ -4,35 +4,16 @@ namespace EzrSquared.Runtime.Nodes; /// /// The representation of an ezrĀ² source code construct. This is the base class of all nodes. Only for inheritance! /// -/// The starting of the . -/// The ending of the . -public abstract class Node(Position startPosition, Position endPosition) +/// The starting of the . +/// The ending of the . +public abstract record Node(Position StartPosition, Position EndPosition) { - /// - /// The starting of the . - /// - public Position StartPosition = startPosition; - - /// - /// The ending of the . - /// - public Position EndPosition = endPosition; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(Node)}()"; - } } /// /// The dummy invalid structure. For returning instead of if an occurs during parsing. /// -public class InvalidNode : Node +public record InvalidNode : Node { /// /// The static object. @@ -45,14 +26,4 @@ public class InvalidNode : Node /// The starting of the . /// The ending of the . InvalidNode(Position startPosition, Position endPosition) : base(startPosition, endPosition) { } - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(InvalidNode)}()"; - } } diff --git a/src/Runtime/Nodes/CollectionNodes/ArrayLikeNode.cs b/src/Runtime/Nodes/CollectionNodes/ArrayLikeNode.cs index ae3d7f5..396c08d 100644 --- a/src/Runtime/Nodes/CollectionNodes/ArrayLikeNode.cs +++ b/src/Runtime/Nodes/CollectionNodes/ArrayLikeNode.cs @@ -4,33 +4,10 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for an arraylike (array or list). /// -/// The elements of the arraylike. -/// The check for if the should create a list instead of an array. -/// The starting of the . -/// The ending of the . -public class ArrayLikeNode(List elements, bool createList, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The elements of the arraylike. +/// The check for if the should create a list instead of an array. +/// The starting of the . +/// The ending of the . +public record ArrayLikeNode(IReadOnlyCollection Elements, bool CreateList, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The elements of the arraylike. - /// - public List Elements = elements; - - /// - /// The check for if the should create a list instead of an array. - /// - public bool CreateList = createList; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string[] elements = new string[Elements.Count]; - for (int i = 0; i < Elements.Count; i++) - elements[i] = Elements[i].ToString(); - - return $"{nameof(ArrayLikeNode)}([{string.Join(", ", elements)}], {CreateList})"; - } } diff --git a/src/Runtime/Nodes/CollectionNodes/DictionaryNode.cs b/src/Runtime/Nodes/CollectionNodes/DictionaryNode.cs index 7242980..e4d2411 100644 --- a/src/Runtime/Nodes/CollectionNodes/DictionaryNode.cs +++ b/src/Runtime/Nodes/CollectionNodes/DictionaryNode.cs @@ -4,27 +4,9 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for a dictionary. /// -/// The key-value pairs of the dictionary. -/// The starting of the . -/// The ending of the . -public class DictionaryNode(List<(Node Key, Node Value)> keyValuePairs, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The key-value pairs of the dictionary. +/// The starting of the . +/// The ending of the . +public record DictionaryNode(IReadOnlyCollection<(Node Key, Node Value)> KeyValuePairs, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The key-value pairs of the dictionary. - /// - public List<(Node Key, Node Value)> KeyValuePairs = keyValuePairs; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string[] keyValuePairs = new string[KeyValuePairs.Count]; - for (int i = 0; i < KeyValuePairs.Count; i++) - keyValuePairs[i] = $"{KeyValuePairs[i].Key} : {KeyValuePairs[i].Value}"; - - return $"{nameof(DictionaryNode)}([{string.Join(", ", keyValuePairs)}])"; - } } diff --git a/src/Runtime/Nodes/CollectionNodes/StatementsNode.cs b/src/Runtime/Nodes/CollectionNodes/StatementsNode.cs index b96616f..2eb9460 100644 --- a/src/Runtime/Nodes/CollectionNodes/StatementsNode.cs +++ b/src/Runtime/Nodes/CollectionNodes/StatementsNode.cs @@ -4,23 +4,9 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for holding multiple statements. /// -/// The statements. -/// The starting of the . -/// The ending of the . -public class StatementsNode(List statements, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The statements. +/// The starting of the . +/// The ending of the . +public record StatementsNode(IReadOnlyCollection Statements, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The statements being held by the node. - /// - public List Statements = statements; - - /// - public override string ToString() - { - string[] elements = new string[Statements.Count]; - for (int i = 0; i < Statements.Count; i++) - elements[i] = Statements[i].ToString(); - - return $"{nameof(StatementsNode)}([{string.Join(", ", elements)}])"; - } } diff --git a/src/Runtime/Nodes/ContextManipulationNodes/DefineBlockNode.cs b/src/Runtime/Nodes/ContextManipulationNodes/DefineBlockNode.cs index e8ab996..ffe5911 100644 --- a/src/Runtime/Nodes/ContextManipulationNodes/DefineBlockNode.cs +++ b/src/Runtime/Nodes/ContextManipulationNodes/DefineBlockNode.cs @@ -3,29 +3,10 @@ /// /// The structure for a define block. /// -/// The body of the block. -/// The accessibility modifiers for the define block. -/// The starting of the . -/// The ending of the . -public class DefineBlockNode(Node body, AccessMod accessibilityModifiers, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The body of the block. +/// The accessibility modifiers for the define block. +/// The starting of the . +/// The ending of the . +public record DefineBlockNode(Node Body, AccessMod AccessibilityModifiers, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The body of the block. - /// - public Node Body = body; - - /// - /// The accessibility modifiers of the define block. - /// - public AccessMod AccessibilityModifiers = accessibilityModifiers; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(DefineBlockNode)}({Body}, {AccessibilityModifiers})"; - } } diff --git a/src/Runtime/Nodes/ContextManipulationNodes/VariableAccessNode.cs b/src/Runtime/Nodes/ContextManipulationNodes/VariableAccessNode.cs index 0ebe1b5..f322c7e 100644 --- a/src/Runtime/Nodes/ContextManipulationNodes/VariableAccessNode.cs +++ b/src/Runtime/Nodes/ContextManipulationNodes/VariableAccessNode.cs @@ -3,29 +3,10 @@ /// /// The structure for accesing a variable from the context. /// -/// The name of the variable, a object of type . -/// The accessibility modifiers for the variable access operation. -/// The starting of the . -/// The ending of the . -public class VariableAccessNode(Token name, AccessMod accessibilityModifiers, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The name of the variable, a object of type . +/// The accessibility modifiers for the variable access operation. +/// The starting of the . +/// The ending of the . +public record VariableAccessNode(Token Name, AccessMod AccessibilityModifiers, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The name of the variable to access. - /// - public Token Name = name; - - /// - /// The accessibility modifiers for the variable access operation. - /// - public AccessMod AccessibilityModifiers = accessibilityModifiers; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(VariableAccessNode)}({Name}, {AccessibilityModifiers})"; - } } diff --git a/src/Runtime/Nodes/ContextManipulationNodes/VariableAssignmentNode.cs b/src/Runtime/Nodes/ContextManipulationNodes/VariableAssignmentNode.cs index abe0e5f..37bb2c8 100644 --- a/src/Runtime/Nodes/ContextManipulationNodes/VariableAssignmentNode.cs +++ b/src/Runtime/Nodes/ContextManipulationNodes/VariableAssignmentNode.cs @@ -3,41 +3,12 @@ /// /// The structure for assigning a value to a variable in the context. /// -/// The variable to be assigned to. -/// The operation , if not , between the existing value of and . The result of the operation will be assigned to . -/// The value to be assigned to . -/// The accessibility modifiers for the variable assignment operation. -/// The starting of the . -/// The ending of the . -public class VariableAssignmentNode(Node variable, TokenType assignmentOperator, Node value, AccessMod accessibilityModifiers, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The variable to be assigned to. +/// The operation , if not , between the existing value of and . The result of the operation will be assigned to . +/// The value to be assigned to . +/// The accessibility modifiers for the variable assignment operation. +/// The starting of the . +/// The ending of the . +public record VariableAssignmentNode(Node Variable, TokenType AssignmentOperator, Node Value, AccessMod AccessibilityModifiers, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The variable to be assigned to. - /// - public Node Variable = variable; - - /// - /// The operation , if not , between the existing value of and . The result of the operation will be assigned to . - /// - public TokenType AssignmentOperator = assignmentOperator; - - /// - /// The value to be assigned to . - /// - public Node Value = value; - - /// - /// The accessibility modifiers for the variable assignment operation. - /// - public AccessMod AccessibilityModifiers = accessibilityModifiers; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(VariableAssignmentNode)}({Variable}, {AssignmentOperator}, {Value}, {AccessibilityModifiers})"; - } } diff --git a/src/Runtime/Nodes/ControlFlowStructureNodes/CountNode.cs b/src/Runtime/Nodes/ControlFlowStructureNodes/CountNode.cs index d2bd754..127d706 100644 --- a/src/Runtime/Nodes/ControlFlowStructureNodes/CountNode.cs +++ b/src/Runtime/Nodes/ControlFlowStructureNodes/CountNode.cs @@ -3,47 +3,13 @@ /// /// The structure for a count expression. /// -/// The amount to count to. -/// The amount to count from - optional. -/// The increment of each iteration - optional. -/// The variable to store the iteration number in - optional. -/// The body of the count loop. -/// The starting of the . -/// The ending of the . -public class CountNode(Node to, Node? from, Node? step, Node? iterationVariable, Node body, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The amount to count to. +/// The amount to count from - optional. +/// The increment of each iteration - optional. +/// The variable to store the iteration number in - optional. +/// The body of the count loop. +/// The starting of the . +/// The ending of the . +public record CountNode(Node To, Node? From, Node? Step, Node? IterationVariable, Node Body, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The amount to count to. - /// - public Node To = to; - - /// - /// The amount to count from - optional. - /// - public Node? From = from; - - /// - /// The increment of each iteration - optional. - /// - public Node? Step = step; - - /// - /// The variable to store the iteration number in - optional. - /// - public Node? IterationVariable = iterationVariable; - - /// - /// The body of the count loop. - /// - public Node Body = body; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(CountNode)}({To}, {From?.ToString() ?? "null"}, {Step?.ToString() ?? "null"}, {IterationVariable?.ToString() ?? "null"}, {Body})"; - } } diff --git a/src/Runtime/Nodes/ControlFlowStructureNodes/ForEachNode.cs b/src/Runtime/Nodes/ControlFlowStructureNodes/ForEachNode.cs index f4fe64e..7c6bd53 100644 --- a/src/Runtime/Nodes/ControlFlowStructureNodes/ForEachNode.cs +++ b/src/Runtime/Nodes/ControlFlowStructureNodes/ForEachNode.cs @@ -3,29 +3,10 @@ /// /// The structure for a for-each expression. /// -/// A check-in expression, where the LHS is the variable to store the iterated values and RHS is the collection to iterate. -/// The body of the loop. -/// The starting of the . -/// The ending of the . -public class ForEachNode(BinaryOperationNode expression, Node body, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// A check-in expression, where the LHS is the variable to store the iterated values and RHS is the collection to iterate. +/// The body of the loop. +/// The starting of the . +/// The ending of the . +public record ForEachNode(BinaryOperationNode Expression, Node Body, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// A check-in expression, where the LHS is the variable to store the iterated values and RHS is the collection to iterate. - /// - public BinaryOperationNode Expression = expression; - - /// - /// The body of the count loop. - /// - public Node Body = body; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(ForEachNode)}({Expression}, {Body})"; - } } diff --git a/src/Runtime/Nodes/ControlFlowStructureNodes/IfNode.cs b/src/Runtime/Nodes/ControlFlowStructureNodes/IfNode.cs index ceb0de3..3a490eb 100644 --- a/src/Runtime/Nodes/ControlFlowStructureNodes/IfNode.cs +++ b/src/Runtime/Nodes/ControlFlowStructureNodes/IfNode.cs @@ -4,33 +4,10 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for an if expression. /// -/// The cases of the if expression. -/// The body of the else case. -/// The starting of the . -/// The ending of the . -public class IfNode(List<(Node Condition, Node Body)> cases, Node? elseCase, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The cases of the if expression. +/// The body of the else case. +/// The starting of the . +/// The ending of the . +public record IfNode(IReadOnlyCollection<(Node Condition, Node Body)> Cases, Node? ElseCase, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The cases of the if expression. - /// - public List<(Node Condition, Node Body)> Cases = cases; - - /// - /// The body of the else case. - /// - public Node? ElseCase = elseCase; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string?[] cases = new string[Cases.Count]; - for (int i = 0; i < Cases.Count; i++) - cases[i] = $"({Cases[i].Condition}, {Cases[i].Body})"; - - return $"{nameof(IfNode)}([{string.Join(", ", cases)}], {ElseCase?.ToString() ?? "null"})"; - } } diff --git a/src/Runtime/Nodes/ControlFlowStructureNodes/TryNode.cs b/src/Runtime/Nodes/ControlFlowStructureNodes/TryNode.cs index 18bad2a..9b694f1 100644 --- a/src/Runtime/Nodes/ControlFlowStructureNodes/TryNode.cs +++ b/src/Runtime/Nodes/ControlFlowStructureNodes/TryNode.cs @@ -4,41 +4,11 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for a try expression. /// -/// The try block. -/// The error cases of the try expression. -/// The (optional) where the error will be stored and the body of the empty else case. -/// The starting of the . -/// The ending of the . -public class TryNode(Node block, List<(Node ErrorType, Node? Variable, Node Body)> cases, (Node? Variable, Node Body)? emptyCase, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The try block. +/// The error cases of the try expression. +/// The (optional) where the error will be stored and the body of the empty else case. +/// The starting of the . +/// The ending of the . +public record TryNode(Node Block, IReadOnlyCollection<(Node ErrorType, Node? Variable, Node Body)> Cases, (Node? Variable, Node Body)? EmptyCase, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The try block. - /// - public Node Block = block; - - /// - /// The error cases of the try expression. - /// - public List<(Node ErrorType, Node? Variable, Node Body)> Cases = cases; - - /// - /// The (optional) where the error will be stored and the body of the empty else case. - /// - public (Node? Variable, Node Body)? EmptyCase = emptyCase; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string?[] cases = new string[Cases.Count]; - for (int i = 0; i < Cases.Count; i++) - cases[i] = $"({Cases[i].ErrorType}, {Cases[i].Variable?.ToString() ?? "null"}, {Cases[i].Body})"; - - return (EmptyCase is not null) - ? $"{nameof(TryNode)}({Block}, [{string.Join(", ", cases)}], ({EmptyCase?.Variable?.ToString() ?? "null"}, {EmptyCase?.Body}))" - : $"{nameof(TryNode)}({Block}, [{string.Join(", ", cases)}])"; - } } diff --git a/src/Runtime/Nodes/ControlFlowStructureNodes/WhileNode.cs b/src/Runtime/Nodes/ControlFlowStructureNodes/WhileNode.cs index 4d6c9c3..1669f1d 100644 --- a/src/Runtime/Nodes/ControlFlowStructureNodes/WhileNode.cs +++ b/src/Runtime/Nodes/ControlFlowStructureNodes/WhileNode.cs @@ -3,29 +3,10 @@ /// /// The structure for an while expression. /// -/// The condition of the while loop. -/// The body of the while loop. -/// The starting of the . -/// The ending of the . -public class WhileNode(Node condition, Node body, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The condition of the while loop. +/// The body of the while loop. +/// The starting of the . +/// The ending of the . +public record WhileNode(Node Condition, Node Body, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The condition of the while loop. - /// - public Node Condition = condition; - - /// - /// The body of the while loop. - /// - public Node Body = body; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(WhileNode)}({Condition}, {Body})"; - } } diff --git a/src/Runtime/Nodes/ExecutableNodes/CallNode.cs b/src/Runtime/Nodes/ExecutableNodes/CallNode.cs index b63c43d..d690282 100644 --- a/src/Runtime/Nodes/ExecutableNodes/CallNode.cs +++ b/src/Runtime/Nodes/ExecutableNodes/CallNode.cs @@ -4,33 +4,10 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for a function call. /// -/// The function/object to be called. -/// The array of arguments. -/// The starting of the . -/// The ending of the . -public class CallNode(Node receiver, List arguments, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The function/object to be called. +/// The array of arguments. +/// The starting of the . +/// The ending of the . +public record CallNode(Node Receiver, IReadOnlyCollection Arguments, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The function/object to be called. - /// - public Node Receiver = receiver; - - /// - /// The array of arguments. - /// - public List Arguments = arguments; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string[] arguments = new string[Arguments.Count]; - for (int i = 0; i < Arguments.Count; i++) - arguments[i] = Arguments[i].ToString(); - - return $"{nameof(CallNode)}({Receiver}, [{string.Join(", ", arguments)}])"; - } } diff --git a/src/Runtime/Nodes/ExecutableNodes/ClassDefinitionNode.cs b/src/Runtime/Nodes/ExecutableNodes/ClassDefinitionNode.cs index e39e1fe..a8c9c8b 100644 --- a/src/Runtime/Nodes/ExecutableNodes/ClassDefinitionNode.cs +++ b/src/Runtime/Nodes/ExecutableNodes/ClassDefinitionNode.cs @@ -4,51 +4,13 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for a class definition. /// -/// The (optional) name of the class. -/// The accessibility modifiers for the class definition. -/// The check for if the class should be declared read-only. -/// The parents of the class. -/// The body of the class. -/// The starting of the . -/// The ending of the . -public class ClassDefinitionNode(Node? name, AccessMod accessibilityModifiers, bool @readonly, List parents, Node body, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The (optional) name of the class. +/// The accessibility modifiers for the class definition. +/// The check for if the class should be declared read-only. +/// The parents of the class. +/// The body of the class. +/// The starting of the . +/// The ending of the . +public record ClassDefinitionNode(Node? Name, AccessMod AccessibilityModifiers, bool Readonly, IReadOnlyCollection Parents, Node Body, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The name of the class. May be . - /// - public Node? Name = name; - - /// - /// The accessibility modifiers for the class definition. - /// - public AccessMod AccessibilityModifiers = accessibilityModifiers; - - /// - /// The check for if the class should be declared read-only. - /// - public bool Readonly = @readonly; - - /// - /// The parents of the class. - /// - public List Parents = parents; - - /// - /// The body of the class. - /// - public Node Body = body; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string?[] parents = new string[Parents.Count]; - for (int i = 0; i < Parents.Count; i++) - parents[i] = Parents[i].ToString(); - - return $"{nameof(ClassDefinitionNode)}({Name?.ToString() ?? "null"}, {AccessibilityModifiers}, {Readonly}, [{string.Join(", ", parents)}], {Body})"; - } } diff --git a/src/Runtime/Nodes/ExecutableNodes/FunctionDefinitionNode.cs b/src/Runtime/Nodes/ExecutableNodes/FunctionDefinitionNode.cs index a3fe011..e056866 100644 --- a/src/Runtime/Nodes/ExecutableNodes/FunctionDefinitionNode.cs +++ b/src/Runtime/Nodes/ExecutableNodes/FunctionDefinitionNode.cs @@ -4,64 +4,15 @@ namespace EzrSquared.Runtime.Nodes; /// /// The structure for a function definition. /// -/// The (optional) name of the function. -/// The accessibility modifiers for the function definition. -/// The check for if the last expression of the function should be returned as its result. Only used in oneliners. -/// The parameters of the function. -/// The reference to store the extra keyword arguments in. -/// The reference to store the extra positional arguments in. -/// The body of the function. -/// The starting of the . -/// The ending of the . -public class FunctionDefinitionNode(Node? name, AccessMod accessibilityModifiers, bool returnLast, List parameters, Node? extraKeywordArguments, Node? extraPositionalArguments, Node body, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The (optional) name of the function. +/// The accessibility modifiers for the function definition. +/// The check for if the last expression of the function should be returned as its result. Only used in oneliners. +/// The parameters of the function. +/// The reference to store the extra keyword arguments in. +/// The reference to store the extra positional arguments in. +/// The body of the function. +/// The starting of the . +/// The ending of the . +public record FunctionDefinitionNode(Node? Name, AccessMod AccessibilityModifiers, bool ReturnLast, IReadOnlyCollection Parameters, Node? ExtraKeywordArguments, Node? ExtraPositionalArguments, Node Body, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The (optional) name of the function. - /// - public Node? Name = name; - - /// - /// The accessibility modifiers for the function definition. - /// - public AccessMod AccessibilityModifiers = accessibilityModifiers; - - /// - /// The check for if the last expression of the function should be returned as its result. - /// Only used in oneliners. - /// - public bool ReturnLast = returnLast; - - /// - /// The parameters of the function. - /// - public List Parameters = parameters; - - /// - /// The reference to store the extra keyword arguments in. - /// - public Node? ExtraKeywordArguments = extraKeywordArguments; - - /// - /// The reference to store the extra positional arguments in. - /// - public Node? ExtraPositionalArguments = extraPositionalArguments; - - /// - /// The body of the function. - /// - public Node Body = body; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - string?[] parameters = new string[Parameters.Count]; - for (int i = 0; i < Parameters.Count; i++) - parameters[i] = Parameters[i].ToString(); - - return $"{nameof(FunctionDefinitionNode)}({Name?.ToString() ?? "null"}, {AccessibilityModifiers}, {ReturnLast}, [{string.Join(", ", parameters)}], {ExtraKeywordArguments?.ToString() ?? "null"}, {ExtraPositionalArguments?.ToString() ?? "null"}, {Body})"; - } } diff --git a/src/Runtime/Nodes/ExecutableNodes/IncludeNode.cs b/src/Runtime/Nodes/ExecutableNodes/IncludeNode.cs index 3d62f0c..73e4471 100644 --- a/src/Runtime/Nodes/ExecutableNodes/IncludeNode.cs +++ b/src/Runtime/Nodes/ExecutableNodes/IncludeNode.cs @@ -3,41 +3,12 @@ /// /// The structure for an include expression. /// -/// The script to include. -/// The (optional) specific sub-structure or object to be included from the script. -/// Specifies if all contents of the script need to be dumped into the current context. -/// The (optional) nickname of the object to be included. -/// The starting of the . -/// The ending of the . -public class IncludeNode(Node script, Node? subStructure, bool isDumped, Node? nickname, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The script to include. +/// The (optional) specific sub-structure or object to be included from the script. +/// Specifies if all contents of the script need to be dumped into the current context. +/// The (optional) nickname of the object to be included. +/// The starting of the . +/// The ending of the . +public record IncludeNode(Node Script, Node? SubStructure, bool IsDumped, Node? Nickname, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The script to include. - /// - public readonly Node Script = script; - - /// - /// The (optional) specific sub-structure or object to be included from the script. - /// - public readonly Node? SubStructure = subStructure; - - /// - /// Specifies if all contents of the script need to be dumped into the current context. - /// - public readonly bool IsDumped = isDumped; - - /// - /// The (optional) nickname of the object to be included. - /// - public readonly Node? Nickname = nickname; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(IncludeNode)}({Script}, {SubStructure?.ToString() ?? "null"}, {IsDumped}, {Nickname?.ToString() ?? "null"})"; - } } diff --git a/src/Runtime/Nodes/ExecutableNodes/ReturnNode.cs b/src/Runtime/Nodes/ExecutableNodes/ReturnNode.cs index 5e331db..a8bbf81 100644 --- a/src/Runtime/Nodes/ExecutableNodes/ReturnNode.cs +++ b/src/Runtime/Nodes/ExecutableNodes/ReturnNode.cs @@ -3,29 +3,10 @@ /// /// The structure for a return statement. /// -/// The optional value to be returned. -/// Return the last element of , which should be a list or array. -/// The starting of the . -/// The ending of the . -public class ReturnNode(Node? value, bool returnLast, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The optional value to be returned. +/// Return the last element of , which should be a list or array. +/// The starting of the . +/// The ending of the . +public record ReturnNode(Node? Value, bool ReturnLast, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The optional value to be returned. - /// - public Node? Value = value; - - /// - /// Return the last element of , which should be a list or array. - /// - public bool ReturnLast = returnLast; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(ReturnNode)}({Value?.ToString() ?? "null"})"; - } } diff --git a/src/Runtime/Nodes/OperationNodes/BinaryOperationNode.cs b/src/Runtime/Nodes/OperationNodes/BinaryOperationNode.cs index edbedfd..71b98a8 100644 --- a/src/Runtime/Nodes/OperationNodes/BinaryOperationNode.cs +++ b/src/Runtime/Nodes/OperationNodes/BinaryOperationNode.cs @@ -3,35 +3,11 @@ /// /// The structure for a binary operation. /// -/// The first operand of the binary operation. -/// The second operand of the binary operation. -/// The operator of the binary operation. -/// The starting of the . -/// The ending of the . -public class BinaryOperationNode(Node left, Node right, TokenType @operator, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The first operand of the binary operation. +/// The second operand of the binary operation. +/// The operator of the binary operation. +/// The starting of the . +/// The ending of the . +public record BinaryOperationNode(Node Left, Node Right, TokenType Operator, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The first operand of the binary operation. - /// - public Node Left = left; - - /// - /// The second operand of the binary operation. - /// - public Node Right = right; - - /// - /// The operator of the binary operation. - /// - public TokenType Operator = @operator; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(BinaryOperationNode)}({Left}, {Operator}, {Right})"; - } } diff --git a/src/Runtime/Nodes/OperationNodes/UnaryOperationNode.cs b/src/Runtime/Nodes/OperationNodes/UnaryOperationNode.cs index 4bedf6d..179d3bf 100644 --- a/src/Runtime/Nodes/OperationNodes/UnaryOperationNode.cs +++ b/src/Runtime/Nodes/OperationNodes/UnaryOperationNode.cs @@ -3,29 +3,10 @@ /// /// The structure for a unary operation. /// -/// The operand of the unary operation. -/// The operator of the unary operation. -/// The starting of the . -/// The ending of the . -public class UnaryOperationNode(Node operand, TokenType @operator, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The operand of the unary operation. +/// The operator of the unary operation. +/// The starting of the . +/// The ending of the . +public record UnaryOperationNode(Node Operand, TokenType Operator, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The operand of the unary operation. - /// - public Node Operand = operand; - - /// - /// The operator of the unary operation. - /// - public TokenType Operator = @operator; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(UnaryOperationNode)}({Operand}, {Operator})"; - } } diff --git a/src/Runtime/Nodes/SimpleNodes/NoValueNode.cs b/src/Runtime/Nodes/SimpleNodes/NoValueNode.cs index 2cfa002..a5d521e 100644 --- a/src/Runtime/Nodes/SimpleNodes/NoValueNode.cs +++ b/src/Runtime/Nodes/SimpleNodes/NoValueNode.cs @@ -3,23 +3,9 @@ /// /// The structure for a statement or expression without any value (used as skip and stop statement nodes). /// -/// The identifying of the . -/// The starting of the . -/// The ending of the . -public class NoValueNode(TokenType valueType, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The identifying of the . +/// The starting of the . +/// The ending of the . +public record NoValueNode(TokenType ValueType, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The identifying of the . - /// - public TokenType ValueType = valueType; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(NoValueNode)}({ValueType})"; - } } diff --git a/src/Runtime/Nodes/SimpleNodes/ValueNode.cs b/src/Runtime/Nodes/SimpleNodes/ValueNode.cs index bcf6634..8ddd431 100644 --- a/src/Runtime/Nodes/SimpleNodes/ValueNode.cs +++ b/src/Runtime/Nodes/SimpleNodes/ValueNode.cs @@ -3,23 +3,9 @@ /// /// The structure of a simple value like literals and variables. /// -/// The value the represents. -/// The starting of the . -/// The ending of the . -public class ValueNode(Token value, Position startPosition, Position endPosition) : Node(startPosition, endPosition) +/// The value the represents. +/// The starting of the . +/// The ending of the . +public record ValueNode(Token Value, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition) { - /// - /// The value the represents. - /// - public Token Value = value; - - /// - /// Creates a representation of the .
- /// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)". - ///
- /// The representation. - public override string ToString() - { - return $"{nameof(ValueNode)}({Value})"; - } } diff --git a/src/Syntax/Parser/Parser.cs b/src/Syntax/Parser/Parser.cs index 06e2aaa..cfbc339 100644 --- a/src/Syntax/Parser/Parser.cs +++ b/src/Syntax/Parser/Parser.cs @@ -1086,13 +1086,11 @@ private void ParseArrayOrParentheticalExpression() Advance(); } - if (!isArray && elements.Count > 0) - { - _result.Node.StartPosition = startPosition; - _result.Node.EndPosition = endPosition; - } - else - _result.Success(new ArrayLikeNode(elements, false, startPosition, endPosition)); + _result.Success( + !isArray && elements.Count > 0 + ? new StatementsNode([_result.Node], startPosition, endPosition) + : new ArrayLikeNode(elements, false, startPosition, endPosition) + ); } ///