diff --git a/.travis.yml b/.travis.yml index 53b3bd4..f48959f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,7 +36,10 @@ before_install: - echo "CC=$CC CXX=$CXX" script: - - cmake -Htest -Bbuild - - cmake --build build - - CTEST_OUTPUT_ON_FAILURE=1 cmake --build build --target test + - cmake -Htests/simple -Bbuild/simple + - cmake --build build/simple + - CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/simple --target test + - cmake -Htests/complex -Bbuild/complex + - cmake --build build/complex + - CTEST_OUTPUT_ON_FAILURE=1 cmake --build build/complex --target test \ No newline at end of file diff --git a/test/CMakeLists.txt b/tests/complex/CMakeLists.txt similarity index 92% rename from test/CMakeLists.txt rename to tests/complex/CMakeLists.txt index 7c893ac..27f8123 100644 --- a/test/CMakeLists.txt +++ b/tests/complex/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) project(CPMTest) -include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/CPM.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake) CPMAddPackage( NAME LHC diff --git a/tests/complex/test.cpp b/tests/complex/test.cpp new file mode 100644 index 0000000..ee137fb --- /dev/null +++ b/tests/complex/test.cpp @@ -0,0 +1,32 @@ +#include +#include + +int main() { + // Define the return value + int result = 1; + + // Define grammar and evaluation rules + lars::ParserGenerator g; + g.setSeparator(g["Whitespace"] << "[\t ]"); + g["Sum" ] << "Add | Subtract | Product"; + g["Product" ] << "Multiply | Divide | Atomic"; + g["Atomic" ] << "Number | '(' Sum ')'"; + g["Add" ] << "Sum '+' Product" >> [](auto e){ return e[0].evaluate() + e[1].evaluate(); }; + g["Subtract"] << "Sum '-' Product" >> [](auto e){ return e[0].evaluate() - e[1].evaluate(); }; + g["Multiply"] << "Product '*' Atomic" >> [](auto e){ return e[0].evaluate() * e[1].evaluate(); }; + g["Divide" ] << "Product '/' Atomic" >> [](auto e){ return e[0].evaluate() / e[1].evaluate(); }; + g["Number" ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e){ return stof(e.string()); }; + g.setStart(g["Sum"]); + + // Execute a string + + // create an event + lars::Event onResult; + onResult.connect([&](float v){ result = !(int(v) == 5); }); + + // emit the result of a parsed string + onResult.notify(g.run("1 + 2 * (3+4)/2 - 3")); + + // return the result + return result; +} diff --git a/tests/simple/CMakeLists.txt b/tests/simple/CMakeLists.txt new file mode 100644 index 0000000..c8173ea --- /dev/null +++ b/tests/simple/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) + +project(CPMTest) + +# add dependencies +include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake) + +CPMAddPackage( + NAME LarsParser + GIT_REPOSITORY https://github.com/TheLartians/Parser.git + VERSION 1.2 +) + +# add executable +set (CMAKE_CXX_STANDARD 17) +add_executable(cpm-test test.cpp) +target_link_libraries(cpm-test LarsParser) + +# tests +enable_testing() +add_test(cpm-test cpm-test) diff --git a/test/test.cpp b/tests/simple/test.cpp similarity index 100% rename from test/test.cpp rename to tests/simple/test.cpp