Skip to content

Commit

Permalink
feat: Add Enum declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 23, 2024
1 parent eff5aa5 commit 900f2a4
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ public ClassDeclaration(string name, List<TypeName> inheritances, List<AstNode>

public string Name => Properties.GetOrThrow<string>(nameof(Name));
public List<TypeName> Inheritances => Properties.GetOrThrow<List<TypeName>>(nameof(Inheritances));
}

public class EnumDeclaration : Declaration
{
public EnumDeclaration(string name, TypeName baseType, List<AstNode> members)
{
Properties.Set(nameof(Name), name);
Properties.Set(nameof(BaseType), baseType);
Children.Add(members);
}

public string Name => Properties.GetOrThrow<string>(nameof(Name));
public TypeName BaseType => Properties.GetOrThrow<TypeName>(nameof(BaseType));
}
3 changes: 2 additions & 1 deletion NewSource/Socordia.CodeAnalysis/AST/Signature.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.AST.TypeNames;

namespace Socordia.CodeAnalysis.AST;

Expand All @@ -12,6 +13,6 @@ public Signature(AstNode name, AstNode? returnType, List<ParameterDeclaration> p
}

public Identifier Name => (Identifier)Children[0];
public AstNode? ReturnType => Children[1];
public TypeName? ReturnType => (TypeName)Children[1];
public IEnumerable<ParameterDeclaration> Parameters => Children.OfType<ParameterDeclaration>();
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.AST.TypeNames;

/*
public sealed class EnumDeclaration : IParsePoint
namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;

public sealed class EnumDeclarationParser : IParsePoint
{
public static LNode Parse(TokenIterator iterator, Parser parser)
public static AstNode Parse(TokenIterator iterator, Parser parser)
{
var keywordToken = iterator.Prev;

var nameToken = iterator.Match(TokenType.Identifier);
TypeName baseType = new SimpleTypeName("i32");
if (iterator.ConsumeIfMatch(TokenType.Colon))
{
baseType = TypeNameParser.Parse(parser);
}

iterator.Match(TokenType.OpenCurly);

var members = ParsingHelpers.ParseSeperated<EnumMemberDeclaration>(parser, TokenType.CloseCurly);
List<AstNode> members = []; //ParsingHelpers.ParseUntil<TypeMemberDeclaration>(parser, TokenType.CloseCurly);

iterator.Match(TokenType.CloseCurly); //remove if member parsing works

return SyntaxTree.Enum(LNode.Id(nameToken.Text), members).WithRange(keywordToken, iterator.Prev);
return new EnumDeclaration(nameToken.Text, baseType, members);
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public void InitParsePoints()
//AddDeclarationParsePoint<UnionDeclaration>(TokenType.Union);
AddDeclarationParsePoint<ClassDeclarationParser>(TokenType.Class);
AddDeclarationParsePoint<FunctionDefinitionParser>(TokenType.Function);
AddDeclarationParsePoint<EnumDeclarationParser>(TokenType.Enum);
/* AddDeclarationParsePoint<ConstructorDeclarationParser>(TokenType.Constructor);
AddDeclarationParsePoint<DestructorDeclaration>(TokenType.Destructor);
AddDeclarationParsePoint<DiscriminatedUnionDeclaration>(TokenType.Type);
AddDeclarationParsePoint<EnumDeclaration>(TokenType.Enum);
AddDeclarationParsePoint<MacroDeclaration>(TokenType.Macro);
AddDeclarationParsePoint<InterfaceDeclaration>(TokenType.Interface);
AddDeclarationParsePoint<ImplementationDeclaration>(TokenType.Implement);*/
Expand Down
7 changes: 3 additions & 4 deletions NewSource/SocordiaC/Compilation/CollectClassesListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

namespace SocordiaC.Compilation;

public class CollectClassesListener<TNode> : Listener<Driver, AstNode, TNode>
where TNode : ClassDeclaration
public class CollectClassesListener : Listener<Driver, AstNode, ClassDeclaration>
{
protected override void ListenToNode(Driver context, TNode node)
protected override void ListenToNode(Driver context, ClassDeclaration node)
{
var type = context.Compilation.Module.CreateType(context.Settings.RootNamespace, node.Name,
GetModifiers(node), GetBaseType(node, context.Compilation));

}

private TypeDefOrSpec? GetBaseType(TNode node, DistIL.Compilation compilation)
private TypeDefOrSpec? GetBaseType(ClassDeclaration node, DistIL.Compilation compilation)
{
if (node.Inheritances.Count == 0)
{
Expand Down
46 changes: 46 additions & 0 deletions NewSource/SocordiaC/Compilation/CollectEnumListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Reflection;
using DistIL.AsmIO;
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;

namespace SocordiaC.Compilation;

public class CollectEnumListener : Listener<Driver, AstNode, EnumDeclaration>
{
protected override void ListenToNode(Driver context, EnumDeclaration node)
{
var type = context.Compilation.Module.CreateType(context.Settings.RootNamespace, node.Name,
GetModifiers(node), context.Compilation.Module.Resolver.Import(typeof(Enum)));

type.CreateField("value__", new TypeSig(Utils.GetTypeFromNode(node.BaseType, type)), FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName);

// .field public static literal valuetype Color R = int32(0)
//type.CreateField("R", new TypeSig(type), FieldAttributes.Public | FieldAttributes.Literal | FieldAttributes.Static | FieldAttributes.HasDefault, 42);
}

private TypeAttributes GetModifiers(Declaration node)
{
var attrs = TypeAttributes.Public;

foreach (var modifier in node.Modifiers)
{
attrs |= modifier switch
{
Modifier.Static => TypeAttributes.Sealed | TypeAttributes.Abstract,
Modifier.Internal => TypeAttributes.NotPublic,
Modifier.Public => TypeAttributes.Public,
_ => throw new NotImplementedException()
};
}

if (node.Modifiers.Contains(Modifier.Private) || node.Modifiers.Contains(Modifier.Internal))
{
attrs &= ~TypeAttributes.Public;
}

return attrs | TypeAttributes.Sealed;
}

protected override bool ShouldListenToChildren(Driver context, AstNode node) => true;
}
4 changes: 2 additions & 2 deletions NewSource/SocordiaC/Compilation/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SocordiaC.Compilation;

public static class Utils
{
public static TypeDesc? GetTypeFromNode(AstNode node, TypeDef containingType)
public static TypeDesc? GetTypeFromNode(TypeName node, TypeDef containingType)
{
if (node is SimpleTypeName id)
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public static class Utils
throw new Exception("cannot get type from node");
}

public static TypeDefOrSpec? GetTypeFromNode(AstNode node, ModuleDef module)
public static TypeDefOrSpec? GetTypeFromNode(TypeName node, ModuleDef module)
{
if (node is QualifiedTypeName qname)
{
Expand Down
3 changes: 2 additions & 1 deletion NewSource/SocordiaC/Stages/ConvertToIrStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
{
var pipeline = CompositeListener<Driver, AstNode>.Build()
.With(new CollectFunctionsListener(context.FunctionsType))
.With(new CollectClassesListener<ClassDeclaration>())
.With(new CollectClassesListener())
.With(new CollectEnumListener())
.ToListener();

foreach (var tree in context.Trees)
Expand Down
10 changes: 10 additions & 0 deletions NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ internal class Hello : System.Text.StringBuilder
static class Blub
{

}

enum Color
{

}

enum ShortColor : i8
{

}

0 comments on commit 900f2a4

Please sign in to comment.