Remove hook concept
This commit is contained in:
@@ -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<StructFieldNode> Fields, List<StructFuncNode> Functions) : DefinitionNode(Module, Name);
|
||||
|
||||
|
||||
@@ -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<NubType> parameters, NubType returnType)
|
||||
public class NubStructFuncType(string name, List<NubType> parameters, NubType returnType)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public string? Hook { get; set; } = hook;
|
||||
public List<NubType> Parameters { get; } = parameters;
|
||||
public NubType ReturnType { get; } = returnType;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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<Variable> Variables = [];
|
||||
public readonly Stack<DeferNode> Defers = [];
|
||||
}
|
||||
|
||||
public record Variable(string Name, NubType Type);
|
||||
|
||||
public class StringLiteral(string value, string name)
|
||||
{
|
||||
public string Value { get; } = value;
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@ public record FuncSyntax(List<Token> Tokens, string Name, bool Exported, string?
|
||||
|
||||
public record StructFieldSyntax(List<Token> Tokens, string Name, TypeSyntax Type, ExpressionSyntax? Value) : SyntaxNode(Tokens);
|
||||
|
||||
public record StructFuncSyntax(List<Token> Tokens, string Name, string? Hook, FuncSignatureSyntax Signature, BlockSyntax Body) : SyntaxNode(Tokens);
|
||||
public record StructFuncSyntax(List<Token> Tokens, string Name, FuncSignatureSyntax Signature, BlockSyntax Body) : SyntaxNode(Tokens);
|
||||
|
||||
public record StructSyntax(List<Token> Tokens, string Name, bool Exported, List<StructFieldSyntax> Fields, List<StructFuncSyntax> Functions) : DefinitionSyntax(Tokens, Name, Exported);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user