From: Jay Freeman (saurik) Date: Sun, 5 Jan 2014 12:24:57 +0000 (-0800) Subject: Add support for C++11 lambda expression syntax. X-Git-Tag: v0.9.500~103 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/690cf1a8442f51ef6d03d9a0a4fb4ee2e9bb714d?hp=b6ace22552e41cb05d69eb0325a91dc0b7aa1d40 Add support for C++11 lambda expression syntax. --- diff --git a/Cycript.yy.in b/Cycript.yy.in index 53673a2..8fd76aa 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -1577,20 +1577,6 @@ PrimaryExpression ; /* }}} */ /* Cycript (Objective-C): Block Expressions {{{ */ -TypedParameterList_ - : "," TypedParameterList { $$ = $2; } - | { $$ = NULL; } - ; - -TypedParameterList - : TypedIdentifier TypedParameterList_ { $$ = CYNew CYTypedParameter($1, $2); } - ; - -TypedParameterListOpt - : TypedParameterList { $$ = $1; } - | { $$ = NULL; } - ; - PrimaryExpression : "^" ModifiedType "(" LexPushInOff TypedParameterListOpt ")" LexPopIn BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYObjCBlock($2, $5, $10); } ; @@ -1618,6 +1604,25 @@ Var_ : "auto" ; /* }}} */ +/* Cycript (C): Lambda Expressions {{{ */ +TypedParameterList_ + : "," TypedParameterList { $$ = $2; } + | { $$ = NULL; } + ; + +TypedParameterList + : TypedIdentifier TypedParameterList_ { $$ = CYNew CYTypedParameter($1, $2); } + ; + +TypedParameterListOpt + : TypedParameterList { $$ = $1; } + | { $$ = NULL; } + ; + +PrimaryExpression + : "[" LexPushInOff LexSetRegExp "=" "]" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn "->" ModifiedType BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYLambda($13, $9, $16); } + ; +/* }}} */ @end /* YUI: Documentation Comments {{{ */ diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index 9197a26..7c47eea 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -78,22 +78,6 @@ CYStatement *CYClassStatement::Replace(CYContext &context) { return $E(Replace_(context)); } -CYExpression *CYTypeArrayOf::Replace(CYContext &context) { - return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("arrayOf")), $ CYArgument(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 type_->Replace(context); } @@ -222,11 +206,3 @@ CYExpression *CYSendDirect::Replace(CYContext &context) { CYExpression *CYSendSuper::Replace(CYContext &context) { return $ CYSendDirect($V("$cyr"), arguments_); } - -CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL) - return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context)); -} - -CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix) - return next_->TypeSignature(context, $ CYAdd(prefix, typed_->type_->Replace(context))); -} diff --git a/ObjectiveC/Syntax.hpp b/ObjectiveC/Syntax.hpp index 92e4363..343cb37 100644 --- a/ObjectiveC/Syntax.hpp +++ b/ObjectiveC/Syntax.hpp @@ -24,103 +24,6 @@ #include "Parser.hpp" -struct CYTypeModifier : - CYNext -{ - CYTypeModifier(CYTypeModifier *next) : - CYNext(next) - { - } - - virtual CYExpression *Replace(CYContext &context) = 0; -}; - -struct CYTypeArrayOf : - CYTypeModifier -{ - CYExpression *size_; - - CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) : - CYTypeModifier(next), - size_(size) - { - } - - CYPrecedence(2) - - virtual CYExpression *Replace(CYContext &context); -}; - -struct CYTypeConstant : - CYTypeModifier -{ - CYTypeConstant(CYTypeModifier *next = NULL) : - CYTypeModifier(next) - { - } - - 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 CYTypedParameter : - CYNext -{ - CYTypedIdentifier *typed_; - - CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : - CYNext(next), - typed_(typed) - { - } - - CYFunctionParameter *Parameters(CYContext &context); - CYExpression *TypeSignature(CYContext &context, CYExpression *prefix); -}; - struct CYObjCBlock : CYExpression { diff --git a/Output.cpp b/Output.cpp index d1d033c..084bf71 100644 --- a/Output.cpp +++ b/Output.cpp @@ -474,6 +474,14 @@ void CYLabel::Output(CYOutput &out, CYFlags flags) const { statement_->Single(out, CYRight(flags)); } +void CYLambda::Output(CYOutput &out, CYFlags flags) const { + // XXX: this is seriously wrong + out << "[]("; + out << ")->"; + out << "{"; + out << "}"; +} + void CYLetStatement::Output(CYOutput &out, CYFlags flags) const { out << "let" << ' ' << '(' << *declarations_ << ')'; code_->Single(out, CYRight(flags)); diff --git a/Parser.hpp b/Parser.hpp index 2102091..e120123 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1600,6 +1600,123 @@ struct CYFinally : virtual void Output(CYOutput &out) const; }; +struct CYTypeModifier : + CYNext +{ + CYTypeModifier(CYTypeModifier *next) : + CYNext(next) + { + } + + virtual CYExpression *Replace(CYContext &context) = 0; +}; + +struct CYTypeArrayOf : + CYTypeModifier +{ + CYExpression *size_; + + CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) : + CYTypeModifier(next), + size_(size) + { + } + + CYPrecedence(2) + + virtual CYExpression *Replace(CYContext &context); +}; + +struct CYTypeConstant : + CYTypeModifier +{ + CYTypeConstant(CYTypeModifier *next = NULL) : + CYTypeModifier(next) + { + } + + 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 CYTypedParameter : + CYNext +{ + CYTypedIdentifier *typed_; + + CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) : + CYNext(next), + typed_(typed) + { + } + + CYFunctionParameter *Parameters(CYContext &context); + CYExpression *TypeSignature(CYContext &context, CYExpression *prefix); +}; + +struct CYLambda : + CYExpression +{ + CYTypeModifier *type_; + CYTypedParameter *parameters_; + CYStatement *statements_; + + CYLambda(CYTypeModifier *type, CYTypedParameter *parameters, CYStatement *statements) : + type_(type), + parameters_(parameters), + statements_(statements) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + namespace cy { namespace Syntax { diff --git a/Replace.cpp b/Replace.cpp index de404d5..7a499f7 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -521,6 +521,10 @@ CYStatement *CYLabel::Replace(CYContext &context) { return this; } +CYExpression *CYLambda::Replace(CYContext &context) { + return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), statements_), parameters_->TypeSignature(context, type_->Replace(context))); +} + CYStatement *CYLetStatement::Replace(CYContext &context) { return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context))); } @@ -861,6 +865,30 @@ CYStatement *Try::Replace(CYContext &context) { } } +CYExpression *CYTypeArrayOf::Replace(CYContext &context) { + return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("arrayOf")), $ CYArgument(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_; +} + +CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL) + return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context)); +} + +CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix) + return next_->TypeSignature(context, $ CYAdd(prefix, typed_->type_->Replace(context))); +} + CYStatement *CYVar::Replace(CYContext &context) { declarations_->Replace(context); return $E(declarations_->Compound(context));