Skip to content

Commit

Permalink
feat: Add a new library to make it easier to work with a backend
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Nov 6, 2024
1 parent 66e1b63 commit b0580e1
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
sed -i "s/<Version>.*<\/Version>/<Version>${RELEASE_VERSION}<\/Version>/" Source/Silverfly.Generator/Silverfly.Generator.csproj
sed -i "s/<Version>.*<\/Version>/<Version>${RELEASE_VERSION}<\/Version>/" Source/Silverfly.Repl/Silverfly.Repl.csproj
sed -i "s/<Version>.*<\/Version>/<Version>${RELEASE_VERSION}<\/Version>/" Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj
sed -i "s/<Version>.*<\/Version>/<Version>${RELEASE_VERSION}<\/Version>/" Source/Silverfly.Backend/Silverfly.Backend.csproj
- name: GIT commit and push overriding conflicts with local changes (verbose)
run: |
Expand All @@ -61,6 +62,7 @@ jobs:
dotnet build -c Release -o ./nuget Source/Silverfly.Testing/Silverfly.Testing.csproj
dotnet build -c Release -o ./nuget Source/Silverfly.Repl/Silverfly.Repl.csproj
dotnet build -c Release -o ./nuget Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj
dotnet build -c Release -o ./nuget Source/Silverfly.Backend/Silverfly.Backend.csproj
dotnet build -c Release -o ./nuget Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj
dotnet build -o ./nuget Source/Silverfly.Generator/Silverfly.Generator.csproj
Expand Down
25 changes: 25 additions & 0 deletions Source/Silverfly.Backend/Silverfly.Backend.csproj
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 Source/Silverfly.Backend/TypeSystem/Rules/BinaryTypeRule.cs
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 Source/Silverfly.Backend/TypeSystem/Rules/LiteralTypeRule.cs
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")
};
}
}
37 changes: 37 additions & 0 deletions Source/Silverfly.Backend/TypeSystem/TypeEngine.cs
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));
}
}
11 changes: 11 additions & 0 deletions Source/Silverfly.Backend/TypeSystem/TypeRule.cs
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);
}
6 changes: 6 additions & 0 deletions Source/Silverfly.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Brainfuck", "Samples
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silverfly.TreeVisualizer", "Silverfly.TreeVisualizer\Silverfly.TreeVisualizer.csproj", "{A0D943E1-2579-4C51-84C6-375374FA0104}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silverfly.Backend", "Silverfly.Backend\Silverfly.Backend.csproj", "{BBCFB38E-73FC-4FD7-8CFE-11D43E00119C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -77,6 +79,10 @@ Global
{A0D943E1-2579-4C51-84C6-375374FA0104}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0D943E1-2579-4C51-84C6-375374FA0104}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0D943E1-2579-4C51-84C6-375374FA0104}.Release|Any CPU.Build.0 = Release|Any CPU
{BBCFB38E-73FC-4FD7-8CFE-11D43E00119C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBCFB38E-73FC-4FD7-8CFE-11D43E00119C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBCFB38E-73FC-4FD7-8CFE-11D43E00119C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBCFB38E-73FC-4FD7-8CFE-11D43E00119C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b0580e1

Please sign in to comment.