mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-18 07:07:47 -05:00
add complex test
This commit is contained in:
40
tests/complex/CMakeLists.txt
Normal file
40
tests/complex/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
|
||||
|
||||
project(CPMTest)
|
||||
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LHC
|
||||
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
|
||||
VERSION 0.3
|
||||
)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME LarsEvent
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Event.git
|
||||
VERSION 1.0
|
||||
)
|
||||
|
||||
# Add project that depends on previous project
|
||||
CPMAddPackage(
|
||||
NAME LarsParser
|
||||
GIT_REPOSITORY https://github.com/TheLartians/Parser.git
|
||||
VERSION 1.2
|
||||
)
|
||||
|
||||
# add project twice
|
||||
CPMAddPackage(
|
||||
NAME LHC
|
||||
GIT_REPOSITORY https://github.com/TheLartians/LHC.git
|
||||
VERSION 0.3
|
||||
)
|
||||
|
||||
# add executable
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
add_executable(cpm-test test.cpp)
|
||||
target_link_libraries(cpm-test LHC LarsEvent LarsParser)
|
||||
|
||||
# tests
|
||||
enable_testing()
|
||||
add_test(cpm-test cpm-test)
|
||||
32
tests/complex/test.cpp
Normal file
32
tests/complex/test.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <lars/parser_generator.h>
|
||||
#include <lars/event.h>
|
||||
|
||||
int main() {
|
||||
// Define the return value
|
||||
int result = 1;
|
||||
|
||||
// Define grammar and evaluation rules
|
||||
lars::ParserGenerator<float> 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<float> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user