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.Caret:
case Symbol.Ampersand: case Symbol.Ampersand:
return White; return White;
case Symbol.Semicolon:
case Symbol.Colon: case Symbol.Colon:
case Symbol.Comma: case Symbol.Comma:
case Symbol.Period: case Symbol.Period:

View File

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

View File

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

View File

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