/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013 Jay Freeman (saurik)
+ * Copyright (C) 2009-2015 Jay Freeman (saurik)
*/
-/* GNU General Public License, Version 3 {{{ */
+/* GNU Affero General Public License, Version 3 {{{ */
/*
- * Cycript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, either version 3 of the License,
- * or (at your option) any later version.
- *
- * Cycript is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#include <cstdlib>
#include "List.hpp"
+#include "Location.hpp"
#include "Pooling.hpp"
#include "Options.hpp"
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-class CYStream :
- public std::istream
-{
- private:
- class CYBuffer :
- public std::streambuf
- {
- public:
- CYBuffer(const char *start, const char *end) {
- setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
- }
- } buffer_;
-
- public:
- CYStream(const char *start, const char *end) :
- std::istream(&buffer_),
- buffer_(start, end)
- {
- }
-};
-
struct CYForInitialiser {
virtual ~CYForInitialiser() {
}
struct CYString;
struct CYExpression :
- CYNext<CYExpression>,
CYForInitialiser,
CYForInInitialiser,
CYClassName,
CYThing
{
- virtual unsigned Precedence() const = 0;
+ virtual int Precedence() const = 0;
virtual bool RightHand() const {
return true;
virtual void Output(CYOutput &out) const;
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
- void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
+ void Output(CYOutput &out, int precedence, CYFlags flags) const;
virtual CYExpression *ClassName(CYContext &context, bool object);
virtual void ClassName(CYOutput &out, bool object) const;
virtual CYAssignment *Assignment(CYContext &context);
virtual CYExpression *Primitive(CYContext &context) {
- return this;
+ return NULL;
}
virtual CYNumber *Number(CYContext &context) {
}
#define CYPrecedence(value) \
- static const unsigned Precedence_ = value; \
- virtual unsigned Precedence() const { \
+ static const int Precedence_ = value; \
+ virtual int Precedence() const { \
return Precedence_; \
}
struct CYCompound :
CYExpression
{
- CYExpression *expressions_;
+ CYExpression *expression_;
+ CYExpression *next_;
- CYCompound(CYExpression *expressions = NULL) :
- expressions_(expressions)
+ CYCompound(CYExpression *expression, CYExpression *next = NULL) :
+ expression_(expression),
+ next_(next)
{
- }
-
- void AddPrev(CYExpression *expression) {
- CYSetLast(expression) = expressions_;
- expressions_ = expression;
+ if (expression_ == NULL)
+ throw;
+ _assert(expression_ != NULL);
}
CYPrecedence(17)
{
}
+ CYComprehension *Modify(CYComprehension *next) {
+ next_ = next;
+ return this;
+ }
+
virtual const char *Name() const = 0;
virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
{
CYExpression *test_;
- CYIfComprehension(CYExpression *test) :
+ CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
+ CYComprehension(next),
test_(test)
{
}
{
CYPrecedence(0)
CYRightHand(false)
+
+ virtual CYExpression *Primitive(CYContext &context) {
+ return this;
+ }
};
struct CYTrivial :
{
}
- virtual unsigned Precedence() const {
+ virtual int Precedence() const {
return arguments_ == NULL ? 2 : 1;
}
CYExpress(CYExpression *expression) :
expression_(expression)
{
- if (expression == NULL)
+ if (expression_ == NULL)
throw;
}
virtual void Output(CYOutput &out) const;
};
+struct CYTypeSpecifier :
+ CYThing
+{
+ virtual CYExpression *Replace(CYContext &context) = 0;
+};
+
+struct CYTypeError :
+ CYTypeSpecifier
+{
+ CYTypeError() {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeVoid :
+ CYTypeSpecifier
+{
+ CYTypeVoid() {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeVariable :
+ CYTypeSpecifier
+{
+ CYIdentifier *name_;
+
+ CYTypeVariable(CYIdentifier *name) :
+ name_(name)
+ {
+ }
+
+ CYTypeVariable(const char *name) :
+ name_(new($pool) CYIdentifier(name))
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeUnsigned :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeUnsigned(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeSigned :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeSigned(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeLong :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeLong(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeShort :
+ CYTypeSpecifier
+{
+ CYTypeSpecifier *specifier_;
+
+ CYTypeShort(CYTypeSpecifier *specifier) :
+ specifier_(specifier)
+ {
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeFunctionWith;
+
struct CYTypeModifier :
CYNext<CYTypeModifier>
{
{
}
- virtual CYExpression *Replace(CYContext &context) = 0;
+ virtual int Precedence() const = 0;
+
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
+ CYExpression *Replace(CYContext &context, CYExpression *type);
+
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
+ void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
+
+ virtual CYTypeFunctionWith *Function() { return NULL; }
};
struct CYTypeArrayOf :
{
}
- CYPrecedence(2)
+ CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
struct CYTypeConstant :
{
}
- CYPrecedence(3)
+ CYPrecedence(0)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
struct CYTypePointerTo :
{
}
- CYPrecedence(3)
+ CYPrecedence(0)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
-struct CYTypeVariable :
+struct CYTypeVolatile :
CYTypeModifier
{
- CYExpression *expression_;
-
- CYTypeVariable(CYExpression *expression) :
- CYTypeModifier(NULL),
- expression_(expression)
+ CYTypeVolatile(CYTypeModifier *next = NULL) :
+ CYTypeModifier(next)
{
}
- CYPrecedence(1)
+ CYPrecedence(0)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
struct CYTypedIdentifier :
CYNext<CYTypedIdentifier>,
CYThing
{
+ CYLocation location_;
CYIdentifier *identifier_;
- CYTypeModifier *type_;
+ CYTypeSpecifier *specifier_;
+ CYTypeModifier *modifier_;
- CYTypedIdentifier(CYIdentifier *identifier) :
+ CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
+ location_(location),
identifier_(identifier),
- type_(NULL)
+ specifier_(NULL),
+ modifier_(NULL)
+ {
+ }
+
+ CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
+ identifier_(NULL),
+ specifier_(specifier),
+ modifier_(modifier)
{
}
+ inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
+ CYSetLast(modifier_) = modifier;
+ return this;
+ }
+
+ virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
+
+ CYTypeFunctionWith *Function();
+};
+
+struct CYEncodedType :
+ CYExpression
+{
+ CYTypedIdentifier *typed_;
+
+ CYEncodedType(CYTypedIdentifier *typed) :
+ typed_(typed)
+ {
+ }
+
+ CYPrecedence(1)
+
+ virtual CYExpression *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYTypedParameter :
- CYNext<CYTypedParameter>
+ CYNext<CYTypedParameter>,
+ CYThing
{
CYTypedIdentifier *typed_;
{
}
+ CYArgument *Argument(CYContext &context);
CYFunctionParameter *Parameters(CYContext &context);
CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
+
+ virtual void Output(CYOutput &out) const;
};
struct CYLambda :
CYExpression
{
- CYTypeModifier *type_;
+ CYTypedIdentifier *typed_;
CYTypedParameter *parameters_;
CYStatement *statements_;
- CYLambda(CYTypeModifier *type, CYTypedParameter *parameters, CYStatement *statements) :
- type_(type),
+ CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
+ typed_(typed),
parameters_(parameters),
statements_(statements)
{
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYModule :
+ CYNext<CYModule>,
+ CYThing
+{
+ CYWord *part_;
+
+ CYModule(CYWord *part, CYModule *next = NULL) :
+ CYNext<CYModule>(next),
+ part_(part)
+ {
+ }
+
+ CYString *Replace(CYContext &context, const char *separator) const;
+ void Output(CYOutput &out) const;
+};
+
+struct CYImport :
+ CYStatement
+{
+ CYModule *module_;
+
+ CYImport(CYModule *module) :
+ module_(module)
+ {
+ }
+
+ virtual CYStatement *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYExternal :
+ CYStatement
+{
+ CYString *abi_;
+ CYTypedIdentifier *typed_;
+
+ CYExternal(CYString *abi, CYTypedIdentifier *typed) :
+ abi_(abi),
+ typed_(typed)
+ {
+ }
+
+ virtual CYStatement *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYTypeDefinition :
CYStatement
{
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYTypeBlockWith :
+ CYTypeModifier
+{
+ CYTypedParameter *parameters_;
+
+ CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
+ CYTypeModifier(next),
+ parameters_(parameters)
+ {
+ }
+
+ CYPrecedence(0)
+
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+};
+
+struct CYTypeFunctionWith :
+ CYTypeModifier
+{
+ CYTypedParameter *parameters_;
+
+ CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
+ CYTypeModifier(next),
+ parameters_(parameters)
+ {
+ }
+
+ CYPrecedence(1)
+
+ virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+
+ virtual CYTypeFunctionWith *Function() { return this; }
+};
+
namespace cy {
namespace Syntax {
CYPrefix_(false, "~", BitwiseNot)
CYPrefix_(false, "!", LogicalNot)
-CYInfix_(false, 5, "*", Multiply)
+CYInfix_(false, 5, "*", Multiply, CYReplace)
CYInfix_(false, 5, "/", Divide)
CYInfix_(false, 5, "%", Modulus)
CYInfix_(false, 6, "+", Add, CYReplace)