Skip to content

Commit

Permalink
Merge pull request #8 from Mooshua/workspace
Browse files Browse the repository at this point in the history
Improve Scanning and Mutator APIs
  • Loading branch information
Mooshua committed Jul 29, 2023
2 parents 3a8382f + 88f585f commit dfb4767
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 28 deletions.
20 changes: 18 additions & 2 deletions Lilikoi/Compiler/Mahogany/MahoganyMethod.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ========================
// Lilikoi::MahoganyMethod.cs
// (c) 2023. Distributed under the MIT License
//
//
// -> Created: 22.12.2022
// -> Bumped: 06.02.2023
// ========================
Expand All @@ -19,6 +19,22 @@ namespace Lilikoi.Compiler.Mahogany;

public class MahoganyMethod
{
public MahoganyMethod(MethodInfo method)
{
Parameters = method.GetParameters().Select(x => x.ParameterType).ToList();
Return = method.ReturnType;
HaltTarget = Expression.Label(method.ReturnType, "Halt");
Entry = method;
Host = method.DeclaringType;
NamedVariables = new Dictionary<string, ParameterExpression>()
{
{ MahoganyConstants.HOST_VAR, Expression.Parameter(method.DeclaringType, MahoganyConstants.HOST_VAR) }
};
SymbolDocument = Expression.SymbolDocument("Lk__Autogenerated.cs");
}

public SymbolDocumentInfo SymbolDocument { get; }

public List<ParameterExpression> Temporaries = new();

public LabelTarget HaltTarget { get; set; }
Expand Down Expand Up @@ -132,4 +148,4 @@ public LambdaExpression Lambda()
public Type Result { get; set; }

#endregion
}
}
13 changes: 1 addition & 12 deletions Lilikoi/Compiler/Public/LilikoiMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ public static LilikoiMethod FromMethodInfo(MethodInfo method)
{
return new LilikoiMethod()
{
Implementation = new MahoganyMethod()
{
Parameters = method.GetParameters().Select(x => x.ParameterType).ToList(),
Return = method.ReturnType,
HaltTarget = Expression.Label(method.ReturnType, "Halt"),
Entry = method,
Host = method.DeclaringType,
NamedVariables = new Dictionary<string, ParameterExpression>()
{
{ MahoganyConstants.HOST_VAR, Expression.Parameter(method.DeclaringType, MahoganyConstants.HOST_VAR) }
}
}
Implementation = new MahoganyMethod(method)
};
}

Expand Down
21 changes: 15 additions & 6 deletions Lilikoi/Compiler/Public/LilikoiMutator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ========================
// Lilikoi::LilikoiMutator.cs
// (c) 2023. Distributed under the MIT License
//
//
// -> Created: 31.01.2023
// -> Bumped: 06.02.2023
// ========================
Expand Down Expand Up @@ -44,8 +44,7 @@ public LilikoiMutator Implicit(LkWrapBuilderAttribute value)
public LilikoiMutator Implicit<TWrap>(TWrap value = null)

Check warning on line 44 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net48)

Cannot convert null literal to non-nullable reference type.

Check warning on line 44 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net6.0)

Cannot convert null literal to non-nullable reference type.

Check warning on line 44 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net7.0)

Cannot convert null literal to non-nullable reference type.

Check warning on line 44 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net47)

Cannot convert null literal to non-nullable reference type.

Check warning on line 44 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
where TWrap : LkWrapBuilderAttribute, new()
{
if (value is null)
value = new TWrap();
value ??= new TWrap();

Compiler.ImplicitWraps.Add(value);
return this;
Expand All @@ -68,12 +67,12 @@ public LilikoiMutator Wildcard<TType>(LkParameterBuilderAttribute value)
/// </summary>
/// <param name="value"></param>
/// <typeparam name="TParameter"></typeparam>
/// <typeparam name="TType"></typeparam>
/// <returns></returns>
public LilikoiMutator Wildcard<TType, TParameter>(TParameter value = null)

Check warning on line 72 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net48)

Cannot convert null literal to non-nullable reference type.

Check warning on line 72 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net6.0)

Cannot convert null literal to non-nullable reference type.

Check warning on line 72 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net7.0)

Cannot convert null literal to non-nullable reference type.

Check warning on line 72 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / test (net47)

Cannot convert null literal to non-nullable reference type.

