From 127cdaf48e0aa331da5320774b5a6611510fd886 Mon Sep 17 00:00:00 2001 From: nub31 Date: Thu, 2 Oct 2025 14:23:09 +0200 Subject: [PATCH] Remove hook concept --- compiler/NubLang/Ast/Node.cs | 2 +- compiler/NubLang/Ast/NubType.cs | 3 +- compiler/NubLang/Ast/TypeChecker.cs | 8 +-- compiler/NubLang/Generation/QBEGenerator.cs | 71 +-------------------- compiler/NubLang/Syntax/Parser.cs | 8 +-- compiler/NubLang/Syntax/Syntax.cs | 2 +- 6 files changed, 11 insertions(+), 83 deletions(-) diff --git a/compiler/NubLang/Ast/Node.cs b/compiler/NubLang/Ast/Node.cs index 08fad0e..e92b362 100644 --- a/compiler/NubLang/Ast/Node.cs +++ b/compiler/NubLang/Ast/Node.cs @@ -14,7 +14,7 @@ public record FuncNode(string Module, string Name, string? ExternSymbol, FuncSig public record StructFieldNode(string Name, NubType Type, ExpressionNode? Value) : Node; -public record StructFuncNode(string Name, string? Hook, FuncSignatureNode Signature, BlockNode Body) : Node; +public record StructFuncNode(string Name, FuncSignatureNode Signature, BlockNode Body) : Node; public record StructNode(string Module, string Name, List Fields, List Functions) : DefinitionNode(Module, Name); diff --git a/compiler/NubLang/Ast/NubType.cs b/compiler/NubLang/Ast/NubType.cs index a655c6e..dea20f0 100644 --- a/compiler/NubLang/Ast/NubType.cs +++ b/compiler/NubLang/Ast/NubType.cs @@ -122,10 +122,9 @@ public class NubStructFieldType(string name, NubType type, bool hasDefaultValue) public bool HasDefaultValue { get; } = hasDefaultValue; } -public class NubStructFuncType(string name, string? hook, List parameters, NubType returnType) +public class NubStructFuncType(string name, List parameters, NubType returnType) { public string Name { get; } = name; - public string? Hook { get; set; } = hook; public List Parameters { get; } = parameters; public NubType ReturnType { get; } = returnType; } diff --git a/compiler/NubLang/Ast/TypeChecker.cs b/compiler/NubLang/Ast/TypeChecker.cs index 4263bb6..4c0684d 100644 --- a/compiler/NubLang/Ast/TypeChecker.cs +++ b/compiler/NubLang/Ast/TypeChecker.cs @@ -149,7 +149,7 @@ public sealed class TypeChecker { var parameters = x.Signature.Parameters.Select(y => ResolveType(y.Type)).ToList(); var returnType = ResolveType(x.Signature.ReturnType); - return new NubStructFuncType(x.Name, x.Hook, parameters, returnType); + return new NubStructFuncType(x.Name, parameters, returnType); }) .ToList(); @@ -173,7 +173,7 @@ public sealed class TypeChecker _funcReturnTypes.Push(ResolveType(function.Signature.ReturnType)); var body = CheckBlock(function.Body); _funcReturnTypes.Pop(); - return new StructFuncNode(function.Name, function.Hook, CheckFuncSignature(function.Signature), body); + return new StructFuncNode(function.Name, CheckFuncSignature(function.Signature), body); } private StructFieldNode CheckStructField(StructFieldSyntax field) @@ -1060,7 +1060,7 @@ public sealed class TypeChecker .Select(y => ResolveType(y.Type)) .ToList(); - return new NubStructFuncType(x.Name, x.Hook, parameters, ResolveType(x.Signature.ReturnType)); + return new NubStructFuncType(x.Name, parameters, ResolveType(x.Signature.ReturnType)); }) .ToList(); @@ -1127,7 +1127,7 @@ public sealed class TypeChecker { var parameters = x.Signature.Parameters.Select(y => ResolveType(y.Type)).ToList(); var returnType = ResolveType(x.Signature.ReturnType); - return new NubStructFuncType(x.Name, x.Hook, parameters, returnType); + return new NubStructFuncType(x.Name, parameters, returnType); }) .ToList(); diff --git a/compiler/NubLang/Generation/QBEGenerator.cs b/compiler/NubLang/Generation/QBEGenerator.cs index dd6918a..e6d30cb 100644 --- a/compiler/NubLang/Generation/QBEGenerator.cs +++ b/compiler/NubLang/Generation/QBEGenerator.cs @@ -308,16 +308,6 @@ public class QBEGenerator { var value = EmitExpression(source); _writer.Indented($"blit {value}, {destinationAddress}, {SizeOf(source.Type)}"); - - if (source.Type is NubStructType structType) - { - var copyFunc = structType.Functions.FirstOrDefault(x => x.Hook == "oncopy"); - if (copyFunc != null) - { - _writer.Indented($"call {StructFuncName(structType.Module, structType.Name, copyFunc.Name)}(l {destinationAddress})"); - } - } - return; } @@ -409,13 +399,7 @@ public class QBEGenerator _writer.WriteLine(") {"); _writer.WriteLine("@start"); - var scope = new Scope(); - foreach (var parameter in function.Signature.Parameters) - { - scope.Variables.Push(new Variable(parameter.Name, parameter.Type)); - } - - EmitBlock(function.Body, scope); + EmitBlock(function.Body); // Implicit return for void functions if no explicit return has been set if (function.Signature.ReturnType is NubVoidType && function.Body.Statements.LastOrDefault() is not ReturnNode) @@ -453,13 +437,7 @@ public class QBEGenerator _writer.WriteLine(") {"); _writer.WriteLine("@start"); - var scope = new Scope(); - foreach (var parameter in funcDef.Signature.Parameters) - { - scope.Variables.Push(new Variable(parameter.Name, parameter.Type)); - } - - EmitBlock(funcDef.Body, scope); + EmitBlock(funcDef.Body); _writer.WriteLine("}"); _writer.NewLine(); @@ -530,18 +508,6 @@ public class QBEGenerator { EmitStatement(defer.Statement); } - - while (Scope.Variables.TryPop(out var variable)) - { - if (variable.Type is NubStructType structType) - { - var destroyFunc = structType.Functions.FirstOrDefault(x => x.Hook == "ondestroy"); - if (destroyFunc != null) - { - _writer.Indented($"call {StructFuncName(structType.Module, structType.Name, destroyFunc.Name)}(l %{variable.Name})"); - } - } - } } private void EmitIf(IfNode ifStatement) @@ -588,8 +554,6 @@ public class QBEGenerator { EmitCopyInto(variableDeclaration.Assignment, name); } - - Scope.Variables.Push(new Variable(variableDeclaration.Name, variableDeclaration.Type)); } private void EmitWhile(WhileNode whileStatement) @@ -956,12 +920,6 @@ public class QBEGenerator { _writer.Indented($"call {StructCtorName(structInitializer.StructType.Module, structInitializer.StructType.Name)}(l {destinationAddress})"); - var createFunc = structInitializer.StructType.Functions.FirstOrDefault(x => x.Hook == "oncreate"); - if (createFunc != null) - { - _writer.Indented($"call {StructFuncName(structInitializer.StructType.Module, structInitializer.StructType.Name, createFunc.Name)}(l {destinationAddress})"); - } - foreach (var (field, value) in structInitializer.Initializers) { var offset = TmpName(); @@ -1028,16 +986,6 @@ public class QBEGenerator foreach (var parameter in structFuncCall.Parameters) { var value = EmitExpression(parameter); - - if (parameter.Type is NubStructType structType) - { - var copyFunc = structType.Functions.FirstOrDefault(x => x.Hook == "oncopy"); - if (copyFunc != null) - { - _writer.Indented($"call {StructFuncName(structType.Module, structType.Name, copyFunc.Name)}(l {value})"); - } - } - parameterStrings.Add($"{FuncQBETypeName(parameter.Type)} {value}"); } @@ -1141,16 +1089,6 @@ public class QBEGenerator foreach (var parameter in funcCall.Parameters) { var value = EmitExpression(parameter); - - if (parameter.Type is NubStructType structType) - { - var copyFunc = structType.Functions.FirstOrDefault(x => x.Hook == "oncopy"); - if (copyFunc != null) - { - _writer.Indented($"call {StructFuncName(structType.Module, structType.Name, copyFunc.Name)}(l {value})"); - } - } - parameterStrings.Add($"{FuncQBETypeName(parameter.Type)} {value}"); } @@ -1267,7 +1205,7 @@ public class QBEGenerator foreach (var function in definition.Functions) { var parameters = function.Signature.Parameters.Select(x => x.Type).ToList(); - functionTypes.Add(new NubStructFuncType(function.Name, function.Hook, parameters, function.Signature.ReturnType)); + functionTypes.Add(new NubStructFuncType(function.Name, parameters, function.Signature.ReturnType)); } return new NubStructType(definition.Module, definition.Name, fieldTypes, functionTypes); @@ -1316,12 +1254,9 @@ public class QBEGenerator public class Scope { - public readonly Stack Variables = []; public readonly Stack Defers = []; } -public record Variable(string Name, NubType Type); - public class StringLiteral(string value, string name) { public string Value { get; } = value; diff --git a/compiler/NubLang/Syntax/Parser.cs b/compiler/NubLang/Syntax/Parser.cs index 5488c80..5d27562 100644 --- a/compiler/NubLang/Syntax/Parser.cs +++ b/compiler/NubLang/Syntax/Parser.cs @@ -198,19 +198,13 @@ public sealed class Parser { var memberStartIndex = _tokenIndex; - string? hook = null; - if (TryExpectSymbol(Symbol.At)) - { - hook = ExpectIdentifier().Value; - } - if (TryExpectSymbol(Symbol.Func)) { var funcName = ExpectIdentifier().Value; var funcSignature = ParseFuncSignature(); var funcBody = ParseBlock(); - funcs.Add(new StructFuncSyntax(GetTokens(memberStartIndex), funcName, hook, funcSignature, funcBody)); + funcs.Add(new StructFuncSyntax(GetTokens(memberStartIndex), funcName, funcSignature, funcBody)); } else { diff --git a/compiler/NubLang/Syntax/Syntax.cs b/compiler/NubLang/Syntax/Syntax.cs index 28c5679..235a2d8 100644 --- a/compiler/NubLang/Syntax/Syntax.cs +++ b/compiler/NubLang/Syntax/Syntax.cs @@ -14,7 +14,7 @@ public record FuncSyntax(List Tokens, string Name, bool Exported, string? public record StructFieldSyntax(List Tokens, string Name, TypeSyntax Type, ExpressionSyntax? Value) : SyntaxNode(Tokens); -public record StructFuncSyntax(List Tokens, string Name, string? Hook, FuncSignatureSyntax Signature, BlockSyntax Body) : SyntaxNode(Tokens); +public record StructFuncSyntax(List Tokens, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : SyntaxNode(Tokens); public record StructSyntax(List Tokens, string Name, bool Exported, List Fields, List Functions) : DefinitionSyntax(Tokens, Name, Exported);