Skip to content

Commit fe1c28c

Browse files
cnfcnf
authored andcommitted
[New Compiler] Produce an error / fail compilation if two variables share the same name within the same scope.
1 parent afc5c92 commit fe1c28c

5 files changed

Lines changed: 20 additions & 3 deletions

File tree

nvse/nvse/Compiler/Passes/VariableResolution.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace Compiler::Passes {
2626
expr->Accept(this);
2727
}
2828
}
29+
// Existing variable within the same scope, produce an error
30+
else {
31+
CompErr("[line %d:%d] Error - Variable name '%s' already exists in this scope.\n", token.line, token.column, token.lexeme.c_str());
32+
this->had_error = true;
33+
}
2934
}
3035
}
3136

nvse/nvse/Compiler/Passes/VariableResolution.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace Compiler::Passes {
8181
static bool Resolve(Script *pScript, AST* pAST) {
8282
auto resolver = VariableResolution{ pScript };
8383
resolver.Visit(pAST);
84-
return true;
84+
return !resolver.HadError();
8585
}
8686

8787
VariableResolution(Script* pScript) : pScript{ pScript } {

nvse/nvse/Compiler/Visitor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Compiler {
66
class Visitor {
7+
protected:
8+
boolean had_error = false;
79
public:
810
virtual ~Visitor() = default;
911

@@ -39,5 +41,9 @@ namespace Compiler {
3941
virtual void VisitMapLiteralExpr(Expressions::MapLiteralExpr* expr);
4042
virtual void VisitGroupingExpr(Expressions::GroupingExpr* expr);
4143
virtual void VisitLambdaExpr(Expressions::LambdaExpr* expr);
44+
45+
virtual bool HadError() {
46+
return had_error;
47+
}
4248
};
4349
}

nvse/nvse/Hooks_Script.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ PrecompileResult __stdcall HandleBeginCompile(ScriptBuffer* buf, Script* script)
544544

545545
if (auto astOpt = parser.Parse(); astOpt.has_value()) {
546546
auto ast = std::move(astOpt.value());
547-
Compiler::Passes::VariableResolution::Resolve(script, &ast);
547+
const auto variablePass = Compiler::Passes::VariableResolution::Resolve(script, &ast);
548+
if (!variablePass) {
549+
return kPrecompile_Failure;
550+
}
548551

549552
auto tc = Compiler::NVSETypeChecker(&ast, script);
550553
bool typeCheckerPass = tc.check();

nvse/nvse/ScriptUtils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2289,7 +2289,10 @@ bool ExpressionParser::ParseUserFunctionParameters(std::vector<UserFunctionParam
22892289
Compiler::Lexer lexer{ funcScriptText };
22902290
Compiler::Parser parser{ lexer };
22912291
if (auto result = parser.Parse()) {
2292-
Compiler::Passes::VariableResolution::Resolve(nullptr, &*result);
2292+
const auto variablePass = Compiler::Passes::VariableResolution::Resolve(nullptr, &*result);
2293+
if (!variablePass) {
2294+
return false;
2295+
}
22932296

22942297
const auto blocks = result->blocks;
22952298
if (blocks.size() != 1) {

0 commit comments

Comments
 (0)