-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add variables scopes in the form of trees of symboltables
- Loading branch information
Showing
11 changed files
with
305 additions
and
69 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 |
---|---|---|
|
@@ -33,7 +33,7 @@ option(ENABLE_Z3 "Enables the download and compilation of the expr::z3_driver dr | |
|
||
# DEPENDENCIES | ||
CPMAddPackage("gh:yalibs/[email protected]") | ||
CPMAddPackage("gh:yalibs/yatree@1.0.1") | ||
CPMAddPackage("gh:yalibs/yatree#feature/parent") | ||
CPMAddPackage("gh:yalibs/[email protected]") | ||
CPMAddPackage("gh:yalibs/[email protected]") | ||
CPMAddPackage("gh:sillydan1/[email protected]") | ||
|
@@ -74,6 +74,7 @@ add_library(${PROJECT_NAME} SHARED | |
${BISON_expr_parser_OUTPUTS} | ||
${FLEX_expr_lexer_OUTPUTS} | ||
src/drivers/interpreter.cpp | ||
src/drivers/tree_interpreter.cpp | ||
src/drivers/compiler.cpp | ||
|
||
src/clock.cpp | ||
|
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,51 @@ | ||
/* MIT License | ||
* | ||
* Copyright (c) 2022 Asger Gitz-Johansen | ||
* | ||
* 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. | ||
*/ | ||
#ifndef EXPR_GENERIC_SYMBOL_OPERATOR_H | ||
#define EXPR_GENERIC_SYMBOL_OPERATOR_H | ||
#include "operations.h" | ||
|
||
namespace expr { | ||
struct generic_symbol_operator : public arithmetic_operator, boolean_operator, compare_operator { | ||
auto add(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ::operator+(a,b); }; | ||
auto sub(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a - b; }; | ||
auto mul(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a * b; }; | ||
auto div(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a / b; }; | ||
auto mod(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a % b; }; | ||
auto pow(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a ^ b; }; | ||
|
||
auto _not(const symbol_value_t &a) const -> symbol_value_t override { return not_(a); } | ||
auto _and(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return and_(a,b); } | ||
auto _or(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return or_(a,b); } | ||
auto _xor(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return xor_(a,b); } | ||
auto _implies(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return implies_(a,b); } | ||
|
||
auto gt(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return gt_(a,b); } | ||
auto ge(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ge_(a,b); } | ||
auto ee(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ee_(a,b); } | ||
auto ne(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ne_(a,b); } | ||
auto le(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return le_(a,b); } | ||
auto lt(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return lt_(a,b); } | ||
}; | ||
} | ||
|
||
#endif //EXPR_GENERIC_SYMBOL_OPERATOR_H |
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,47 @@ | ||
/* MIT License | ||
* | ||
* Copyright (c) 2022 Asger Gitz-Johansen | ||
* | ||
* 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. | ||
*/ | ||
#ifndef EXPR_TREE_DRIVER_H | ||
#define EXPR_TREE_DRIVER_H | ||
#include "driver.h" | ||
|
||
namespace expr { | ||
struct tree_driver : public driver { | ||
tree_driver(const symbol_table_tree_t::_left_df_iterator& it) : driver{}, it{it} {} | ||
auto find(const std::string& identifier) const -> expr::symbol_table_t::const_iterator override { | ||
auto* x = &(*it); | ||
while(x) { | ||
auto i = x->node.find(identifier); | ||
if(i != x->node.end()) | ||
return i; | ||
if(!x->parent().has_value()) | ||
return end; | ||
x = x->parent().value(); | ||
} | ||
return end; | ||
} | ||
private: | ||
symbol_table_tree_t::_left_df_iterator it; | ||
}; | ||
} | ||
|
||
#endif //EXPR_TREE_DRIVER_H |
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,27 @@ | ||
#ifndef EXPR_TREE_INTERPRETER_H | ||
#define EXPR_TREE_INTERPRETER_H | ||
#include "tree_driver.h" | ||
#include "compiler.h" | ||
#include "generic_symbol_operator.h" | ||
|
||
namespace expr { | ||
struct tree_interpreter : public tree_driver, generic_symbol_operator { | ||
tree_interpreter(const symbol_table_tree_t::_left_df_iterator& it); | ||
~tree_interpreter() override = default; | ||
|
||
auto parse(const std::string &f) -> int override; | ||
auto interpret_declarations(const std::string& f) -> symbol_table_t; | ||
auto interpret_expression(const std::string& f) -> symbol_value_t; | ||
auto get_symbol(const std::string& identifier) -> syntax_tree_t override; | ||
void add_tree(const syntax_tree_t& tree) override; | ||
void add_tree(const std::string& identifier, const syntax_tree_t& tree) override; | ||
|
||
symbol_table_t result{}; | ||
symbol_value_t expression_result{}; | ||
|
||
auto evaluate(const syntax_tree_t& tree) -> symbol_value_t; | ||
auto evaluate(const compiler::compiled_expr_collection_t& tree) -> symbol_table_t; | ||
}; | ||
} | ||
|
||
#endif //EXPR_TREE_INTERPRETER_H |
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
Oops, something went wrong.