Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Sep 19, 2023
1 parent 5b79c27 commit fa616b6
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 176 deletions.
9 changes: 1 addition & 8 deletions Source/Backlang-Compiler/compilation.back
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@

func main() {
if true {
return 1;
}
else {
return 2;
}

print("after");
print("hello world");
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@

namespace Backlang.Driver.Compiling.Targets.Dotnet;

public class ClrTypeEnvironmentBuilder
public static class ClrTypeEnvironmentBuilder
{
private static ConcurrentBag<(MethodBase, DescribedMethod)> toAdjustParameters = new();
private static readonly ConcurrentBag<(MethodBase, DescribedMethod)> toAdjustParameters = new();

public static IAssembly CollectTypes(Assembly ass)
{
var assembly = new DescribedAssembly(new QualifiedName(ass.GetName().Name));
var assembly = new DescribedAssembly(new(ass.GetName().Name));

var types = ass.GetTypes();

foreach (var type in types)
{
if (!type.IsPublic) continue;
if (!type.IsPublic || type.Namespace.StartsWith("Microsoft.VSDesigner"))
{
continue;
}

var ns = ConversionUtils.QualifyNamespace(type.Namespace);

Expand All @@ -32,8 +35,12 @@ public static IAssembly CollectTypes(Assembly ass)

public static void FillTypes(Assembly ass, CompilerContext context)
{
Parallel.ForEach(ass.GetTypes(), type => {
if (!type.IsPublic) return;
foreach (var type in ass.GetTypes())
{
if (!type.IsPublic)
{
continue;
}

var t = Utils.ResolveType(context.Binder, type);

Expand Down Expand Up @@ -62,14 +69,14 @@ public static void FillTypes(Assembly ass, CompilerContext context)
var value = prop.GetValue(attr);

attribute.ConstructorArguments.Add(
new AttributeArgument(Utils.ResolveType(context.Binder, prop.PropertyType), value));
new(Utils.ResolveType(context.Binder, prop.PropertyType), value));
}

t.AddAttribute(attribute);
}

AddMembers(type, t, context.Binder);
});
}

foreach (var toadjust in toAdjustParameters)
{
Expand All @@ -81,7 +88,8 @@ public static void FillTypes(Assembly ass, CompilerContext context)

public static void AddMembers(Type type, DescribedType t, TypeResolver resolver)
{
Parallel.ForEach(type.GetMembers(), member => {
foreach (var member in type.GetMembers())
{
if (member is ConstructorInfo ctor && ctor.IsPublic)
{
var method = new DescribedMethod(t,
Expand All @@ -93,7 +101,8 @@ public static void AddMembers(Type type, DescribedType t, TypeResolver resolver)

foreach (DescribedGenericParameter gp in t.GenericParameters)
{
method.AddGenericParameter(new DescribedGenericParameter(method, new SimpleName(gp.Name.ToString())));
method.AddGenericParameter(
new DescribedGenericParameter(method, new SimpleName(gp.Name.ToString())));
}

t.AddMethod(method);
Expand All @@ -112,11 +121,11 @@ public static void AddMembers(Type type, DescribedType t, TypeResolver resolver)
else if (member is PropertyInfo prop)
{
var p = new DescribedProperty(new SimpleName(prop.Name),
Utils.ResolveType(resolver, prop.PropertyType), t);
Utils.ResolveType(resolver, prop.PropertyType), t);

t.AddProperty(p);
}
});
}
}

public static void AddMethod(DescribedType t, TypeResolver resolver, MethodInfo m,
Expand Down Expand Up @@ -148,21 +157,25 @@ public static void ConvertParameter(ParameterInfo[] parameterInfos, DescribedMet

if (p.ParameterType.IsByRef)
{
type = Utils.ResolveType(context.Binder, p.ParameterType.Name.Replace("&", ""), p.ParameterType.Namespace)?.MakePointerType(PointerKind.Reference);
type = Utils
.ResolveType(context.Binder, p.ParameterType.Name.Replace("&", ""), p.ParameterType.Namespace)
?.MakePointerType(PointerKind.Reference);
}
else if (p.ParameterType.IsArray)
{
type = Utils.ResolveType(context.Binder, p.ParameterType.Name.Replace("&", ""), p.ParameterType.Namespace);
type = Utils.ResolveType(context.Binder, p.ParameterType.Name.Replace("&", ""),
p.ParameterType.Namespace);

if (type != null)
{
type = context.Environment.MakeArrayType(type, p.ParameterType.GetArrayRank());
}
}

if (type != null)
{
var pa = new Parameter(type, p.Name);
method.AddParameter(pa);
continue;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void WriteTo(Stream output)

foreach (var type in _assembly.Types.Cast<DescribedType>())
{
var clrType = new TypeDefinition(type.FullName.Slice(0, type.FullName.PathLength - 1).FullName.ToString(),
var clrType = new TypeDefinition(type.FullName.Slice(0, type.FullName.PathLength - 1).FullName,
type.Name.ToString(), TypeAttributes.Class);

MakeStructReadonly(type, clrType);
Expand Down
14 changes: 10 additions & 4 deletions Source/Backlang.Driver/Compiling/Targets/Dotnet/DotNetTarget.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Backlang.Core;
using Backlang.Driver.Compiling.Targets.Dotnet.RuntimeOptionsModels;
using Furesoft.Core.CodeDom.Backends.CLR;
using Furesoft.Core.CodeDom.Compiler.Pipeline;
using LeMP;
using System.Collections;
Expand Down Expand Up @@ -60,7 +61,7 @@ public TypeEnvironment Init(CompilerContext context)
ClrTypeEnvironmentBuilder.FillTypes(typeof(Result<>).Assembly, context);
ClrTypeEnvironmentBuilder.FillTypes(typeof(ArrayList).Assembly, context);

return new Furesoft.Core.CodeDom.Backends.CLR.CorlibTypeEnvironment(corLib);
return new CorlibTypeEnvironment(corLib);
}

public void InitReferences(CompilerContext context)
Expand All @@ -70,11 +71,16 @@ public void InitReferences(CompilerContext context)
AddFromAssembly(context, r);
}

if (context.CorLib == null) return;
if (context.CorLib == null)
{
return;
}

foreach (var r in context.CorLib.Split(';'))
foreach (var r in context.CorLib.Split(new[] {".dll"}, StringSplitOptions.RemoveEmptyEntries))
{
AddFromAssembly(context, r);
var rr = r.TrimEnd(';').TrimStart(';') + ".dll";

AddFromAssembly(context, rr);
}

static void AddFromAssembly(CompilerContext context, string r)
Expand Down
Loading

0 comments on commit fa616b6

Please sign in to comment.