X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/2eb8215d4156f442f856026f1714aef7ed185752..9777bd50251d7e72c44bfbee7fbad2d8df148e47:/Replace.cpp diff --git a/Replace.cpp b/Replace.cpp index db438b2..5803478 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -1,40 +1,22 @@ -/* Cycript - Inlining/Optimizing JavaScript Compiler - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cycript - Optimizing JavaScript Compiler/Runtime + * Copyright (C) 2009-2010 Jay Freeman (saurik) */ -/* Modified BSD License {{{ */ +/* GNU Lesser General Public License, Version 3 {{{ */ /* - * Redistribution and use in source and binary - * forms, with or without modification, are permitted - * provided that the following conditions are met: + * Cycript is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. * - * 1. Redistributions of source code must retain the - * above copyright notice, this list of conditions - * and the following disclaimer. - * 2. Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions - * and the following disclaimer in the documentation - * and/or other materials provided with the - * distribution. - * 3. The name of the author may not be used to endorse - * or promote products derived from this software - * without specific prior written permission. + * Cycript is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR - * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + * You should have received a copy of the GNU Lesser General Public License + * along with Cycript. If not, see . +**/ /* }}} */ #include "Parser.hpp" @@ -104,7 +86,7 @@ CYExpression *CYAssignment::Replace(CYContext &context) { } CYStatement *CYBlock::Replace(CYContext &context) { - statements_ = statements_->ReplaceAll(context); + context.ReplaceAll(statements_); if (statements_ == NULL) return $ CYEmpty(); return this; @@ -145,7 +127,7 @@ void Catch::Replace(CYContext &context) { $T() void CYClause::Replace(CYContext &context) { $T() context.Replace(case_); - statements_ = statements_->ReplaceAll(context); + context.ReplaceAll(statements_); next_->Replace(context); } @@ -154,8 +136,8 @@ CYStatement *CYComment::Replace(CYContext &context) { } CYExpression *CYCompound::Replace(CYContext &context) { - expressions_ = expressions_->ReplaceAll(context); - return expressions_ == NULL ? NULL : this; + context.ReplaceAll(expressions_); + return this; } CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL) @@ -190,11 +172,11 @@ void CYContext::NonLocal(CYStatement *&statements) { cy::Syntax::Catch *rescue( $ cy::Syntax::Catch(cye, $$->* - $ CYIf($ CYIdentical($M($ CYVariable(cye), $S("$cyk")), $ CYVariable(unique)), $$->* - $ CYReturn($M($ CYVariable(cye), $S("$cyv"))))->* - $ cy::Syntax::Throw($ CYVariable(cye)))); + $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->* + $ CYReturn($M($V(cye), $S("$cyv"))))->* + $ cy::Syntax::Throw($V(cye)))); - declare = declare->Replace(context); + context.Replace(declare); rescue->Replace(context); statements = $$->* @@ -217,13 +199,13 @@ CYAssignment *CYDeclaration::Assignment(CYContext &context) { } CYExpression *CYDeclaration::ForEachIn(CYContext &context) { - return $ CYVariable(identifier_); + return $V(identifier_); } CYExpression *CYDeclaration::Replace(CYContext &context) { context.Replace(identifier_); context.scope_->Declare(context, identifier_, CYIdentifierVariable); - return $ CYVariable(identifier_); + return $V(identifier_); } CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) @@ -231,15 +213,7 @@ CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL) } CYCompound *CYDeclarations::Replace(CYContext &context) { - CYCompound *compound; - if (next_ == NULL) compound: - compound = $ CYCompound(); - else { - compound = next_->Replace(context); - if (compound == NULL) - goto compound; - } - + CYCompound *compound(next_ == NULL ? $ CYCompound() : next_->Replace(context)); if (CYAssignment *assignment = declaration_->Assignment(context)) compound->AddPrev(assignment); return compound; @@ -301,18 +275,6 @@ CYExpression *CYExpression::ForEachIn(CYContext &context) { return this; } -CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL) - CYExpression *replace(this); - context.Replace(replace); - - if (CYExpression *next = next_->ReplaceAll(context)) - replace->SetNext(next); - else - replace->SetNext(next_); - - return replace; -} - CYNumber *CYFalse::Number(CYContext &context) { return $D(0); } @@ -346,7 +308,7 @@ CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { } CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const { - return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement)); + return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement)); } CYStatement *CYForEachIn::Replace(CYContext &context) { @@ -423,7 +385,7 @@ CYExpression *CYFunctionExpression::Replace(CYContext &context) { } CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) { - name_ = name_->Replace(context); + context.Replace(name_); context.scope_->Declare(context, name_, CYIdentifierArgument); if (next_ != NULL) next_ = next_->Replace(context, code); @@ -491,10 +453,7 @@ namespace cy { namespace Syntax { CYExpression *New::AddArgument(CYContext &context, CYExpression *value) { - CYArgument **argument(&arguments_); - while (*argument != NULL) - argument = &(*argument)->next_; - *argument = $ CYArgument(value); + CYSetLast(arguments_, $ CYArgument(value)); return this; } @@ -531,9 +490,9 @@ CYExpression *CYObject::Replace(CYContext &context) { CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) { CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_)); parameter = parameter->Replace(context, code); - initializer_ = initializer_->Replace(context); + context.Replace(initializer_); - CYVariable *name($ CYVariable(name_)); + CYVariable *name($V(name_)); code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->* $E($ CYAssign(name, initializer_)) )); @@ -573,7 +532,7 @@ void CYProgram::Replace(CYContext &context) { CYScope scope(CYScopeProgram, context, statements_); context.nextlocal_ = $ CYNonLocal(); - statements_ = statements_->ReplaceAll(context); + context.ReplaceAll(statements_); context.NonLocal(statements_); scope.Close(); @@ -587,8 +546,8 @@ void CYProgram::Replace(CYContext &context) { IdentifierUsages usages; if (offset < context.rename_.size()) - for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_) - usages.insert(i); + CYForEach (i, context.rename_[offset].identifier_) + usages.insert(i); // XXX: totalling the probable occurrences and sorting by them would improve the result for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) { @@ -620,7 +579,7 @@ void CYProgram::Replace(CYContext &context) { // XXX: at some point, this could become a keyword } - for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_) + CYForEach (identifier, i->identifier_) identifier->Set(name); } } @@ -634,7 +593,7 @@ CYStatement *CYReturn::Replace(CYContext &context) { if (context.nonlocal_ != NULL) { CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_)); return $ cy::Syntax::Throw($ CYObject( - $ CYProperty($S("$cyk"), $ CYVariable(context.nonlocal_->Target(context)), value) + $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value) )); } @@ -776,13 +735,6 @@ CYStatement *CYStatement::Collapse(CYContext &context) { return this; } -CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL) - CYStatement *replace(this); - context.Replace(replace); - replace->SetNext(next_->ReplaceAll(context)); - return replace->Collapse(context); -} - CYString *CYString::Concat(CYContext &context, CYString *rhs) const { size_t size(size_ + rhs->size_); char *value($ char[size + 1]); @@ -850,7 +802,7 @@ CYStatement *CYVar::Replace(CYContext &context) { } CYExpression *CYVariable::Replace(CYContext &context) { - name_ = name_->Replace(context); + context.Replace(name_); return this; }