Skip to content

Commit c1beaf0

Browse files
authored
Merge pull request #14 from sillydan1/feature/tree_interpreter
Feature/tree interpreter
2 parents d3df7d1 + f74524e commit c1beaf0

23 files changed

+596
-118
lines changed

.github/workflows/cmake.yml

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,3 @@ jobs:
4040
with:
4141
name: library
4242
path: ${{ github.workspace }}/build/libexpr.so
43-
build-windows:
44-
if: ${{ github.event.pull_request.draft == false }}
45-
name: Build For MS Windows Systems
46-
runs-on: windows-latest
47-
env:
48-
CC: x86_64-w64-mingw32-gcc
49-
CXX: x86_64-w64-mingw32-g++
50-
LD: x86_64-w64-mingw32-ld
51-
AR: x86_64-w64-mingw32-ar
52-
AS: x86_64-w64-mingw32-as
53-
NM: x86_64-w64-mingw32-nm
54-
STRIP: x86_64-w64-mingw32-strip
55-
RANLIB: x86_64-w64-mingw32-ranlib
56-
DLLTOOL: x86_64-w64-mingw32-dlltool
57-
OBJDUMP: x86_64-w64-mingw32-objdump
58-
RESCOMP: x86_64-w64-mingw32-windres
59-
steps:
60-
- name: Setup CygWin
61-
uses: egor-tensin/setup-cygwin@v3
62-
with:
63-
packages: flex bison mingw-w64-gcc
64-
- name: Install MinGW
65-
uses: egor-tensin/setup-mingw@v2
66-
with:
67-
cygwin: 1
68-
- name: Checkout Repository
69-
uses: actions/checkout@v2
70-
- name: Initialize Submodules
71-
uses: snickerbockers/submodules-init@v4
72-
- name: Create Build Environment
73-
run: cmake -E make_directory ${{github.workspace}}/build
74-
- name: Configure CMake
75-
shell: bash
76-
working-directory: ${{github.workspace}}/build
77-
run: cmake $GITHUB_WORKSPACE -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc
78-
- name: Build
79-
working-directory: ${{github.workspace}}/build
80-
shell: bash
81-
# Execute the build. You can specify a specific target with "--target <NAME>"
82-
run: cmake --build . --config $BUILD_TYPE -j 8

