Skip to content

Commit

Permalink
parser: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dvshapkin committed Aug 23, 2021
1 parent 87f7d47 commit abeb139
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ namespace runtime {
}

void ClassInstance::Print(std::ostream &os, Context &context) {
if (HasMethod("__str__", 0)) {
//os << Call("__str__", {}, context).TryAs<String>()->GetValue();
Call("__str__", {}, context)->Print(os, context);
const string STR_METHOD = "__str__"s;
if (HasMethod(STR_METHOD, 0)) {
Call(STR_METHOD, {}, context)->Print(os, context);
} else {
os << this;
}
Expand Down Expand Up @@ -162,8 +162,9 @@ namespace runtime {
if (lhs.TryAs<String>() && rhs.TryAs<String>()) {
return lhs.TryAs<String>()->GetValue() == rhs.TryAs<String>()->GetValue();
}
if (lhs.TryAs<ClassInstance>() && lhs.TryAs<ClassInstance>()->HasMethod("__eq__", 1)) {
return lhs.TryAs<ClassInstance>()->Call("__eq__", {rhs}, context).TryAs<Bool>()->GetValue();
const string EQ_METHOD = "__eq__"s;
if (lhs.TryAs<ClassInstance>() && lhs.TryAs<ClassInstance>()->HasMethod(EQ_METHOD, 1)) {
return lhs.TryAs<ClassInstance>()->Call(EQ_METHOD, {rhs}, context).TryAs<Bool>()->GetValue();
}
if (!lhs.operator bool() && !rhs.operator bool()) {
return true;
Expand All @@ -181,8 +182,9 @@ namespace runtime {
if (lhs.TryAs<String>() && rhs.TryAs<String>()) {
return lhs.TryAs<String>()->GetValue() < rhs.TryAs<String>()->GetValue();
}
if (lhs.TryAs<ClassInstance>() && lhs.TryAs<ClassInstance>()->HasMethod("__lt__", 1)) {
return lhs.TryAs<ClassInstance>()->Call("__lt__", {rhs}, context).TryAs<Bool>()->GetValue();
const string LT_METHOD = "__lt__"s;
if (lhs.TryAs<ClassInstance>() && lhs.TryAs<ClassInstance>()->HasMethod(LT_METHOD, 1)) {
return lhs.TryAs<ClassInstance>()->Call(LT_METHOD, {rhs}, context).TryAs<Bool>()->GetValue();
}
throw std::runtime_error("Cannot compare objects for less"s);
}
Expand Down
2 changes: 1 addition & 1 deletion statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace ast {

[[nodiscard]] std::string GetName() const;

const std::vector<std::string> &GetDottedIds() const;
[[nodiscard]] const std::vector<std::string> &GetDottedIds() const;

private:
std::vector<std::string> dotted_ids_;
Expand Down

0 comments on commit abeb139

Please sign in to comment.