Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Something is very wrong here...
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarWitch7 committed Mar 17, 2024
1 parent 3a23dc5 commit fc68536
Show file tree
Hide file tree
Showing 24 changed files with 374 additions and 215 deletions.
2 changes: 1 addition & 1 deletion Moth.CLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"MothCLI": {
"commandName": "Project",
"commandLineArgs": "-v --no-advanced-ir-opt --debug-test -t exe -o test -i /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Program.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Foreign.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/System.moth",
"commandLineArgs": "-v -n --no-advanced-ir-opt --debug-test -t exe -o test -i /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Program.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/Foreign.moth /home/aurora/RiderProjects/moth-lang/TestScripts/moth/System.moth",
"workingDirectory": "/home/aurora/RiderProjects/moth-lang/run",
"executablePath": "/home/aurora/RiderProjects/moth-lang/Moth.CLI/bin/Debug/net7.0/MothCLI.exe",
"externalTerminal": true,
Expand Down
52 changes: 35 additions & 17 deletions Moth/AST/ASTGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,26 +571,47 @@ public static List<ExpressionNode> ProcessArgs(ParseContext context, TokenType t

public static TypeRefNode ProcessTypeRef(ParseContext context)
{
if (context.Current?.Type is TokenType.TypeRef
or TokenType.GenericTypeRef) //TODO: handle this patheticness
if (context.Current?.Type == TokenType.TemplateTypeRef)
{
TokenType? startToken = context.Current?.Type;
if (context.MoveNext()?.Type != TokenType.Name)
{
throw new UnexpectedTokenException(context.Current.Value, TokenType.Name);
}

string retTypeName = context.Current.Value.Text.ToString();
uint pointerDepth = 0;

context.MoveNext();

while (context.Current?.Type == TokenType.Asterix)
{
pointerDepth++;
context.MoveNext();
}

return new LocalTypeRefNode(retTypeName, pointerDepth);
}
else
{
if (context.Current?.Type != TokenType.TypeRef)
{
throw new UnexpectedTokenException(context.Current.Value, TokenType.TypeRef);
}

if (context.MoveNext()?.Type == TokenType.Name)
{
string retTypeName = context.Current.Value.Text.ToString();
var genericParams = new List<ExpressionNode>();
uint pointerDepth = 0;

if (context.MoveNext()?.Type == TokenType.OpeningGenericBracket
&& startToken != TokenType.GenericTypeRef)
if (context.MoveNext()?.Type == TokenType.OpeningGenericBracket)
{
context.MoveNext();

while (context.Current?.Type != TokenType.ClosingGenericBracket)
{
if (context.Current?.Type is TokenType.TypeRef
or TokenType.GenericTypeRef)
or TokenType.TemplateTypeRef)
{
genericParams.Add(ProcessTypeRef(context));
}
Expand All @@ -608,6 +629,8 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value);
}
}

context.MoveNext();
}

while (context.Current?.Type == TokenType.Asterix)
Expand All @@ -617,16 +640,16 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
}

return genericParams.Count != 0
? new GenericTypeRefNode(retTypeName, genericParams, pointerDepth)
? new TemplateTypeRefNode(retTypeName, genericParams, pointerDepth)
: new TypeRefNode(retTypeName, pointerDepth);
}
else if (startToken != TokenType.GenericTypeRef && context.Current?.Type == TokenType.OpeningParentheses)
else if (context.Current?.Type == TokenType.OpeningParentheses)
{
var @params = new List<TypeRefNode>();
uint pointerDepth = 0;
TypeRefNode retType;

while (context.MoveNext()?.Type is TokenType.TypeRef or TokenType.GenericTypeRef)
while (context.MoveNext()?.Type is TokenType.TypeRef or TokenType.TemplateTypeRef)
{
@params.Add(ProcessTypeRef(context));

Expand Down Expand Up @@ -655,11 +678,11 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value, TokenType.TypeRef);
}
}
else if (startToken != TokenType.GenericTypeRef && context.Current?.Type == TokenType.OpeningSquareBrackets)
else if (context.Current?.Type == TokenType.OpeningSquareBrackets)
{
uint pointerDepth = 0;

if (!(context.MoveNext()?.Type is TokenType.TypeRef or TokenType.GenericTypeRef))
if (!(context.MoveNext()?.Type is TokenType.TypeRef or TokenType.TemplateTypeRef))
{
throw new UnexpectedTokenException(context.Current.Value);
}
Expand All @@ -686,10 +709,6 @@ public static TypeRefNode ProcessTypeRef(ParseContext context)
throw new UnexpectedTokenException(context.Current.Value, TokenType.Name);
}
}
else
{
throw new UnexpectedTokenException(context.Current.Value, TokenType.TypeRef);
}
}

