]> git.saurik.com Git - cycript.git/blame - ObjectiveC/Replace.cpp
New syntax: let declarations, initialised arguments.
[cycript.git] / ObjectiveC / Replace.cpp
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
b53b30c1
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
b53b30c1 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
b53b30c1 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
b53b30c1 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
b53b30c1
JF
20/* }}} */
21
4de0686f 22#include "Replace.hpp"
3c1c3635 23#include "ObjectiveC/Syntax.hpp"
4de0686f 24
4de0686f
JF
25#include <sstream>
26
4de0686f
JF
27CYStatement *CYCategory::Replace(CYContext &context) {
28 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
29
c8a0500b 30 return $E($C1($F(NULL, $P5($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt")), $$->*
4de0686f
JF
31 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
32 $E($ CYAssign(cyc, cys))->*
725075ae 33 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
4de0686f
JF
34 messages_->Replace(context, true)
35 ), name_->ClassName(context, true)));
36}
37
38CYExpression *CYClass::Replace_(CYContext &context) {
39 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
40
41 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
42
c8a0500b 43 return $C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
4de0686f
JF
44 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
45 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
46 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
64b8d29f 47 protocols_->Replace(context)->*
4de0686f
JF
48 fields_->Replace(context)->*
49 messages_->Replace(context, false)->*
50 $E($C1($V("objc_registerClassPair"), cyc))->*
51 $ CYReturn(cyc)
52 ), super_ == NULL ? $ CYNull() : super_);
53}
54
55CYExpression *CYClassExpression::Replace(CYContext &context) {
56 return Replace_(context);
57}
58
59CYStatement *CYClassStatement::Replace(CYContext &context) {
60 return $E(Replace_(context));
61}
62
63CYStatement *CYField::Replace(CYContext &context) const {
64 return NULL;
65}
66
1ba6903e
JF
67CYStatement *CYImport::Replace(CYContext &context) {
68 return this;
69}
70
4de0686f
JF
71CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
72 CYVariable *cyn($V("$cyn"));
73 CYVariable *cyt($V("$cyt"));
cacd1a88
JF
74 CYVariable *self($V("self"));
75 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
4de0686f
JF
76
77 return $ CYBlock($$->*
78 next_->Replace(context, replace)->*
79 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
cacd1a88 80 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->*
4de0686f
JF
81 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
82 $V(instance_ ? "$cyc" : "$cym"),
83 cyn,
c8a0500b
JF
84 $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->*
85 $ CYVar($L1($L("$cyr", $N2($V("Super"), self, _class))))->*
cacd1a88 86 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
4de0686f
JF
87 ), cyt),
88 cyt
89 ))
90 );
91}
92
93CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
94 CYFunctionParameter *next(next_->Parameters(context));
c8a0500b 95 return name_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(name_), next);
4de0686f
JF
96}
97
98CYSelector *CYMessageParameter::Selector(CYContext &context) const {
99 return $ CYSelector(SelectorPart(context));
100}
101
102CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
103 CYSelectorPart *next(next_->SelectorPart(context));
104 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
105}
106
f2f0d1d1
JF
107CYExpression *CYBox::Replace(CYContext &context) {
108 return $C1($M($V("Instance"), $S("box")), value_);
109}
110
64b8d29f
JF
111CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
112 return $ CYBlock($$->*
113 next_->Replace(context)->*
114 $E($C2($V("class_addProtocol"),
115 $V("$cyc"), name_
116 ))
117 );
118}
119
4de0686f
JF
120CYExpression *CYSelector::Replace(CYContext &context) {
121 return $N1($V("Selector"), name_->Replace(context));
122}
123
124CYString *CYSelectorPart::Replace(CYContext &context) {
125 std::ostringstream str;
c2c9f509 126 CYForEach (part, this) {
4de0686f 127 if (part->name_ != NULL)
69688ca1 128 str << part->name_->Word();
4de0686f
JF
129 if (part->value_)
130 str << ':';
131 }
2eb8215d 132 return $S(apr_pstrdup($pool, str.str().c_str()));
4de0686f
JF
133}
134
cacd1a88 135CYExpression *CYSendDirect::Replace(CYContext &context) {
4de0686f
JF
136 std::ostringstream name;
137 CYArgument **argument(&arguments_);
2385c806 138 CYSelectorPart *selector(NULL), *current(NULL);
4de0686f
JF
139
140 while (*argument != NULL) {
141 if ((*argument)->name_ != NULL) {
2385c806
JF
142 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
143 if (selector == NULL)
144 selector = part;
145 if (current != NULL)
146 current->SetNext(part);
147 current = part;
4de0686f 148 (*argument)->name_ = NULL;
4de0686f
JF
149 }
150
151 if ((*argument)->value_ == NULL)
152 *argument = (*argument)->next_;
153 else
154 argument = &(*argument)->next_;
155 }
156
2385c806 157 return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_);
4de0686f 158}
cacd1a88
JF
159
160CYExpression *CYSendSuper::Replace(CYContext &context) {
57a65431 161 return $ CYSendDirect($V("$cyr"), arguments_);
cacd1a88 162}