Skip to content

Commit

Permalink
Add CreateRed and Update methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Jan 29, 2024
1 parent b07e935 commit b03d9c1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ internal IdentifierExpressionSample(global::Tsu.Trees.RedGreen.Sample.SampleKind

public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? GetSlot(int index) =>
null;

public override global::Tsu.Trees.RedGreen.Sample.SampleNode CreateRed(global::Tsu.Trees.RedGreen.Sample.SampleNode? parent) =>
new global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample(this, parent);

public global::Tsu.Trees.RedGreen.Sample.Internal.IdentifierExpressionSample Update(string name)
{
if (name != this.Name)
{
return global::Tsu.Trees.RedGreen.Sample.Internal.SampleFactory.IdentifierExpression(name);
}

return this;
}
}

partial class NumericalLiteralExpressionSample : global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample
Expand All @@ -127,6 +140,19 @@ internal NumericalLiteralExpressionSample(global::Tsu.Trees.RedGreen.Sample.Samp

public override global::Tsu.Trees.RedGreen.Sample.Internal.GreenNode? GetSlot(int index) =>
null;

public override global::Tsu.Trees.RedGreen.Sample.SampleNode CreateRed(global::Tsu.Trees.RedGreen.Sample.SampleNode? parent) =>
new global::Tsu.Trees.RedGreen.Sample.NumericalLiteralExpressionSample(this, parent);

public global::Tsu.Trees.RedGreen.Sample.Internal.NumericalLiteralExpressionSample Update(double value)
{
if (value != this.Value)
{
return global::Tsu.Trees.RedGreen.Sample.Internal.SampleFactory.NumericalLiteralExpression(value);
}

return this;
}
}

partial class BinaryOperationExpressionSample : global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample
Expand All @@ -148,6 +174,9 @@ internal BinaryOperationExpressionSample(global::Tsu.Trees.RedGreen.Sample.Sampl
1 => this.Right,
_ => null
};

public override global::Tsu.Trees.RedGreen.Sample.SampleNode CreateRed(global::Tsu.Trees.RedGreen.Sample.SampleNode? parent) =>
new global::Tsu.Trees.RedGreen.Sample.BinaryOperationExpressionSample(this, parent);
}

partial class FunctionCallExpressionSample : global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample
Expand All @@ -172,6 +201,19 @@ internal FunctionCallExpressionSample(global::Tsu.Trees.RedGreen.Sample.SampleKi
2 => this.SecondArg,
_ => null
};

public override global::Tsu.Trees.RedGreen.Sample.SampleNode CreateRed(global::Tsu.Trees.RedGreen.Sample.SampleNode? parent) =>
new global::Tsu.Trees.RedGreen.Sample.FunctionCallExpressionSample(this, parent);

public global::Tsu.Trees.RedGreen.Sample.Internal.FunctionCallExpressionSample Update(global::Tsu.Trees.RedGreen.Sample.Internal.IdentifierExpressionSample identifier, global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample firstArg, global::Tsu.Trees.RedGreen.Sample.Internal.ExpressionSample secondArg)
{
if (identifier != this.Identifier && firstArg != this.FirstArg && secondArg != this.SecondArg)
{
return global::Tsu.Trees.RedGreen.Sample.Internal.SampleFactory.FunctionCallExpression(identifier, firstArg, secondArg);
}

return this;
}
}
}

74 changes: 74 additions & 0 deletions Tsu.Trees.RedGreen/sourcegen/GreenTreeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,72 @@ public static void WriteGreenNode(this IndentedTextWriter writer, Tree tree, Nod
writer.Indent--;
}
#endregion TGreenBase? GetSlot(int index)

#region TRedBase CreateRed(TRedBase? parent)
if (!node.TypeSymbol.IsAbstract)
{
writer.WriteLineNoTabs("");
writer.WriteLine("public override {0} CreateRed({0}? parent) =>", tree.RedBase.ToCSharpString());
writer.Indent++;
writer.WriteLine("new global::{0}.{1}(this, parent);", tree.RedBase.ContainingNamespace.ToCSharpString(), node.TypeSymbol.Name);
writer.Indent--;
}
#endregion TRedBase CreateRed(TRedBase? parent)

#region TNode Update(...)
if (!node.TypeSymbol.IsAbstract && node.ExtraData.Concat(node.Children).Where(x => node.Kinds.Length == 1 && x.FieldName != "_kind").Any())
{
writer.WriteLineNoTabs("");
writer.Write("public {0} Update(", node.TypeSymbol.ToCSharpString());
var components = node.ExtraData.Concat(node.Children);
// Don't need to specify the kind if there's only one possible kind.
if (node.Kinds.Length == 1)
components = components.Where(x => x.FieldName != "_kind");
var first = true;
foreach (var component in components)
{
if (!first) writer.Write(", ");
first = false;
writer.Write("{0} {1}", component.Type.ToCSharpString(), component.ParameterName);
}
writer.WriteLine(')');
writer.WriteLine('{');
writer.Indent++;
{
writer.Write("if (");
first = true;
foreach (var component in components)
{
if (!first) writer.Write(" && ");
first = false;
writer.Write("{0} != this.{1}", component.ParameterName, component.PropertyName);
}
writer.WriteLine(')');
writer.WriteLine('{');
writer.Indent++;
{
writer.Write("return global::{0}.{1}Factory.{2}(",
tree.GreenBase.ContainingNamespace.ToCSharpString(),
tree.Suffix,
node.TypeSymbol.Name.WithoutSuffix(tree.Suffix));
first = true;
foreach (var component in components)
{
if (!first) writer.Write(", ");
first = false;
writer.Write(component.ParameterName);
}
writer.WriteLine(");");
}
writer.Indent--;
writer.WriteLine('}');
writer.WriteLineNoTabs("");
writer.WriteLine("return this;");
}
writer.Indent--;
writer.WriteLine('}');
}
#endregion TNode Update(...)
}
writer.Indent--;
writer.WriteLine('}');
Expand Down Expand Up @@ -365,4 +431,12 @@ private static void WriteGreenConstructor(this IndentedTextWriter writer, Node n
public static void WriteGreenFactory(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<Tree> trees)
{
}

private static string WithoutSuffix(this string name, string suffix)
{
if (name.EndsWith(suffix, StringComparison.Ordinal))
return name.Substring(0, name.Length - suffix.Length);
else
return name;
}
}

0 comments on commit b03d9c1

Please sign in to comment.