Skip to content

Commit

Permalink
fix: initializer being emitted for type patterns & compile is pattern…
Browse files Browse the repository at this point in the history
… declarations for variables/assignments

fix #7
  • Loading branch information
R-unic committed Jul 30, 2024
1 parent a16fcae commit d902da4
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions RobloxCS/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,28 +328,10 @@ public override void VisitIfStatement(IfStatementSyntax node)
{
Write("if ");
Visit(node.Condition);

void writePatternDeclarations()
{
if (node.Condition is IsPatternExpressionSyntax isPattern)
{
if (isPattern.Pattern is DeclarationPatternSyntax declarationPattern)
{
Visit(declarationPattern.Designation);
}
else if (isPattern.Pattern is VarPatternSyntax varPattern)
{
Visit(varPattern.Designation);
}
Write(" = ");
Visit(isPattern.Expression);
WriteLine();
}
}
WriteLine(" then");
_indent++;

writePatternDeclarations();
WritePatternDeclarations(node.Condition);
Visit(node.Statement);

_indent--;
Expand Down Expand Up @@ -1120,7 +1102,6 @@ void writeTypePattern(TypeSyntax type)
}

var (willBeHandled, writePattern) = getPatternWriter();

if (!willBeHandled && pattern.IsKind(SyntaxKind.NotPattern))
{
Write("not ");
Expand Down Expand Up @@ -1169,6 +1150,11 @@ public override void VisitVariableDeclarator(VariableDeclaratorSyntax node)
{
Write(" = ");
Visit(node.Initializer);
if (node.Initializer.Value.IsKind(SyntaxKind.IsPatternExpression))
{
WriteLine();
}
WritePatternDeclarations(node.Initializer.Value);
}
WriteLine();
}
Expand All @@ -1178,6 +1164,11 @@ public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
Visit(node.Left);
Write(" = ");
Visit(node.Right);
if (node.Right.IsKind(SyntaxKind.IsPatternExpression))
{
WriteLine();
}
WritePatternDeclarations(node.Right);
}

public override void VisitGenericName(GenericNameSyntax node)
Expand Down Expand Up @@ -1802,6 +1793,30 @@ private void WriteDefaultReturn(BlockSyntax? block)
}
}

private void WritePatternDeclarations(SyntaxNode node)
{
if (node is IsPatternExpressionSyntax isPattern)
{
void writeInitializer()
{
Write(" = ");
Visit(isPattern.Expression);
WriteLine();
}

if (isPattern.Pattern is DeclarationPatternSyntax declarationPattern)
{
Visit(declarationPattern.Designation);
writeInitializer();
}
else if (isPattern.Pattern is VarPatternSyntax varPattern)
{
Visit(varPattern.Designation);
writeInitializer();
}
}
}

private void WriteTypeAnnotation(TypeSyntax type, bool isReturnType = false)
{
if (!type.IsVar)
Expand Down

0 comments on commit d902da4

Please sign in to comment.