-/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2015 Jay Freeman (saurik)
+/* Cycript - The Truly Universal Scripting Language
+ * Copyright (C) 2009-2016 Jay Freeman (saurik)
*/
/* GNU Affero General Public License, Version 3 {{{ */
double CYCastDouble(CYUTF8String value);
void CYNumerify(std::ostringstream &str, double value);
-void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c = false);
+
+enum CYStringifyMode {
+ CYStringifyModeLegacy,
+ CYStringifyModeCycript,
+ CYStringifyModeNative,
+};
+
+void CYStringify(std::ostringstream &str, const char *data, size_t size, CYStringifyMode mode);
// XXX: this really should not be here ... :/
void *CYPoolFile(CYPool &pool, const char *path, size_t *psize);
struct CYExpression;
struct CYAssignment;
+struct CYIdentifier;
+struct CYNumber;
struct CYPropertyName {
virtual bool Computed() const {
return false;
}
+ virtual CYIdentifier *Identifier() {
+ return NULL;
+ }
+
+ virtual CYNumber *Number(CYContext &context) {
+ return NULL;
+ }
+
virtual CYExpression *PropertyName(CYContext &context) = 0;
virtual void PropertyName(CYOutput &out) const = 0;
};
CYNoRightHand = (1 << 5),
CYNoDangle = (1 << 6),
CYNoInteger = (1 << 7),
+ CYNoColon = (1 << 8),
CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
};
{
}
+ CYIdentifier *Identifier() override {
+ return this;
+ }
+
virtual const char *Word() const;
CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
};
return false;
}
+ virtual bool IsNew() const {
+ return false;
+ }
+
virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
virtual CYTarget *Replace(CYContext &context) = 0;
return value_;
}
+ virtual CYIdentifier *Identifier() const;
virtual const char *Word() const;
virtual CYNumber *Number(CYContext &context);
CYPrecedence(0)
+ virtual CYString *String(CYContext &context);
+
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
virtual CYFunctionParameter *Parameter() const;
};
+struct CYSymbol :
+ CYTarget
+{
+ const char *name_;
+
+ CYSymbol(const char *name) :
+ name_(name)
+ {
+ }
+
+ CYPrecedence(0)
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYPrefix :
CYExpression
{
};
struct CYElement :
+ CYNext<CYElement>,
CYThing
{
+ CYElement(CYElement *next) :
+ CYNext<CYElement>(next)
+ {
+ }
+
virtual bool Elision() const = 0;
virtual void Replace(CYContext &context) = 0;
};
struct CYElementValue :
- CYNext<CYElement>,
CYElement
{
CYExpression *value_;
CYElementValue(CYExpression *value, CYElement *next = NULL) :
- CYNext<CYElement>(next),
+ CYElement(next),
value_(value)
{
}
{
CYExpression *value_;
- CYElementSpread(CYExpression *value) :
+ CYElementSpread(CYExpression *value, CYElement *next = NULL) :
+ CYElement(next),
value_(value)
{
}
{
}
+ CYTarget *Replace(CYContext &context, CYTarget *seed);
+
virtual CYTarget *Replace(CYContext &context);
void Output(CYOutput &out, CYFlags flags) const;
};
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYAttemptMember :
+ CYMember
+{
+ CYAttemptMember(CYExpression *object, CYExpression *property) :
+ CYMember(object, property)
+ {
+ }
+
+ CYPrecedence(1)
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYIndirectMember :
CYMember
{
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYResolveMember :
+ CYMember
+{
+ CYResolveMember(CYExpression *object, CYExpression *property) :
+ CYMember(object, property)
+ {
+ }
+
+ CYPrecedence(1)
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYSubscriptMember :
+ CYMember
+{
+ CYSubscriptMember(CYExpression *object, CYExpression *property) :
+ CYMember(object, property)
+ {
+ }
+
+ CYPrecedence(1)
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
namespace cy {
namespace Syntax {
return arguments_ == NULL ? 2 : 1;
}
+ virtual bool IsNew() const {
+ return true;
+ }
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
struct CYRubyProc;
-struct CYRubyBlock :
+struct CYBraced :
CYTarget
{
- CYExpression *call_;
- CYRubyProc *proc_;
+ CYTarget *lhs_;
- CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
- call_(call),
- proc_(proc)
+ CYBraced(CYTarget *lhs = NULL) :
+ lhs_(lhs)
{
}
CYPrecedence(1)
+ void SetLeft(CYTarget *lhs) {
+ lhs_ = lhs;
+ }
+};
+
+struct CYRubyBlock :
+ CYBraced
+{
+ CYRubyProc *proc_;
+
+ CYRubyBlock(CYTarget *lhs, CYRubyProc *proc) :
+ CYBraced(lhs),
+ proc_(proc)
+ {
+ }
+
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
};
+struct CYExtend :
+ CYBraced
+{
+ CYObject object_;
+
+ CYExtend(CYTarget *lhs, CYProperty *properties = NULL) :
+ CYBraced(lhs),
+ object_(properties)
+ {
+ }
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
struct CYIf :
CYStatement
{
virtual void Output(CYOutput &out) const;
};
-struct CYTypeVoid :
+enum CYTypeSigning {
+ CYTypeNeutral,
+ CYTypeSigned,
+ CYTypeUnsigned,
+};
+
+struct CYTypeCharacter :
CYTypeSpecifier
{
- CYTypeVoid() {
+ CYTypeSigning signing_;
+
+ CYTypeCharacter(CYTypeSigning signing) :
+ signing_(signing)
+ {
}
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
-struct CYTypeReference :
+struct CYTypeInt128 :
CYTypeSpecifier
{
- CYIdentifier *name_;
+ CYTypeSigning signing_;
- CYTypeReference(CYIdentifier *name) :
- name_(name)
+ CYTypeInt128(CYTypeSigning signing) :
+ signing_(signing)
{
}
virtual void Output(CYOutput &out) const;
};
-struct CYTypeVariable :
+struct CYTypeIntegral :
CYTypeSpecifier
{
- CYIdentifier *name_;
+ CYTypeSigning signing_;
+ int length_;
- CYTypeVariable(CYIdentifier *name) :
- name_(name)
+ CYTypeIntegral(CYTypeSigning signing, int length = 1) :
+ signing_(signing),
+ length_(length)
{
}
- CYTypeVariable(const char *name) :
- name_(new($pool) CYIdentifier(name))
- {
+ CYTypeIntegral *Long() {
+ if (length_ != 1 && length_ != 2)
+ return NULL;
+ ++length_;
+ return this;
+ }
+
+ CYTypeIntegral *Short() {
+ if (length_ != 1)
+ return NULL;
+ --length_;
+ return this;
+ }
+
+ CYTypeIntegral *Signed() {
+ if (signing_ != CYTypeNeutral)
+ return NULL;
+ signing_ = CYTypeSigned;
+ return this;
+ }
+
+ CYTypeIntegral *Unsigned() {
+ if (signing_ != CYTypeNeutral)
+ return NULL;
+ signing_ = CYTypeUnsigned;
+ return this;
}
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
-struct CYTypeUnsigned :
+struct CYTypeFloating :
CYTypeSpecifier
{
- CYTypeSpecifier *specifier_;
+ int length_;
- CYTypeUnsigned(CYTypeSpecifier *specifier) :
- specifier_(specifier)
+ CYTypeFloating(int length) :
+ length_(length)
{
}
virtual void Output(CYOutput &out) const;
};
-struct CYTypeSigned :
+struct CYTypeVoid :
CYTypeSpecifier
{
- CYTypeSpecifier *specifier_;
-
- CYTypeSigned(CYTypeSpecifier *specifier) :
- specifier_(specifier)
- {
+ CYTypeVoid() {
}
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
-struct CYTypeLong :
+enum CYTypeReferenceKind {
+ CYTypeReferenceStruct,
+ CYTypeReferenceEnum,
+};
+
+struct CYTypeReference :
CYTypeSpecifier
{
- CYTypeSpecifier *specifier_;
+ CYTypeReferenceKind kind_;
+ CYIdentifier *name_;
- CYTypeLong(CYTypeSpecifier *specifier) :
- specifier_(specifier)
+ CYTypeReference(CYTypeReferenceKind kind, CYIdentifier *name) :
+ kind_(kind),
+ name_(name)
{
}
virtual void Output(CYOutput &out) const;
};
-struct CYTypeShort :
+struct CYTypeVariable :
CYTypeSpecifier
{
- CYTypeSpecifier *specifier_;
+ CYIdentifier *name_;
- CYTypeShort(CYTypeSpecifier *specifier) :
- specifier_(specifier)
+ CYTypeVariable(CYIdentifier *name) :
+ name_(name)
+ {
+ }
+
+ CYTypeVariable(const char *name) :
+ name_(new($pool) CYIdentifier(name))
{
}
virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
CYTarget *Replace(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
- void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
+ virtual void Output(CYOutput &out, CYPropertyName *name) const = 0;
+ void Output(CYOutput &out, int precedence, CYPropertyName *name, bool space) const;
virtual CYTypeFunctionWith *Function() { return NULL; }
};
CYPrecedence(1)
virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+ void Output(CYOutput &out, CYPropertyName *name) const override;
};
struct CYTypeConstant :
CYPrecedence(0)
virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+ void Output(CYOutput &out, CYPropertyName *name) const override;
};
struct CYTypePointerTo :
CYPrecedence(0)
virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+ void Output(CYOutput &out, CYPropertyName *name) const override;
};
struct CYTypeVolatile :
CYPrecedence(0)
virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+ void Output(CYOutput &out, CYPropertyName *name) const override;
};
-struct CYTypedIdentifier :
- CYNext<CYTypedIdentifier>,
+struct CYType :
CYThing
{
- CYLocation location_;
- CYIdentifier *identifier_;
CYTypeSpecifier *specifier_;
CYTypeModifier *modifier_;
- CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
- location_(location),
- identifier_(identifier),
- specifier_(NULL),
- modifier_(NULL)
- {
- }
-
- CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
- identifier_(NULL),
+ CYType(CYTypeSpecifier *specifier = NULL, CYTypeModifier *modifier = NULL) :
specifier_(specifier),
modifier_(modifier)
{
}
- inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
+ inline CYType *Modify(CYTypeModifier *modifier) {
CYSetLast(modifier_) = modifier;
return this;
}
+ void Output(CYOutput &out, CYPropertyName *name) const;
+
virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
CYTypeFunctionWith *Function();
};
+struct CYTypedLocation :
+ CYType
+{
+ CYLocation location_;
+
+ CYTypedLocation(const CYLocation &location) :
+ location_(location)
+ {
+ }
+};
+
+struct CYTypedName :
+ CYTypedLocation
+{
+ CYPropertyName *name_;
+
+ CYTypedName(const CYLocation &location, CYPropertyName *name = NULL) :
+ CYTypedLocation(location),
+ name_(name)
+ {
+ }
+};
+
struct CYEncodedType :
CYTarget
{
- CYTypedIdentifier *typed_;
+ CYType *typed_;
- CYEncodedType(CYTypedIdentifier *typed) :
+ CYEncodedType(CYType *typed) :
typed_(typed)
{
}
CYNext<CYTypedParameter>,
CYThing
{
- CYTypedIdentifier *typed_;
+ CYType *type_;
+ CYIdentifier *name_;
- CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
+ CYTypedParameter(CYType *type, CYIdentifier *name, CYTypedParameter *next = NULL) :
CYNext<CYTypedParameter>(next),
- typed_(typed)
+ type_(type),
+ name_(name)
{
}
virtual void Output(CYOutput &out) const;
};
+struct CYTypedFormal {
+ bool variadic_;
+ CYTypedParameter *parameters_;
+
+ CYTypedFormal(bool variadic) :
+ variadic_(variadic),
+ parameters_(NULL)
+ {
+ }
+};
+
struct CYLambda :
CYTarget
{
- CYTypedIdentifier *typed_;
+ CYType *typed_;
CYTypedParameter *parameters_;
CYStatement *code_;
- CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
+ CYLambda(CYType *typed, CYTypedParameter *parameters, CYStatement *code) :
typed_(typed),
parameters_(parameters),
code_(code)
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-struct CYExternal :
+struct CYExternalExpression :
+ CYTarget
+{
+ CYString *abi_;
+ CYType *type_;
+ CYPropertyName *name_;
+
+ CYExternalExpression(CYString *abi, CYType *type, CYPropertyName *name) :
+ abi_(abi),
+ type_(type),
+ name_(name)
+ {
+ }
+
+ CYPrecedence(0)
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYExternalDefinition :
CYStatement
{
CYString *abi_;
- CYTypedIdentifier *typed_;
+ CYType *type_;
+ CYIdentifier *name_;
- CYExternal(CYString *abi, CYTypedIdentifier *typed) :
+ CYExternalDefinition(CYString *abi, CYType *type, CYIdentifier *name) :
abi_(abi),
- typed_(typed)
+ type_(type),
+ name_(name)
{
}
struct CYTypeExpression :
CYTarget
{
- CYTypedIdentifier *typed_;
+ CYType *typed_;
- CYTypeExpression(CYTypedIdentifier *typed) :
+ CYTypeExpression(CYType *typed) :
typed_(typed)
{
}
struct CYTypeDefinition :
CYStatement
{
- CYTypedIdentifier *typed_;
+ CYType *type_;
+ CYIdentifier *name_;
- CYTypeDefinition(CYTypedIdentifier *typed) :
- typed_(typed)
+ CYTypeDefinition(CYType *type, CYIdentifier *name) :
+ type_(type),
+ name_(name)
{
}
CYPrecedence(0)
virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+ void Output(CYOutput &out, CYPropertyName *name) const override;
};
struct CYTypeFunctionWith :
CYTypeModifier
{
+ bool variadic_;
CYTypedParameter *parameters_;
- CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
+ CYTypeFunctionWith(bool variadic, CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
CYTypeModifier(next),
+ variadic_(variadic),
parameters_(parameters)
{
}
CYPrecedence(1)
virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
- virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+ void Output(CYOutput &out, CYPropertyName *name) const override;
virtual CYTypeFunctionWith *Function() { return this; }
};
struct CYTypeStructField :
CYNext<CYTypeStructField>
{
- CYTypedIdentifier *typed_;
+ CYType *type_;
+ CYPropertyName *name_;
- CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) :
+ CYTypeStructField(CYType *type, CYPropertyName *name, CYTypeStructField *next = NULL) :
CYNext<CYTypeStructField>(next),
- typed_(typed)
+ type_(type),
+ name_(name)
{
}
};
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+struct CYEnumConstant :
+ CYNext<CYEnumConstant>
+{
+ CYIdentifier *name_;
+ CYNumber *value_;
+
+ CYEnumConstant(CYIdentifier *name, CYNumber *value, CYEnumConstant *next = NULL) :
+ CYNext<CYEnumConstant>(next),
+ name_(name),
+ value_(value)
+ {
+ }
+};
+
+struct CYTypeEnum :
+ CYTypeSpecifier
+{
+ CYIdentifier *name_;
+ CYTypeSpecifier *specifier_;
+ CYEnumConstant *constants_;
+
+ CYTypeEnum(CYIdentifier *name, CYTypeSpecifier *specifier, CYEnumConstant *constants) :
+ name_(name),
+ specifier_(specifier),
+ constants_(constants)
+ {
+ }
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out) const;
+};
+
namespace cy {
namespace Syntax {