/* }}} */
#include "cycript.hpp"
-#include "Context.hpp"
#ifdef CY_EXECUTE
#include "JavaScript.hpp"
+++ /dev/null
-/* Cycript - Inlining/Optimizing JavaScript Compiler
- * Copyright (C) 2009 Jay Freeman (saurik)
-*/
-
-/* Modified BSD License {{{ */
-/*
- * Redistribution and use in source and binary
- * forms, with or without modification, are permitted
- * provided that the following conditions are met:
- *
- * 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.
- *
- * 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.
-*/
-/* }}} */
-
-#ifndef CYCRIPT_CONTEXT_HPP
-#define CYCRIPT_CONTEXT_HPP
-
-#include "Options.hpp"
-
-class CYProgram;
-class CYScope;
-
-struct CYContext {
- apr_pool_t *pool_;
- CYOptions &options_;
- CYScope *scope_;
- CYProgram *program_;
-
- CYContext(apr_pool_t *pool, CYOptions &options) :
- pool_(pool),
- options_(options),
- scope_(NULL),
- program_(NULL)
- {
- }
-
- template <typename Type_>
- void Replace(Type_ *&value) {
- for (;;) if (value == NULL)
- break;
- else {
- Type_ *replace(value->Replace(*this));
- if (replace != value)
- value = replace;
- else break;
- }
- }
-};
-
-#endif/*CYCRIPT_CONTEXT_HPP*/
#include "Pooling.hpp"
#include "Parser.hpp"
-#include "Context.hpp"
#include "Cycript.tab.hh"
#include "cycript.hpp"
#include "Pooling.hpp"
-#include "Context.hpp"
#include <sys/mman.h>
CYWord
{
CYIdentifier *replace_;
+ size_t offset_;
CYIdentifier(const char *word) :
CYWord(word),
- replace_(NULL)
+ replace_(NULL),
+ offset_(0)
{
}
CYIdentifierAddressFlagsMap internal_;
CYIdentifierValueSet identifiers_;
- size_t offset_;
CYScope() :
- parent_(NULL),
- offset_(0)
+ parent_(NULL)
{
}
void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
- void Merge(CYContext &context, CYIdentifierAddressVector &external);
+ void Merge(CYContext &context, CYIdentifier *identifier);
void Scope(CYContext &context, CYStatement *&statements);
};
{
CYStatement *statements_;
CYIdentifierAddressVector rename_;
- CYCStringSet external_;
CYProgram(CYStatement *statements) :
statements_(statements)
virtual void Output(CYOutput &out) const;
};
+struct CYContext :
+ CYScope
+{
+ apr_pool_t *pool_;
+ CYOptions &options_;
+ CYScope *scope_;
+ CYProgram *program_;
+
+ CYContext(apr_pool_t *pool, CYOptions &options) :
+ pool_(pool),
+ options_(options),
+ scope_(this),
+ program_(NULL)
+ {
+ }
+
+ template <typename Type_>
+ void Replace(Type_ *&value) {
+ for (;;) if (value == NULL)
+ break;
+ else {
+ Type_ *replace(value->Replace(*this));
+ if (replace != value)
+ value = replace;
+ else break;
+ }
+ }
+};
+
struct CYBlock :
CYStatement,
CYThing
/* }}} */
#include "Parser.hpp"
-#include "Context.hpp"
+#include "Replace.hpp"
#include <iomanip>
-#include "Replace.hpp"
-
CYExpression *CYAdd::Replace(CYContext &context) {
CYInfix::Replace(context);
}
void CYFunction::Inject(CYContext &context) {
- name_ = name_->Replace(context);
+ context.Replace(name_);
context.scope_->Declare(context, name_, CYIdentifierOther);
}
size_t offset(0);
+ CYCStringSet external;
+ for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
+ external.insert((*i)->Word());
+
// XXX: totalling the probable occurrences and sorting by them would improve the result
for (CYIdentifierAddressVector::const_iterator i(rename_.begin()); i != rename_.end(); ++i, ++offset) {
const char *name;
id[--position] = index == 0 ? '0' : index < 27 ? index - 1 + 'a' : index - 27 + 'A';
} while (local != 0);
- if (external_.find(id + position) != external_.end()) {
+ if (external.find(id + position) != external.end()) {
++offset;
goto id;
}
return *insert.first;
}
-void CYScope::Merge(CYContext &context, CYIdentifierAddressVector &external) {
- for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i) {
- std::pair<CYIdentifierAddressSet::iterator, bool> insert(identifiers_.insert(*i));
- if (!insert.second)
- (*i)->replace_ = *insert.first;
+void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
+ std::pair<CYIdentifierAddressSet::iterator, bool> insert(identifiers_.insert(identifier));
+ if (!insert.second) {
+ if ((*insert.first)->offset_ < identifier->offset_)
+ (*insert.first)->offset_ = identifier->offset_;
+ identifier->replace_ = *insert.first;
}
}
void CYScope::Scope(CYContext &context, CYStatement *&statements) {
- CYIdentifierAddressVector external;
-
- for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
- if (internal_.find(*i) == internal_.end())
- external.push_back(*i);
-
CYDeclarations *last(NULL), *curr(NULL);
CYProgram *program(context.program_);
+ typedef std::multimap<size_t, CYIdentifier *> IdentifierOffsetMap;
+ IdentifierOffsetMap offsetted;
+
// XXX: we don't want to do this in order, we want to sort it by probable occurrence
for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) {
- if (program != NULL && i->second != CYIdentifierMagic) {
- if (program->rename_.size() <= offset_)
- program->rename_.resize(offset_ + 1);
- CYIdentifier *&identifier(program->rename_[offset_++]);
- i->first->SetNext(identifier);
- identifier = i->first;
- }
-
+ if (program != NULL && i->second != CYIdentifierMagic)
+ offsetted.insert(IdentifierOffsetMap::value_type(i->first->offset_, i->first));
if (i->second == CYIdentifierVariable) {
CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->first)));
if (last == NULL)
}
}
+ size_t offset(0);
+
+ for (IdentifierOffsetMap::const_iterator i(offsetted.begin()); i != offsetted.end(); ++i) {
+ if (offset < i->first)
+ offset = i->first;
+ if (program->rename_.size() <= offset)
+ program->rename_.resize(offset + 1);
+ CYIdentifier *&identifier(program->rename_[offset++]);
+ i->second->SetNext(identifier);
+ identifier = i->second;
+ }
+
if (last != NULL) {
CYVar *var($ CYVar(last));
var->SetNext(statements);
statements = var;
}
- if (parent_ != NULL) {
- if (parent_->offset_ < offset_)
- parent_->offset_ = offset_;
- parent_->Merge(context, external);
- } else if (program != NULL)
- for (CYIdentifierAddressVector::const_iterator i(external.begin()); i != external.end(); ++i)
- program->external_.insert((*i)->Word());
+ for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
+ if (internal_.find(*i) == internal_.end()) {
+ if ((*i)->offset_ < offset)
+ (*i)->offset_ = offset;
+ parent_->Merge(context, *i);
+ }
}
CYStatement *CYStatement::Collapse(CYContext &context) {
arch := $(shell $(dpkg_architecture) -qDEB_HOST_ARCH 2>/dev/null)
endif
-header := Cycript.tab.hh Parser.hpp Pooling.hpp cycript.hpp Internal.hpp Error.hpp String.hpp Exception.hpp Standard.hpp Context.hpp
+header := Cycript.tab.hh Parser.hpp Pooling.hpp cycript.hpp Internal.hpp Error.hpp String.hpp Exception.hpp Standard.hpp
code :=
code += Replace.o Output.o