-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a07a23
commit e5eb76d
Showing
10 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "ast/passes/pass.h" | ||
|
||
class ControlFlow : public Pass { | ||
private: | ||
bool in_function; | ||
bool in_loop; | ||
|
||
public: | ||
ControlFlow(); | ||
void visit_function(shared_ptr<ast::Function>) override; | ||
void visit_return(shared_ptr<ast::Return>) override; | ||
void visit_loop(shared_ptr<ast::Loop>) override; | ||
void visit_continue(shared_ptr<ast::Continue>) override; | ||
void visit_break(shared_ptr<ast::Break>) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "ast/passes/control_flow.h" | ||
#include "errors/errors.h" | ||
|
||
ControlFlow::ControlFlow() : Pass("Control Flow") { | ||
this->in_function = false; | ||
this->in_loop = false; | ||
} | ||
|
||
void ControlFlow::visit_function(shared_ptr<ast::Function> node) { | ||
for (const auto& param : node->params) { | ||
visit(param); | ||
} | ||
|
||
in_function = true; | ||
visit(node->body); | ||
in_function = false; | ||
} | ||
|
||
void ControlFlow::visit_return(shared_ptr<ast::Return> node) { | ||
if (!in_function) { | ||
throw SyntaxError(node->token->getLine(), | ||
"found `return` outside of function"); | ||
} | ||
visit(node->expr); | ||
} | ||
|
||
void ControlFlow::visit_loop(shared_ptr<ast::Loop> node) { | ||
in_loop = true; | ||
visit(node->variable); | ||
visit(node->condition); | ||
visit(node->assignment); | ||
visit(node->body); | ||
in_loop = false; | ||
} | ||
|
||
void ControlFlow::visit_continue(shared_ptr<ast::Continue> node) { | ||
if (!in_loop) { | ||
throw SyntaxError(node->token->getLine(), | ||
"found `continue` outside of loop"); | ||
} | ||
} | ||
|
||
void ControlFlow::visit_break(shared_ptr<ast::Break> node) { | ||
if (!in_loop) { | ||
throw SyntaxError(node->token->getLine(), | ||
"found `break` outside of loop"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn main(): i32 { | ||
break; | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main(): i32 { | ||
continue; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
return 0; | ||
|
||
fn main(): i32 { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SyntaxError on Line 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SyntaxError on Line 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SyntaxError on Line 1 |