#include <iostream>
#include <iomanip>
+#include <objc/runtime.h>
+#include <sstream>
+
_finline CYFlags operator ~(CYFlags rhs) {
return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
}
void CYAddressOf::Output(std::ostream &out, CYFlags flags) const {
rhs_->Output(out, 1, CYLeft(flags));
- out << ".$()";
+ out << ".addressOf()";
}
void CYArgument::Output(std::ostream &out) const {
code_->Output(out, true);
}
+void CYCategory::Output(std::ostream &out) const {
+ out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
+ out << "$cyp=object_getClass($cys);";
+ out << "$cyc=$cys;";
+ if (messages_ != NULL)
+ messages_->Output(out, true);
+ out << "})(";
+ name_->ClassName(out);
+ out << ");";
+}
+
void CYClass::Output(std::ostream &out) const {
- out << "(function($cys,$cyc,$cym,$cyn,$cyt){";
+ out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
+ out << "$cyp=object_getClass($cys);";
out << "$cyc=objc_allocateClassPair($cys,\"" << *name_ << "\",0);";
out << "$cym=object_getClass($cyc);";
if (fields_ != NULL)
fields_->Output(out);
if (messages_ != NULL)
- messages_->Output(out);
+ messages_->Output(out, false);
out << "objc_registerClassPair($cyc);";
out << "})(";
if (super_ != NULL)
out << ");";
}
+void CYCompound::Output(std::ostream &out, CYFlags flags) const {
+ if (CYExpression *expression = expressions_)
+ if (CYExpression *next = expression->next_) {
+ expression->Output(out, CYLeft(flags));
+ CYFlags center(CYCenter(flags));
+ while (next != NULL) {
+ expression = next;
+ out << ',';
+ next = expression->next_;
+ CYFlags right(next != NULL ? center : CYRight(flags));
+ expression->Output(out, right);
+ }
+ } else
+ expression->Output(out, flags);
+}
+
void CYCondition::Output(std::ostream &out, CYFlags flags) const {
test_->Output(out, Precedence() - 1, CYLeft(flags));
out << '?';
out << ';';
}
+void CYExpression::ClassName(std::ostream &out) const {
+ Output(out, CYPA, CYNoFlags);
+}
+
void CYExpression::Part(std::ostream &out) const {
// XXX: this should handle LeftHandSideExpression
Output(out, CYNoIn);
}
-void CYCompound::Output(std::ostream &out, CYFlags flags) const {
- if (CYExpression *expression = expressions_)
- if (CYExpression *next = expression->next_)
- expression->Output(out, flags);
- else {
- expression->Output(out, CYLeft(flags));
- CYFlags center(CYCenter(flags));
- while (next != NULL) {
- out << ',';
- next = expression->next_;
- CYFlags right(next != NULL ? center : CYRight(flags));
- expression->Output(out, right);
- }
- }
-}
-
void CYExpression::Output(std::ostream &out, unsigned precedence, CYFlags flags) const {
if (precedence < Precedence()) {
out << '(';
}
}
-void CYMessage::Output(std::ostream &out) const {
+void CYMessage::Output(std::ostream &out, bool replace) const {
+ if (next_ != NULL)
+ next_->Output(out, replace);
out << "$cyn=new Selector(\"";
for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
if (parameter->tag_ != NULL) {
out << ':';
}
out << "\");";
- out << "$cyt=$cyn.type($cys," << (instance_ ? "true" : "false") << ");";
- out << "class_addMethod($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
- out << "new Functor(function(";
- bool comma(false);
+ out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ");";
+ out << "class_" << (replace ? "replace" : "add") << "Method($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
+ out << "new Functor(function(self,_cmd";
for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
- if (parameter->name_ != NULL) {
- if (comma)
- out << ',';
- else
- comma = true;
- out << *parameter->name_;
- }
- out << "){";
+ if (parameter->name_ != NULL)
+ out << ',' << *parameter->name_;
+ out << "){return function(){";
if (body_ != NULL)
body_->Show(out);
- out << "},$cyt),$cyt);";
+ out << "}.call(self);},$cyt),$cyt);";
}
void CYNew::Output(std::ostream &out, CYFlags flags) const {
+ if ((flags & CYNoLeader) != 0)
+ out << ' ';
out << "new";
constructor_->Output(out, Precedence(), CYCenter(flags) | CYNoLeader);
out << '(';
void CYNumber::Output(std::ostream &out, CYFlags flags) const {
if ((flags & CYNoLeader) != 0)
out << ' ';
- // XXX: this is not a useful formatting
- out << Value();
+ // XXX: decide on correct precision
+ out << std::setprecision(9) << Value();
if ((flags & CYNoTrailer) != 0)
out << ' ';
}
+void CYNumber::PropertyName(std::ostream &out) const {
+ Output(out);
+}
+
void CYObject::Output(std::ostream &out, CYFlags flags) const {
bool protect((flags & CYNoBrace) != 0);
if (protect)
}
void CYProperty::Output(std::ostream &out) const {
- out << *name_ << ':';
+ name_->PropertyName(out);
+ out << ':';
value_->Output(out, CYPA, CYNoFlags);
if (next_ != NULL) {
out << ',';
}
void CYSelector::Output(std::ostream &out, CYFlags flags) const {
+ if ((flags & CYNoLeader) != 0)
+ out << ' ';
out << "new Selector(\"";
if (name_ != NULL)
name_->Output(out);
void CYSend::Output(std::ostream &out, CYFlags flags) const {
out << "objc_msgSend(";
self_->Output(out, CYPA, CYNoFlags);
- out << ",\"";
+ out << ",";
+ std::ostringstream name;
for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
if (argument->name_ != NULL) {
- out << *argument->name_;
+ name << *argument->name_;
if (argument->value_ != NULL)
- out << ':';
+ name << ':';
}
- out << "\"";
+ out << reinterpret_cast<void *>(sel_registerName(name.str().c_str()));
for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
if (argument->value_ != NULL) {
out << ",";
}
void CYString::Output(std::ostream &out, CYFlags flags) const {
- out << '\"';
+ unsigned quot(0), apos(0);
+ for (const char *value(value_), *end(value_ + size_); value != end; ++value)
+ if (*value == '"')
+ ++quot;
+ else if (*value == '\'')
+ ++apos;
+
+ bool single(quot > apos);
+
+ out << (single ? '\'' : '"');
for (const char *value(value_), *end(value_ + size_); value != end; ++value)
switch (*value) {
- case '"': out << "\\\""; break;
case '\\': out << "\\\\"; break;
case '\b': out << "\\b"; break;
case '\f': out << "\\f"; break;
case '\t': out << "\\t"; break;
case '\v': out << "\\v"; break;
+ case '"':
+ if (!single)
+ out << "\\\"";
+ else goto simple;
+ break;
+
+ case '\'':
+ if (single)
+ out << "\\'";
+ else goto simple;
+ break;
+
default:
if (*value < 0x20 || *value >= 0x7f)
out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
- else
+ else simple:
out << *value;
}
- out << '\"';
+ out << (single ? '\'' : '"');
+}
+
+void CYString::PropertyName(std::ostream &out) const {
+ if (const char *word = Word())
+ out << word;
+ else
+ Output(out);
}
void CYSwitch::Output(std::ostream &out) const {
}
void CYVariable::Output(std::ostream &out, CYFlags flags) const {
- bool protect((flags & CYNoLeader) != 0);
- if (protect)
- out << '(';
+ if ((flags & CYNoLeader) != 0)
+ out << ' ';
out << *name_;
- if (protect)
- out << ')';
+ if ((flags & CYNoTrailer) != 0)
+ out << ' ';
}
void CYWhile::Output(std::ostream &out) const {
code_->Output(out, false);
}
+void CYWord::ClassName(std::ostream &out) const {
+ out << "objc_getClass(\"" << Value() << "\")";
+}
+
void CYWord::Output(std::ostream &out) const {
out << Value();
}
+
+void CYWord::PropertyName(std::ostream &out) const {
+ Output(out);
+}