Skip to content

Commit

Permalink
Merge branch 'feature/codegeneration'
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele-tomassetti committed Dec 16, 2024
2 parents 399621c + 66dface commit 2dbd12e
Show file tree
Hide file tree
Showing 15 changed files with 1,365 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## 1.0.0
- Added Code Generation support

## 0.9.4
- Downgrade MSTest and remove support for .NETStandard 2.1 to solve potential compatibility issues with some platforms, IDEs
- Remove automatically-added dependency to Microsoft.CSharp
Expand Down
166 changes: 166 additions & 0 deletions Sharplasu.Tests/CodeGeneration/ASTCodeGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using Strumenta.Sharplasu.CodeGeneration;

namespace Strumenta.Sharplasu.Tests.CodeGeneration;

using System.Collections.Generic;
using System.Linq.Expressions;
using Strumenta.Sharplasu.Model;
using Strumenta.Sharplasu.Tests.Transformation;
using Strumenta.Sharplasu.Transformation;

[TestClass]
public class ASTCodeGeneratorTest
{
[TestMethod]
public void PrintSimpleKotlinExpression()
{
var ex = new KMethodCallExpression(
new KThisExpression(),
new ReferenceByName<KMethodSymbol>("myMethod"),
new List<KExpression>
{
new KStringLiteral("abc"),
new KIntLiteral(123),
new KStringLiteral("qer")
}
);
TestUtils.CheckDestinationIsNull(ex);
var code = new KotlinPrinter().PrintToString(ex);
TestUtils.CheckDestinationIsNotNull(ex);
Assert.AreEqual(@"this.myMethod(""abc"", 123, ""qer"")", code);
}

[TestMethod]
public void PrintSimpleFile()
{
var cu = new KCompilationUnit(
new KPackageDecl("my.splendid.packag"),
new List<KImport> { new KImport("my.imported.stuff") },
new List<KTopLevelDeclaration> { new KFunctionDeclaration("foo") }
);

TestUtils.CheckDestinationIsNull(cu);
var code = new KotlinPrinter().PrintToString(cu);
TestUtils.CheckDestinationIsNotNull(cu);
Assert.AreEqual(
@"package my.splendid.packag
import my.imported.stuff
fun foo() {
}
".ReplaceLineEndings("\n"), code);
}

public void PrintIndentation()
{
var em = new KExtensionMethod(new KSimpleName("magic"), "foo");
em.Body.Add(new KExpressionStatement(new KFunctionCall(new ReferenceByName<KFunctionSymbol>("hello"))));
var cu = new KCompilationUnit(
new KPackageDecl("my.splendid.packag"),
new List<KImport> { new KImport("my.imported.stuff") },
new List<KTopLevelDeclaration> { em }
);

TestUtils.CheckDestinationIsNull(cu);
var code = new KotlinPrinter().PrintToString(cu);
TestUtils.CheckDestinationIsNotNull(cu);
Assert.AreEqual(
@"package my.splendid.packag
import my.imported.stuff
fun foo() {
hello()
}
".ReplaceLineEndings("\n"), code);
}

[TestMethod]
public void PrintUsingNodePrinterOverrider()
{
var ex = new KMethodCallExpression(
new KThisExpression(),
new ReferenceByName<KMethodSymbol>("myMethod"),
new List<KExpression>
{
new KStringLiteral("abc"),
new KIntLiteral(123),
new KStringLiteral("qer")
}
);

TestUtils.CheckDestinationIsNull(ex);
var code = new KotlinPrinter().PrintToString(ex);
TestUtils.CheckDestinationIsNotNull(ex);
Assert.AreEqual(@"this.myMethod(""abc"", 123, ""qer"")", code);

var printer = new KotlinPrinter();
printer.NodePrinterOverrider = node =>
{
return node switch
{
KStringLiteral => new ActionNodePrinter<KStringLiteral>((output, ast) => output.Print("YYY")),
KIntLiteral => new ActionNodePrinter<KIntLiteral>((output, ast) => output.Print("XXX")),
_ => null
};
};

var codeWithNodePrinterOverrider = printer.PrintToString(ex);
Assert.AreEqual(@"this.myMethod(YYY, XXX, YYY)", codeWithNodePrinterOverrider);
}

[TestMethod]
public void PrintUntranslatedNodes()
{
var failedNode = new KImport("my.imported.stuff");
failedNode.Origin = new MissingAstTransformation(failedNode);

var cu = new KCompilationUnit(
new KPackageDecl("my.splendid.packag"),
new List<KImport> { failedNode },
new List<KTopLevelDeclaration> { new KFunctionDeclaration("foo") }
);
TestUtils.CheckDestinationIsNull(cu);

var code = new KotlinPrinter().PrintToString(cu);
TestUtils.CheckDestinationIsNotNull(cu);
Assert.AreEqual(
@"package my.splendid.packag
/* Translation of a node is not yet implemented: KImport */
fun foo() {
}
".ReplaceLineEndings("\n"), code);
}

[TestMethod]
public void PrintTransformationFailure()
{
var failedNode = new KImport("my.imported.stuff")
{
Origin = new FailingAstTransformation(null, "Something made BOOM!")
};

var cu = new KCompilationUnit(
new KPackageDecl("my.splendid.packag"),
new List<KImport> { failedNode },
new List<KTopLevelDeclaration> { new KFunctionDeclaration("foo") }
);
TestUtils.CheckDestinationIsNull(cu);

var code = new KotlinPrinter().PrintToString(cu);
TestUtils.CheckDestinationIsNotNull(cu);
Assert.AreEqual(
@"package my.splendid.packag
/* Something made BOOM! */
fun foo() {
}
".ReplaceLineEndings("\n"), code);
}
}
Loading

0 comments on commit 2dbd12e

Please sign in to comment.