From c04051c034a5de33ba25e3182191d854edbbb516 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Fri, 27 Sep 2013 18:02:33 +0200 Subject: Modified Makefile to generate shared library * library is called libSimpleParser.so * parser was renamed to clc * test and clc are using the shared library --- Makefile | 53 ++++++++++++++++++++++++++++++++++++++++------------- clc.cc | 25 +++++++++++++++++++++++++ main.cc | 25 ------------------------- src/nodes.cc | 4 ++-- src/parser.h | 12 ++---------- src/tree.cc | 50 +++++++++++++++++++++++++------------------------- 6 files changed, 94 insertions(+), 75 deletions(-) create mode 100644 clc.cc delete mode 100644 main.cc diff --git a/Makefile b/Makefile index 09fdd49..f71ad1c 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,46 @@ -LIB_FILES = src/nodes.cc src/tree.cc src/utils.cc -PROG_FILES = main.cc -TEST_FILES = test.cc +CXX = g++ +CXXFLAGS = -std=c++11 -W -Wall -Wextra -pedantic -fpic -FLAGS = -std=c++11 -W -Wall -Wextra -pedantic -PARSER = -o bin/parser $(FLAGS) $(PROG_FILES) $(LIB_FILES) +SRC_DIR = src +BIN_DIR = bin -all: dev test +LIB_SRC = $(SRC_DIR)/nodes.cc \ + $(SRC_DIR)/tree.cc \ + $(SRC_DIR)/utils.cc \ + $(SRC_DIR)/parser.cc +LIB_OBJ = $(subst .cc,.o,$(LIB_SRC)) -dev: $(PROG_FILES) $(LIB_FILES) - g++ -g $(PARSER) +TEST_SRC = test.cc +CLC_SRC = clc.cc -release: $(PROG_FILES) $(LIB_FILES) - g++ -O3 $(PARSER) +all: lib test clc -test: $(LIB_FILES) $(TEST_FILES) - g++ -O3 -o bin/test -lgtest $(TEST_FILES) $(FLAGS) $(LIB_FILES) - ./bin/test +lib: $(BIN_DIR)/libSimpleParser.so; +test: $(BIN_DIR)/test; +clc: $(BIN_DIR)/clc; +$(BIN_DIR)/libSimpleParser.so: $(LIB_OBJ) + $(CXX) -shared -o $(BIN_DIR)/libSimpleParser.so $(LIB_OBJ) $(CXXFLAGS) + +$(BIN_DIR)/test: $(BIN_DIR)/libSimpleParser.so + $(CXX) -o $(BIN_DIR)/test $(TEST_SRC) -lgtest -L$(BIN_DIR)/ -lSimpleParser + ./$(BIN_DIR)/test + +$(BIN_DIR)/clc: $(BIN_DIR)/libSimpleParser.so + $(CXX) -o $(BIN_DIR)/clc $(CLC_SRC) -L$(BIN_DIR)/ -lSimpleParser + +install: $(BIN_DIR)/libSimpleParser.so $(BIN_DIR)/clc + install -m 0755 $(BIN_DIR)/libSimpleParser.so /usr/lib + install -m 0755 $(BIN_DIR)/clc /usr/bin + +.PHONY: clean; +clean: + rm -f $(LIB_OBJ) + rm -f $(BIN_DIR)/* + +depend: .depend + +.depend: $(LIB_SRC) + $(CXX) -M $(CXXFLAGS) $< > $@ + +include .depend diff --git a/clc.cc b/clc.cc new file mode 100644 index 0000000..3378e14 --- /dev/null +++ b/clc.cc @@ -0,0 +1,25 @@ +#include +#include + +#include "src/parser.h" + +int main() +{ + std::string inputTerm; + + std::cin >> inputTerm; + + try { + typedef std::numeric_limits dbl; + std::cout.precision(dbl::digits10); + + std::cout << SimpleParser::calculate(inputTerm) << std::endl; + } + catch ( std::exception &e ) + { + std::cerr << e.what() << std::endl; + return 1; + } + + return 0; +} diff --git a/main.cc b/main.cc deleted file mode 100644 index 3378e14..0000000 --- a/main.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -#include "src/parser.h" - -int main() -{ - std::string inputTerm; - - std::cin >> inputTerm; - - try { - typedef std::numeric_limits dbl; - std::cout.precision(dbl::digits10); - - std::cout << SimpleParser::calculate(inputTerm) << std::endl; - } - catch ( std::exception &e ) - { - std::cerr << e.what() << std::endl; - return 1; - } - - return 0; -} diff --git a/src/nodes.cc b/src/nodes.cc index 4710400..390eac7 100644 --- a/src/nodes.cc +++ b/src/nodes.cc @@ -15,7 +15,7 @@ double OperandNode::solve() { } NodeType OperandNode::getType() { - return OPERAND_NODE; + return NodeType::OPERAND_NODE; } std::string OperandNode::print() { @@ -63,7 +63,7 @@ double OperatorNode::solve() { } NodeType OperatorNode::getType() { - return OPERATOR_NODE; + return NodeType::OPERATOR_NODE; } std::string OperatorNode::print() { diff --git a/src/parser.h b/src/parser.h index a27d1c0..54e540a 100644 --- a/src/parser.h +++ b/src/parser.h @@ -2,19 +2,11 @@ #define PARSER_SRC_PARSER_H_ #include -#include - -#include "tree.h" namespace SimpleParser { -double calculate(std::string term) { - return Tree(term).solve(); -} - -std::string exportTree(std::string term) { - return Tree(term).print(); -} +double calculate(std::string); +std::string exportTree(std::string); } diff --git a/src/tree.cc b/src/tree.cc index 868183d..45484c6 100644 --- a/src/tree.cc +++ b/src/tree.cc @@ -18,30 +18,6 @@ double Tree::solve() { return this->root_node_->solve(); } -Node* Tree::addOperand(Node** place, double value) { - this->node_collection_.emplace_back( - new OperandNode(value) - ); - - if ( place != nullptr ) { - *place = this->node_collection_.back().get(); - } - - return this->node_collection_.back().get(); -} - -Node* Tree::addOperator(Node** place, char oper) { - this->node_collection_.emplace_back( - new OperatorNode(oper) - ); - - if ( place != nullptr ) { - *place = this->node_collection_.back().get(); - } - - return this->node_collection_.back().get(); -} - std::string Tree::print() { std::stringstream out; out.precision(std::numeric_limits::digits10); @@ -65,7 +41,7 @@ std::string Tree::print() { << "\"];" << std::endl; - if ( (*it)->getType() == OPERATOR_NODE ) { + if ( (*it)->getType() == NodeType::OPERATOR_NODE ) { for ( auto iter = this->node_collection_.begin(); iter != this->node_collection_.end(); ++iter ) { @@ -96,6 +72,30 @@ std::string Tree::print() { return out.str(); } +Node* Tree::addOperand(Node** place, double value) { + this->node_collection_.emplace_back( + new OperandNode(value) + ); + + if ( place != nullptr ) { + *place = this->node_collection_.back().get(); + } + + return this->node_collection_.back().get(); +} + +Node* Tree::addOperator(Node** place, char oper) { + this->node_collection_.emplace_back( + new OperatorNode(oper) + ); + + if ( place != nullptr ) { + *place = this->node_collection_.back().get(); + } + + return this->node_collection_.back().get(); +} + Node* Tree::buildTree(std::string term) { std::stack operandStack; std::stack operatorStack; -- cgit v1.2.3