Skip to content

Commit

Permalink
Adds Type::toString()
Browse files Browse the repository at this point in the history
Issue: #17
  • Loading branch information
0x7CFE committed May 15, 2016
1 parent 1f0da05 commit 8294226
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ struct TSymbol : public TByteObject {
// Strings are binary objects that hold raw character bytes.
struct TString : public TByteObject {
static const char* InstanceClassName() { return "String"; }
std::string toString() const { return std::string(reinterpret_cast<const char*>(bytes), getSize()); }
};

// Chars are intermediate representation of single printable character
Expand Down
45 changes: 45 additions & 0 deletions src/TypeAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@

using namespace type;

std::string Type::toString() const {
std::stringstream stream;

switch (m_kind) {
case tkUndefined: return "?";
case tkPolytype: return "*";

case tkLiteral:
if (isSmallInteger(getValue()))
stream << TInteger(getValue()).getValue();
else if (getValue() == globals.nilObject)
return "nil";
else if (getValue() == globals.trueObject)
return "true";
else if (getValue() == globals.falseObject)
return "false";
else if (getValue()->getClass() == globals.stringClass)
stream << "'" << getValue()->cast<TString>()->toString() << "'";
else if (getValue()->getClass() == globals.badMethodSymbol->getClass())
stream << "'" << getValue()->cast<TSymbol>()->toString() << "'";
break;

case tkMonotype:
stream << "(" << getValue()->cast<TClass>()->name->toString() << ")";
break;

case tkArray:
stream << getValue()->cast<TClass>()->name->toString();
case tkComposite: {
stream << (m_kind == tkComposite ? "(" : "[");

for (std::size_t index = 0; index < getSubTypes().size(); index++) {
if (index)
stream << ", ";

stream << m_subTypes[index].toString();
}

stream << (m_kind == tkComposite ? ")" : "]");
};
}

return stream.str();
}

void TypeAnalyzer::processInstruction(const InstructionNode& instruction) {
const TSmalltalkInstruction::TArgument argument = instruction.getInstruction().getArgument();

Expand Down

0 comments on commit 8294226

Please sign in to comment.