Skip to content

Commit

Permalink
Update generated sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Feb 5, 2024
1 parent 1c277f2 commit 857cabc
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Modified by the Tsu (https://github.com/GGG-KILLER/Tsu) project for embedding into other projects.
// <auto-generated />

#nullable enable

namespace Tsu.Trees.RedGreen.Sample.Internal
{
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

internal class SampleListBuilder
{
private global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode?[] _nodes;
public int Count { get; private set; }

public SampleListBuilder(int size)
{
_nodes = new global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode?[size];
}

public static SampleListBuilder Create() => new(8);

public void Clear() => Count = 0;

public global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? this[int index]
{
get => _nodes[index];

set => _nodes[index] = value;
}

public void Add(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? item)
{
if (item == null) return;

if (item.IsList)
{
int slotCount = item.SlotCount;

// Necessary, but not sufficient (e.g. for nested lists).
EnsureAdditionalCapacity(slotCount);

for (int i = 0; i < slotCount; i++)
{
Add(item.GetSlot(i));
}
}
else
{
EnsureAdditionalCapacity(1);

_nodes[Count++] = item;
}
}

public void AddRange(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[] items) => AddRange(items, 0, items.Length);

public void AddRange(global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[] items, int offset, int length)
{
// Necessary, but not sufficient (e.g. for nested lists).
EnsureAdditionalCapacity(length - offset);

int oldCount = Count;

for (int i = offset; i < length; i++)
{
Add(items[i]);
}

Validate(oldCount, Count);
}

private void Validate(int start, int end)
{
for (int i = start; i < end; i++)
{
global::System.Diagnostics.Debug.Assert(_nodes[i] != null);
}
}

public void AddRange(SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode> list) => AddRange(list, 0, list.Count);

public void AddRange(SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode> list, int offset, int length)
{
// Necessary, but not sufficient (e.g. for nested lists).
EnsureAdditionalCapacity(length - offset);

int oldCount = Count;

for (int i = offset; i < length; i++)
{
Add(list[i]);
}

Validate(oldCount, Count);
}

public void AddRange<TNode>(SampleList<TNode> list) where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode =>
AddRange(list, 0, list.Count);

public void AddRange<TNode>(SampleList<TNode> list, int offset, int length) where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode =>
AddRange(new SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode>(list.Node), offset, length);

public void RemoveLast()
{
Count--;
_nodes[Count] = null;
}

private void EnsureAdditionalCapacity(int additionalCount)
{
int currentSize = _nodes.Length;
int requiredSize = Count + additionalCount;

if (requiredSize <= currentSize) return;

int newSize =
requiredSize < 8 ? 8 :
requiredSize >= (int.MaxValue / 2) ? int.MaxValue :
Math.Max(requiredSize, currentSize * 2); // NB: Size will *at least* double.
global::System.Diagnostics.Debug.Assert(newSize >= requiredSize);

Array.Resize(ref _nodes, newSize);
}

public bool Any(global::Tsu.Trees.RedGreen.Sample.SampleKind kind)
{
for (int i = 0; i < Count; i++)
{
if (_nodes[i]!.Kind == kind)
{
return true;
}
}

return false;
}

public global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[] ToArray()
{
var array = new global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[Count];
for (int i = 0; i < array.Length; i++)
{
array[i] = _nodes[i]!;
}

return array;
}

internal global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? ToListNode()
{
switch (Count)
{
case 0:
return null;
case 1:
return _nodes[0];
case 2:
return SampleList.List(_nodes[0]!, _nodes[1]!);
case 3:
return SampleList.List(_nodes[0]!, _nodes[1]!, _nodes[2]!);
default:
var tmp = new global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode[Count];
Array.Copy(_nodes, tmp, Count);
return SampleList.List(tmp);
}
}

public SampleList<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode> ToList() => new(ToListNode());

public SampleList<TNode> ToList<TNode>() where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode => new(ToListNode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Modified by the Tsu (https://github.com/GGG-KILLER/Tsu) project for embedding into other projects.
// <auto-generated />

#nullable enable

namespace Tsu.Trees.RedGreen.Sample.Internal
{
internal readonly struct SampleListBuilder<TNode> where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode
{
private readonly SampleListBuilder _builder;

public SampleListBuilder(int size)
: this(new SampleListBuilder(size))
{
}

public static SampleListBuilder<TNode> Create() => new(8);

internal SampleListBuilder(SampleListBuilder builder)
{
_builder = builder;
}

public bool IsNull => _builder == null;

public int Count => _builder.Count;

public TNode? this[int index]
{
get => (TNode?) _builder[index];

set => _builder[index] = value;
}

public void Clear() => _builder.Clear();

public SampleListBuilder<TNode> Add(TNode node)
{
_builder.Add(node);
return this;
}

public void AddRange(TNode[] items, int offset, int length) =>
_builder.AddRange(items, offset, length);

public void AddRange(SampleList<TNode> nodes) => _builder.AddRange(nodes);

public void AddRange(SampleList<TNode> nodes, int offset, int length) =>
_builder.AddRange(nodes, offset, length);

public bool Any(global::Tsu.Trees.RedGreen.Sample.SampleKind kind) => _builder.Any(kind);

public SampleList<TNode> ToList() => _builder.ToList();

public global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? ToListNode() => _builder.ToListNode();

public static implicit operator SampleListBuilder(SampleListBuilder<TNode> builder) => builder._builder;

public static implicit operator SampleList<TNode>(SampleListBuilder<TNode> builder)
{
if (builder._builder != null)
{
return builder.ToList();
}

return default;
}

public SampleList<TDerived> ToList<TDerived>() where TDerived : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode =>
new(ToListNode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,18 @@ internal partial class SampleVisitor<T1, T2, T3, TResult>
}


public partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.SampleVisitor<global::Tsu.Trees.RedGreen.Sample.SampleNode>
internal partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.Internal.SampleVisitor<global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode>
{
public global::Tsu.Trees.RedGreen.Sample.SampleList<TNode> VisitList<TNode>(global::Tsu.Trees.RedGreen.Sample.SampleList<TNode> list) where TNode : global::Tsu.Trees.RedGreen.Sample.SampleNode
public global::Tsu.Trees.RedGreen.Sample.Internal.SampleList<TNode> VisitList<TNode>(global::Tsu.Trees.RedGreen.Sample.Internal.SampleList<TNode> list) where TNode : global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode
{
global::Tsu.Trees.RedGreen.Sample.SampleListBuilder? alternate = null;
global::Tsu.Trees.RedGreen.Sample.Internal.SampleListBuilder? alternate = null;
for (int i = 0, n = list.Count; i < n; i++)
{
var item = list[i];
var visited = Visit(item);
if (item != visited && alternate == null)
{
alternate = new global::Tsu.Trees.RedGreen.Sample.SampleListBuilder(n);
alternate = new global::Tsu.Trees.RedGreen.Sample.Internal.SampleListBuilder(n);
alternate.AddRange(list, 0, i);
}

Expand All @@ -202,20 +202,20 @@ public partial class SampleRewriter : Tsu.Trees.RedGreen.Sample.SampleVisitor<gl
return list;
}

public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitSemicolonToken(global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample node) =>
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitSemicolonToken(global::Tsu.Trees.RedGreen.Sample.Internal.SemicolonTokenSample node) =>
node;
public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitAssignmentStatement(global::Tsu.Trees.RedGreen.Sample.AssignmentStatement node) =>
node.Update((global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample?)Visit(node.Identifier) ?? throw new global::System.InvalidOperationException("Identifier cannot be null."), (global::Tsu.Trees.RedGreen.Sample.ExpressionSample?)Visit(node.Value) ?? throw new global::System.InvalidOperationException("Value cannot be null."), (global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample?)Visit(node.Semicolon) ?? throw new global::System.InvalidOperationException("Semicolon cannot be null."));
public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitExpressionStatement(global::Tsu.Trees.RedGreen.Sample.ExpressionStatementSample node) =>
node.Update((global::Tsu.Trees.RedGreen.Sample.ExpressionSample?)Visit(node.Expression) ?? throw new global::System.InvalidOperationException("Expression cannot be null."), (global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample?)Visit(node.Semicolon) ?? throw new global::System.InvalidOperationException("Semicolon cannot be null."));
public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitIdentifierExpression(global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample node) =>
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitAssignmentStatement(global::Tsu.Trees.RedGreen.Sample.Internal.AssignmentStatement node) =>
node.Update((global::Tsu.Trees.RedGreen.Sample.Internal.IdentifierExpressionSample?)Visit(node.Identifier) ?? throw new global::System.InvalidOperationException("Identifier cannot be null."), (global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample?)Visit(node.Value) ?? throw new global::System.InvalidOperationException("Value cannot be null."), (global::Tsu.Trees.RedGreen.Sample.Internal.SemicolonTokenSample?)Visit(node.Semicolon) ?? throw new global::System.InvalidOperationException("Semicolon cannot be null."));
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitExpressionStatement(global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionStatementSample node) =>
node.Update((global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample?)Visit(node.Expression) ?? throw new global::System.InvalidOperationException("Expression cannot be null."), (global::Tsu.Trees.RedGreen.Sample.Internal.SemicolonTokenSample?)Visit(node.Semicolon) ?? throw new global::System.InvalidOperationException("Semicolon cannot be null."));
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitIdentifierExpression(global::Tsu.Trees.RedGreen.Sample.Internal.IdentifierExpressionSample node) =>
node.Update(node.Name);
public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitNumericalLiteralExpression(global::Tsu.Trees.RedGreen.Sample.NumericalLiteralExpressionSample node) =>
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitNumericalLiteralExpression(global::Tsu.Trees.RedGreen.Sample.Internal.NumericalLiteralExpressionSample node) =>
node.Update(node.Value);
public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitBinaryOperationExpression(global::Tsu.Trees.RedGreen.Sample.BinaryOperationExpressionSample node) =>
node.Update(node.Kind, (global::Tsu.Trees.RedGreen.Sample.ExpressionSample?)Visit(node.Left) ?? throw new global::System.InvalidOperationException("Left cannot be null."), (global::Tsu.Trees.RedGreen.Sample.ExpressionSample?)Visit(node.Right) ?? throw new global::System.InvalidOperationException("Right cannot be null."));
public override global::Tsu.Trees.RedGreen.Sample.SampleNode VisitFunctionCallExpression(global::Tsu.Trees.RedGreen.Sample.FunctionCallExpressionSample node) =>
node.Update((global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample?)Visit(node.Identifier) ?? throw new global::System.InvalidOperationException("Identifier cannot be null."), VisitList(node.Args));
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitBinaryOperationExpression(global::Tsu.Trees.RedGreen.Sample.Internal.BinaryOperationExpressionSample node) =>
node.Update(node.Kind, (global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample?)Visit(node.Left) ?? throw new global::System.InvalidOperationException("Left cannot be null."), (global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample?)Visit(node.Right) ?? throw new global::System.InvalidOperationException("Right cannot be null."));
public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode VisitFunctionCallExpression(global::Tsu.Trees.RedGreen.Sample.Internal.FunctionCallExpressionSample node) =>
node.Update((global::Tsu.Trees.RedGreen.Sample.Internal.IdentifierExpressionSample?)Visit(node.Identifier) ?? throw new global::System.InvalidOperationException("Identifier cannot be null."), VisitList(node.Args));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public static class SampleFactory
public static global::Tsu.Trees.RedGreen.Sample.ExpressionStatementSample ExpressionStatement(global::Tsu.Trees.RedGreen.Sample.ExpressionSample expression, global::Tsu.Trees.RedGreen.Sample.SemicolonTokenSample semicolon) =>
(global::Tsu.Trees.RedGreen.Sample.ExpressionStatementSample) global::Tsu.Trees.RedGreen.Sample.Internal.SampleFactory.ExpressionStatement((global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample)expression.Green, (global::Tsu.Trees.RedGreen.Sample.Internal.SemicolonTokenSample)semicolon.Green).CreateRed();

public static global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample IdentifierExpression(global::System.String name) =>
public static global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample IdentifierExpression(string name) =>
(global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample) global::Tsu.Trees.RedGreen.Sample.Internal.SampleFactory.IdentifierExpression(name).CreateRed();

public static global::Tsu.Trees.RedGreen.Sample.NumericalLiteralExpressionSample NumericalLiteralExpression(global::System.Double value) =>
public static global::Tsu.Trees.RedGreen.Sample.NumericalLiteralExpressionSample NumericalLiteralExpression(double value) =>
(global::Tsu.Trees.RedGreen.Sample.NumericalLiteralExpressionSample) global::Tsu.Trees.RedGreen.Sample.Internal.SampleFactory.NumericalLiteralExpression(value).CreateRed();

public static global::Tsu.Trees.RedGreen.Sample.BinaryOperationExpressionSample BinaryOperationExpression(global::Tsu.Trees.RedGreen.Sample.SampleKind kind, global::Tsu.Trees.RedGreen.Sample.ExpressionSample left, global::Tsu.Trees.RedGreen.Sample.ExpressionSample right) =>
Expand Down

0 comments on commit 857cabc

Please sign in to comment.