This commit is contained in:
nub31
2026-02-28 00:57:48 +01:00
parent e7aad861d3
commit 84627dde45
6 changed files with 185 additions and 41 deletions

View File

@@ -145,11 +145,21 @@ public class TypeChecker
private TypedNodeStatementReturn CheckStatementReturn(NodeStatementReturn statement)
{
var value = CheckExpression(statement.Value, functionReturnType);
if (!value.Type.IsAssignableTo(functionReturnType))
throw BasicError($"Type of returned value ({value.Type}) is not assignable to the return type of the function ({functionReturnType})", value);
if (statement.Value == null)
{
if (functionReturnType is not NubTypeVoid)
throw BasicError($"Missing return value. Expected '{functionReturnType}'", statement);
return new TypedNodeStatementReturn(statement.Tokens, value);
return new TypedNodeStatementReturn(statement.Tokens, null);
}
else
{
var value = CheckExpression(statement.Value, functionReturnType);
if (!value.Type.IsAssignableTo(functionReturnType))
throw BasicError($"Type of returned value ({value.Type}) is not assignable to the return type of the function ({functionReturnType})", value);
return new TypedNodeStatementReturn(statement.Tokens, value);
}
}
private TypedNodeStatementVariableDeclaration CheckStatementVariableDeclaration(NodeStatementVariableDeclaration statement)
@@ -796,9 +806,9 @@ public class TypedNodeStatementFuncCall(List<Token> tokens, TypedNodeExpression
public List<TypedNodeExpression> Parameters { get; } = parameters;
}
public class TypedNodeStatementReturn(List<Token> tokens, TypedNodeExpression value) : TypedNodeStatement(tokens)
public class TypedNodeStatementReturn(List<Token> tokens, TypedNodeExpression? value) : TypedNodeStatement(tokens)
{
public TypedNodeExpression Value { get; } = value;
public TypedNodeExpression? Value { get; } = value;
}
public class TypedNodeStatementVariableDeclaration(List<Token> tokens, TokenIdent name, NubType type, TypedNodeExpression value) : TypedNodeStatement(tokens)