Skip to content

Commit

Permalink
All nodes are records now!
Browse files Browse the repository at this point in the history
  • Loading branch information
Uralstech committed Dec 24, 2024
1 parent 68877eb commit c2d09d5
Show file tree
Hide file tree
Showing 23 changed files with 152 additions and 659 deletions.
60 changes: 30 additions & 30 deletions src/Runtime/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Node> 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;

Expand All @@ -282,25 +282,23 @@ private void VisitArrayLikeNodeArray(ArrayLikeNode node, Context executionContex
/// <param name="accessibilityModifiers">The accessibility modifiers for objects that will be assigned from the executing <see cref="Node"/>.</param>
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;
}
Expand Down Expand Up @@ -809,19 +807,19 @@ private void ExecuteBinaryOperation(IEzrObject first, IEzrObject second, Node no
/// <param name="accessibilityModifiers">The accessibility modifiers for objects that will be assigned from the executing <see cref="Node"/>.</param>
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;
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1248,9 +1244,11 @@ private void VisitFunctionDefinitionNode(FunctionDefinitionNode node, Context ex
Context newContext = new($"<function parameters>", 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<Node> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Node> 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;
Expand Down Expand Up @@ -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<Node> 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;

Expand Down
37 changes: 4 additions & 33 deletions src/Runtime/Nodes/BaseNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,16 @@ namespace EzrSquared.Runtime.Nodes;
/// <summary>
/// The representation of an ezr² source code construct. This is the base class of all nodes. Only for inheritance!
/// </summary>
/// <param name="startPosition">The starting <see cref="Position"/> of the <see cref="Node"/>.</param>
/// <param name="endPosition">The ending <see cref="Position"/> of the <see cref="Node"/>.</param>
public abstract class Node(Position startPosition, Position endPosition)
/// <param name="StartPosition">The starting <see cref="Position"/> of the <see cref="Node"/>.</param>
/// <param name="EndPosition">The ending <see cref="Position"/> of the <see cref="Node"/>.</param>
public abstract record Node(Position StartPosition, Position EndPosition)
{
/// <summary>
/// The starting <see cref="Position"/> of the <see cref="Node"/>.
/// </summary>
public Position StartPosition = startPosition;

/// <summary>
/// The ending <see cref="Position"/> of the <see cref="Node"/>.
/// </summary>
public Position EndPosition = endPosition;

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="Node"/>.<br/>
/// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)".
/// </summary>
/// <returns>The <see cref="string"/> representation.</returns>
public override string ToString()
{
return $"{nameof(Node)}()";
}
}

/// <summary>
/// The dummy invalid <see cref="Node"/> structure. For returning instead of <see langword="null"/> if an <see cref="EzrSyntaxError"/> occurs during parsing.
/// </summary>
public class InvalidNode : Node
public record InvalidNode : Node
{
/// <summary>
/// The static <see cref="InvalidNode"/> object.
Expand All @@ -45,14 +26,4 @@ public class InvalidNode : Node
/// <param name="startPosition">The starting <see cref="Position"/> of the <see cref="InvalidNode"/>.</param>
/// <param name="endPosition">The ending <see cref="Position"/> of the <see cref="InvalidNode"/>.</param>
InvalidNode(Position startPosition, Position endPosition) : base(startPosition, endPosition) { }

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="InvalidNode"/>.<br/>
/// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)".
/// </summary>
/// <returns>The <see cref="string"/> representation.</returns>
public override string ToString()
{
return $"{nameof(InvalidNode)}()";
}
}
33 changes: 5 additions & 28 deletions src/Runtime/Nodes/CollectionNodes/ArrayLikeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,10 @@ namespace EzrSquared.Runtime.Nodes;
/// <summary>
/// The <see cref="Node"/> structure for an arraylike (array or list).
/// </summary>
/// <param name="elements">The elements of the arraylike.</param>
/// <param name="createList">The check for if the <see cref="ArrayLikeNode"/> should create a list instead of an array.</param>
/// <param name="startPosition">The starting <see cref="Position"/> of the <see cref="ArrayLikeNode"/>.</param>
/// <param name="endPosition">The ending <see cref="Position"/> of the <see cref="ArrayLikeNode"/>.</param>
public class ArrayLikeNode(List<Node> elements, bool createList, Position startPosition, Position endPosition) : Node(startPosition, endPosition)
/// <param name="Elements">The elements of the arraylike.</param>
/// <param name="CreateList">The check for if the <see cref="ArrayLikeNode"/> should create a list instead of an array.</param>
/// <param name="StartPosition">The starting <see cref="Position"/> of the <see cref="ArrayLikeNode"/>.</param>
/// <param name="EndPosition">The ending <see cref="Position"/> of the <see cref="ArrayLikeNode"/>.</param>
public record ArrayLikeNode(IReadOnlyCollection<Node> Elements, bool CreateList, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition)
{
/// <summary>
/// The elements of the arraylike.
/// </summary>
public List<Node> Elements = elements;

/// <summary>
/// The check for if the <see cref="ArrayLikeNode"/> should create a list instead of an array.
/// </summary>
public bool CreateList = createList;

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="ArrayLikeNode"/>.<br/>
/// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)".
/// </summary>
/// <returns>The <see cref="string"/> representation.</returns>
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})";
}
}
26 changes: 4 additions & 22 deletions src/Runtime/Nodes/CollectionNodes/DictionaryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,9 @@ namespace EzrSquared.Runtime.Nodes;
/// <summary>
/// The <see cref="Node"/> structure for a dictionary.
/// </summary>
/// <param name="keyValuePairs">The key-value pairs of the dictionary.</param>
/// <param name="startPosition">The starting <see cref="Position"/> of the <see cref="DictionaryNode"/>.</param>
/// <param name="endPosition">The ending <see cref="Position"/> of the <see cref="DictionaryNode"/>.</param>
public class DictionaryNode(List<(Node Key, Node Value)> keyValuePairs, Position startPosition, Position endPosition) : Node(startPosition, endPosition)
/// <param name="KeyValuePairs">The key-value pairs of the dictionary.</param>
/// <param name="StartPosition">The starting <see cref="Position"/> of the <see cref="DictionaryNode"/>.</param>
/// <param name="EndPosition">The ending <see cref="Position"/> of the <see cref="DictionaryNode"/>.</param>
public record DictionaryNode(IReadOnlyCollection<(Node Key, Node Value)> KeyValuePairs, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition)
{
/// <summary>
/// The key-value pairs of the dictionary.
/// </summary>
public List<(Node Key, Node Value)> KeyValuePairs = keyValuePairs;

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="DictionaryNode"/>.<br/>
/// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)".
/// </summary>
/// <returns>The <see cref="string"/> representation.</returns>
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)}])";
}
}
22 changes: 4 additions & 18 deletions src/Runtime/Nodes/CollectionNodes/StatementsNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,9 @@ namespace EzrSquared.Runtime.Nodes;
/// <summary>
/// The <see cref="Node"/> structure for holding multiple statements.
/// </summary>
/// <param name="statements">The statements.</param>
/// <param name="startPosition">The starting <see cref="Position"/> of the <see cref="StatementsNode"/>.</param>
/// <param name="endPosition">The ending <see cref="Position"/> of the <see cref="StatementsNode"/>.</param>
public class StatementsNode(List<Node> statements, Position startPosition, Position endPosition) : Node(startPosition, endPosition)
/// <param name="Statements">The statements.</param>
/// <param name="StartPosition">The starting <see cref="Position"/> of the <see cref="StatementsNode"/>.</param>
/// <param name="EndPosition">The ending <see cref="Position"/> of the <see cref="StatementsNode"/>.</param>
public record StatementsNode(IReadOnlyCollection<Node> Statements, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition)
{
/// <summary>
/// The statements being held by the node.
/// </summary>
public List<Node> Statements = statements;

/// <inheritdoc/>
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)}])";
}
}
29 changes: 5 additions & 24 deletions src/Runtime/Nodes/ContextManipulationNodes/DefineBlockNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,10 @@
/// <summary>
/// The <see cref="Node"/> structure for a define block.
/// </summary>
/// <param name="body">The body of the block.</param>
/// <param name="accessibilityModifiers">The accessibility modifiers for the define block.</param>
/// <param name="startPosition">The starting <see cref="Position"/> of the <see cref="DefineBlockNode"/>.</param>
/// <param name="endPosition">The ending <see cref="Position"/> of the <see cref="DefineBlockNode"/>.</param>
public class DefineBlockNode(Node body, AccessMod accessibilityModifiers, Position startPosition, Position endPosition) : Node(startPosition, endPosition)
/// <param name="Body">The body of the block.</param>
/// <param name="AccessibilityModifiers">The accessibility modifiers for the define block.</param>
/// <param name="StartPosition">The starting <see cref="Position"/> of the <see cref="DefineBlockNode"/>.</param>
/// <param name="EndPosition">The ending <see cref="Position"/> of the <see cref="DefineBlockNode"/>.</param>
public record DefineBlockNode(Node Body, AccessMod AccessibilityModifiers, Position StartPosition, Position EndPosition) : Node(StartPosition, EndPosition)
{
/// <summary>
/// The body of the block.
/// </summary>
public Node Body = body;

/// <summary>
/// The accessibility modifiers of the define block.
/// </summary>
public AccessMod AccessibilityModifiers = accessibilityModifiers;

/// <summary>
/// Creates a <see cref="string"/> representation of the <see cref="DefineBlockNode"/>.<br/>
/// All fields excluding the start and end positions are included in the representation, like "ExampleNode(Property1, Property2)".
/// </summary>
/// <returns>The <see cref="string"/> representation.</returns>
public override string ToString()
{
return $"{nameof(DefineBlockNode)}({Body}, {AccessibilityModifiers})";
}
}
Loading

0 comments on commit c2d09d5

Please sign in to comment.