Skip to content

Commit

Permalink
Fix for behavior that adds to symbol table on runtime issue. (#45)
Browse files Browse the repository at this point in the history
* Fix for issue #44. throw exception instead of bind on null, move runtime errors within evaluate and add test cases.

* disable osxcross build step, not clear why this stopped working
  • Loading branch information
farzonl authored Sep 24, 2023
1 parent 7b5a59d commit dbacb1e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ if ("${_cmake_compiler_output}" MATCHES "[Ee]mscripten")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEmscripten")
endif()

if(BUILD_ASAN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
endif()

if (BUILD_FUZZER AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT isEmscripten )
add_subdirectory (fuzz)
endif()
Expand Down
6 changes: 6 additions & 0 deletions cli-test-cases/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
"comparePath" : "cli-test-cases/help.txt",
"error" : "Invalid flag: -q\n"
},
{
"execpath" : "build/src/cli/Warf",
"arguments" : ["-s","cli-test-cases/literal_runtime_error.wf"],
"comparePath" : "cli-test-cases/invalid_literal_bind.txt",
"compareErrorPath" : "cli-test-cases/literal_runtime_error.txt"
},
{
"execpath" : "build/src/cli/Warf",
"arguments" : ["-e", "2147483648"],
Expand Down
15 changes: 15 additions & 0 deletions cli-test-cases/invalid_literal_bind.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
L--AssignmentExpression
|--IdentifierToken
a
|--EqualsToken
L--BinaryExpression
|--LiteralExpression
| L--NumberToken
| 1
|--PlusToken
L--IdentifierExpression
L--IdentifierToken
b
L--IdentifierExpression
L--IdentifierToken
a
4 changes: 4 additions & 0 deletions cli-test-cases/literal_runtime_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Failed to Bind LiteralExpression
BinderError: Undefined name b Starting at Position 8 Ending at: 17.
Failed to Bind LiteralExpression
BinderError: Undefined name a Starting at Position 0 Ending at: 1.
2 changes: 2 additions & 0 deletions cli-test-cases/literal_runtime_error.wf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = 1 + b
a
4 changes: 4 additions & 0 deletions scripts/run_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_config(self):
with open(test["comparePath"]) as f:
contents = f.read()
self.assertEqual(stdout.decode("utf-8"), contents)
if(test.get("compareErrorPath")):
with open(test["compareErrorPath"]) as f:
contents = f.read()
self.assertEqual(stderr.decode("utf-8"), contents)

if __name__ == '__main__':
unittest.main()
10 changes: 6 additions & 4 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ void evaluate(std::string &line, bool showTree) {
auto syntaxTree = SyntaxTree::Parse(line);
globalScope->GetTextSpan()->SetLength(line.size());
auto binder = std::make_unique<Binder>();
auto boundExpression = binder->BindExpression(syntaxTree->Root());
std::unique_ptr<BoundExpressionNode> boundExpression;
try {
boundExpression = binder->BindExpression(syntaxTree->Root());
} catch (std::runtime_error &error) {
std::cerr << error.what() << std::endl;
}

if (showTree) {
syntaxTree->PrintTree();
Expand Down Expand Up @@ -135,9 +140,6 @@ void startRepl(bool showTree) {
try {
consoleRead(showTree);

} catch (std::runtime_error &error) {
std::cerr << error.what() << std::endl;
std::cin.get();
} catch (std::exception &error) {
std::cerr << error.what() << std::endl;
std::cin.get();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Binding/Binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Binder::BindAssignmentExpression(AssignmentExpressionNode *assignment) {
SymbolTableMgr::find(name);
if (existingVariable == VariableSymbol::failSymbol()) {
mRecords.ReportUndefinedIdentifier(assignment->IdentifierToken());
return std::make_unique<BoundLiteralExpressionNode>(0);
throw std::runtime_error("Failed to Bind LiteralExpression");
}
return std::make_unique<BoundAssignmentExpressionNode>(
existingVariable, std::move(boundExpression), boundOperator);
Expand All @@ -126,7 +126,7 @@ Binder::BindIdentifierExpression(IdentifierExpressionNode *identifier) {
std::shared_ptr<VariableSymbol> variable = SymbolTableMgr::find(name);
if (variable == VariableSymbol::failSymbol()) {
mRecords.ReportUndefinedIdentifier(identifier->IdentifierToken());
return std::make_unique<BoundLiteralExpressionNode>(0);
throw std::runtime_error("Failed to Bind LiteralExpression");
}

return std::make_unique<BoundIdentifierExpressionNode>(variable);
Expand Down
7 changes: 6 additions & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ bool testCaseSyntaxErrors(std::string s, std::string errorStr) {
return errorStr == syntaxTree->Errors()[0].Message();
}
auto binder = std::make_unique<Binder>();
auto boundExpression = binder->BindExpression(syntaxTree->Root());
std::unique_ptr<BoundExpressionNode> boundExpression;
try {
boundExpression = binder->BindExpression(syntaxTree->Root());
} catch (std::runtime_error &error) {
return errorStr == error.what();
}
if (!binder->Errors().empty()) {
return errorStr == binder->Errors()[0].Message();
}
Expand Down

0 comments on commit dbacb1e

Please sign in to comment.