From: Jay Freeman (saurik) Date: Mon, 6 Jan 2014 13:59:44 +0000 (-0800) Subject: Add syntax to support C-style typedef assignment. X-Git-Tag: v0.9.500~102 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/6009702382b8710a9abf6fd62c1f30e991d6986e Add syntax to support C-style typedef assignment. --- diff --git a/Cycript.l.in b/Cycript.l.in index cb422cc..5915fc9 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -257,6 +257,10 @@ XMLName {XMLNameStart}{XMLNamePart}* "@class" L C F(tk::AtClass, hi::Meta); @end +@begin C +"typedef" L C I(identifier, Identifier("typedef"), tk::Typedef, hi::Meta); +@end + @begin ObjectiveC "@encode" L C F(tk::AtEncode, hi::Meta); "@end" L C F(tk::AtEnd, hi::Meta); diff --git a/Cycript.yy.in b/Cycript.yy.in index 8fd76aa..8adbbf5 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -230,6 +230,10 @@ int cylex(YYSTYPE *, cy::location *, void *); %token AtClass "@class" @end +@begin C +%token Typedef "typedef" +@end + @begin ObjectiveC %token AtImplementation "@implementation" %token AtImplementation_ ";@implementation" @@ -645,6 +649,10 @@ WordOpt Identifier : Identifier_ { $$ = $1; } +@begin C + | "typedef" { $$ = $1; } +@end + | "implements" { $$ = $1; } | "interface" { $$ = $1; } | "package" { $$ = $1; } @@ -1623,6 +1631,11 @@ PrimaryExpression : "[" LexPushInOff LexSetRegExp "=" "]" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn "->" ModifiedType BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYLambda($13, $9, $16); } ; /* }}} */ +/* Cycript (C): Type Definitions {{{ */ +Statement__ + : "typedef" TypedIdentifier Terminator { $$ = CYNew CYTypeDefinition($2); } + ; +/* }}} */ @end /* YUI: Documentation Comments {{{ */ diff --git a/Output.cpp b/Output.cpp index 084bf71..3bf157f 100644 --- a/Output.cpp +++ b/Output.cpp @@ -474,6 +474,11 @@ void CYLabel::Output(CYOutput &out, CYFlags flags) const { statement_->Single(out, CYRight(flags)); } +void CYTypedIdentifier::Output(CYOutput &out) const { + // XXX: this is clearly wrong + out << "XXX"; +} + void CYLambda::Output(CYOutput &out, CYFlags flags) const { // XXX: this is seriously wrong out << "[]("; @@ -482,6 +487,10 @@ void CYLambda::Output(CYOutput &out, CYFlags flags) const { out << "}"; } +void CYTypeDefinition::Output(CYOutput &out, CYFlags flags) const { + out << "typedef" << *typed_; +} + 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 e120123..110cf0d 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1670,7 +1670,8 @@ struct CYTypeVariable : }; struct CYTypedIdentifier : - CYNext + CYNext, + CYThing { CYIdentifier *identifier_; CYTypeModifier *type_; @@ -1680,6 +1681,8 @@ struct CYTypedIdentifier : type_(NULL) { } + + virtual void Output(CYOutput &out) const; }; struct CYTypedParameter : @@ -1717,6 +1720,20 @@ struct CYLambda : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYTypeDefinition : + CYStatement +{ + CYTypedIdentifier *typed_; + + CYTypeDefinition(CYTypedIdentifier *typed) : + typed_(typed) + { + } + + virtual CYStatement *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + namespace cy { namespace Syntax { diff --git a/Replace.cpp b/Replace.cpp index 7a499f7..a4a31ea 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -873,6 +873,10 @@ CYExpression *CYTypeConstant::Replace(CYContext &context) { return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("constant"))); } +CYStatement *CYTypeDefinition::Replace(CYContext &context) { + return $E($ CYAssign($V(typed_->identifier_), typed_->type_->Replace(context))); +} + CYExpression *CYTypePointerTo::Replace(CYContext &context) { return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("pointerTo"))); }