Skip to content

Commit

Permalink
feat: Add parameter declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 23, 2024
1 parent 0f4982c commit 4590911
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.AST.TypeNames;

namespace Socordia.CodeAnalysis.AST.Declarations;

public class ParameterDeclaration : Declaration
{
Expand All @@ -16,5 +18,5 @@ public ParameterDeclaration(AstNode type, string name, AstNode? defaultValue, bo
public AstNode? DefaultValue => Properties.GetOrDefault<AstNode?>(nameof(DefaultValue));
public bool AssertNotNull => Properties.GetOrDefault<bool>(nameof(AssertNotNull));

public AstNode Type => Children[0];
public TypeName Type =>(TypeName) Children[0];
}
7 changes: 4 additions & 3 deletions NewSource/SocordiaC/Compilation/CollectClassesListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

namespace SocordiaC.Compilation;

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

}

private TypeDefOrSpec? GetBaseType(ClassDeclaration node, DistIL.Compilation compilation)
private TypeDefOrSpec? GetBaseType(TNode node, DistIL.Compilation compilation)
{
if (node.Inheritances.Count == 0)
{
Expand Down
17 changes: 16 additions & 1 deletion NewSource/SocordiaC/Compilation/CollectFunctionsListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public class CollectFunctionsListener(TypeDef type) : Listener<Driver, AstNode,
protected override void ListenToNode(Driver context, FunctionDefinition node)
{
var attrs = GetModifiers(node);
var parameters = GetParameters(node);

var method = type.CreateMethod(node.Signature.Name.Name,
Utils.GetTypeFromNode(node.Signature.ReturnType, type), [], attrs);
Utils.GetTypeFromNode(node.Signature.ReturnType, type), [..parameters], attrs);

if (!node.Modifiers.Contains(Modifier.Extern))
{
Expand All @@ -30,6 +31,20 @@ protected override void ListenToNode(Driver context, FunctionDefinition node)
}
}

private IEnumerable<ParamDef> GetParameters(FunctionDefinition node)
{
var result = new List<ParamDef>();

foreach (var param in node.Signature.Parameters)
{
var paramDef = new ParamDef(new TypeSig(Utils.GetTypeFromNode(param.Type, type)), param.Name);
//paramDef.DefaultValue = param.DefaultValue; //ToDo: convert default value
result.Add(paramDef);
}

return result;
}

private MethodAttributes GetModifiers(Declaration node)
{
var attrs = (MethodAttributes)0;
Expand Down
4 changes: 2 additions & 2 deletions NewSource/SocordiaC/Stages/ConvertToIrStage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Flo;
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;
using SocordiaC.Compilation;

namespace SocordiaC.Stages;
Expand All @@ -10,9 +11,8 @@ 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 BlockListener())
.With(new CollectFunctionsListener(context.FunctionsType))
.With(new CollectClassesListener())
.With(new CollectClassesListener<ClassDeclaration>())
.ToListener();

foreach (var tree in context.Trees)
Expand Down
2 changes: 1 addition & 1 deletion NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ func main() -> none {

}

private func test() -> i32
private func test(hello: i32, flag: bool) -> i32
{

}
Expand Down

0 comments on commit 4590911

Please sign in to comment.