Skip to content

Commit

Permalink
feat: Add struct declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 29, 2024
1 parent 25ccd9a commit 42eeee9
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 84 deletions.
45 changes: 27 additions & 18 deletions NewSource/.idea/.idea.Socordia/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Socordia.CodeAnalysis.AST.TypeNames;

namespace Socordia.CodeAnalysis.AST.Declarations;

public class StructDeclaration(
string name,
TypeName? baseType,
List<TypeName> inheritances,
List<TypeMemberDeclaration> members)
: ClassDeclaration(name, baseType, inheritances, members);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.Core;

namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;

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

var nameToken = iterator.Match(TokenType.Identifier);
var members = ParsingHelpers.ParseDeclarationMembers<TypeMemberDeclaration>(parser);

return new StructDeclaration(nameToken.Text, null, [], members);
}
}
26 changes: 13 additions & 13 deletions NewSource/Socordia.CodeAnalysis/Parsing/Parser.ParsePoints.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using DistIL.Util;
using Loyc.Syntax;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.Core;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.Parsing.ParsePoints;
using Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;
using Socordia.CodeAnalysis.Parsing.ParsePoints.Expressions;
Expand All @@ -18,24 +15,27 @@ public sealed partial class Parser

public void InitParsePoints()
{
//AddDeclarationParsePoint<BitFieldDeclaration>(TokenType.Bitfield);
AddDeclarationParsePoint<UnionDeclarationParser>(TokenType.Union);
AddDeclarationParsePoint<ImportStatementParser>(TokenType.Import);
AddDeclarationParsePoint<ModuleDeclarationParser>(TokenType.Module);
AddDeclarationParsePoint<TypeAliasDeclarationParser>(TokenType.Using);

AddDeclarationParsePoint<EnumDeclarationParser>(TokenType.Enum);
AddDeclarationParsePoint<StructDeclarationParser>(TokenType.Struct);
AddDeclarationParsePoint<ClassDeclarationParser>(TokenType.Class);
AddDeclarationParsePoint<InterfaceDeclarationParser>(TokenType.Interface);
AddDeclarationParsePoint<DelegateDeclarationParser>(TokenType.Delegate);
AddDeclarationParsePoint<UnionDeclarationParser>(TokenType.Union);
AddDeclarationParsePoint<UnitDeclarationParser>(TokenType.Unit);

AddDeclarationParsePoint<FunctionDefinitionParser>(TokenType.Function);
AddDeclarationParsePoint<EnumDeclarationParser>(TokenType.Enum);
AddDeclarationParsePoint<InterfaceDeclarationParser>(TokenType.Interface);
AddDeclarationParsePoint<RulesForDeclarationParser>(TokenType.Rules);

/* AddDeclarationParsePoint<ConstructorDeclarationParser>(TokenType.Constructor);
AddDeclarationParsePoint<DestructorDeclaration>(TokenType.Destructor);
AddDeclarationParsePoint<BitFieldDeclaration>(TokenType.Bitfield);
AddDeclarationParsePoint<DiscriminatedUnionDeclaration>(TokenType.Type);
AddDeclarationParsePoint<MacroDeclaration>(TokenType.Macro);
AddDeclarationParsePoint<ImplementationDeclaration>(TokenType.Implement);*/
AddDeclarationParsePoint<ImportStatementParser>(TokenType.Import);
// AddDeclarationParsePoint<StructDeclaration>(TokenType.Struct);
AddDeclarationParsePoint<ModuleDeclarationParser>(TokenType.Module);
AddDeclarationParsePoint<TypeAliasDeclarationParser>(TokenType.Using);
AddDeclarationParsePoint<UnitDeclarationParser>(TokenType.Unit);
// AddDeclarationParsePoint<MacroBlockDeclaration>(TokenType.Identifier);

AddExpressionParsePoint<IdentifierParser>(TokenType.Identifier);
Expand Down
26 changes: 1 addition & 25 deletions NewSource/SocordiaC/Compilation/CollectClassesListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Reflection;
using DistIL.AsmIO;
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
Expand All @@ -12,7 +11,7 @@ protected override void ListenToNode(Driver context, ClassDeclaration node)
{
var ns = context.GetNamespaceOf(node);
var type = context.Compilation.Module.CreateType(ns, node.Name,
GetModifiers(node), (TypeDefOrSpec)GetBaseType(node, context.Compilation));
Utils.GetTypeModifiers(node), (TypeDefOrSpec)GetBaseType(node, context.Compilation));

Mappings.Types[node] = type;
}
Expand All @@ -27,28 +26,5 @@ protected override void ListenToNode(Driver context, ClassDeclaration node)
return Utils.GetTypeFromNode(node.BaseType, compilation.Module);
}

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;
}

protected override bool ShouldListenToChildren(Driver context, AstNode node) => true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected override void ListenToNode(Driver context, InterfaceDeclaration node)
{
var ns = context.GetNamespaceOf(node);
var type = context.Compilation.Module.CreateType(ns, node.Name,
Utils.GetModifiers(node) | TypeAttributes.Interface | TypeAttributes.Abstract,
Utils.GetTypeModifiers(node) | TypeAttributes.Interface | TypeAttributes.Abstract,
context.Compilation.Resolver.SysTypes.Object);
}

Expand Down
19 changes: 19 additions & 0 deletions NewSource/SocordiaC/Compilation/CollectStructsListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Reflection;
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;

namespace SocordiaC.Compilation;

public class CollectStructsListener : Listener<Driver, AstNode, StructDeclaration>
{
protected override void ListenToNode(Driver context, StructDeclaration node)
{
var ns = context.GetNamespaceOf(node);
var type = context.Compilation.Module.CreateType(ns, node.Name,
Utils.GetTypeModifiers(node) | TypeAttributes.Sealed | TypeAttributes.SequentialLayout,
context.Compilation.Resolver.SysTypes.ValueType);
}

protected override bool ShouldListenToChildren(Driver context, AstNode node) => false;
}
4 changes: 2 additions & 2 deletions NewSource/SocordiaC/Compilation/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public static partial class Utils
throw new Exception("cannot get type from node");
}

public static TypeAttributes GetModifiers(Declaration node)
public static TypeAttributes GetTypeModifiers(Declaration node)
{
var attrs = TypeAttributes.Public;
var attrs = TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

foreach (var modifier in node.Modifiers)
{
Expand Down
1 change: 1 addition & 0 deletions NewSource/SocordiaC/Stages/CollectTypesStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
{
var collectTypesPipeline = CompositeListener<Driver, AstNode>.Build()
.With(new CollectClassesListener())
.With(new CollectStructsListener())
.With(new CollectEnumListener())
.With(new CollectUnitsListener())
.With(new CollectUnionsListener())
Expand Down
5 changes: 5 additions & 0 deletions NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ rules for unit seconds
{
// from seconds to minutes = seconds / 60;
// from minutes to seconds = derive from seconds; // automaticly derive conversion rule. In this case the opposite of seconds: minutes * 60
}

struct MyValue
{

}

0 comments on commit 42eeee9

Please sign in to comment.