Skip to content

Commit

Permalink
feat: Add expression body function syntax and emit return statement
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Dec 26, 2024
1 parent d337b34 commit 8b50f20
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public ReturnStatement(AstNode expression)
Children.Add(expression);
}

public AstNode Expression => Children.First;
public AstNode Value => Children.First;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.AST.Statements;

namespace Socordia.CodeAnalysis.Parsing.ParsePoints.Declarations;

Expand All @@ -10,6 +11,21 @@ public static AstNode Parse(TokenIterator iterator, Parser parser)
var keywordToken = iterator.Prev;
var signature = SignatureParser.Parse(parser);

return new FunctionDefinition(signature, Statements.Statement.ParseBlock(parser));
Block body = null;
if (iterator.IsMatch(TokenType.OpenCurly))
{
body = Statements.Statement.ParseBlock(parser);
}
else if (iterator.ConsumeIfMatch(TokenType.Arrow))
{
body = new Block([new ReturnStatement(Expression.Parse(parser))]);
iterator.Match(TokenType.Semicolon);
}
else
{
signature.AddError("Function body is missing");
}

return new FunctionDefinition(signature, body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public static Signature Parse(Parser parser)
//generics.Add(LNode.Call(Symbols.Where, LNode.List(genericName, LNode.Call(CodeSymbols.Base, bases))));
}

if (iterator.IsMatch(TokenType.Arrow))
if (iterator.IsMatch(TokenType.Colon))
{
iterator.NextToken();

returnType = TypeNameParser.Parse(parser);
}

return SyntaxTree.Signature(name, returnType, parameters, generics);
return new Signature(name, returnType, parameters, generics);
}
}
5 changes: 0 additions & 5 deletions NewSource/Socordia.CodeAnalysis/Parsing/SyntaxTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ public static LNode NullableType(LNode type)
return Factory.Call(Symbols.NullableType, LNode.List(type));
}

public static Signature Signature(AstNode name, AstNode type, List<ParameterDeclaration> parameters, List<AstNode> generics)
{
return new Signature(name, type, parameters, generics);
}

public static AstNode SizeOf(AstNode type)
{
return new SizeOf(type);
Expand Down
1 change: 1 addition & 0 deletions NewSource/SocordiaC/Compilation/Body/BodyCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public record BodyCompilation(Driver Driver, MethodDef Method, IRBuilder Builder
CompositeListener<BodyCompilation, AstNode>.Build()
.With(new VariableDeclarationListener())
.With(new CallExpressionListener())
.With(new ReturnStatementListener())
.ToListener();
}
15 changes: 15 additions & 0 deletions NewSource/SocordiaC/Compilation/Body/ReturnStatementListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using DistIL.IR;
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Statements;

namespace SocordiaC.Compilation.Body;

public class ReturnStatementListener : Listener<BodyCompilation, AstNode, ReturnStatement>
{
protected override void ListenToNode(BodyCompilation context, ReturnStatement node)
{
var value = Utils.CreateValue(node.Value);
context.Builder.Emit(new ReturnInst(value));
}
}
10 changes: 9 additions & 1 deletion NewSource/SocordiaC/Compilation/CollectFunctionsListener.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
using System.Reflection;
using DistIL.AsmIO;
using DistIL.IR;
using MrKWatkins.Ast.Listening;
using Socordia.CodeAnalysis.AST;
using Socordia.CodeAnalysis.AST.Declarations;
using Socordia.CodeAnalysis.AST.Statements;
using MethodBody = DistIL.IR.MethodBody;

namespace SocordiaC.Compilation;

public class CollectFunctionsListener() : Listener<Driver, AstNode, FunctionDefinition>
{
private TypeDesc GetReturnType(FunctionDefinition node, TypeDef type)
{
return Utils.GetTypeFromNode(node.Signature.ReturnType, type);
}

protected override void ListenToNode(Driver context, FunctionDefinition node)
{
var attrs = GetModifiers(node);
var type = context.GetFunctionType(context.GetNamespaceOf(node));
var parameters = GetParameters(node, type);

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

if (!node.Modifiers.Contains(Modifier.Extern))
{
Expand Down
13 changes: 5 additions & 8 deletions NewSource/SocordiaC/compilation.sc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TestSuite;

func main() -> none {
func main(): none {
let myFlag = true;

print("hello");
Expand All @@ -9,22 +9,19 @@ func main() -> none {
test(42, true);
}

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

}
private func test(hello: i32, flag: bool): i32 -> 42;

func complex() -> Functions
func complex(): Functions
{

}

func external() -> System.Text.StringBuilder
func external(): System.Text.StringBuilder
{

}

func internal_type() -> Hello
func internal_type(): Hello
{

}
Expand Down

0 comments on commit 8b50f20

Please sign in to comment.