From ae52e910698148b4e84c1ddcdd0a094c2cc2c1ed Mon Sep 17 00:00:00 2001 From: nub31 Date: Wed, 13 Aug 2025 20:02:22 +0200 Subject: [PATCH] Remove arrow functions for now --- .../Generation/QBE/QBEGenerator.Expression.cs | 8 ----- .../NubLang/Generation/QBE/QBEGenerator.cs | 10 ------ src/compiler/NubLang/Parsing/Parser.cs | 31 ------------------ .../Parsing/Syntax/ExpressionSyntax.cs | 13 -------- .../TypeChecking/Node/ExpressionNode.cs | 2 -- .../NubLang/TypeChecking/TypeChecker.cs | 32 ------------------- 6 files changed, 96 deletions(-) diff --git a/src/compiler/NubLang/Generation/QBE/QBEGenerator.Expression.cs b/src/compiler/NubLang/Generation/QBE/QBEGenerator.Expression.cs index b2caa72..e1b85a9 100644 --- a/src/compiler/NubLang/Generation/QBE/QBEGenerator.Expression.cs +++ b/src/compiler/NubLang/Generation/QBE/QBEGenerator.Expression.cs @@ -15,7 +15,6 @@ public partial class QBEGenerator StructInitializerNode structInitializer => EmitStructInitializer(structInitializer), AddressOfNode addressOf => EmitAddressOf(addressOf), DereferenceNode dereference => EmitDereference(dereference), - ArrowFuncNode arrowFunc => EmitArrowFunc(arrowFunc), BinaryExpressionNode binaryExpression => EmitBinaryExpression(binaryExpression), FuncCallNode funcCallExpression => EmitFuncCall(funcCallExpression), InterfaceFuncAccessNode interfaceFuncAccess => EmitInterfaceFuncAccess(interfaceFuncAccess), @@ -32,13 +31,6 @@ public partial class QBEGenerator }; } - private Val EmitArrowFunc(ArrowFuncNode arrowFunc) - { - var name = $"$arrow_func{++_arrowFuncIndex}"; - _arrowFunctions.Enqueue((arrowFunc, name)); - return new Val(name, arrowFunc.Type, ValKind.Direct); - } - private Val EmitArrayIndexAccess(ArrayIndexAccessNode arrayIndexAccess) { var array = EmitUnwrap(EmitExpression(arrayIndexAccess.Target)); diff --git a/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs b/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs index 7486345..58a2a41 100644 --- a/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs +++ b/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs @@ -16,11 +16,9 @@ public partial class QBEGenerator private readonly List _stringLiterals = []; private readonly Stack _breakLabels = []; private readonly Stack _continueLabels = []; - private readonly Queue<(ArrowFuncNode Func, string Name)> _arrowFunctions = []; private readonly Stack _scopes = []; private int _tmpIndex; private int _labelIndex; - private int _arrowFuncIndex; private int _cStringLiteralIndex; private int _stringLiteralIndex; private bool _codeIsReachable = true; @@ -40,11 +38,9 @@ public partial class QBEGenerator _stringLiterals.Clear(); _breakLabels.Clear(); _continueLabels.Clear(); - _arrowFunctions.Clear(); _scopes.Clear(); _tmpIndex = 0; _labelIndex = 0; - _arrowFuncIndex = 0; _cStringLiteralIndex = 0; _stringLiteralIndex = 0; _codeIsReachable = true; @@ -71,12 +67,6 @@ public partial class QBEGenerator _writer.NewLine(); } - while (_arrowFunctions.TryDequeue(out var arrowFunc)) - { - EmitFuncDefinition(arrowFunc.Name, arrowFunc.Func.Parameters, arrowFunc.Func.ReturnType, arrowFunc.Func.Body); - _writer.NewLine(); - } - foreach (var structDef in _syntaxTree.Definitions.OfType().Where(x => x.InterfaceImplementations.Count > 0)) { _writer.Write($"data {StructVtableName(structDef.Name)} = {{ "); diff --git a/src/compiler/NubLang/Parsing/Parser.cs b/src/compiler/NubLang/Parsing/Parser.cs index 2372792..c9e4863 100644 --- a/src/compiler/NubLang/Parsing/Parser.cs +++ b/src/compiler/NubLang/Parsing/Parser.cs @@ -421,7 +421,6 @@ public sealed class Parser IdentifierToken identifier => new IdentifierSyntax(GetTokens(startIndex), identifier.Value), SymbolToken symbolToken => symbolToken.Symbol switch { - Symbol.Func => ParseArrowFunction(), Symbol.OpenParen => ParseParenthesizedExpression(), Symbol.Minus => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Negate, ParsePrimaryExpression()), Symbol.Bang => new UnaryExpressionSyntax(GetTokens(startIndex), UnaryOperatorSyntax.Invert, ParsePrimaryExpression()), @@ -443,36 +442,6 @@ public sealed class Parser return ParsePostfixOperators(expr); } - private ExpressionSyntax ParseArrowFunction() - { - var startIndex = _tokenIndex; - List parameters = []; - ExpectSymbol(Symbol.OpenParen); - while (!TryExpectSymbol(Symbol.CloseParen)) - { - var parameterIndex = _tokenIndex; - var name = ExpectIdentifier(); - parameters.Add(new ArrowFuncParameterSyntax(GetTokens(parameterIndex), name.Value)); - } - - ExpectSymbol(Symbol.Arrow); - BlockSyntax body; - - if (CurrentToken is SymbolToken { Symbol: Symbol.OpenBrace }) - { - var bodyStartIndex = _tokenIndex; - var returnValue = ParseExpression(); - var arrowExpression = new ReturnSyntax(GetTokens(bodyStartIndex), returnValue); - body = new BlockSyntax(GetTokens(bodyStartIndex), [arrowExpression]); - } - else - { - body = ParseBlock(); - } - - return new ArrowFuncSyntax(GetTokens(startIndex), parameters, body); - } - private ExpressionSyntax ParseParenthesizedExpression() { var expression = ParseExpression(); diff --git a/src/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs b/src/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs index be41736..9c16e93 100644 --- a/src/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs +++ b/src/compiler/NubLang/Parsing/Syntax/ExpressionSyntax.cs @@ -81,19 +81,6 @@ public record ArrowFuncParameterSyntax(IEnumerable Tokens, string Name) : public override IEnumerable GetChildren() => []; } -public record ArrowFuncSyntax(IEnumerable Tokens, IReadOnlyList Parameters, BlockSyntax Body) : ExpressionSyntax(Tokens) -{ - public override IEnumerable GetChildren() - { - foreach (var parameter in Parameters) - { - yield return parameter; - } - - yield return Body; - } -} - public record AddressOfSyntax(IEnumerable Tokens, ExpressionSyntax Expression) : ExpressionSyntax(Tokens) { public override IEnumerable GetChildren() diff --git a/src/compiler/NubLang/TypeChecking/Node/ExpressionNode.cs b/src/compiler/NubLang/TypeChecking/Node/ExpressionNode.cs index 19becfe..43acb5f 100644 --- a/src/compiler/NubLang/TypeChecking/Node/ExpressionNode.cs +++ b/src/compiler/NubLang/TypeChecking/Node/ExpressionNode.cs @@ -40,8 +40,6 @@ public record ArrayInitializerNode(TypeNode Type, ExpressionNode Capacity, TypeN public record ArrayIndexAccessNode(TypeNode Type, ExpressionNode Target, ExpressionNode Index) : ExpressionNode(Type); -public record ArrowFuncNode(TypeNode Type, IReadOnlyList Parameters, TypeNode ReturnType, BlockNode Body) : ExpressionNode(Type); - public record AddressOfNode(TypeNode Type, ExpressionNode Expression) : ExpressionNode(Type); public record LiteralNode(TypeNode Type, string Value, LiteralKind Kind) : ExpressionNode(Type); diff --git a/src/compiler/NubLang/TypeChecking/TypeChecker.cs b/src/compiler/NubLang/TypeChecking/TypeChecker.cs index 77d0537..69c2374 100644 --- a/src/compiler/NubLang/TypeChecking/TypeChecker.cs +++ b/src/compiler/NubLang/TypeChecking/TypeChecker.cs @@ -251,7 +251,6 @@ public sealed class TypeChecker var result = node switch { AddressOfSyntax expression => CheckAddressOf(expression), - ArrowFuncSyntax expression => CheckArrowFunc(expression, expectedType), ArrayIndexAccessSyntax expression => CheckArrayIndexAccess(expression), ArrayInitializerSyntax expression => CheckArrayInitializer(expression), BinaryExpressionSyntax expression => CheckBinaryExpression(expression), @@ -293,37 +292,6 @@ public sealed class TypeChecker return new AddressOfNode(new PointerTypeNode(inner.Type), inner); } - private ArrowFuncNode CheckArrowFunc(ArrowFuncSyntax expression, TypeNode? expectedType = null) - { - if (expectedType == null) - { - throw new TypeCheckerException(Diagnostic.Error("Cannot infer argument types for arrow function").Build()); - } - - if (expectedType is not FuncTypeNode funcType) - { - throw new TypeCheckerException(Diagnostic.Error($"Expected {expectedType}, but got arrow function").Build()); - } - - var parameters = new List(); - - for (var i = 0; i < expression.Parameters.Count; i++) - { - if (i >= funcType.Parameters.Count) - { - throw new TypeCheckerException(Diagnostic.Error($"Arrow function expected a maximum of {funcType.Parameters.Count} arguments").Build()); - } - - var expectedParameterType = funcType.Parameters[i]; - var parameter = expression.Parameters[i]; - parameters.Add(new FuncParameterNode(parameter.Name, expectedParameterType)); - } - - var body = CheckFuncBody(expression.Body, funcType.ReturnType, parameters); - - return new ArrowFuncNode(new FuncTypeNode(parameters.Select(x => x.Type).ToList(), funcType.ReturnType), parameters, funcType.ReturnType, body); - } - private ArrayIndexAccessNode CheckArrayIndexAccess(ArrayIndexAccessSyntax expression) { var boundArray = CheckExpression(expression.Target);