#include <map>
#include <set>
+#include <cstdio>
#include <cstdlib>
#include "location.hh"
CYWord
{
CYIdentifier *replace_;
+ size_t offset_;
+ size_t usage_;
CYIdentifier(const char *word) :
CYWord(word),
- replace_(NULL)
+ replace_(NULL),
+ offset_(0),
+ usage_(0)
{
}
typedef std::set<const char *, CYCStringLess> CYCStringSet;
typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
-typedef std::set<CYIdentifier *> CYIdentifierAddressSet;
-typedef std::vector<CYIdentifier *> CYIdentifierAddressVector;
typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
+struct CYIdentifierUsage {
+ CYIdentifier *identifier_;
+ size_t usage_;
+};
+
+typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
+
struct CYScope {
CYScope *parent_;
- CYIdentifierAddressFlagsMap internal_;
+ CYIdentifierAddressFlagsMap internal_;
CYIdentifierValueSet identifiers_;
- size_t offset_;
CYScope() :
- parent_(NULL),
- offset_(0)
+ parent_(NULL)
{
}
+ virtual ~CYScope() {
+ }
+
void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
- void Merge(CYContext &context, CYIdentifierAddressVector &external);
+ void Merge(CYContext &context, CYIdentifier *identifier);
void Scope(CYContext &context, CYStatement *&statements);
};
struct CYProgram :
- CYScope,
CYThing
{
CYStatement *statements_;
- CYIdentifierAddressVector rename_;
- CYCStringSet external_;
CYProgram(CYStatement *statements) :
statements_(statements)
virtual void Output(CYOutput &out) const;
};
+struct CYContext {
+ apr_pool_t *pool_;
+ CYOptions &options_;
+ CYScope *scope_;
+ CYIdentifierUsageVector rename_;
+
+ CYContext(apr_pool_t *pool, CYOptions &options) :
+ pool_(pool),
+ options_(options),
+ scope_(NULL)
+ {
+ }
+
+ virtual ~CYContext() {
+ }
+
+ template <typename Type_>
+ void Replace(Type_ *&value) {
+ for (;;) if (value == NULL)
+ break;
+ else {
+ Type_ *replace(value->Replace(*this));
+ if (replace != value)
+ value = replace;
+ else break;
+ }
+ }
+};
+
struct CYBlock :
CYStatement,
CYThing
{
CYStatement *statements_;
- CYScope *scope_;
- CYBlock(CYStatement *statements, CYScope *scope = NULL) :
- statements_(statements),
- scope_(scope)
+ CYBlock(CYStatement *statements) :
+ statements_(statements)
{
}
CYProgram *program_;
Errors errors_;
+ bool auto_;
+
+ struct Context {
+ CYExpression *context_;
+
+ Context(CYExpression *context) :
+ context_(context)
+ {
+ }
+
+ typedef std::vector<CYWord *> Words;
+ Words words_;
+ };
+
+ typedef std::vector<Context> 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();
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;
{
}
- 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;
};
virtual CYExpression *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
+
+ virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
};
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 :
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_;
CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
name_(name),
parameters_(parameters),
- code_(statements, this)
+ code_(statements)
{
}
virtual void Output(CYOutput &out, CYFlags flags) const;
};
+// XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
struct CYFunctionExpression :
CYFunction,
CYExpression
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