X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/0a35647487f508be5a48feaf18734086e6d460d4..6c093cce11bcc8875e4459b3670811227e8dafa7:/Parser.hpp?ds=sidebyside diff --git a/Parser.hpp b/Parser.hpp index 6bf18a7..49e0722 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -50,6 +50,7 @@ #include #include +#include #include #include "location.hh" @@ -244,11 +245,13 @@ struct CYIdentifier : { CYIdentifier *replace_; size_t offset_; + size_t usage_; CYIdentifier(const char *word) : CYWord(word), replace_(NULL), - offset_(0) + offset_(0), + usage_(0) { } @@ -311,13 +314,19 @@ enum CYIdentifierFlags { typedef std::set CYCStringSet; typedef std::set CYIdentifierValueSet; -typedef std::vector CYIdentifierAddressVector; typedef std::map CYIdentifierAddressFlagsMap; +struct CYIdentifierUsage { + CYIdentifier *identifier_; + size_t usage_; +}; + +typedef std::vector CYIdentifierUsageVector; + struct CYScope { CYScope *parent_; - CYIdentifierAddressFlagsMap internal_; + CYIdentifierAddressFlagsMap internal_; CYIdentifierValueSet identifiers_; CYScope() : @@ -335,11 +344,9 @@ struct CYScope { }; struct CYProgram : - CYScope, CYThing { CYStatement *statements_; - CYIdentifierAddressVector rename_; CYProgram(CYStatement *statements) : statements_(statements) @@ -350,19 +357,16 @@ struct CYProgram : virtual void Output(CYOutput &out) const; }; -struct CYContext : - CYScope -{ +struct CYContext { apr_pool_t *pool_; CYOptions &options_; CYScope *scope_; - CYProgram *program_; + CYIdentifierUsageVector rename_; CYContext(apr_pool_t *pool, CYOptions &options) : pool_(pool), options_(options), - scope_(this), - program_(NULL) + scope_(NULL) { } @@ -387,11 +391,9 @@ struct CYBlock : CYThing { CYStatement *statements_; - CYScope *scope_; - CYBlock(CYStatement *statements, CYScope *scope = NULL) : - statements_(statements), - scope_(scope) + CYBlock(CYStatement *statements) : + statements_(statements) { } @@ -451,12 +453,39 @@ class CYDriver { CYProgram *program_; Errors errors_; + bool auto_; + + struct Context { + CYExpression *context_; + + Context(CYExpression *context) : + context_(context) + { + } + + typedef std::vector Words; + Words words_; + }; + + typedef std::vector Contexts; + Contexts contexts_; + + CYExpression *context_; + + enum Mode { + AutoNone, + AutoPrimary, + AutoDirect, + AutoIndirect, + AutoMessage + } mode_; + private: void ScannerInit(); void ScannerDestroy(); public: - CYDriver(const std::string &filename); + CYDriver(apr_pool_t *pool = NULL, const std::string &filename = ""); ~CYDriver(); Condition GetCondition(); @@ -508,6 +537,8 @@ struct CYExpression : virtual const char *ForEachIn() const; virtual CYExpression *ForEachIn(CYContext &out); + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); + virtual void Output(CYOutput &out) const; virtual void Output(CYOutput &out, CYFlags flags) const = 0; void Output(CYOutput &out, unsigned precedence, CYFlags flags) const; @@ -587,7 +618,22 @@ struct CYFunctionParameter : { } - void Replace(CYContext &context); + virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); + virtual void Output(CYOutput &out) const; +}; + +struct CYOptionalFunctionParameter : + CYFunctionParameter +{ + CYExpression *initializer_; + + CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) : + CYFunctionParameter(name, next), + initializer_(initializer) + { + } + + virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code); virtual void Output(CYOutput &out) const; }; @@ -1293,6 +1339,8 @@ struct CYNew : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); }; struct CYCall : @@ -1312,6 +1360,29 @@ struct CYCall : virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; + + virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); +}; + +struct CYRubyProc; + +struct CYRubyBlock : + CYExpression +{ + CYExpression *call_; + CYRubyProc *proc_; + + CYRubyBlock(CYExpression *call, CYRubyProc *proc) : + call_(call), + proc_(proc) + { + } + + CYPrecedence(1) + CYRightHand(false) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; }; struct CYIf : @@ -1364,9 +1435,8 @@ struct CYWhile : virtual void Output(CYOutput &out, CYFlags flags) const; }; -struct CYFunction : - CYScope -{ +// XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass) +struct CYFunction { CYIdentifier *name_; CYFunctionParameter *parameters_; CYBlock code_; @@ -1374,7 +1444,7 @@ struct CYFunction : CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : name_(name), parameters_(parameters), - code_(statements, this) + code_(statements) { } @@ -1386,6 +1456,7 @@ struct CYFunction : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression struct CYFunctionExpression : CYFunction, CYExpression @@ -1402,6 +1473,20 @@ struct CYFunctionExpression : virtual void Output(CYOutput &out, CYFlags flags) const; }; +// XXX: this should derive from CYAnonymousFunctionExpression +struct CYRubyProc : + CYFunctionExpression +{ + CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) : + CYFunctionExpression(NULL, parameters, statements) + { + } + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +// XXX: this should derive from CYNamedFunction struct CYFunctionStatement : CYFunction, CYStatement