CHANGELOG.MD

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [v2.1.0](https://github.com/sillydan1/expr/releases/tag/v2.1.0) - 2022-12-11
8+
9+
<small>[Compare with v2.0.0](https://github.com/sillydan1/expr/compare/v2.0.0...v2.1.0)</small>
10+
11+
### Bug Fixes
12+
- Add a lookup function to symbol table ([797cdee](https://github.com/sillydan1/expr/commit/797cdeeb170cc1aa465adc0331efafe8a616b671) by Asger Gitz-Johansen).
13+
- Removed tree_interpreter functionality in the demo ([1563f5f](https://github.com/sillydan1/expr/commit/1563f5f0689f45371d14bf4752d9998e23d681f4) by Asger Gitz-Johansen).
14+
15+
### Features
16+
- Add as_string function ([b1bf12a](https://github.com/sillydan1/expr/commit/b1bf12a71cfc4270572b430274e979773ae945a2) by Asger Gitz-Johansen).
17+
- Add tree_compiler class ([feba171](https://github.com/sillydan1/expr/commit/feba17187e915172d2dbb832a089291839611475) by Asger Gitz-Johansen).
18+
- Extend the driver interface be aware of the access modifier ([418ce71](https://github.com/sillydan1/expr/commit/418ce71200423b321767698743ae754ed5cb1e78) by Asger Gitz-Johansen).
19+
- Add variables scopes in the form of trees of symboltables ([8f5ec2b](https://github.com/sillydan1/expr/commit/8f5ec2b9de720562d1ad931ba29dc2f0283699de) by Asger Gitz-Johansen).
20+
21+
722
## [v2.0.0](https://github.com/sillydan1/expr/releases/tag/v2.0.0) - 2022-09-19
823

924
<small>[Compare with v1.7.0](https://github.com/sillydan1/expr/compare/v1.7.0...v2.0.0)</small>
@@ -52,7 +67,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
5267

5368
## [v1.4.1](https://github.com/sillydan1/expr/releases/tag/v1.4.1) - 2022-07-19
5469

55-
<small>[Compare with v1.3.2](https://github.com/sillydan1/expr/compare/v1.3.2...v1.4.1)</small>
70+
<small>[Compare with v1.4.0](https://github.com/sillydan1/expr/compare/v1.4.0...v1.4.1)</small>
71+
72+
73+
## [v1.4.0](https://github.com/sillydan1/expr/releases/tag/v1.4.0) - 2022-07-19
74+
75+
<small>[Compare with v1.3.2](https://github.com/sillydan1/expr/compare/v1.3.2...v1.4.0)</small>
5676

5777

5878
## [v1.3.2](https://github.com/sillydan1/expr/releases/tag/v1.3.2) - 2022-05-02
@@ -77,7 +97,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
7797

7898
## [v1.1.2](https://github.com/sillydan1/expr/releases/tag/v1.1.2) - 2022-03-05
7999

80-
<small>[Compare with v1.0.0](https://github.com/sillydan1/expr/compare/v1.0.0...v1.1.2)</small>
100+
<small>[Compare with v1.1.0](https://github.com/sillydan1/expr/compare/v1.1.0...v1.1.2)</small>
101+
102+
103+
## [v1.1.0](https://github.com/sillydan1/expr/releases/tag/v1.1.0) - 2022-03-05
104+
105+
<small>[Compare with v1.0.0](https://github.com/sillydan1/expr/compare/v1.0.0...v1.1.0)</small>
81106

82107

83108
## [v1.0.0](https://github.com/sillydan1/expr/releases/tag/v1.0.0) - 2022-03-05

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
cmake_minimum_required(VERSION 3.21)
2323
# to generate a changelog, use: (python3 -m pip install git-changelog)
2424
# $ git-changelog -s conventional . -o CHANGELOG.MD
25-
project(expr VERSION 2.0.0)
25+
project(expr VERSION 2.1.0)
2626
include(cmake/CPM.cmake)
2727
configure_file(src/config.h.in config.h)
2828
set(CMAKE_CXX_STANDARD 20)
@@ -33,12 +33,12 @@ option(ENABLE_Z3 "Enables the download and compilation of the expr::z3_driver dr
3333

3434
# DEPENDENCIES
3535
CPMAddPackage("gh:yalibs/[email protected]")
36-
CPMAddPackage("gh:yalibs/yatree@1.0.1")
36+
CPMAddPackage("gh:yalibs/yatree@1.2.1")
3737
CPMAddPackage("gh:yalibs/[email protected]")
3838
CPMAddPackage("gh:yalibs/[email protected]")
3939
CPMAddPackage("gh:sillydan1/[email protected]")
4040
if(ENABLE_Z3)
41-
CPMAddPackage("gh:Z3Prover/z3#z3-4.11.0")
41+
CPMAddPackage("gh:Z3Prover/z3#z3-4.11.2")
4242
endif()
4343

4444
set(${PROJECT_NAME}_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE STRING "expr_BUILD_DIR" FORCE)
@@ -74,6 +74,8 @@ add_library(${PROJECT_NAME} SHARED
7474
${BISON_expr_parser_OUTPUTS}
7575
${FLEX_expr_lexer_OUTPUTS}
7676
src/drivers/interpreter.cpp
77+
src/drivers/tree_interpreter.cpp
78+
src/drivers/tree_compiler.cpp
7779
src/drivers/compiler.cpp
7880

7981
src/clock.cpp

include/drivers/compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace expr {
4040
auto get_symbol(const std::string &identifier) -> syntax_tree_t override;
4141
void add_tree(const syntax_tree_t& tree) override;
4242
void add_tree(const std::string& identifier, const syntax_tree_t& tree) override;
43+
void add_tree(const std::string& access_modifier, const std::string& identifier, const syntax_tree_t& tree) override;
4344

4445
compiled_expr_collection_t trees;
4546
};

include/drivers/driver.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
YY_DECL;
3030

3131
namespace expr {
32+
// TODO: Break this into an interface (functions) and a base abstract class (basic implementation)
3233
using symbol_table_ref_t = std::reference_wrapper<const expr::symbol_table_t>;
3334
using symbol_table_ref_collection_t = std::vector<std::reference_wrapper<const expr::symbol_table_t>>;
3435
struct driver {
@@ -40,10 +41,12 @@ namespace expr {
4041
virtual auto get_symbol(const std::string &identifier) -> syntax_tree_t = 0;
4142
virtual void add_tree(const syntax_tree_t& tree) = 0;
4243
virtual void add_tree(const std::string& identifier, const syntax_tree_t& tree) = 0;
43-
auto contains(const std::string& identifier) const -> bool {
44+
virtual void add_tree(const std::string& access_modifier, const std::string& identifier, const syntax_tree_t& tree) = 0;
45+
virtual auto contains(const std::string& identifier) const -> bool {
4446
return find(identifier) != end;
4547
}
46-
auto find(const std::string& identifier) const -> expr::symbol_table_t::const_iterator {
48+
virtual auto find(const std::string& identifier) const -> expr::symbol_table_t::const_iterator {
49+
// TODO: Tree of environments
4750
for(auto& env : environments) {
4851
auto env_it = env.get().find(identifier);
4952
if(env_it != env.get().end())
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* MIT License
2+
*
3+
* Copyright (c) 2022 Asger Gitz-Johansen
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
#ifndef EXPR_GENERIC_SYMBOL_OPERATOR_H
24+
#define EXPR_GENERIC_SYMBOL_OPERATOR_H
25+
#include "operations.h"
26+
27+
namespace expr {
28+
struct generic_symbol_operator : public arithmetic_operator, boolean_operator, compare_operator {
29+
auto add(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ::operator+(a,b); };
30+
auto sub(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a - b; };
31+
auto mul(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a * b; };
32+
auto div(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a / b; };
33+
auto mod(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a % b; };
34+
auto pow(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a ^ b; };
35+
36+
auto _not(const symbol_value_t &a) const -> symbol_value_t override { return not_(a); }
37+
auto _and(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return and_(a,b); }
38+
auto _or(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return or_(a,b); }
39+
auto _xor(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return xor_(a,b); }
40+
auto _implies(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return implies_(a,b); }
41+
42+
auto gt(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return gt_(a,b); }
43+
auto ge(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ge_(a,b); }
44+
auto ee(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ee_(a,b); }
45+
auto ne(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ne_(a,b); }
46+
auto le(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return le_(a,b); }
47+
auto lt(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return lt_(a,b); }
48+
};
49+
}
50+
51+
#endif //EXPR_GENERIC_SYMBOL_OPERATOR_H

include/drivers/interpreter.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
#include "operations.h"
2626
#include "drivers/driver.h"
2727
#include "compiler.h"
28+
#include "generic_symbol_operator.h"
2829

2930
namespace expr {
30-
struct interpreter : public driver, arithmetic_operator, boolean_operator, compare_operator {
31+
struct interpreter : public driver, generic_symbol_operator {
3132
interpreter(std::initializer_list<symbol_table_ref_t> environments);
3233
~interpreter() override = default;
3334

@@ -37,26 +38,7 @@ namespace expr {
3738
auto get_symbol(const std::string& identifier) -> syntax_tree_t override;
3839
void add_tree(const syntax_tree_t& tree) override;
3940
void add_tree(const std::string& identifier, const syntax_tree_t& tree) override;
40-
41-
auto add(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ::operator+(a,b); };
42-
auto sub(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a - b; };
43-
auto mul(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a * b; };
44-
auto div(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a / b; };
45-
auto mod(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a % b; };
46-
auto pow(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return a ^ b; };
47-
48-
auto _not(const symbol_value_t &a) const -> symbol_value_t override { return not_(a); }
49-
auto _and(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return and_(a,b); }
50-
auto _or(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return or_(a,b); }
51-
auto _xor(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return xor_(a,b); }
52-
auto _implies(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return implies_(a,b); }
53-
54-
auto gt(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return gt_(a,b); }
55-
auto ge(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ge_(a,b); }
56-
auto ee(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ee_(a,b); }
57-
auto ne(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return ne_(a,b); }
58-
auto le(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return le_(a,b); }
59-
auto lt(const symbol_value_t &a, const symbol_value_t &b) const -> symbol_value_t override { return lt_(a,b); }
41+
void add_tree(const std::string& access_modifier, const std::string& identifier, const syntax_tree_t& tree) override;
6042

6143
symbol_table_t result{};
6244
symbol_value_t expression_result{};

include/drivers/tree_compiler.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* MIT License
2+
*
3+
* Copyright (c) 2022 Asger Gitz-Johansen
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
#ifndef EXPR_TREE_COMPILER_H
24+
#define EXPR_TREE_COMPILER_H
25+
#include "tree_driver.h"
26+
27+
namespace expr {
28+
struct tree_compiler : tree_driver {
29+
using compiled_expr_t = syntax_tree_t;
30+
#ifndef NDEBUG
31+
using compiled_expr_collection_t = std::map<std::string, compiled_expr_t>;
32+
#else
33+
using compiled_expr_collection_t = std::unordered_map<std::string, compiled_expr_t>;
34+
#endif
35+
explicit tree_compiler(const symbol_table_tree_t::iterator& scope) : tree_driver{scope}, trees{} {}
36+
int parse(const std::string &f) override;
37+
auto get_symbol(const std::string &identifier) -> syntax_tree_t override;
38+
void add_tree(const syntax_tree_t& tree) override;
39+
void add_tree(const std::string& identifier, const syntax_tree_t& tree) override;
40+
void add_tree(const std::string& access_modifier, const std::string& identifier, const syntax_tree_t& tree) override;
41+
42+
compiled_expr_collection_t trees;
43+
};
44+
}
45+
46+
#endif //EXPR_TREE_COMPILER_H

include/drivers/tree_driver.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* MIT License
2+
*
3+
* Copyright (c) 2022 Asger Gitz-Johansen
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
#ifndef EXPR_TREE_DRIVER_H
24+
#define EXPR_TREE_DRIVER_H
25+
#include "driver.h"
26+
27+
namespace expr {
28+
struct tree_driver : public driver {
29+
tree_driver(const symbol_table_tree_t::iterator& it) : driver{}, it{it} {}
30+
auto find(const std::string& identifier) const -> expr::symbol_table_t::const_iterator override {
31+
auto* x = &(*it);
32+
while(x) {
33+
auto i = x->node.find(identifier);
34+
if(i != x->node.end())
35+
return i;
36+
if(!x->parent().has_value())
37+
return end;
38+
x = x->parent().value();
39+
}
40+
return end;
41+
}
42+
private:
43+
symbol_table_tree_t::iterator it;
44+
};
45+
}
46+
47+
#endif //EXPR_TREE_DRIVER_H

0 commit comments

Comments
 (0)