diff --git a/NewSource/Socordia.CodeAnalysis/AST/Declarations/ClassDeclaration.cs b/NewSource/Socordia.CodeAnalysis/AST/Declarations/ClassDeclaration.cs index 5bc6f99d..c09b4916 100644 --- a/NewSource/Socordia.CodeAnalysis/AST/Declarations/ClassDeclaration.cs +++ b/NewSource/Socordia.CodeAnalysis/AST/Declarations/ClassDeclaration.cs @@ -4,13 +4,17 @@ namespace Socordia.CodeAnalysis.AST.Declarations; public class ClassDeclaration : Declaration { - public ClassDeclaration(string name, List inheritances, List members) + public ClassDeclaration(string name, TypeName? baseType, List inheritances, List members) { Properties.Set(nameof(Name), name); - Properties.Set(nameof(Inheritances), inheritances); + Properties.Set(nameof(Implementations), inheritances); + Properties.Set(nameof(BaseType), baseType); Children.Add(members); } public string Name => Properties.GetOrThrow(nameof(Name)); - public List Inheritances => Properties.GetOrThrow>(nameof(Inheritances)); + + public TypeName? BaseType => Properties.GetOrThrow(nameof(BaseType))!; + + public List Implementations => Properties.GetOrThrow>(nameof(Implementations)); } \ No newline at end of file diff --git a/NewSource/Socordia.CodeAnalysis/AST/Declarations/InterfaceDeclaration.cs b/NewSource/Socordia.CodeAnalysis/AST/Declarations/InterfaceDeclaration.cs index 73b3dbb6..a738d4e3 100644 --- a/NewSource/Socordia.CodeAnalysis/AST/Declarations/InterfaceDeclaration.cs +++ b/NewSource/Socordia.CodeAnalysis/AST/Declarations/InterfaceDeclaration.cs @@ -3,4 +3,4 @@ namespace Socordia.CodeAnalysis.AST.Declarations; public class InterfaceDeclaration(string name, List inheritances, List members) - : ClassDeclaration(name, inheritances, members); \ No newline at end of file + : ClassDeclaration(name, null, inheritances, members); \ No newline at end of file diff --git a/NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/ClassDeclarationParser.cs b/NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/ClassDeclarationParser.cs index 73133c3a..043d9497 100644 --- a/NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/ClassDeclarationParser.cs +++ b/NewSource/Socordia.CodeAnalysis/Parsing/ParsePoints/Declarations/ClassDeclarationParser.cs @@ -7,17 +7,30 @@ namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations; public sealed class ClassDeclarationParser : IParsePoint { - public static AstNode Parse(TokenIterator iterator, Parser parser) { var keywordToken = iterator.Prev; var nameToken = iterator.Match(TokenType.Identifier); var inheritances = new List(); + TypeName? baseType = null; + + var implementsParsed = false; + var extendsParsed = false; - if (iterator.ConsumeIfMatch(TokenType.Colon)) + while (iterator.ConsumeIfMatch(TokenType.Implements) || iterator.ConsumeIfMatch(TokenType.Extends)) { - inheritances = ParsingHelpers.ParseSeperated(parser, TokenType.OpenCurly, TypeNameParser.Parse, TokenType.Comma,false); + if (iterator.Current.Type == TokenType.Implements && !implementsParsed) + { + inheritances = ParsingHelpers.ParseSeperated(parser, TokenType.OpenCurly, TypeNameParser.Parse, + TokenType.Comma, false); + implementsParsed = true; + } + else if (iterator.Current.Type == TokenType.Extends && !extendsParsed) + { + baseType = TypeNameParser.Parse(parser); + extendsParsed = true; + } } iterator.Match(TokenType.OpenCurly); @@ -26,6 +39,6 @@ public static AstNode Parse(TokenIterator iterator, Parser parser) iterator.Match(TokenType.CloseCurly); //remove if member parsing works - return new ClassDeclaration(nameToken.Text, inheritances, members); + return new ClassDeclaration(nameToken.Text, baseType, inheritances, members); } } \ No newline at end of file diff --git a/NewSource/Socordia.CodeAnalysis/Parsing/TokenType.cs b/NewSource/Socordia.CodeAnalysis/Parsing/TokenType.cs index 4c9fcae7..7830cc9f 100644 --- a/NewSource/Socordia.CodeAnalysis/Parsing/TokenType.cs +++ b/NewSource/Socordia.CodeAnalysis/Parsing/TokenType.cs @@ -159,6 +159,10 @@ public enum TokenType [Keyword("mut")] Mutable, + [Keyword("extends")] Extends, + + [Keyword("implements")] Implements, + [Keyword("enum")] Enum, [Keyword("try")] Try, diff --git a/NewSource/SocordiaC/Compilation/CollectClassesListener.cs b/NewSource/SocordiaC/Compilation/CollectClassesListener.cs index 9f744727..f6672473 100644 --- a/NewSource/SocordiaC/Compilation/CollectClassesListener.cs +++ b/NewSource/SocordiaC/Compilation/CollectClassesListener.cs @@ -14,16 +14,29 @@ protected override void ListenToNode(Driver context, ClassDeclaration node) var type = context.Compilation.Module.CreateType(ns, node.Name, GetModifiers(node), GetBaseType(node, context.Compilation)); + foreach (var baseType in node.Implementations) + { + var t = Utils.GetTypeFromNode(baseType, context.Compilation.Module); + + if (t.IsInterface) + { + type.Interfaces.Add(t); + } + else + { + baseType.AddError(baseType + " is not an interface"); + } + } } private TypeDefOrSpec? GetBaseType(ClassDeclaration node, DistIL.Compilation compilation) { - if (node.Inheritances.Count == 0) + if (node.BaseType == null) { return compilation.Module.Resolver.Import(typeof(object)); } - return Utils.GetTypeFromNode(node.Inheritances[0], compilation.Module); + return Utils.GetTypeFromNode(node.BaseType, compilation.Module); } private TypeAttributes GetModifiers(Declaration node) diff --git a/NewSource/SocordiaC/Compilation/CollectInterfacesListener.cs b/NewSource/SocordiaC/Compilation/CollectInterfacesListener.cs index a1565331..faf1932f 100644 --- a/NewSource/SocordiaC/Compilation/CollectInterfacesListener.cs +++ b/NewSource/SocordiaC/Compilation/CollectInterfacesListener.cs @@ -18,12 +18,12 @@ protected override void ListenToNode(Driver context, InterfaceDeclaration node) private TypeDefOrSpec? GetBaseType(ClassDeclaration node, DistIL.Compilation compilation) { - if (node.Inheritances.Count == 0) + if (node.Implementations.Count == 0) { return compilation.Module.Resolver.Import(typeof(object)); } - return Utils.GetTypeFromNode(node.Inheritances[0], compilation.Module); + return Utils.GetTypeFromNode(node.Implementations[0], compilation.Module); } private TypeAttributes GetModifiers(Declaration node) diff --git a/NewSource/SocordiaC/Stages/ConvertToIrStage.cs b/NewSource/SocordiaC/Stages/ConvertToIrStage.cs index 27b133f4..0102ca21 100644 --- a/NewSource/SocordiaC/Stages/ConvertToIrStage.cs +++ b/NewSource/SocordiaC/Stages/ConvertToIrStage.cs @@ -10,8 +10,7 @@ public class ConvertToIrStage : IHandler { public async Task HandleAsync(Driver context, Func> next) { - var pipeline = CompositeListener.Build() - .With(new CollectFunctionsListener()) + var collectTypesPipeline = CompositeListener.Build() .With(new CollectClassesListener()) .With(new CollectEnumListener()) .With(new CollectUnitsListener()) @@ -23,10 +22,20 @@ public async Task HandleAsync(Driver context, Func> { foreach (var decl in tree.Declarations.Children) { - pipeline.Listen(context, decl); + collectTypesPipeline.Listen(context, decl); } } + var functionCollector = new CollectFunctionsListener(); + foreach (var tree in context.Trees) + { + foreach (var decl in tree.Declarations.Children) + { + functionCollector.Listen(context, decl); + } + } + + return await next.Invoke(context); } } \ No newline at end of file diff --git a/NewSource/SocordiaC/compilation.sc b/NewSource/SocordiaC/compilation.sc index 79d0731f..0a9e802a 100644 --- a/NewSource/SocordiaC/compilation.sc +++ b/NewSource/SocordiaC/compilation.sc @@ -24,6 +24,7 @@ class MyClass } + internal class Hello : System.Text.StringBuilder, ILixou {