Skip to content

Commit

Permalink
Disallow function declarations inside local scope
Browse files Browse the repository at this point in the history
  • Loading branch information
loumadev committed Dec 3, 2023
1 parent 22cea54 commit 3bd1664
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/compiler/analyser/Analyser.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,15 @@ AnalyserResult __Analyser_analyseBlock(Analyser *analyser, BlockASTNode *block)
case NODE_FUNCTION_DECLARATION: {
FunctionDeclarationASTNode *function = (FunctionDeclarationASTNode*)statement;

// Local scope function declarations are not supported
if(block->scope->parent) {
return AnalyserError(
RESULT_ERROR_SEMANTIC_OTHER,
String_fromFormat("function declarations are not allowed inside local scope"),
NULL
);
}

AnalyserResult result = __Analyser_analyseBlock(analyser, function->body);
if(!result.success) return result;

Expand Down
13 changes: 13 additions & 0 deletions test/compiler/analyser/Analyser.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,19 @@ DESCRIBE(function_dec_analysis, "Function declaration analysis") {
analyserResult = Analyser_analyse(&analyser, (ProgramASTNode*)parserResult.node);
EXPECT_FALSE(analyserResult.success);
}
{
Lexer_setSource(
&lexer,
"if(true) {" LF
TAB "func a() { }" LF
"}" LF
);
parserResult = Parser_parse(&parser);
EXPECT_TRUE(parserResult.success);

analyserResult = Analyser_analyse(&analyser, (ProgramASTNode*)parserResult.node);
EXPECT_FALSE(analyserResult.success);
}
{
Lexer_setSource(
&lexer,
Expand Down

0 comments on commit 3bd1664

Please sign in to comment.