Skip to content

Commit

Permalink
expr: Impemented basic cell referencing
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Laštovička committed May 11, 2022
1 parent 7a62030 commit 597d1ec
Show file tree
Hide file tree
Showing 19 changed files with 422 additions and 61 deletions.
1 change: 1 addition & 0 deletions Semestralni_prace/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ lastope2
lastope2.zip
test
logs.txt
err.txt
*.o
*.d
doc/
Expand Down
6 changes: 3 additions & 3 deletions Semestralni_prace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ compile: $(USERNAME)

# Runs compiled binary
run:
@./$(USERNAME) $()
@./$(USERNAME) 2> err.txt

# Tests
compile_test: $(TEST_FILE)
run_test:
@./$(TEST_FILE)
@./$(TEST_FILE) 2> err.txt



Expand Down Expand Up @@ -122,5 +122,5 @@ clean:
rm -f $(TEST_OBJECTS) $(TEST_DEPENDS) $(TEST_FILE)
rm -f core
rm -fr doc
rm -f "logs.txt"
rm -f logs.txt err.txt

3 changes: 1 addition & 2 deletions Semestralni_prace/polymorfismus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
### Buňky
různé druhy buňek
- prázdná
- textová/číselná
- s výrazem
- s obsahem, tj. textová/číselná

### Výrazy
Výraz bude převeden na binární strom, který bude postupně vyhodnocován.
Expand Down
13 changes: 13 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/expr/nodes/terms.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#include "terms.hpp"
#include <sstream>

using namespace std;
namespace cz::lastaapps::vimxel::expr {

DoubleTerm::DoubleTerm(long double value)
: mValue(value) {}
long double DoubleTerm::getValue() const { return mValue; }
string DoubleTerm::toString() const {
ostringstream text;
text.precision(6);
text << mValue;
return text.str();
}

TextTerm::TextTerm(const string& value)
: mValue(value) {}
const string& TextTerm::getValue() const { return mValue; }
string TextTerm::toString() const {
return mValue;
}

AreaTerm::AreaTerm(vector<shared_ptr<SingleTerm>> value)
: mValue(value) {}
const vector<shared_ptr<SingleTerm>>& AreaTerm::getValue() const {
return mValue;
}
string AreaTerm::toString() const {
return "Area of "s + to_string(mValue.size());
}
} // namespace cz::lastaapps::vimxel::expr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace cz::lastaapps::vimxel::expr {

struct Term {
virtual ~Term() = default;
virtual string toString() const = 0;
};
struct SingleTerm : public Term {};

Expand All @@ -18,6 +19,7 @@ class DoubleTerm final : public SingleTerm {
public:
explicit DoubleTerm(long double value);
long double getValue() const;
string toString() const override;
};

