Add partial return analysis

This commit is contained in:
augustin64 2024-01-03 17:37:05 +01:00
parent 1bfbd45c4a
commit e9fb7c1e2b
3 changed files with 10 additions and 0 deletions

View File

@ -369,6 +369,14 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
return get<0>(get<FunctionPrototype>(function.type.data).at(0)); return get<0>(get<FunctionPrototype>(function.type.data).at(0));
} break; } break;
case NodeType::Return: {
try {
memory.get_function_scope();
} catch (const InternalError& _) {
throw RuntimeError(ErrorType::UnexpectedReturn, node.pos, {});
}
return {};
}
default: default:
return {}; return {};
} }

View File

@ -29,6 +29,7 @@ string UserError::get_message(void) const {
case ErrorType::IncompatibleRedefinition: return "Redefinition of '"+get<string>(this->data)+"' as different kind of symbol"; case ErrorType::IncompatibleRedefinition: return "Redefinition of '"+get<string>(this->data)+"' as different kind of symbol";
case ErrorType::IncompatibleDefinition: return "Declaration of '"+get<string>(this->data)+"' is incompatible with previous prototype"; case ErrorType::IncompatibleDefinition: return "Declaration of '"+get<string>(this->data)+"' is incompatible with previous prototype";
case ErrorType::UnexpectedArgumentCount: return "Expected "+to_string(get<int>(this->data))+" arguments to function call"; case ErrorType::UnexpectedArgumentCount: return "Expected "+to_string(get<int>(this->data))+" arguments to function call";
case ErrorType::UnexpectedReturn: return "Return not within a function";
case ErrorType::ExpectedArithmeticType: return "Expression must have arithmetic type"; case ErrorType::ExpectedArithmeticType: return "Expression must have arithmetic type";
case ErrorType::UnknownIdentifier: return "Unknown identifier '"+get<string>(this->data)+"'"; case ErrorType::UnknownIdentifier: return "Unknown identifier '"+get<string>(this->data)+"'";
case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get<string>(this->data)+"'"; case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get<string>(this->data)+"'";

View File

@ -38,6 +38,7 @@ enum class ErrorType {
IncompatibleRedefinition, // redefinition of a variable as a function IncompatibleRedefinition, // redefinition of a variable as a function
IncompatibleDefinition, // function declaration incompatible with prototype IncompatibleDefinition, // function declaration incompatible with prototype
UnexpectedArgumentCount, UnexpectedArgumentCount,
UnexpectedReturn,
// Runtime // Runtime
UnknownIdentifier, UnknownIdentifier,