Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
Add ForIter
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Aug 7, 2023
1 parent acd6eff commit 8d48893
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ add_library(compiler_lib
ast/Switch.cpp
ast/SwitchCase.cpp
ast/ForI.cpp
ast/ForIter.cpp
)
target_include_directories(compiler_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
20 changes: 20 additions & 0 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,26 @@ namespace filc::ast {
AbstractExpression *_iteration;
std::vector<AbstractExpression *> _body;
};

class ForIter : public AbstractExpression {
public:
ForIter(bool constant, Identifier *identifier, AbstractExpression *array,
const std::vector<AbstractExpression *> &body);

[[nodiscard]] auto isConstant() const -> bool;

[[nodiscard]] auto getIdentifier() const -> Identifier *;

[[nodiscard]] auto getArray() const -> AbstractExpression *;

[[nodiscard]] auto getBody() const -> const std::vector<AbstractExpression *> &;

private:
bool _constant;
Identifier *_identifier;
AbstractExpression *_array;
std::vector<AbstractExpression *> _body;
};
}

#endif //FILC_AST_H
2 changes: 2 additions & 0 deletions src/lib/ast/AST_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace filc::ast {
class SwitchCase;

class ForI;

class ForIter;
}

#endif //FILC_AST_DECL_H
46 changes: 46 additions & 0 deletions src/lib/ast/ForIter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "AST.h"

namespace filc::ast {
ForIter::ForIter(bool constant, filc::ast::Identifier *identifier, filc::ast::AbstractExpression *array,
const std::vector<AbstractExpression *> &body)
: AbstractExpression(), _constant(constant), _identifier(identifier), _array(array), _body(body) {}

auto ForIter::isConstant() const -> bool {
return _constant;
}

auto ForIter::getIdentifier() const -> Identifier * {
return _identifier;
}

auto ForIter::getArray() const -> AbstractExpression * {
return _array;
}

auto ForIter::getBody() const -> const std::vector<AbstractExpression *> & {
return _body;
}
}
23 changes: 18 additions & 5 deletions src/lib/grammar/FilParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ switch_pattern returns[filc::ast::AbstractExpression *tree]
loop returns[filc::ast::AbstractExpression *tree]
: fi=for_i {
$tree = $fi.tree;
} | for_iter | while_l;
} | fit=for_iter {
$tree = $fit.tree;
} | while_l;

for_i returns[filc::ast::ForI *tree]
: FOR fic=for_i_condition ib=if_body {
Expand All @@ -511,11 +513,22 @@ for_i_condition returns[filc::ast::VariableDeclaration *declaration, filc::ast::
$iteration = $e2.tree;
})? RPAREN;

for_iter
: FOR for_iter_condition if_body;
for_iter returns[filc::ast::ForIter *tree]
: FOR fic=for_iter_condition ib=if_body {
$tree = new filc::ast::ForIter($fic.constant, $fic.identifier, $fic.array, $ib.tree);
};

for_iter_condition
: LPAREN (VAL | VAR) IDENTIFIER COLON expression RPAREN;
for_iter_condition returns[bool constant, filc::ast::Identifier *identifier, filc::ast::AbstractExpression *array]
@init {
$constant = true;
}
: LPAREN (VAL | VAR {
$constant = false;
}) i=IDENTIFIER {
$identifier = new filc::ast::Identifier($i);
} COLON e=expression {
$array = $e.tree;
} RPAREN;

while_l
: WHILE if_condition if_body;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_executable(tests
unit/ast/SwitchTest.cpp
unit/ast/SwitchCaseTest.cpp
unit/ast/ForITest.cpp
unit/ast/ForIterTest.cpp
)
target_include_directories(tests PUBLIC unit)

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/Fixtures/grammar/for_iter1.fil
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module test

for (val item : my_array) {
&item
}
38 changes: 38 additions & 0 deletions tests/unit/ast/ForIterTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "AST.h"
#include "tools.h"

TEST(ForIter, constructor) {
filc::ast::ForIter fi1(
true,
new filc::ast::Identifier("item"),
new filc::ast::Identifier("my_array"),
{}
);
ASSERT_TRUE(fi1.isConstant());
ASSERT_IDENTIFIER("item", fi1.getIdentifier());
ASSERT_IDENTIFIER("my_array", fi1.getArray());
ASSERT_THAT(fi1.getBody(), IsEmpty());
}
16 changes: 16 additions & 0 deletions tests/unit/grammar/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,19 @@ TEST(Parser, ForI) {
ASSERT_THAT(expression1_2->getBody(), SizeIs(1));
ASSERT_IDENTIFIER("i", expression1_2->getBody()[0]);
}

TEST(Parser, ForIter) {
filc::grammar::Parser parser1(FIXTURES_PATH "/for_iter1.fil");
auto *program1 = parser1.getProgram();
ASSERT_THAT(program1->getExpressions(), SizeIs(1));
auto *expression1 = static_cast<filc::ast::ForIter *>(program1->getExpressions()[0]);
ASSERT_NE(nullptr, expression1);
ASSERT_TRUE(expression1->isConstant());
ASSERT_IDENTIFIER("item", expression1->getIdentifier());
ASSERT_IDENTIFIER("my_array", expression1->getArray());
ASSERT_THAT(expression1->getBody(), SizeIs(1));
auto *body1 = static_cast<filc::ast::PreUnaryCalcul *>(expression1->getBody()[0]);
ASSERT_NE(nullptr, body1);
ASSERT_IDENTIFIER("item", body1->getVariable());
ASSERT_CLASSIC_OPERATOR(REF, body1->getOperator());
}

0 comments on commit 8d48893

Please sign in to comment.