class TextTerm final : public SingleTerm {
Expand All @@ -26,6 +28,7 @@ class TextTerm final : public SingleTerm {
public:
explicit TextTerm(const string& value);
const string& getValue() const;
string toString() const override;
};

class AreaTerm final : public Term {
Expand All @@ -34,6 +37,7 @@ class AreaTerm final : public Term {
public:
explicit AreaTerm(vector<shared_ptr<SingleTerm>> value);
const vector<shared_ptr<SingleTerm>>& getValue() const;
string toString() const override;
};
} // namespace cz::lastaapps::vimxel::expr

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,9 @@ namespace cz::lastaapps::vimxel::expr {
string ConcatNode::getName() const { return "concat"; }
ST ConcatNode::getValue() const {
ostringstream text;
text.precision(8);
for (const auto& term : mChildren) {
auto castedText = dynamic_pointer_cast<TextTerm>(term);
if (castedText != nullptr) {
text << castedText -> getValue();
continue;
}
auto castedDouble = dynamic_pointer_cast<DoubleTerm>(term);
if (castedDouble != nullptr) {
text<< castedDouble -> getValue();
continue;
}
// else shouldn't happen
}
for (const auto& term : mChildren)
text << term->toString();

return make_shared<TextTerm>(text.str());
}

Expand Down
11 changes: 11 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/expr/parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "parser.hpp"

using namespace std;
using namespace cz::lastaapps::vimxel::table;
namespace cz::lastaapps::vimxel::expr {

Parser::Parser(STokenzizer, function<ST(const table::Coordinates&)>){}
shared_ptr<Term> Parser::evaulate() {
return make_shared<TextTerm>("Juhu");
}
}
26 changes: 26 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/expr/parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef H_PARSER
#define H_PARSER
#include <functional>
#include <memory>

#include "tokenizer.hpp"
#include "nodes/terms.hpp"
#include "../table/coordinate.hpp"

using namespace std;
using namespace cz::lastaapps::vimxel::table;
namespace cz::lastaapps::vimxel::expr {

class Parser final {
using STokenzizer = shared_ptr<Tokenizer>;
using ST = shared_ptr<Term>;

public:
Parser(STokenzizer, function<ST(const table::Coordinates&)>);
ST evaulate();
};
} // namespace cz::lastaapps::vimxel::exptr



#endif
8 changes: 6 additions & 2 deletions Semestralni_prace/src/cz/lastaapps/vimxel/expr/tokenizer.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "tokenizer.hpp"

#include <stdexcept>

using namespace std;
namespace cz::lastaapps::vimxel::expr {
Tokenizer::Tokenizer(const string& string)
: mString(string), mPos(mString.begin()) {}

ST Tokenizer::nextToken() {
shared_ptr<Token> Tokenizer::nextToken() {
skipSpaces();
Token* token = nullptr;
if (mPos == mString.end()) {
Expand All @@ -23,6 +22,10 @@ ST Tokenizer::nextToken() {
throw invalid_argument("Unexpected character at position: "s + to_string(mPos - mString.begin()));
}

void Tokenizer::skipAll() {
while (nextToken() -> type != TT::NONE);
}

bool Tokenizer::parseOperators(Token*& out) {
char c = *mPos++;
switch (c) {
Expand Down Expand Up @@ -119,6 +122,7 @@ bool Tokenizer::parseCell(Token*& out) {
size_t cellIndex;
while (mPos != mString.end() && (c = *mPos++, 'A' <= c && c <= 'Z'))
cellName += c;
if (mPos != mString.end()) mPos--;
while (mPos != mString.end() && (c = *mPos++, '0' <= c && c <= '9')) {
cellIndex *= 10;
cellIndex += c - '0';
Expand Down
7 changes: 4 additions & 3 deletions Semestralni_prace/src/cz/lastaapps/vimxel/expr/tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

using namespace std;
namespace cz::lastaapps::vimxel::expr {
using TT = TokenType;
using ST = shared_ptr<Token>;
class Tokenizer final {
using SItr = string::const_iterator;
using TT = TokenType;
using ST = shared_ptr<Token>;
using SItr = string::const_iterator;
string mString;
SItr mPos;

Expand All @@ -23,6 +23,7 @@ class Tokenizer final {
public:
Tokenizer(const string& string);
ST nextToken();
void skipAll();
const set<table::Coordinates>& getDependent() const;
private:
bool parseOperators(Token* & out);
Expand Down
26 changes: 22 additions & 4 deletions Semestralni_prace/src/cz/lastaapps/vimxel/storage/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <sstream>

#include "../log.hpp"
#include "../table/cell.hpp"
#include "../table/coordinate.hpp"
#include "../table/table.hpp"

Expand Down Expand Up @@ -36,8 +35,27 @@ void Storage::saveData(shared_ptr<table::Table> table, const string& path) {
}

void Storage::exportData(shared_ptr<table::Table> table, const string& path) {
// TODO update after expression evaulation is done
saveData(table, path);
auto size = table->tableSize();
ofstream file;
openFileForWrite(path, file);
for (size_t y = 0; y < size.y(); y++) {
bool isFirst = true;
for (size_t x = 0; x < size.x(); x++) {
if (isFirst)
isFirst = false;
else
file << DELIMITER;
const table::Cell& cell = table->getCell(table::Coordinates(x, y));
file << escapeText(cell.getValue());
}
file << LINE_DELIMITER;
}
// drop latests \n
file.close();
auto filePath = filesystem::path(path);
size_t fileSize = file_size(filePath);
if (fileSize != 0)
resize_file(filePath, fileSize - 1);
}

void Storage::loadData(const string& path, shared_ptr<table::Table>& table) {
Expand All @@ -47,7 +65,7 @@ void Storage::loadData(const string& path, shared_ptr<table::Table>& table) {
for (size_t x = 0; file; x++) {
bool lineEnd;
string cellContent = move(importText(file, lineEnd));
table->updateCell(table::Coordinates(x, y), table::TextCell(cellContent));
table->updateCell(table::Coordinates(x, y), cellContent);
if (lineEnd) break;
}
}
Expand Down
22 changes: 21 additions & 1 deletion Semestralni_prace/src/cz/lastaapps/vimxel/table/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@ using namespace std;
namespace cz::lastaapps::vimxel::table {

// --- TextCell ---------------------------------------------------------------
TextCell::TextCell(string content) : mContent(move(content)) {}
TextCell::TextCell(string content, ST term, bool inCycle)
: mContent(move(content)), mTerm(term), mInCycle(inCycle) {}

const string& TextCell::getContent() const {
return mContent;
}
string TextCell::getValue() const {
if (mInCycle) return "CYCLE!";
return mTerm -> toString();
}
ST TextCell::getTerm() const {
if (mInCycle) return EMPTY_TERM;
return mTerm;
}
bool TextCell::isInCycle() const {
return mInCycle;
}
Cell* TextCell::clone() const {
return new TextCell(*this);
}


// --- EmptyCell --------------------------------------------------------------
const string& EmptyCell::getContent() const { return emptyString; }
string EmptyCell::getValue() const { return ""; }
ST EmptyCell::getTerm() const {
return EMPTY_TERM;
}
bool EmptyCell::isInCycle() const { return false; }

Cell* EmptyCell::clone() const {
return new EmptyCell(*this);
}
Expand Down
30 changes: 26 additions & 4 deletions Semestralni_prace/src/cz/lastaapps/vimxel/table/cell.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
#ifndef CELL_H
#define CELL_H
#include <string>
#include "../expr/nodes/terms.hpp"
using namespace std;

namespace cz::lastaapps::vimxel::table {

using ST = shared_ptr<expr::Term>;
class Cell {
public:
virtual ~Cell() {}
// real content
virtual const string& getContent() const = 0;
// value in table
virtual string getValue() const = 0;
// value for calculations
virtual ST getTerm() const = 0;
// cell in in a cycle
virtual bool isInCycle() const = 0;

virtual Cell* clone() const = 0;

protected:
};

class TextCell : public Cell {
const static auto EMPTY_TERM = make_shared<expr::DoubleTerm>(0.0L);

class TextCell final : public Cell {
private:
string mContent;
ST mTerm;
bool mInCycle;
public:
TextCell(string);
TextCell(string, ST term, bool inCycle);
~TextCell() = default;
const string& getContent() const override;
string getValue() const override;
ST getTerm() const override;
bool isInCycle() const override;
virtual Cell* clone() const override;
};

class EmptyCell : public Cell {
class EmptyCell final : public Cell {
private:
string emptyString = "";
const string emptyString = "";
public:
~EmptyCell() = default;
const string& getContent() const override;
string getValue() const override;
ST getTerm() const override;
bool isInCycle() const override;
virtual Cell* clone() const override;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define COORDINATE_H
#include <string>
#include <iostream>
#include <functional>

using namespace std;
namespace cz::lastaapps::vimxel::table {
Expand Down Expand Up @@ -47,4 +48,11 @@ class Coordinates final {
ostream& operator<<(ostream& out, const Coordinates& c);

} // namespace cz::lastaapps::vimxel::table

template<>
struct std::hash<cz::lastaapps::vimxel::table::Coordinates> {
std::size_t operator()(const cz::lastaapps::vimxel::table::Coordinates & coord) const noexcept {
return coord.x() + (coord.y() << (sizeof(size_t) * 8 / 2));
}
};
#endif
Loading

0 comments on commit 597d1ec

Please sign in to comment.