...
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user