Skip to content

Commit

Permalink
feat: string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Jan 6, 2025
1 parent 0d5efad commit 5d15239
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
22 changes: 22 additions & 0 deletions RobloxCS.Luau/AST/InterpolatedString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace RobloxCS.Luau
{
public class InterpolatedString : Expression
{
public List<Expression> Parts { get; }

public InterpolatedString(List<Expression> parts)
{
Parts = parts;
AddChildren(Parts);
}

public override void Render(LuauWriter luau)
{
luau.Write('`');
foreach (var part in Parts)
part.Render(luau);

luau.Write('`');
}
}
}
22 changes: 22 additions & 0 deletions RobloxCS.Luau/AST/Interpolation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace RobloxCS.Luau;

/// <summary>
/// An interpolated section of an <see cref="InterpolatedString"/>
/// </summary>
public class Interpolation : Expression
{
public Expression Expression { get; }

public Interpolation(Expression expression)
{
Expression = expression;
AddChild(Expression);
}

public override void Render(LuauWriter luau)
{
luau.Write('{');
Expression.Render(luau);
luau.Write('}');
}
}
14 changes: 3 additions & 11 deletions RobloxCS.Luau/AST/Literal.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
namespace RobloxCS.Luau
{
public class Literal : Expression
public class Literal(string valueText) : Expression
{
public string ValueText { get; }
public string ValueText { get; } = valueText;

public Literal(string valueText)
{
ValueText = valueText;
}

public override void Render(LuauWriter luau)
{
luau.Write(ValueText);
}
public override void Render(LuauWriter luau) => luau.Write(ValueText);
}
}
12 changes: 12 additions & 0 deletions RobloxCS/LuauGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,18 @@ public override Luau.Node VisitExpressionStatement(ExpressionStatementSyntax nod
: expressionNode;
}

public override Node? VisitInterpolatedStringExpression(InterpolatedStringExpressionSyntax node)
{
var parts = node.Contents.Select(Visit<Luau.Expression>).ToList();
return new Luau.InterpolatedString(parts);
}

public override Luau.Literal VisitInterpolatedStringText(InterpolatedStringTextSyntax node) =>
new(node.TextToken.Text);

public override Luau.Interpolation VisitInterpolation(InterpolationSyntax node) =>
new(Visit<Luau.Expression>(node.Expression));

public override Luau.Literal VisitLiteralExpression(LiteralExpressionSyntax node)
{
var valueText = "";
Expand Down

0 comments on commit 5d15239

Please sign in to comment.