Skip to content

Commit

Permalink
feat: Add extends && implements keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 26, 2024
1 parent 6b00fa9 commit 43f86e1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ namespace Socordia.CodeAnalysis.AST.Declarations;

public class ClassDeclaration : Declaration
{
public ClassDeclaration(string name, List<TypeName> inheritances, List<AstNode> members)
public ClassDeclaration(string name, TypeName? baseType, List<TypeName> inheritances, List<AstNode> 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<string>(nameof(Name));
public List<TypeName> Inheritances => Properties.GetOrThrow<List<TypeName>>(nameof(Inheritances));

public TypeName? BaseType => Properties.GetOrThrow<TypeName>(nameof(BaseType))!;

public List<TypeName> Implementations => Properties.GetOrThrow<List<TypeName>>(nameof(Implementations));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
namespace Socordia.CodeAnalysis.AST.Declarations;

public class InterfaceDeclaration(string name, List<TypeName> inheritances, List<AstNode> members)
: ClassDeclaration(name, inheritances, members);
: ClassDeclaration(name, null, inheritances, members);
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
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);
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions NewSource/Socordia.CodeAnalysis/Parsing/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public enum TokenType

[Keyword("mut")] Mutable,

[Keyword("extends")] Extends,

[Keyword("implements")] Implements,

[Keyword("enum")] Enum,

[Keyword("try")] Try,
Expand Down
17 changes: 15 additions & 2 deletions NewSource/SocordiaC/Compilation/CollectClassesListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions NewSource/SocordiaC/Compilation/CollectInterfacesListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions NewSource/SocordiaC/Stages/ConvertToIrStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class ConvertToIrStage : IHandler<Driver, Driver>
{
public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>> next)
{
var pipeline = CompositeListener<Driver, AstNode>.Build()
.With(new CollectFunctionsListener())
var collectTypesPipeline = CompositeListener<Driver, AstNode>.Build()
.With(new CollectClassesListener())
.With(new CollectEnumListener())
.With(new CollectUnitsListener())
Expand All @@ -23,10 +22,20 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
{
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);
}
}
1 change: 1 addition & 0 deletions NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MyClass

}


internal class Hello : System.Text.StringBuilder, ILixou
{

Expand Down

0 comments on commit 43f86e1

Please sign in to comment.