From 561e7f1c23ea3804db1625ab806be939b4e9c5d1 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 15 Sep 2012 04:34:50 -0700 Subject: [PATCH] Reorganize typed identifier storage for other uses. --- Cycript.yy.in | 38 ++++++++--------- ObjectiveC/Output.mm | 2 + ObjectiveC/Replace.cpp | 18 +++++++-- ObjectiveC/Syntax.hpp | 92 +++++++++++++++++++++++++++++++++++------- 4 files changed, 114 insertions(+), 36 deletions(-) diff --git a/Cycript.yy.in b/Cycript.yy.in index b6f355c..90588f7 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -73,7 +73,6 @@ typedef struct { CYMember *member_; CYNull *null_; CYNumber *number_; - CYEncodedPart *part_; CYProgram *program_; CYProperty *property_; CYPropertyName *propertyName_; @@ -82,7 +81,8 @@ typedef struct { CYString *string_; CYThis *this_; CYTrue *true_; - CYEncodedType *type_; + CYTypeModifier *type_; + CYTypedIdentifier *typed_; CYWord *word_; @begin ObjectiveC @@ -471,6 +471,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ClassProtocolListOpt %type ClassProtocols %type ClassProtocolsOpt +%type EncodedType %type MessageExpression %type MessageParameter %type MessageParameters @@ -478,8 +479,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type MessageParameterListOpt %type MessageScope %type ModifiedType -%type NamedTypeOpt -%type PrefixedType +%type PrefixedType %type SelectorCall_ %type SelectorCall %type SelectorExpression_ @@ -487,9 +487,9 @@ int cylex(YYSTYPE *, cy::location *, void *); %type SelectorExpressionOpt %type SelectorList %type SelectorWordOpt -%type SuffixedType +%type SuffixedType %type TypeOpt -%type TypedIdentifier +%type TypedIdentifier %type VariadicCall @end @@ -1340,33 +1340,33 @@ ProgramBodyOpt @begin ObjectiveC /* Cycript (Objective-C): Type Encoding {{{ */ -NamedTypeOpt - : Identifier { $$ = CYNew CYEncodedPart(NULL, "withName", CYNew CYArgument(CYNew CYString($1->word_))); } - | { $$ = NULL; } - ; - SuffixedType - : NamedTypeOpt { $$ = $1; } + : IdentifierOpt { $$ = CYNew CYTypedIdentifier($1); } | "(" PrefixedType ")" { $$ = $2; } - | SuffixedType "[" NumericLiteral "]" { $$ = CYNew CYEncodedPart($1, "arrayOf", CYNew CYArgument($3)); } + | SuffixedType "[" NumericLiteral "]" { CYSetLast($1->type_) = CYNew CYTypeArrayOf($3->Value()); $$ = $1; } ; PrefixedType : SuffixedType { $$ = $1; } - | "*" PrefixedType { $$ = CYNew CYEncodedPart($2, "pointerTo"); } + | "const" PrefixedType { CYSetLast($2->type_) = CYNew CYTypeConstant(); $$ = $2; } + | "*" PrefixedType { CYSetLast($2->type_) = CYNew CYTypePointerTo(); $$ = $2; } ; ModifiedType - : Variable { $$ = CYNew CYEncodedType($1); } - | "const" ModifiedType { $2->base_ = CYNew CYCall(CYNew CYDirectMember($2->base_, CYNew CYString("constant"))); $$ = $2; } + : Variable { $$ = CYNew CYTypeVariable($1); } + | "const" ModifiedType { $$ = CYNew CYTypeConstant($2); } ; TypedIdentifier - : ModifiedType PrefixedType { $1->parts_ = $2; $$ = $1;} + : ModifiedType PrefixedType { CYSetLast($2->type_) = $1; $$ = $2;} + ; + +EncodedType + : TypedIdentifier { $$ = CYNew CYEncodedType($1->type_); } ; PrimaryExpression - : AtEncode "(" TypedIdentifier ")" { $$ = $3; } + : AtEncode "(" EncodedType ")" { $$ = $3; } ; /* }}} */ /* Cycript (Objective-C): @class Declaration {{{ */ @@ -1391,7 +1391,7 @@ MessageScope ; TypeOpt - : "(" TypedIdentifier ")" { $$ = $2; } + : "(" EncodedType ")" { $$ = $2; } | "(" LexSetRegExp "void" ")" { $$ = CYNew CYString("v"); } | { $$ = NULL; } ; diff --git a/ObjectiveC/Output.mm b/ObjectiveC/Output.mm index c90418d..ecc8d1a 100644 --- a/ObjectiveC/Output.mm +++ b/ObjectiveC/Output.mm @@ -76,7 +76,9 @@ void CYClassStatement::Output(CYOutput &out, CYFlags flags) const { } void CYEncodedType::Output(CYOutput &out, CYFlags flags) const { + out << "@encode("; // XXX: this is seriously wrong + out << ")"; } void CYField::Output(CYOutput &out) const { diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index f6e98f1..a8515a5 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -78,12 +78,24 @@ CYStatement *CYClassStatement::Replace(CYContext &context) { return $E(Replace_(context)); } -CYExpression *CYEncodedPart::Replace(CYContext &context, CYExpression *base) { $T(base) - return next_->Replace(context, $ CYCall($ CYDirectMember(base, $ CYString(name_)), arguments_)); +CYExpression *CYTypeArrayOf::Replace(CYContext &context) { + return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("arrayOf")), $ CYArgument($ CYNumber(size_))); +} + +CYExpression *CYTypeConstant::Replace(CYContext &context) { + return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("constant"))); +} + +CYExpression *CYTypePointerTo::Replace(CYContext &context) { + return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("pointerTo"))); +} + +CYExpression *CYTypeVariable::Replace(CYContext &context) { + return expression_; } CYExpression *CYEncodedType::Replace(CYContext &context) { - return parts_->Replace(context, base_); + return type_->Replace(context); } CYStatement *CYField::Replace(CYContext &context) const { $T(NULL) diff --git a/ObjectiveC/Syntax.hpp b/ObjectiveC/Syntax.hpp index 0caaad4..e910154 100644 --- a/ObjectiveC/Syntax.hpp +++ b/ObjectiveC/Syntax.hpp @@ -24,31 +24,95 @@ #include "Parser.hpp" -struct CYEncodedPart : - CYNext +struct CYTypeModifier : + CYNext { - const char *name_; - CYArgument *arguments_; + CYTypeModifier(CYTypeModifier *next) : + CYNext(next) + { + } - CYEncodedPart(CYEncodedPart *next, const char *name, CYArgument *arguments = NULL) : - CYNext(next), - name_(name), - arguments_(arguments) + virtual CYExpression *Replace(CYContext &context) = 0; +}; + +struct CYTypeArrayOf : + CYTypeModifier +{ + size_t size_; + + CYTypeArrayOf(size_t size, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + size_(size) + { + } + + CYPrecedence(2) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypeConstant : + CYTypeModifier +{ + CYTypeConstant(CYTypeModifier *next = NULL) : + CYTypeModifier(next) { } - CYExpression *Replace(CYContext &context, CYExpression *base); + CYPrecedence(3) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypePointerTo : + CYTypeModifier +{ + CYTypePointerTo(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + CYPrecedence(3) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypeVariable : + CYTypeModifier +{ + CYExpression *expression_; + + CYTypeVariable(CYExpression *expression) : + CYTypeModifier(NULL), + expression_(expression) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypedIdentifier : + CYNext +{ + CYIdentifier *identifier_; + CYTypeModifier *type_; + + CYTypedIdentifier(CYIdentifier *identifier) : + identifier_(identifier), + type_(NULL) + { + } }; struct CYEncodedType : CYExpression { - CYExpression *base_; - CYEncodedPart *parts_; + CYTypeModifier *type_; - CYEncodedType(CYExpression *base, CYEncodedPart *parts = NULL) : - base_(base), - parts_(parts) + CYEncodedType(CYTypeModifier *type) : + type_(type) { } -- 2.45.2