Remove need for semicolon after return

This commit is contained in:
nub31
2025-07-05 14:54:48 +02:00
parent 009e091e19
commit c58cb45f2a
4 changed files with 5 additions and 4 deletions

View File

@@ -106,7 +106,6 @@ public static class ConsoleColors
case Symbol.Caret:
case Symbol.Ampersand:
return White;
case Symbol.Semicolon:
case Symbol.Colon:
case Symbol.Comma:
case Symbol.Period:

View File

@@ -10,6 +10,7 @@ namespace Syntax.Parsing;
public static class Parser
{
private static string _namespace = null!;
private static NubType? _functionReturnType;
private static List<Diagnostic> _diagnostics = [];
private static IEnumerable<Token> _tokens = [];
private static int _index;
@@ -20,6 +21,7 @@ public static class Parser
_namespace = "global";
_diagnostics = [];
_index = 0;
_functionReturnType = null;
if (TryExpectSymbol(Symbol.Namespace))
{
@@ -105,6 +107,7 @@ public static class Parser
}
var returnType = TryExpectSymbol(Symbol.Colon) ? ParseType() : new NubVoidType();
_functionReturnType = returnType;
var isExtern = modifiers.RemoveAll(x => x.Modifier == Modifier.Extern) > 0;
if (isExtern)
@@ -371,8 +374,9 @@ public static class Parser
private static ReturnNode ParseReturn(int startIndex)
{
ExpectSymbol(Symbol.Return);
var value = Optional<ExpressionNode>.Empty();
if (!TryExpectSymbol(Symbol.Semicolon))
if (_functionReturnType is not NubVoidType)
{
value = ParseExpression();
}

View File

@@ -14,7 +14,6 @@ public enum Symbol
While,
Break,
Continue,
Semicolon,
Colon,
OpenParen,
CloseParen,

View File

@@ -41,7 +41,6 @@ public static class Tokenizer
private static readonly Dictionary<char, Symbol> Chars = new()
{
[';'] = Symbol.Semicolon,
[':'] = Symbol.Colon,
['('] = Symbol.OpenParen,
[')'] = Symbol.CloseParen,