Skip to content

Commit 7fca334

Browse files
antonsyndclaude
andcommitted
feat(codegen): emit Sharpy.Bytes construction from byte string literals
Add GenerateBytesLiteral() that converts b"hello" to new Sharpy.Bytes(new byte[] { 104, 101, 108, 108, 111 }) using SyntaxFactory. Add SharpyBytes constant to CSharpTypeNames and bytes mapping to TypeSyntaxMapper. Update exhaustiveness test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ad3576f commit 7fca334

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/Sharpy.Compiler.Tests/AstExhaustivenessTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void AllExpressionSubtypes_AreCoveredInCodeGen()
3333
var handledTypes = new HashSet<string>
3434
{
3535
// Literals
36-
"IntegerLiteral", "FloatLiteral", "StringLiteral",
36+
"IntegerLiteral", "FloatLiteral", "StringLiteral", "BytesLiteralExpression",
3737
"BooleanLiteral", "NoneLiteral", "EllipsisLiteral", "FStringLiteral",
3838
// Collections
3939
"ListLiteral", "DictLiteral", "SetLiteral", "TupleLiteral",

src/Sharpy.Compiler/CodeGen/RoslynEmitter.Expressions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private ExpressionSyntax GenerateExpression(Sharpy.Compiler.Parser.Ast.Expressio
2323
IntegerLiteral intLit => GenerateIntegerLiteral(intLit),
2424
FloatLiteral floatLit => GenerateFloatLiteral(floatLit),
2525
StringLiteral strLit => GenerateStringLiteral(strLit),
26+
BytesLiteralExpression bytesLit => GenerateBytesLiteral(bytesLit),
2627
BooleanLiteral boolLit => LiteralExpression(boolLit.Value ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression),
2728
NoneLiteral => LiteralExpression(SyntaxKind.DefaultLiteralExpression),
2829
EllipsisLiteral => GenerateEllipsisLiteral(),
@@ -260,6 +261,26 @@ private ExpressionSyntax GenerateStringLiteral(StringLiteral literal)
260261
return LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(literal.Value));
261262
}
262263

264+
private ExpressionSyntax GenerateBytesLiteral(BytesLiteralExpression literal)
265+
{
266+
// Convert each character to a byte literal: b"hello" -> new Sharpy.Bytes(new byte[] { 104, 101, 108, 108, 111 })
267+
var byteValues = literal.Value.Select(c =>
268+
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal((byte)c)));
269+
270+
var byteArrayInit = ArrayCreationExpression(
271+
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
272+
.WithRankSpecifiers(SingletonList(
273+
ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(
274+
OmittedArraySizeExpression())))))
275+
.WithInitializer(
276+
InitializerExpression(
277+
SyntaxKind.ArrayInitializerExpression,
278+
SeparatedList<ExpressionSyntax>(byteValues)));
279+
280+
return ObjectCreationExpression(ParseTypeName(CSharpTypeNames.SharpyBytes))
281+
.WithArgumentList(ArgumentList(SingletonSeparatedList(Argument(byteArrayInit))));
282+
}
283+
263284
/// <summary>
264285
/// Gets the semantic type of an expression from SemanticInfo, if available.
265286
/// </summary>

src/Sharpy.Compiler/CodeGen/TypeSyntaxMapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static TypeSyntaxMapper()
3434
_builtinTypeMap[BuiltinNames.List] = CSharpTypeNames.SharpyList;
3535
_builtinTypeMap[BuiltinNames.Dict] = CSharpTypeNames.SharpyDict;
3636
_builtinTypeMap[BuiltinNames.Set] = CSharpTypeNames.SharpySet;
37+
_builtinTypeMap[BuiltinNames.Bytes] = CSharpTypeNames.SharpyBytes;
3738
_builtinTypeMap[BuiltinNames.Tuple] = "System.ValueTuple";
3839
}
3940

src/Sharpy.Compiler/Shared/CSharpTypeNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal static class CSharpTypeNames
99
internal const string SharpyList = "Sharpy.List";
1010
internal const string SharpyDict = "Sharpy.Dict";
1111
internal const string SharpySet = "Sharpy.Set";
12+
internal const string SharpyBytes = "Sharpy.Bytes";
1213
internal const string SharpyOptional = "Sharpy.Optional";
1314
internal const string SharpyResult = "Sharpy.Result";
1415
internal const string IEnumerable = "IEnumerable";

0 commit comments

Comments
 (0)