Skip to content

Commit

Permalink
Fully function binary expression for updating arrays and variable dec…
Browse files Browse the repository at this point in the history
…laration.
  • Loading branch information
nthnn committed Sep 8, 2024
1 parent b41c768 commit 86c775b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ast/expression/BinaryExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,37 @@
* along with Zhivo. If not, see <https://www.gnu.org/licenses/>.
*/

#include <ast/expression/ArrayAccessExpression.hpp>
#include <ast/expression/BinaryExpression.hpp>
#include <ast/expression/VariableAccessExpression.hpp>
#include <memory>

DynamicObject BinaryExpression::visit(SymbolTable& symbols) {
if(auto* varAccess = dynamic_cast<VariableAccessExpression*>(this->left.get())) {
DynamicObject rValue = this->right->visit(symbols);
symbols.setSymbol(varAccess->getName().getImage(), rValue);

return rValue;
}
else if(auto* arrayAccess = dynamic_cast<ArrayAccessExpression*>(this->left.get())) {
auto arrayExpr = arrayAccess->getArrayExpression();
auto arrayIdx = arrayAccess->getIndexExpression();

DynamicObject arrayVal = arrayExpr->visit(symbols);
if(!arrayVal.isArray())
throw std::runtime_error("Error updating array element (1)");

DynamicObject indexVal = arrayIdx->visit(symbols);
if(!indexVal.isNumber())
throw std::runtime_error("Error updating array element (2)");

DynamicObject rValue = this->right->visit(symbols);
std::unique_ptr<DynamicObject> rValuePtr = std::make_unique<DynamicObject>(rValue);

arrayVal.setArrayElement((int) indexVal.getNumber(), std::move(rValuePtr));
return arrayVal;
}

DynamicObject lValue = this->left->visit(symbols);
DynamicObject rValue = this->right->visit(symbols);

Expand Down

0 comments on commit 86c775b

Please sign in to comment.