diff --git a/packToken.cpp b/packToken.cpp index 84e50e6..407cdc8 100644 --- a/packToken.cpp +++ b/packToken.cpp @@ -4,14 +4,7 @@ #include "./shunting-yard.h" #include "./shunting-yard-exceptions.h" -struct None_t : public TokenBase { - None_t() { this->type = NONE; } - virtual TokenBase* clone() const { - return new None_t(static_cast(*this)); - } -}; - -const packToken packToken::None = packToken(None_t()); +const packToken packToken::None = packToken(TokenNone()); packToken& packToken::operator=(int t) { if (base) delete base; @@ -154,7 +147,7 @@ std::string packToken::str(TokenBase* base) { if (!base) return "undefined"; if (base->type & REF) { - base = static_cast*>(base)->val.second; + base = static_cast*>(base)->val.value; } switch (base->type) { diff --git a/shunting-yard.cpp b/shunting-yard.cpp index 717c55b..1e6b481 100644 --- a/shunting-yard.cpp +++ b/shunting-yard.cpp @@ -334,7 +334,7 @@ packToken calculator::calculate(TokenQueue_t _rpn, if (b_right->type & REF) { RefValue_t rvalue = static_cast*>(b_right)->val; delete b_right; - b_right = rvalue.second->clone(); + b_right = rvalue.value->clone(); } else if (b_right->type == VAR) { std::string var_name = static_cast*>(b_right)->val; delete b_right; @@ -345,11 +345,13 @@ packToken calculator::calculate(TokenQueue_t _rpn, } std::string r_left; + TokenMap_t* m_left = NULL; if (b_left->type & REF) { RefValue_t rvalue = static_cast*>(b_left)->val; delete b_left; - r_left = rvalue.first; - b_left = rvalue.second->clone(); + r_left = rvalue.name; + b_left = rvalue.value->clone(); + m_left = rvalue.source_map; } else if (b_left->type == VAR) { r_left = static_cast*>(b_left)->val; } @@ -372,7 +374,11 @@ packToken calculator::calculate(TokenQueue_t _rpn, if (r_left.size() > 0) { if (vars) { - vars->asign(r_left, b_right); + if (m_left && b_right->type != NONE) { + (*m_left)[r_left] = packToken(b_right->clone()); + } else { + vars->asign(r_left, b_right); + } evaluation.push(b_right); } else { delete b_right; @@ -490,14 +496,17 @@ packToken calculator::calculate(TokenQueue_t _rpn, if (!str.compare("[]") || !str.compare(".")) { TokenMap_t::iterator it = left->find(right); + TokenBase* value; + uint8_t type; if (it == left->end()) { - cleanRPN(&rpn); - cleanStack(evaluation); - throw std::domain_error( - "Unable to find the variable '" + right + "'."); + value = new TokenNone(); + type = NONE | REF; + } else { + value = it->second->clone(); + type = value->type | REF; } - evaluation.push(it->second->clone()); + evaluation.push(new Token({right, value, left}, type)); } else { cleanRPN(&rpn); cleanStack(evaluation); @@ -578,7 +587,7 @@ packToken calculator::calculate(TokenQueue_t _rpn, if (result->type & REF) { RefValue_t rvalue = static_cast*>(result)->val; delete result; - result = rvalue.second; + result = rvalue.value; } return packToken(result); } diff --git a/shunting-yard.h b/shunting-yard.h index f132281..30b4919 100644 --- a/shunting-yard.h +++ b/shunting-yard.h @@ -25,13 +25,29 @@ template class Token : public TokenBase { } }; +struct TokenNone : public TokenBase { + TokenNone() { this->type = NONE; } + virtual TokenBase* clone() const { + return new TokenNone(static_cast(*this)); + } +}; + class packToken; -typedef std::pair RefValue_t; typedef std::queue TokenQueue_t; typedef std::map TokenMap_t; typedef std::map OppMap_t; typedef std::list Tuple_t; +struct RefValue_t { + std::string name; + TokenBase* value; + TokenMap_t* source_map; + RefValue_t(std::string n, TokenBase* v, TokenMap_t* m) : + name(n), value(v), source_map(m) {} + RefValue_t(std::string n, TokenBase* v) : + name(n), value(v), source_map(0) {} +}; + #include "./packToken.h" // Define the `Function` class diff --git a/test-shunting-yard.cpp b/test-shunting-yard.cpp index 101ff1e..b980087 100644 --- a/test-shunting-yard.cpp +++ b/test-shunting-yard.cpp @@ -82,7 +82,7 @@ TEST_CASE("Map access expressions") { REQUIRE(calculator::calculate("map.key3.map1", &vars).asString() == "inception1"); REQUIRE(calculator::calculate("map.key3['map2']", &vars).asString() == "inception2"); - REQUIRE_THROWS(calculator::calculate("map[\"no_key\"]", &vars)); + REQUIRE(calculator::calculate("map[\"no_key\"]", &vars) == packToken::None); } TEST_CASE("Function usage expressions") { @@ -114,7 +114,7 @@ TEST_CASE("Function usage expressions") { // REQUIRE_NOTHROW(calculator::calculate("print()")); } -TEST_CASE("Assignment expressions", "[!mayfail]") { +TEST_CASE("Assignment expressions") { calculator::calculate("assignment = 10", &vars); // Assigning to an unexistent variable works. @@ -130,6 +130,27 @@ TEST_CASE("Assignment expressions", "[!mayfail]") { REQUIRE(calculator::calculate("a == b && b == c && b == d && d == 30", &vars) == true); } +TEST_CASE("Assignment expressions on maps") { + TokenMap_t asn_map; + vars["m"] = &asn_map; + calculator::calculate("m['asn'] = 10", &vars); + + // Assigning to an unexistent variable works. + REQUIRE(calculator::calculate("m['asn']", &vars).asDouble() == 10); + + // Assigning to existent variables should work as well. + REQUIRE_NOTHROW(calculator::calculate("m['asn'] = 20", &vars)); + REQUIRE(calculator::calculate("m['asn']", &vars).asDouble() == 20); + + // Chain assigning should work with a right-to-left order: + REQUIRE_NOTHROW(calculator::calculate("m.a = m.b = 20", &vars)); + REQUIRE_NOTHROW(calculator::calculate("m.a = m.b = m.c = m.d = 30", &vars)); + REQUIRE(calculator::calculate("m.a == m.b && m.b == m.c && m.b == m.d && m.d == 30", &vars) == true); + + REQUIRE_NOTHROW(calculator::calculate("m.m = m", &vars)); + REQUIRE(calculator::calculate("10 + (a = m.a = m.m.b)", &vars) == 40); +} + TEST_CASE("Scope management") { calculator c("pi+b1+b2"); Scope scope;