diff --git a/src/analysis.cpp b/src/analysis.cpp index 1f906bb..bb81956 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -369,6 +369,14 @@ AnalysisResult analyze(Node &ast, Memory &memory) { return get<0>(get(function.type.data).at(0)); } break; + case NodeType::Return: { + try { + memory.get_function_scope(); + } catch (const InternalError& _) { + throw RuntimeError(ErrorType::UnexpectedReturn, node.pos, {}); + } + return {}; + } default: return {}; } diff --git a/src/errors.cpp b/src/errors.cpp index f08a03d..4b9af77 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -29,6 +29,7 @@ string UserError::get_message(void) const { case ErrorType::IncompatibleRedefinition: return "Redefinition of '"+get(this->data)+"' as different kind of symbol"; case ErrorType::IncompatibleDefinition: return "Declaration of '"+get(this->data)+"' is incompatible with previous prototype"; case ErrorType::UnexpectedArgumentCount: return "Expected "+to_string(get(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::UnknownIdentifier: return "Unknown identifier '"+get(this->data)+"'"; case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get(this->data)+"'"; diff --git a/src/include/errors.h b/src/include/errors.h index fe673c1..62e0a59 100644 --- a/src/include/errors.h +++ b/src/include/errors.h @@ -38,6 +38,7 @@ enum class ErrorType { IncompatibleRedefinition, // redefinition of a variable as a function IncompatibleDefinition, // function declaration incompatible with prototype UnexpectedArgumentCount, + UnexpectedReturn, // Runtime UnknownIdentifier,