Skip to content

Commit

Permalink
Merge pull request #18 from sriharivishnu/fix-list-assignment
Browse files Browse the repository at this point in the history
list indexing assignment working (fixes #15)
  • Loading branch information
sriharivishnu committed Oct 8, 2020
2 parents 99eabf7 + 9b002eb commit 501b51c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
15 changes: 15 additions & 0 deletions include/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ class UpdateExpression : public Expression {
shared_ptr<Expression> right;
};

class ObjectIndexUpdateExpression : public Expression {
public:
ObjectIndexUpdateExpression(
const Token& tok,
shared_ptr<Expression> objToUpdate,
shared_ptr<Expression> index,
shared_ptr<Expression> newVal);
shared_obj accept(Context& context, Visitor& v) override;
std::string toString() override;
Token tok;
shared_ptr<Expression> objToUpdate;
shared_ptr<Expression> index;
shared_ptr<Expression> newVal;
};

class ConditionalExpression : public Expression {
public:
ConditionalExpression(
Expand Down
2 changes: 2 additions & 0 deletions include/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class StringExpression;
class ListExpression;
class AssignmentExpression;
class UpdateExpression;
class ObjectIndexUpdateExpression;
class NameExpression;
class ConditionalExpression;
class CallFunctionExpression;
Expand All @@ -42,6 +43,7 @@ class Visitor {
shared_obj visit(Context& context, ListExpression* e);
shared_obj visit(Context& context, AssignmentExpression* e);
shared_obj visit(Context& context, UpdateExpression* e);
shared_obj visit(Context& context, ObjectIndexUpdateExpression* e);
shared_obj visit(Context& context, NameExpression* e);
shared_obj visit(Context& context, ConditionalExpression* e);
shared_obj visit(Context& context, CallFunctionExpression* e);
Expand Down
2 changes: 2 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct object {
virtual shared_obj lshift(const shared_obj& obj);
virtual shared_obj rshift(const shared_obj& obj);
virtual shared_obj index(const shared_obj& obj);
virtual shared_obj indexUpdate(const shared_obj& index, const shared_obj& newObj);
virtual shared_obj bnot();
virtual shared_obj inc();
virtual shared_obj dec();
Expand Down Expand Up @@ -121,6 +122,7 @@ struct list_type : object {
shared_obj multBy(const shared_obj& obj) override;
shared_obj toBool() override;
shared_obj index(const shared_obj& obj) override;
shared_obj indexUpdate(const shared_obj& obj, const shared_obj& newObj) override;
shared_obj dot(const std::string& name, const std::vector<shared_obj>& args) override;
std::string toString() override;
};
Expand Down
14 changes: 14 additions & 0 deletions src/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ shared_obj UpdateExpression::accept(Context& context, Visitor& v) {
return v.visit(context, this);
}

ObjectIndexUpdateExpression::ObjectIndexUpdateExpression(
const Token& tok,
shared_ptr<Expression> objToUpdate,
shared_ptr<Expression> index,
shared_ptr<Expression> newVal) :
tok(tok), objToUpdate(objToUpdate), index(index), newVal(newVal)
{}
shared_obj ObjectIndexUpdateExpression::accept(Context& context, Visitor& v) {
return v.visit(context, this);
}
std::string ObjectIndexUpdateExpression::toString() {
return tok.getValue() + index->toString() + "]" + " = " + newVal->toString();
}

//Conditional
ConditionalExpression::ConditionalExpression(
Token tok_,
Expand Down
10 changes: 10 additions & 0 deletions src/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ shared_obj Visitor::visit(Context& context, UpdateExpression* e) {
throw UndefinedVariable(make_shared<Context>(context), e->name, e->getToken().startPos);
}

shared_obj Visitor::visit(Context& context, ObjectIndexUpdateExpression* e) {
shared_obj res = e->newVal->accept(context, *this);
shared_obj obj = e->objToUpdate->accept(context, *this);
shared_obj index = e->index->accept(context, *this);
shared_obj newValue = e->newVal->accept(context, *this);

return obj->indexUpdate(index, newValue);
}


shared_obj Visitor::visit(Context& context, NameExpression* e) {
std::optional<shared_obj> value = context.symbols->get(e->name);
if (value) {
Expand Down
14 changes: 10 additions & 4 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ shared_ptr<Expression> Parser::getInfixExpression(const shared_ptr<Expression>&
case Token::Type::BOR:
return make_shared<BinOpExpression>(left, tok, parseExpression(Precedence::SUM - 1));
case Token::Type::EQUALS: {
if (!left->getToken().is(Token::Type::IDENTIFIER)) {
throw SyntaxError(left->getToken().startPos, "Expected an identifier");
if (left->getToken().is(Token::Type::IDENTIFIER)) {
shared_ptr<Expression> right = parseExpression(Precedence::ASSIGNMENT - 1);
return make_shared<UpdateExpression>(left->getToken().getValue(), right, tok);
}
shared_ptr<Expression> right = parseExpression(Precedence::ASSIGNMENT - 1);
return make_shared<UpdateExpression>(left->getToken().getValue(), right, tok);
throw SyntaxError(left->getToken().startPos, "Expected an identifier but instead got " + left->getToken().getValue());
}
case Token::Type::INC:
case Token::Type::DEC:
Expand All @@ -168,6 +168,12 @@ shared_ptr<Expression> Parser::getInfixExpression(const shared_ptr<Expression>&
case Token::Type::LSQUARE: {
shared_ptr<Expression> index = parseExpression();
consume(Token::Type::RSQUARE, ", expected ']'");
if (lookAhead(0).is(Token::Type::EQUALS)) {
consume(Token::Type::EQUALS);
shared_ptr<Expression> newObj = parseExpression();
return make_shared<ObjectIndexUpdateExpression>(tok, left, index, newObj);
}

return make_shared<IndexExpression>(left, tok, index);
}
case Token::Type::LPAREN: {
Expand Down
13 changes: 13 additions & 0 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ shared_obj object::rshift(const shared_obj& obj) {
shared_obj object::index(const shared_obj& obj) {
UNDEFINED_OP
}
shared_obj object::indexUpdate(const shared_obj& obj, const shared_obj& newObj) {
UNDEFINED_OP
}
shared_obj object::bnot() {
UNDEFINED_UNARY("~")
}
Expand Down Expand Up @@ -444,6 +447,16 @@ shared_obj list_type::index(const shared_obj& obj) {
}
return getValue<std::vector<shared_obj>>()[toAccess];
}
shared_obj list_type::indexUpdate(const shared_obj& obj, const shared_obj& newObj) {
if (!obj->value.isType<int>()) UNDEFINED_OP
int toAccess = obj->getValue<int>();
int size = getValue<std::vector<shared_obj>>().size();
if (toAccess >= size || toAccess < 0) {
throw Error("Index Out of Bounds", "Attempted to access index: " + std::to_string(toAccess) + ", with list size: " + std::to_string(size));
}
getValue<std::vector<shared_obj>>()[toAccess] = newObj;
return std::make_shared<null_type>();
}
std::string list_type::toString() {
auto values = getValue<std::vector<shared_obj>>();
std::string str("[");
Expand Down

0 comments on commit 501b51c

Please sign in to comment.