-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a new library to make it easier to work with a backend
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Version>1.0.77</Version> | ||
<LangVersion>preview</LangVersion> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Title>Silverfly.Backend</Title> | ||
<Copyright>furesoft</Copyright> | ||
<RepositoryUrl>https://github.com/furesoft/Silverfly</RepositoryUrl> | ||
<Description>Helpers to convert from Silverfly to a Distil backend</Description> | ||
<PackageTags>silverfly, distil</PackageTags> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> | ||
<IsTrimmable>true</IsTrimmable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Silverfly\Silverfly.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DistIL.Core" Version="0.7.0-preview" /> | ||
</ItemGroup> | ||
|
||
</Project> |
32 changes: 32 additions & 0 deletions
32
Source/Silverfly.Backend/TypeSystem/Rules/BinaryTypeRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using DistIL.AsmIO; | ||
using Silverfly.Nodes; | ||
using Silverfly.Nodes.Operators; | ||
|
||
namespace Silverfly.Backend.TypeSystem.Rules; | ||
|
||
public class BinaryTypeRule : TypeRule | ||
{ | ||
public override bool IsCompatible(TypeDesc left, TypeDesc right) | ||
{ | ||
return true; | ||
} | ||
|
||
public override TypeDesc InferType(AstNode node, TypeEngine engine) | ||
{ | ||
if (node is not BinaryOperatorNode binary) | ||
{ | ||
throw new InvalidOperationException("Incompatible type"); | ||
} | ||
|
||
var inferredLeft = engine.InferType(binary.LeftExpr); | ||
var inferredRight = engine.InferType(binary.RightExpr); | ||
|
||
if (inferredLeft == inferredRight) | ||
{ | ||
return inferredLeft; | ||
} | ||
|
||
return TypeDesc.GetCommonAncestor(inferredLeft, inferredRight); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Source/Silverfly.Backend/TypeSystem/Rules/LiteralTypeRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using DistIL.AsmIO; | ||
using Silverfly.Nodes; | ||
|
||
namespace Silverfly.Backend.TypeSystem.Rules; | ||
|
||
|
||
public class LiteralTypeRule : TypeRule | ||
{ | ||
public override bool IsCompatible(TypeDesc left, TypeDesc right) | ||
{ | ||
return true; //ToDo: implement | ||
} | ||
|
||
public override TypeDesc InferType(AstNode node, TypeEngine engine) | ||
{ | ||
if (node is not LiteralNode literal) | ||
{ | ||
throw new InvalidOperationException("Incompatible type"); | ||
} | ||
|
||
return literal.Value switch | ||
{ | ||
bool => PrimType.Bool, | ||
char => PrimType.Char, | ||
int => PrimType.Int32, | ||
double => PrimType.Double, | ||
string => PrimType.String, | ||
_ => throw new InvalidOperationException("Incompatible type") | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using DistIL.AsmIO; | ||
using Silverfly.Nodes; | ||
|
||
namespace Silverfly.Backend.TypeSystem; | ||
|
||
public class TypeEngine | ||
{ | ||
private readonly List<TypeRule> _rules = new(); | ||
|
||
public void AddRule(TypeRule rule) => _rules.Add(rule); | ||
|
||
public TypeDesc InferType(AstNode node) | ||
{ | ||
foreach (var rule in _rules) | ||
{ | ||
try | ||
{ | ||
return rule.InferType(node, this); | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
|
||
} | ||
} | ||
|
||
throw new InvalidOperationException("Cannot infer type"); | ||
} | ||
|
||
public bool IsCompatible(TypeDesc left, TypeDesc right) | ||
{ | ||
return _rules.Any(rule => rule.IsCompatible(left, right)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using DistIL.AsmIO; | ||
using Silverfly.Nodes; | ||
|
||
namespace Silverfly.Backend.TypeSystem; | ||
|
||
public abstract class TypeRule | ||
{ | ||
public abstract bool IsCompatible(TypeDesc left, TypeDesc right); | ||
|
||
public abstract TypeDesc InferType(AstNode node, TypeEngine engine); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters