Skip to content

Commit

Permalink
add pass (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackparsonss committed Jul 8, 2024
1 parent 1a07a23 commit e5eb76d
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
17 changes: 17 additions & 0 deletions include/ast/passes/control_flow.h
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;
};
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(
"${CMAKE_CURRENT_SOURCE_DIR}/ast/passes/def_ref.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ast/passes/type_check.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ast/passes/builtin.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ast/passes/control_flow.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/shared/type/type.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/shared/type/character.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/shared/type/integer.cpp"
Expand Down
48 changes: 48 additions & 0 deletions src/ast/passes/control_flow.cpp
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");
}
}
2 changes: 2 additions & 0 deletions src/ast/passes/pass.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ast/passes/pass.h"
#include "ast/ast.h"
#include "ast/passes/builtin.h"
#include "ast/passes/control_flow.h"
#include "ast/passes/def_ref.h"
#include "ast/passes/type_check.h"

Expand All @@ -13,6 +14,7 @@ constexpr bool debug = false;
void Pass::run_passes(std::shared_ptr<ast::Block> ast,
shared_ptr<SymbolTable> symtab) {
std::vector<std::shared_ptr<Pass>> passes = {
std::make_shared<ControlFlow>(),
std::make_shared<DefRef>(symtab),
std::make_shared<TypeCheck>(),
std::make_shared<Builtin>(symtab),
Expand Down
5 changes: 5 additions & 0 deletions tests/input/errors/syntax/break.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main(): i32 {
break;
return 0;
}

4 changes: 4 additions & 0 deletions tests/input/errors/syntax/continue.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main(): i32 {
continue;
return 0;
}
5 changes: 5 additions & 0 deletions tests/input/errors/syntax/return.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return 0;

fn main(): i32 {
return 0;
}
1 change: 1 addition & 0 deletions tests/output/errors/syntax/break.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SyntaxError on Line 2
1 change: 1 addition & 0 deletions tests/output/errors/syntax/continue.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SyntaxError on Line 2
1 change: 1 addition & 0 deletions tests/output/errors/syntax/return.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SyntaxError on Line 1

0 comments on commit e5eb76d

Please sign in to comment.