Skip to content

Commit

Permalink
feat: Add out parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Feb 7, 2025
1 parent fd3a453 commit c4b39a6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace Socordia.CodeAnalysis.AST.Declarations;
public class ParameterDeclaration : Declaration
{
public ParameterDeclaration(AstNode type, string name, AstNode? defaultValue, bool assertNotNull,
List<Annotation> annotations)
List<Annotation> annotations, bool isOut)
{
Properties.Set(nameof(Name), name);
Properties.Set(nameof(DefaultValue), defaultValue);
Properties.Set(nameof(AssertNotNull), assertNotNull);
Properties.Set(nameof(Annotations), annotations);
Properties.Set(nameof(IsOut), isOut);

Children.Add(type);
}
Expand All @@ -19,5 +20,7 @@ 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 bool IsOut => Properties.GetOrDefault<bool>(nameof(IsOut));

public TypeName Type => (TypeName)Children[0];
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static AstNode Parse(TokenIterator iterator, Parser parser)
{
_ = AnnotationParser.TryParse(parser, out var annotations);

var keywordToken = iterator.Current;
bool isOut = parser.ConsumeIfMatch(TokenType.Out);

var name = iterator.Match(TokenType.Identifier);

var assertNotNull = iterator.ConsumeIfMatch(TokenType.Exclamation);
Expand Down
1 change: 1 addition & 0 deletions NewSource/Socordia.CodeAnalysis/Parsing/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public enum TokenType
[Keyword("do")] Do,

[Keyword("in")] In,
[Keyword("out")] Out,

[Keyword("for")] For,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ public class ParameterScopeItem : ScopeItem

public override TypeDesc Type => Arg.ResultType;

public required bool IsOut { get; init; }

public void Deconstruct(out string name, out Argument parameter, out TypeDesc type)

public void Deconstruct(out string name, out Argument parameter, out TypeDesc type, out bool isOut)
{
name = Name;
parameter = Arg;
type = Type;
isOut = IsOut;
}
}
2 changes: 1 addition & 1 deletion NewSource/SocordiaC/Stages/CompileFunctionsStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<Driver> HandleAsync(Driver context, Func<Driver, Task<Driver>>
var scope = new Scope(null!);
foreach (var arg in def.Body!.Args)
{
scope.Add(new ParameterScopeItem { Name = arg.Name, Arg = arg });
scope.Add(new ParameterScopeItem { Name = arg.Name, Arg = arg, IsOut = def.IsOut });
}

var builder = new IRBuilder(def.Body!.CreateBlock());
Expand Down
5 changes: 5 additions & 0 deletions NewSource/SocordiaC/hello.scs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ func main()

println(x);
println(y);
}

func outTest(out k: i32)
{
k = 5;
}

0 comments on commit c4b39a6

Please sign in to comment.