Check warning on line 72 in Lilikoi/Compiler/Public/LilikoiMutator.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
where TParameter : LkParameterBuilderAttribute, new()
{
if (value is null)
value = new TParameter();
value ??= new TParameter();

Compiler.ImplicitWildcards.Add((value, typeof(TType)));
return this;
Expand All @@ -94,4 +93,14 @@ public LilikoiMutator Wildcard<TType>(LkParameterBuilderAttribute value)

return parameters[paramNum];
}
}

/// <summary>
/// Get the number of parameters on the host function
/// </summary>
public int Parameters => Compiler.Internal.Method.Parameters.Count;

/// <summary>
/// The return type of the underlying function
/// </summary>
public Type Result => Compiler.Internal.Method.Return;
}
51 changes: 48 additions & 3 deletions Lilikoi/Scan/Scanner.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ========================
// Lilikoi::Scanner.cs
// (c) 2023. Distributed under the MIT License
//
//
// -> Created: 31.01.2023
// -> Bumped: 06.02.2023
// ========================
Expand Down Expand Up @@ -53,7 +53,53 @@ public static class Scanner
List<LilikoiContainer> containers = new();

foreach (var type in assembly.GetTypes())
containers.AddRange(Scan<TUserContext, TInput, TOutput>(context, type, sourceMount));

return containers;
}

/// <summary>
/// Scan the provided generic type for Lilikoi Containers.
/// Use the last parameter to supply a mount for each container.
/// </summary>
/// <param name="context"></param>
/// <param name="sourceMount"></param>
/// <typeparam name="TUserContext"></typeparam>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
/// <typeparam name="TType"></typeparam>
/// <returns></returns>
public static List<LilikoiContainer> Scan<TUserContext, TInput, TOutput, TType>(TUserContext context, Func<Mount> sourceMount)
where TUserContext : Mount
=> Scan<TUserContext, TInput, TOutput>(context, typeof(TType), sourceMount);

public static List<LilikoiContainer> Scan<TUserContext, TInput, TOutput>(TUserContext context, Type type, Func<Mount> sourceMount)
where TUserContext : Mount
{
List<LilikoiContainer> containers = new();

foreach (var methodInfo in type.GetMethods())
containers.AddRange(Scan<TUserContext, TInput, TOutput>(context, methodInfo, sourceMount));

return containers;
}

/// <summary>
/// Scan an individual method for lilikoi containers.
/// Note that if there are multiple target attributes then there can be multiple containers returned.
/// </summary>
/// <param name="context"></param>
/// <param name="methodInfo"></param>
/// <param name="sourceMount"></param>
/// <typeparam name="TUserContext"></typeparam>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
/// <returns></returns>
public static List<LilikoiContainer> Scan<TUserContext, TInput, TOutput>(TUserContext context, MethodInfo methodInfo, Func<Mount> sourceMount)
where TUserContext : Mount
{
List<LilikoiContainer> containers = new();

foreach (LkTargetBuilderAttribute attribute in methodInfo.GetCustomAttributes()
.Where(attribute => attribute
.GetType()
Expand All @@ -75,7 +121,6 @@ public static class Scanner

containers.Add(compiler.Finish());
}

return containers;
}
}
}
2 changes: 2 additions & 0 deletions Lilikoi/Standard/Extensions/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// -> Created: 01.02.2023
// -> Bumped: 06.02.2023
// ========================
using Lilikoi.Standard.Factory;

namespace Lilikoi.Standard.Extensions;

public static class RegistrationExtensions
Expand Down
9 changes: 7 additions & 2 deletions Lilikoi/Standard/Factory/FactoryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#endregion

namespace Lilikoi.Standard;
namespace Lilikoi.Standard.Factory;

public class FactoryAttribute : LkInjectionAttribute
{
Expand All @@ -23,6 +23,11 @@ public override bool IsInjectable<TInjectable>(Mount mount)

public override TInjectable Inject<TInjectable>(Mount context)
{
return context.Get<IFactory<TInjectable>>().Create();
var factory = context.Get<IFactory<TInjectable>>();

if (factory is null)
throw new InvalidOperationException($"No factory for type {typeof(TInjectable).FullName} exists.");

return factory.Create();
}
}
6 changes: 3 additions & 3 deletions Lilikoi/Standard/Factory/IFactory.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// ========================
// Lilikoi::IFactory.cs
// (c) 2023. Distributed under the MIT License
//
//
// -> Created: 01.02.2023
// -> Bumped: 06.02.2023
// ========================
namespace Lilikoi.Standard;
namespace Lilikoi.Standard.Factory;

public interface IFactory<TProduct>
{
public TProduct Create();
}
}

0 comments on commit dfb4767

Please sign in to comment.