Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compiler Add ]Recursive pattern #995

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Neo.Compiler.CSharp/MethodConvert/Pattern/Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ private void ConvertPattern(SemanticModel model, PatternSyntax pattern, byte loc
case ParenthesizedPatternSyntax parenthesizedPattern:
ConvertParenthesizedPatternSyntax(model, parenthesizedPattern, localIndex);
break;
case RecursivePatternSyntax recursivePattern:
ConvertRecursivePattern(model, recursivePattern, localIndex);
break;
default:
//Example:
//object greeting = "Hello, World!";
Expand Down
59 changes: 59 additions & 0 deletions src/Neo.Compiler.CSharp/MethodConvert/Pattern/RecursivePattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2015-2023 The Neo Project.
//
// The Neo.Compiler.CSharp is free software distributed under the MIT
// software license, see the accompanying file LICENSE in the main directory
// of the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

extern alias scfx;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.VM;

namespace Neo.Compiler;

partial class MethodConvert
{
private void ConvertRecursivePattern(SemanticModel model, RecursivePatternSyntax pattern, byte localIndex)
{
if (pattern.PropertyPatternClause is { } propertyClause)
{
AccessSlot(OpCode.LDLOC, localIndex);
foreach (var subpattern in propertyClause.Subpatterns)
{
if (subpattern is { Pattern: ConstantPatternSyntax constantPattern })
{
// Example:
// if (newOwner is { IsValid: true, IsZero:false})
// {
// }
var propertySymbol = model.GetSymbolInfo(subpattern.NameColon!.Name).Symbol!;

if (propertySymbol is IPropertySymbol property)
{
Call(model, property.GetMethod!);
}
else
{
throw new CompilationException(subpattern, DiagnosticId.SyntaxNotSupported, $"Unsupported property or field: {subpattern.NameColon.Name}");
}
object? constantValue = model.GetConstantValue(constantPattern.Expression).Value;
Push(constantValue);
AddInstruction(OpCode.EQUAL);
}
else
{
throw new CompilationException(subpattern, DiagnosticId.SyntaxNotSupported, $"Unsupported subpattern: {subpattern}");
}
}
}
else
{
throw new CompilationException(pattern, DiagnosticId.SyntaxNotSupported, $"Unsupported pattern: {pattern}");
}
}
}
3 changes: 3 additions & 0 deletions tests/Neo.Compiler.CSharp.TestContracts/Contract1.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Neo.SmartContract.Framework;

namespace Neo.Compiler.CSharp.UnitTests.TestClasses
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
public class Contract1 : SmartContract.Framework.SmartContract
Expand Down Expand Up @@ -40,5 +42,6 @@ public static int testArgs4(int a, int b)
a = a + 2;
return a + b;
}

shargon marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions tests/Neo.Compiler.CSharp.TestContracts/Contract_Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public bool between3(int value)
};
}

public bool testRecursivePattern()
{
UInt160? newOwner = UInt160.Zero;

return newOwner switch
{
{ IsValid: true, IsZero: false } => true,
_ => false,
};
}

public bool between4(int value)
{
return value is <= 0;
Expand Down
11 changes: 11 additions & 0 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,16 @@ public void Between3_Test()
value = result.Pop().GetBoolean();
Assert.AreEqual(false, value);
}

[TestMethod]
public void RecursivePattern_Test()
{
using var testengine = new TestEngine();
testengine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_Pattern.cs");

var result = testengine.ExecuteTestCaseStandard("testRecursivePattern");
var value = result.Pop().GetBoolean();
Assert.AreEqual(true, value);
}
}
}
Loading