]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Output.cpp
The grammar and lexer should not share a filename.
[cycript.git] / ObjectiveC / Output.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <sstream>
23
24 #include "Replace.hpp"
25
26 #include "ObjectiveC/Syntax.hpp"
27
28 void CYCategory::Output(CYOutput &out, CYFlags flags) const {
29 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
30 out << "$cyp=object_getClass($cys);";
31 out << "$cyc=$cys;";
32 if (messages_ != NULL)
33 messages_->Output(out, true);
34 out << "})(";
35 name_->ClassName(out, true);
36 out << ')';
37 out << ';';
38 }
39
40 void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
41 // XXX: I don't necc. need the ()s
42 out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
43 out << "$cyp=object_getClass($cys);";
44 out << "$cyc=objc_allocateClassPair($cys,";
45 name_->ClassName(out, false);
46 out << ",0);";
47 out << "$cym=object_getClass($cyc);";
48 if (fields_ != NULL)
49 fields_->Output(out);
50 if (messages_ != NULL)
51 messages_->Output(out, false);
52 if (protocols_ != NULL) {
53 out << '<';
54 out << *protocols_;
55 out << '>';
56 }
57 out << "objc_registerClassPair($cyc);";
58 out << "return $cyc;";
59 out << "}(";
60 if (super_ != NULL)
61 super_->Output(out, CYAssign::Precedence_, CYNoFlags);
62 else
63 out << "null";
64 out << "))";
65 }
66
67 void CYClassField::Output(CYOutput &out) const {
68 }
69
70 void CYInstanceLiteral::Output(CYOutput &out, CYFlags flags) const {
71 out << '#';
72 number_->Output(out, CYRight(flags));
73 }
74
75 void CYMessage::Output(CYOutput &out, bool replace) const {
76 out << (instance_ ? '-' : '+');
77
78 CYForEach (parameter, parameters_)
79 if (parameter->tag_ != NULL) {
80 out << ' ' << *parameter->tag_;
81 if (parameter->type_ != NULL)
82 out << ':' << *parameter->type_->identifier_;
83 }
84
85 out << code_;
86 }
87
88 void CYBox::Output(CYOutput &out, CYFlags flags) const {
89 out << '@';
90 value_->Output(out, Precedence(), CYRight(flags));
91 }
92
93 void CYObjCBlock::Output(CYOutput &out, CYFlags flags) const {
94 // XXX: this is seriously wrong
95 out << "^(";
96 out << ")";
97 out << "{";
98 out << "}";
99 }
100
101 void CYProtocol::Output(CYOutput &out) const {
102 name_->Output(out, CYAssign::Precedence_, CYNoFlags);
103 if (next_ != NULL)
104 out << ',' << ' ' << *next_;
105 }
106
107 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
108 out << "@selector" << '(' << name_ << ')';
109 }
110
111 void CYSelectorPart::Output(CYOutput &out) const {
112 out << name_;
113 if (value_)
114 out << ':';
115 out << next_;
116 }
117
118 void CYSend::Output(CYOutput &out, CYFlags flags) const {
119 CYForEach (argument, arguments_)
120 if (argument->name_ != NULL) {
121 out << ' ' << *argument->name_;
122 if (argument->value_ != NULL)
123 out << ':' << *argument->value_;
124 }
125 }
126
127 void CYSendDirect::Output(CYOutput &out, CYFlags flags) const {
128 out << '[';
129 self_->Output(out, CYAssign::Precedence_, CYNoFlags);
130 CYSend::Output(out, flags);
131 out << ']';
132 }
133
134 void CYSendSuper::Output(CYOutput &out, CYFlags flags) const {
135 out << '[' << "super";
136 CYSend::Output(out, flags);
137 out << ']';
138 }