Skip to content

Commit

Permalink
Add braces support
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Dec 3, 2023
1 parent 785aa63 commit 3507fcf
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 64 deletions.
178 changes: 119 additions & 59 deletions libskrypt/skrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using namespace skrypt;
using namespace omnn::math;

using namespace std::literals;


void Skrypt::SetVarhost(decltype(varHost) host)
{
Expand Down Expand Up @@ -67,80 +69,138 @@ namespace {
s.erase(std::remove(s.begin(), s.end(), '?'), s.end());
return s;
}
std::string Questionless(std::string_view s) {
return Questionless(std::string(s));
}
auto FindBracePos(std::string_view line) {
return line.find_first_of("{}[]"sv);
}
}
const omnn::math::Valuable::va_names_t& Skrypt::Load(std::istream& in)
{
std::string line;
while (std::getline(in, line)) {
if (!line.empty()) {
bool Skrypt::ParseNextLine(std::istream& in, std::string_view& line) {
if (!line.empty()) {
auto bracePos = FindBracePos(line);
auto brace = bracePos == std::string_view::npos ? 0 : line[bracePos];
line.remove_prefix(bracePos);
switch (brace)
{
case '{': {
if (disjunctionParseMode || Expressions().empty()) {
Skrypt subsrypt;
auto parsingIsOnTheGo = subsrypt.ParseNextLine(in, line);
if (parsingIsOnTheGo)
subsrypt.Load(in);
if(!subsrypt.IsEmpty())
Add(subsrypt.Total());
return parsingIsOnTheGo;
}
else {
LOG_AND_IMPLEMENT("No need for double conjunction");
}
}
case '}': {
if (disjunctionParseMode) {
LOG_AND_IMPLEMENT("Closing disjunction with conjunction brace");
}
return {};
}
case '[': {
LOG_AND_IMPLEMENT("Implement OR system");
}
case ']': {
return {};
}
case 0: {
break;
}
default: {
IMPLEMENT;
}
}
#ifdef SKRYPT_EQUAL_SIGN
if (boost::algorithm::contains(line, "=")) {
if (boost::algorithm::contains(line, "==")) {
boost::replace_first(_2, "==", "-");
}
else {
boost::replace_first(_2, "=", "-");
}
if (boost::algorithm::contains(line, "=")) {
if (boost::algorithm::contains(line, "==")) {
boost::replace_first(_2, "==", "-");
}
else {
boost::replace_first(_2, "=", "-");
}
if (boost::algorithm::contains(line, "=")) {
LOG_AND_IMPLEMENT("More than one '=' sign");
}
LOG_AND_IMPLEMENT("More than one '=' sign");
}
}
#endif
if (boost::algorithm::contains(line, "?")) {
auto yes = false;
auto& total = Total();
auto questionless = Questionless(line);
if (questionless.empty()) {
for (auto& [name, var] : vars) {
PrintVarKnowns(var);
}
continue;
if (boost::algorithm::contains(line, "?")) {
auto yes = false;
auto& total = Total();
auto questionless = Questionless(line);
if (questionless.empty()) {
for (auto& [name, var] : vars) {
PrintVarKnowns(var);
}
Valuable v(questionless, varHost);
auto lineVars = v.Vars();
if (total.IsSum()) {
auto rest = total / v;
std::cout << "Total: " << total << std::endl
<< total << " / " << v << ": " << rest << std::endl;
auto& totalSum = total.as<Sum>();
if (lineVars.size() == 1) {
std::vector<Valuable> coefficients;
auto& va = *lineVars.begin();
auto totalGrade = totalSum.FillPolyCoeff(coefficients, va);
coefficients.clear();
if (v.IsSum()) {
auto& lineSum = v.as<Sum>();
auto lineGrade = lineSum.FillPolyCoeff(coefficients, va);
if (rest.IsSum()) {
auto restGrade = rest.as<Sum>().FillPolyCoeff(coefficients, va);
yes = totalGrade == restGrade + lineGrade;
}
}
else if (v.IsVa()) {
auto solutions = Solve(v.as<Variable>());
if (solutions.size() == 1) {
yes = solutions.cbegin()->operator==(0);
}
return true;
}
Valuable v(questionless, varHost);
auto lineVars = v.Vars();
if (total.IsSum()) {
auto rest = total / v;
std::cout << "Total: " << total << std::endl
<< total << " / " << v << ": " << rest << std::endl;
auto& totalSum = total.as<Sum>();
if (lineVars.size() == 1) {
std::vector<Valuable> coefficients;
auto& va = *lineVars.begin();
auto totalGrade = totalSum.FillPolyCoeff(coefficients, va);
coefficients.clear();
if (v.IsSum()) {
auto& lineSum = v.as<Sum>();
auto lineGrade = lineSum.FillPolyCoeff(coefficients, va);
if (rest.IsSum()) {
auto restGrade = rest.as<Sum>().FillPolyCoeff(coefficients, va);
yes = totalGrade == restGrade + lineGrade;
}
}
else {
IMPLEMENT
else if (v.IsVa()) {
auto solutions = Solve(v.as<Variable>());
if (solutions.size() == 1) {
yes = solutions.cbegin()->operator==(0);
}
}
}
else if(total == constants::zero) {

} else {
else {
IMPLEMENT
}

std::cout << '\n'
<< v << " ?\n"
<< (yes ? "YES\n" : "IDK\n")
<< std::endl;
}
else if (total == constants::zero) {

}
else {
Add(line);
IMPLEMENT
}

std::cout << '\n'
<< v << " ?\n"
<< (yes ? "YES\n" : "IDK\n")
<< std::endl;
}
else {
Add(line);
}
}
return true;
}

const omnn::math::Valuable::va_names_t& Skrypt::Load(std::istream& in)
{
std::string line;
while (std::getline(in, line)) {
std::string_view substr = line;
if (ParseNextLine(in, substr)) {
if (substr.size()) {
LOG_AND_IMPLEMENT("Multiple braces per line:" << substr);
}
}
else {
break;
}
}
return vars;
Expand Down
7 changes: 6 additions & 1 deletion libskrypt/skrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ namespace skrypt {

omnn::math::Valuable::va_names_t vars;
bool echo = {};
bool disjunctionParseMode = {};

protected:
void SetVarhost(decltype(varHost));
constexpr bool DisjunctionParseMode() const { return disjunctionParseMode; }
void DisjunctionParseMode(bool isDisjunctionMode) {
disjunctionParseMode = isDisjunctionMode;
}
auto GetVarHost() const { return varHost; }

public:
Expand All @@ -34,7 +39,7 @@ namespace skrypt {
using base::Add;

bool Add(std::string_view);

bool ParseNextLine(std::istream&, std::string_view&);
void PrintVarKnowns(const omnn::math::Variable&);

/// <summary>
Expand Down
18 changes: 14 additions & 4 deletions skrypt/skrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ using namespace omnn::math;
#include <boost/archive/binary_oarchive.hpp>

#include <iostream>
#include <vector>


namespace {
bool IsInteractiveMode = true;
boost::program_options::options_description Options("Options");
boost::filesystem::path filepath;
std::vector<boost::filesystem::path> filepath;
auto& desc = Options.add_options()
("help", "produce help message")
("file", boost::program_options::value(&filepath), "Load task description")
("i,interactive", boost::program_options::value(&IsInteractiveMode)->default_value(true), "Continue interactive mode after loading scripts")
;
boost::program_options::variables_map vm;
}
int main(int argc, const char* const argv[])
{
boost::program_options::basic_command_line_parser<char> parser(argc, argv);
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, Options), vm);
boost::program_options::notify(vm);
if (vm.count("help")) {
std::cout << "Usage: options_description [options]\n" << Options;
exit(0);
}

std::cout << "SKRYPT RULEZ:\n"
"Accepts three forms of input :\n"
Expand All @@ -38,8 +46,10 @@ int main(int argc, const char* const argv[])
skrypt::Skrypt s;
s.Echo(true);
s.MakesTotalEqu(true);
s.Load(argv[1]);
s.Load(std::cin);
for(auto& file : filepath)
s.Load(file);
if(IsInteractiveMode)
s.Load(std::cin);
}
else {
skrypt::Skrypt s(std::cin);
Expand Down

0 comments on commit 3507fcf

Please sign in to comment.