This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
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.
- Loading branch information
1 parent
3a23dc5
commit fc68536
Showing
24 changed files
with
374 additions
and
215 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
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
namespace Moth.AST.Node; | ||
|
||
public class LocalTypeRefNode : TypeRefNode | ||
{ | ||
public LocalTypeRefNode(string name, uint pointerDepth = 0) : base(name, pointerDepth) { } | ||
} |
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,8 @@ | ||
namespace Moth.AST.Node; | ||
|
||
public class TemplateTypeRefNode : TypeRefNode | ||
{ | ||
public List<ExpressionNode> Arguments { get; set; } | ||
|
||
public TemplateTypeRefNode(string name, List<ExpressionNode> args, uint pointerDepth = 0) : base(name, pointerDepth) => Arguments = args; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
namespace Moth.LLVM; | ||
|
||
public class CompilerData | ||
public abstract class CompilerData | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,91 @@ | ||
using Moth.AST.Node; | ||
|
||
namespace Moth.LLVM.Data; | ||
|
||
public class Template : CompilerData | ||
{ | ||
public Namespace Parent { get; } | ||
public string Name { get; } | ||
public PrivacyType Privacy { get; } | ||
public ScopeNode Contents { get; } | ||
|
||
public Namespace[] Imports { get; } | ||
public Dictionary<string, IAttribute> Attributes { get; } | ||
public TemplateParameter[] Params { get; } | ||
|
||
private Dictionary<string, Struct> _builtTypes = new Dictionary<string, Struct>(); | ||
|
||
public Template(Namespace parent, string name, PrivacyType privacy, ScopeNode contents, | ||
Namespace[] imports, Dictionary<string, IAttribute> attributes, TemplateParameter[] @params) | ||
{ | ||
Parent = parent; | ||
Name = name; | ||
Privacy = privacy; | ||
Contents = contents; | ||
Imports = imports; | ||
Attributes = attributes; | ||
Params = @params; | ||
} | ||
|
||
public Struct Build(LLVMCompiler compiler, IReadOnlyList<ExpressionNode> args) | ||
{ | ||
string sig = ArgsToSig(args); | ||
|
||
if (args.Count != Params.Length) | ||
{ | ||
throw new Exception($"Template arguments are {args.Count} long, expected {Params.Length} arguments for template \"{Name}\"."); | ||
} | ||
|
||
if (_builtTypes.TryGetValue(sig, out Struct @struct)) | ||
{ | ||
return @struct; | ||
} | ||
|
||
for (var i = 0; i < Params.Length; i++) | ||
{ | ||
var param = Params[i]; | ||
var arg = args[i]; | ||
|
||
if (param.IsConst) | ||
{ | ||
if (arg is not LiteralNode literal) | ||
{ | ||
throw new Exception($"Template argument {i} for template \"{Name}\" is expected to be a constant value."); | ||
} | ||
|
||
throw new NotImplementedException(); | ||
} | ||
else | ||
{ | ||
if (arg is not TypeRefNode typeRef) | ||
{ | ||
throw new Exception($"Template argument {i} for template \"{Name}\" is expected to be a type."); | ||
} | ||
} | ||
} | ||
|
||
var classNode = new ClassNode($"{Name}{Template.ArgsToSig(args)}", Privacy, Contents, true); | ||
@struct = new Struct(Parent, | ||
classNode.Name, | ||
compiler.Context.CreateNamedStruct(classNode.Name), | ||
Privacy); | ||
_builtTypes.Add(sig, @struct); | ||
@compiler.BuildTemplate(this, classNode, @struct, args); | ||
return @struct; | ||
} | ||
|
||
public static string ArgsToSig(IReadOnlyList<ExpressionNode> args) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
builder.Append('<'); | ||
|
||
foreach (var arg in args) | ||
{ | ||
builder.Append(','); | ||
} | ||
|
||
builder.Append('>'); | ||
return builder.ToString(); //TODO: improve | ||
} | ||
} |
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,19 @@ | ||
namespace Moth.LLVM.Data; | ||
|
||
public class TemplateParameter : CompilerData | ||
{ | ||
public string Name { get; } | ||
public TemplateParameterBound[] Bounds { get; } | ||
public Type TypeOfConst { get => IsConst ? _typeOfConst : null; } | ||
public bool IsConst { get; } | ||
|
||
private Type _typeOfConst; | ||
|
||
public TemplateParameter(string name, TemplateParameterBound[] bounds, bool isConst, Type typeOfConst = null) | ||
Check warning on line 12 in Moth/LLVM/Data/TemplateParameter.cs GitHub Actions / build-windows-latest
|
||
{ | ||
Name = name; | ||
Bounds = bounds; | ||
IsConst = isConst; | ||
_typeOfConst = typeOfConst; | ||
} | ||
} |
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,6 @@ | ||
namespace Moth.LLVM.Data; | ||
|
||
public class TemplateParameterBound : CompilerData | ||
{ | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.