public static ExpressionNode ProcessExpression(ParseContext context, bool nullAllowed = false)
Expand Down Expand Up @@ -986,8 +1005,7 @@ public static ExpressionNode ProcessExpression(ParseContext context, bool nullAl
case TokenType.Decrement:
stack.Push(ProcessIncrementDecrement(context));
break;
case TokenType.GenericTypeRef:
throw new NotImplementedException();
case TokenType.TemplateTypeRef:
case TokenType.TypeRef:
stack.Push(ProcessTypeRef(context));
break;
Expand Down
8 changes: 0 additions & 8 deletions Moth/AST/Node/GenericTypeRefNode.cs

This file was deleted.

6 changes: 6 additions & 0 deletions Moth/AST/Node/LocalTypeRefNode.cs
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) { }
}
8 changes: 8 additions & 0 deletions Moth/AST/Node/TemplateTypeRefNode.cs
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;
}
2 changes: 1 addition & 1 deletion Moth/LLVM/CompilerData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Moth.LLVM;

public class CompilerData
public abstract class CompilerData
{
}
9 changes: 0 additions & 9 deletions Moth/LLVM/Data/Class.cs

This file was deleted.

13 changes: 0 additions & 13 deletions Moth/LLVM/Data/GenericClass.cs

This file was deleted.

37 changes: 35 additions & 2 deletions Moth/LLVM/Data/Namespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class Namespace : CompilerData, IContainer
public Dictionary<Signature, Function> Functions { get; } = new Dictionary<Signature, Function>();
public Dictionary<string, Struct> Structs { get; } = new Dictionary<string, Struct>();
public Dictionary<string, IGlobal> GlobalVariables { get; } = new Dictionary<string, IGlobal>();
public Dictionary<string, GenericClassNode> GenericClassTemplates { get; } = new Dictionary<string, GenericClassNode>();
public GenericDictionary GenericClasses { get; } = new GenericDictionary();
public Dictionary<string, Template> Templates { get; } = new Dictionary<string, Template>();

public Namespace(Namespace? parent, string name)
{
Expand Down Expand Up @@ -144,6 +143,40 @@ public bool TryGetStruct(string name, out Struct @struct)
}
}

public Template GetTemplate(string name)
{
if (Templates.TryGetValue(name, out Template template))
{
return template;
}
else
{
return ParentNamespace != null
? ParentNamespace.GetTemplate(name)
: throw new Exception($"Template \"{name}\" was not found in namespace \"{Name}\"");
}
}

public bool TryGetTemplate(string name, out Template template)
{
try
{
template = GetTemplate(name);

if (template == null)
{
throw new Exception();
}

return true;
}
catch
{
template = null;
return false;
}
}

public IGlobal GetGlobal(string name)
{
if (GlobalVariables.TryGetValue(name, out IGlobal global))
Expand Down
91 changes: 91 additions & 0 deletions Moth/LLVM/Data/Template.cs
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
}
}
19 changes: 19 additions & 0 deletions Moth/LLVM/Data/TemplateParameter.cs
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

View workflow job for this annotation

GitHub Actions / build-windows-latest

Cannot convert null literal to non-nullable reference type.

Check warning on line 12 in Moth/LLVM/Data/TemplateParameter.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest

Cannot convert null literal to non-nullable reference type.
{
Name = name;
Bounds = bounds;
IsConst = isConst;
_typeOfConst = typeOfConst;
}
}
6 changes: 6 additions & 0 deletions Moth/LLVM/Data/TemplateParameterBound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Moth.LLVM.Data;

public class TemplateParameterBound : CompilerData
{

}
15 changes: 0 additions & 15 deletions Moth/LLVM/Data/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,4 @@ public static Pointer CreatePtrToTemp(LLVMCompiler compiler, Value temporary)
compiler.Builder.BuildStore(temporary.LLVMValue, tempPtr);
return new Pointer(new PtrType(temporary.Type), tempPtr);
}
}

public class ClassValue : Value //TODO: does this need to exist
{
public ClassValue(Class type, LLVMValueRef value) : base(type, value) { }
}

public class FieldValue : Value //TODO: owo, what's this?
{
public FieldValue(LLVMCompiler compiler, Field field, ClassValue owner)
: base(field.Type,
compiler.Builder.BuildStructGEP2(owner.Type.LLVMType,
owner.LLVMValue,
field.FieldIndex,
field.Name)) { }
}
18 changes: 0 additions & 18 deletions Moth/LLVM/GenericDictionary.cs

This file was deleted.

Loading

0 comments on commit fc68536

Please sign in to comment.