]> git.saurik.com Git - cycript.git/blobdiff - Parser.hpp
Attempt to make all the identifiers look the same.
[cycript.git] / Parser.hpp
index be97c0b49d09d91f1abdcd993adc2f4d76f114e7..a7ac66aa82caa900cd5c50e7c0a5a5d3437f4a26 100644 (file)
@@ -1,29 +1,28 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013  Jay Freeman (saurik)
+ * Copyright (C) 2009-2015  Jay Freeman (saurik)
 */
 
-/* GNU General Public License, Version 3 {{{ */
+/* GNU Affero General Public License, Version 3 {{{ */
 /*
- * Cycript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, either version 3 of the License,
- * or (at your option) any later version.
- *
- * Cycript is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Cycript.  If not, see <http://www.gnu.org/licenses/>.
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
 /* }}} */
 
 #ifndef CYCRIPT_PARSER_HPP
 #define CYCRIPT_PARSER_HPP
 
-#include <iostream>
-
+#include <streambuf>
 #include <string>
 #include <vector>
 #include <map>
 #include <cstdlib>
 
 #include "List.hpp"
+#include "Location.hpp"
 #include "Pooling.hpp"
 #include "Options.hpp"
 
 struct CYContext;
 
 struct CYThing {
-    virtual ~CYThing() {
-    }
-
     virtual void Output(struct CYOutput &out) const = 0;
 };
 
 struct CYOutput {
-    std::ostream &out_;
+    std::streambuf &out_;
+    CYPosition position_;
+
     CYOptions &options_;
     bool pretty_;
     unsigned indent_;
+    unsigned recent_;
     bool right_;
 
     enum {
@@ -60,11 +60,12 @@ struct CYOutput {
         Terminated
     } mode_;
 
-    CYOutput(std::ostream &out, CYOptions &options) :
+    CYOutput(std::streambuf &out, CYOptions &options) :
         out_(out),
         options_(options),
         pretty_(false),
         indent_(0),
+        recent_(0),
         right_(false),
         mode_(NoMode)
     {
@@ -73,6 +74,25 @@ struct CYOutput {
     void Check(char value);
     void Terminate();
 
+    _finline void operator ()(char value) {
+        _assert(out_.sputc(value) != EOF);
+        recent_ = indent_;
+        if (value == '\n')
+            position_.lines(1);
+        else
+            position_.columns(1);
+    }
+
+    _finline void operator ()(const char *data, std::streamsize size) {
+        _assert(out_.sputn(data, size) == size);
+        recent_ = indent_;
+        position_.columns(size);
+    }
+
+    _finline void operator ()(const char *data) {
+        return operator ()(data, strlen(data));
+    }
+
     CYOutput &operator <<(char rhs);
     CYOutput &operator <<(const char *rhs);
 
@@ -90,9 +110,6 @@ struct CYOutput {
 
 struct CYPropertyName {
     virtual void PropertyName(CYOutput &out) const = 0;
-
-    virtual ~CYPropertyName() {
-    }
 };
 
 struct CYExpression;
@@ -144,17 +161,30 @@ _finline CYFlags CYCenter(CYFlags flags) {
     return CYLeft(CYRight(flags));
 }
 
-struct CYStatement :
-    CYNext<CYStatement>
-{
-    virtual ~CYStatement() {
+enum CYCompactType {
+    CYCompactNone,
+    CYCompactLong,
+    CYCompactShort,
+};
+
+#define CYCompact(type) \
+    virtual CYCompactType Compact() const { \
+        return CYCompact ## type; \
     }
 
-    void Single(CYOutput &out, CYFlags flags) const;
+struct CYStatement :
+    CYNext<CYStatement>,
+    CYThing
+{
+    void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
     void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
+    virtual void Output(CYOutput &out) const;
 
     virtual CYStatement *Replace(CYContext &context) = 0;
 
+    virtual CYCompactType Compact() const = 0;
+    virtual CYStatement *Return();
+
   private:
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
@@ -189,9 +219,6 @@ struct CYStatements {
 };
 
 struct CYClassName {
-    virtual ~CYClassName() {
-    }
-
     virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
     virtual void ClassName(CYOutput &out, bool object) const = 0;
 };
@@ -255,6 +282,8 @@ struct CYComment :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -271,6 +300,8 @@ struct CYLabel :
     {
     }
 
+    CYCompact(Short)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -312,33 +343,26 @@ typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
 
 struct CYScope {
     bool transparent_;
-
-    CYContext &context_;
-    CYStatement *&statements_;
-
     CYScope *parent_;
 
     CYIdentifierAddressFlagsMap internal_;
     CYIdentifierValueSet identifiers_;
 
-    CYScope(bool transparent, CYContext &context, CYStatement *&statements);
-    virtual ~CYScope();
-
-    void Close();
+    CYScope(bool transparent, CYContext &context);
 
     void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
     virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
     void Merge(CYContext &context, CYIdentifier *identifier);
-    void Scope(CYContext &context, CYStatement *&statements);
+    void Close(CYContext &context, CYStatement *&statements);
 };
 
 struct CYProgram :
     CYThing
 {
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYProgram(CYStatement *statements) :
-        statements_(statements)
+    CYProgram(CYStatement *code) :
+        code_(code)
     {
     }
 
@@ -371,17 +395,18 @@ struct CYContext {
     {
     }
 
-    virtual ~CYContext() {
-    }
+    void ReplaceAll(CYStatement *&statement) {
+        if (statement == NULL)
+            return;
+        CYStatement *next(statement->next_);
 
-    template <typename Type_>
-    void ReplaceAll(Type_ *&values) {
-        Type_ **last(&values);
-        CYForEach (next, values) {
-            Replace(*last = next);
-            if (*last != NULL)
-                last = &(*last)->next_;
-        }
+        Replace(statement);
+        ReplaceAll(next);
+
+        if (statement == NULL)
+            statement = next;
+        else
+            statement->SetNext(next);
     }
 
     template <typename Type_>
@@ -435,64 +460,30 @@ struct CYThisScope :
 };
 
 struct CYBlock :
-    CYStatement,
-    CYThing
+    CYStatement
 {
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYBlock(CYStatement *statements) :
-        statements_(statements)
+    CYBlock(CYStatement *code) :
+        code_(code)
     {
     }
 
-    operator CYStatement *() const {
-        return statements_;
-    }
-
-    void AddPrev(CYStatement *statement) {
-        CYSetLast(statement) = statements_;
-        statements_ = statement;
-    }
+    CYCompact(Short)
 
     virtual CYStatement *Replace(CYContext &context);
 
-    virtual void Output(CYOutput &out) const;
     virtual void Output(CYOutput &out, CYFlags flags) const;
-};
-
-class CYStream :
-    public std::istream
-{
-  private:
-    class CYBuffer :
-        public std::streambuf
-    {
-      public:
-        CYBuffer(const char *start, const char *end) {
-            setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
-        }
-    } buffer_;
 
-  public:
-    CYStream(const char *start, const char *end) :
-        std::istream(&buffer_),
-        buffer_(start, end)
-    {
-    }
+    virtual CYStatement *Return();
 };
 
 struct CYForInitialiser {
-    virtual ~CYForInitialiser() {
-    }
-
     virtual CYExpression *Replace(CYContext &context) = 0;
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
 
 struct CYForInInitialiser {
-    virtual ~CYForInInitialiser() {
-    }
-
     virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
     virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
 
@@ -502,17 +493,18 @@ struct CYForInInitialiser {
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
 
+struct CYFunctionParameter;
+
 struct CYNumber;
 struct CYString;
 
 struct CYExpression :
-    CYNext<CYExpression>,
     CYForInitialiser,
     CYForInInitialiser,
     CYClassName,
     CYThing
 {
-    virtual unsigned Precedence() const = 0;
+    virtual int Precedence() const = 0;
 
     virtual bool RightHand() const {
         return true;
@@ -525,7 +517,7 @@ struct CYExpression :
 
     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 Output(CYOutput &out, int precedence, CYFlags flags) const;
 
     virtual CYExpression *ClassName(CYContext &context, bool object);
     virtual void ClassName(CYOutput &out, bool object) const;
@@ -534,9 +526,11 @@ struct CYExpression :
     virtual CYAssignment *Assignment(CYContext &context);
 
     virtual CYExpression *Primitive(CYContext &context) {
-        return this;
+        return NULL;
     }
 
+    virtual CYFunctionParameter *Parameter() const;
+
     virtual CYNumber *Number(CYContext &context) {
         return NULL;
     }
@@ -556,8 +550,8 @@ struct CYExpression :
     }
 
 #define CYPrecedence(value) \
-    static const unsigned Precedence_ = value; \
-    virtual unsigned Precedence() const { \
+    static const int Precedence_ = value; \
+    virtual int Precedence() const { \
         return Precedence_; \
     }
 
@@ -569,16 +563,15 @@ struct CYExpression :
 struct CYCompound :
     CYExpression
 {
-    CYExpression *expressions_;
+    CYExpression *expression_;
+    CYExpression *next_;
 
-    CYCompound(CYExpression *expressions = NULL) :
-        expressions_(expressions)
+    CYCompound(CYExpression *expression, CYExpression *next) :
+        expression_(expression),
+        next_(next)
     {
-    }
-
-    void AddPrev(CYExpression *expression) {
-        CYSetLast(expression) = expressions_;
-        expressions_ = expression;
+        _assert(expression_ != NULL);
+        _assert(next != NULL);
     }
 
     CYPrecedence(17)
@@ -586,7 +579,23 @@ struct CYCompound :
     virtual CYExpression *Replace(CYContext &context);
     void Output(CYOutput &out, CYFlags flags) const;
 
-    virtual CYExpression *Primitive(CYContext &context);
+    virtual CYFunctionParameter *Parameter() const;
+};
+
+struct CYParenthetical :
+    CYExpression
+{
+    CYExpression *expression_;
+
+    CYParenthetical(CYExpression *expression) :
+        expression_(expression)
+    {
+    }
+
+    CYPrecedence(0)
+
+    virtual CYExpression *Replace(CYContext &context);
+    void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYDeclaration;
@@ -603,7 +612,7 @@ struct CYFunctionParameter :
     {
     }
 
-    void Replace(CYContext &context, CYBlock &code);
+    void Replace(CYContext &context, CYStatement *&statements);
     void Output(CYOutput &out) const;
 };
 
@@ -616,6 +625,11 @@ struct CYComprehension :
     {
     }
 
+    CYComprehension *Modify(CYComprehension *next) {
+        next_ = next;
+        return this;
+    }
+
     virtual const char *Name() const = 0;
 
     virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
@@ -673,7 +687,8 @@ struct CYIfComprehension :
 {
     CYExpression *test_;
 
-    CYIfComprehension(CYExpression *test) :
+    CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
+        CYComprehension(next),
         test_(test)
     {
     }
@@ -710,6 +725,10 @@ struct CYLiteral :
 {
     CYPrecedence(0)
     CYRightHand(false)
+
+    virtual CYExpression *Primitive(CYContext &context) {
+        return this;
+    }
 };
 
 struct CYTrivial :
@@ -834,14 +853,8 @@ struct CYRegEx :
 };
 
 struct CYNull :
-    CYWord,
     CYTrivial
 {
-    CYNull() :
-        CYWord("null")
-    {
-    }
-
     virtual CYNumber *Number(CYContext &context);
     virtual CYString *String(CYContext &context);
 
@@ -849,14 +862,8 @@ struct CYNull :
 };
 
 struct CYThis :
-    CYWord,
     CYMagic
 {
-    CYThis() :
-        CYWord("this")
-    {
-    }
-
     virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -869,14 +876,8 @@ struct CYBoolean :
 };
 
 struct CYFalse :
-    CYWord,
     CYBoolean
 {
-    CYFalse() :
-        CYWord("false")
-    {
-    }
-
     virtual bool Value() const {
         return false;
     }
@@ -886,14 +887,8 @@ struct CYFalse :
 };
 
 struct CYTrue :
-    CYWord,
     CYBoolean
 {
-    CYTrue() :
-        CYWord("true")
-    {
-    }
-
     virtual bool Value() const {
         return true;
     }
@@ -922,6 +917,8 @@ struct CYVariable :
 
     virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYFunctionParameter *Parameter() const;
 };
 
 struct CYPrefix :
@@ -1033,25 +1030,16 @@ struct CYArgument :
     void Output(CYOutput &out) const;
 };
 
-struct CYBlank :
-    public CYWord
-{
-    CYBlank() :
-        CYWord("")
-    {
-    }
-};
-
 struct CYClause :
     CYThing,
     CYNext<CYClause>
 {
     CYExpression *case_;
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYClause(CYExpression *_case, CYStatement *statements) :
+    CYClause(CYExpression *_case, CYStatement *code) :
         case_(_case),
-        statements_(statements)
+        code_(code)
     {
     }
 
@@ -1144,7 +1132,7 @@ struct CYDeclarations :
 
     void Replace(CYContext &context);
 
-    CYCompound *Compound(CYContext &context);
+    CYExpression *Expression(CYContext &context);
     CYProperty *Property(CYContext &context);
     CYArgument *Argument(CYContext &context);
     CYFunctionParameter *Parameter(CYContext &context);
@@ -1163,7 +1151,7 @@ struct CYForDeclarations :
     {
     }
 
-    virtual CYCompound *Replace(CYContext &context);
+    virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1177,6 +1165,8 @@ struct CYVar :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1193,6 +1183,8 @@ struct CYLetStatement :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1213,6 +1205,8 @@ struct CYFor :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1231,6 +1225,8 @@ struct CYForIn :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1249,6 +1245,8 @@ struct CYForOf :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1329,7 +1327,7 @@ struct New :
     {
     }
 
-    virtual unsigned Precedence() const {
+    virtual int Precedence() const {
         return arguments_ == NULL ? 2 : 1;
     }
 
@@ -1399,8 +1397,12 @@ struct CYIf :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYStatement *Return();
 };
 
 struct CYDoWhile :
@@ -1415,6 +1417,8 @@ struct CYDoWhile :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1431,6 +1435,8 @@ struct CYWhile :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1439,22 +1445,21 @@ struct CYWhile :
 struct CYFunction {
     CYIdentifier *name_;
     CYFunctionParameter *parameters_;
-    CYBlock code_;
+    CYStatement *code_;
 
     CYNonLocal *nonlocal_;
+    bool implicit_;
     CYThisScope this_;
 
-    CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
+    CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
         name_(name),
         parameters_(parameters),
-        code_(statements),
-        nonlocal_(NULL)
+        code_(code),
+        nonlocal_(NULL),
+        implicit_(false)
     {
     }
 
-    virtual ~CYFunction() {
-    }
-
     void Inject(CYContext &context);
     virtual void Replace_(CYContext &context, bool outer);
     virtual void Output(CYOutput &out, CYFlags flags) const;
@@ -1465,8 +1470,8 @@ struct CYFunctionExpression :
     CYFunction,
     CYExpression
 {
-    CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunction(name, parameters, statements)
+    CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunction(name, parameters, code)
     {
     }
 
@@ -1482,8 +1487,8 @@ struct CYFatArrow :
     CYFunction,
     CYExpression
 {
-    CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunction(NULL, parameters, statements)
+    CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunction(NULL, parameters, code)
     {
     }
 
@@ -1498,8 +1503,8 @@ struct CYFatArrow :
 struct CYRubyProc :
     CYFunctionExpression
 {
-    CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunctionExpression(NULL, parameters, statements)
+    CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunctionExpression(NULL, parameters, code)
     {
     }
 
@@ -1512,11 +1517,13 @@ struct CYFunctionStatement :
     CYFunction,
     CYStatement
 {
-    CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunction(name, parameters, statements)
+    CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunction(name, parameters, code)
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1529,12 +1536,16 @@ struct CYExpress :
     CYExpress(CYExpression *expression) :
         expression_(expression)
     {
-        if (expression == NULL)
+        if (expression_ == NULL)
             throw;
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYStatement *Return();
 };
 
 struct CYContinue :
@@ -1547,6 +1558,8 @@ struct CYContinue :
     {
     }
 
+    CYCompact(Short)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1561,6 +1574,8 @@ struct CYBreak :
     {
     }
 
+    CYCompact(Short)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1575,6 +1590,8 @@ struct CYReturn :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1582,6 +1599,8 @@ struct CYReturn :
 struct CYEmpty :
     CYStatement
 {
+    CYCompact(Short)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1589,10 +1608,10 @@ struct CYEmpty :
 struct CYFinally :
     CYThing
 {
-    CYBlock code_;
+    CYStatement *code_;
 
-    CYFinally(CYStatement *statements) :
-        code_(statements)
+    CYFinally(CYStatement *code) :
+        code_(code)
     {
     }
 
@@ -1600,6 +1619,109 @@ struct CYFinally :
     virtual void Output(CYOutput &out) const;
 };
 
+struct CYTypeSpecifier :
+    CYThing
+{
+    virtual CYExpression *Replace(CYContext &context) = 0;
+};
+
+struct CYTypeError :
+    CYTypeSpecifier
+{
+    CYTypeError() {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeVoid :
+    CYTypeSpecifier
+{
+    CYTypeVoid() {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeVariable :
+    CYTypeSpecifier
+{
+    CYIdentifier *name_;
+
+    CYTypeVariable(CYIdentifier *name) :
+        name_(name)
+    {
+    }
+
+    CYTypeVariable(const char *name) :
+        name_(new($pool) CYIdentifier(name))
+    {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeUnsigned :
+    CYTypeSpecifier
+{
+    CYTypeSpecifier *specifier_;
+
+    CYTypeUnsigned(CYTypeSpecifier *specifier) :
+        specifier_(specifier)
+    {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeSigned :
+    CYTypeSpecifier
+{
+    CYTypeSpecifier *specifier_;
+
+    CYTypeSigned(CYTypeSpecifier *specifier) :
+        specifier_(specifier)
+    {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeLong :
+    CYTypeSpecifier
+{
+    CYTypeSpecifier *specifier_;
+
+    CYTypeLong(CYTypeSpecifier *specifier) :
+        specifier_(specifier)
+    {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeShort :
+    CYTypeSpecifier
+{
+    CYTypeSpecifier *specifier_;
+
+    CYTypeShort(CYTypeSpecifier *specifier) :
+        specifier_(specifier)
+    {
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out) const;
+};
+
+struct CYTypeFunctionWith;
+
 struct CYTypeModifier :
     CYNext<CYTypeModifier>
 {
@@ -1608,7 +1730,15 @@ struct CYTypeModifier :
     {
     }
 
-    virtual CYExpression *Replace(CYContext &context) = 0;
+    virtual int Precedence() const = 0;
+
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
+    CYExpression *Replace(CYContext &context, CYExpression *type);
+
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
+    void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
+
+    virtual CYTypeFunctionWith *Function() { return NULL; }
 };
 
 struct CYTypeArrayOf :
@@ -1622,9 +1752,10 @@ struct CYTypeArrayOf :
     {
     }
 
-    CYPrecedence(2)
+    CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
 struct CYTypeConstant :
@@ -1635,9 +1766,10 @@ struct CYTypeConstant :
     {
     }
 
-    CYPrecedence(3)
+    CYPrecedence(0)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
 struct CYTypePointerTo :
@@ -1648,45 +1780,80 @@ struct CYTypePointerTo :
     {
     }
 
-    CYPrecedence(3)
+    CYPrecedence(0)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
-struct CYTypeVariable :
+struct CYTypeVolatile :
     CYTypeModifier
 {
-    CYExpression *expression_;
-
-    CYTypeVariable(CYExpression *expression) :
-        CYTypeModifier(NULL),
-        expression_(expression)
+    CYTypeVolatile(CYTypeModifier *next = NULL) :
+        CYTypeModifier(next)
     {
     }
 
-    CYPrecedence(1)
+    CYPrecedence(0)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
 };
 
 struct CYTypedIdentifier :
     CYNext<CYTypedIdentifier>,
     CYThing
 {
+    CYLocation location_;
     CYIdentifier *identifier_;
-    CYTypeModifier *type_;
+    CYTypeSpecifier *specifier_;
+    CYTypeModifier *modifier_;
 
-    CYTypedIdentifier(CYIdentifier *identifier) :
+    CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
+        location_(location),
         identifier_(identifier),
-        type_(NULL)
+        specifier_(NULL),
+        modifier_(NULL)
     {
     }
 
+    CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
+        identifier_(NULL),
+        specifier_(specifier),
+        modifier_(modifier)
+    {
+    }
+
+    inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
+        CYSetLast(modifier_) = modifier;
+        return this;
+    }
+
+    virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
+
+    CYTypeFunctionWith *Function();
+};
+
+struct CYEncodedType :
+    CYExpression
+{
+    CYTypedIdentifier *typed_;
+
+    CYEncodedType(CYTypedIdentifier *typed) :
+        typed_(typed)
+    {
+    }
+
+    CYPrecedence(1)
+
+    virtual CYExpression *Replace(CYContext &context);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYTypedParameter :
-    CYNext<CYTypedParameter>
+    CYNext<CYTypedParameter>,
+    CYThing
 {
     CYTypedIdentifier *typed_;
 
@@ -1699,19 +1866,21 @@ struct CYTypedParameter :
     CYArgument *Argument(CYContext &context);
     CYFunctionParameter *Parameters(CYContext &context);
     CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
+
+    virtual void Output(CYOutput &out) const;
 };
 
 struct CYLambda :
     CYExpression
 {
-    CYTypeModifier *type_;
+    CYTypedIdentifier *typed_;
     CYTypedParameter *parameters_;
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYLambda(CYTypeModifier *type, CYTypedParameter *parameters, CYStatement *statements) :
-        type_(type),
+    CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
+        typed_(typed),
         parameters_(parameters),
-        statements_(statements)
+        code_(code)
     {
     }
 
@@ -1721,6 +1890,56 @@ struct CYLambda :
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
+struct CYModule :
+    CYNext<CYModule>,
+    CYThing
+{
+    CYWord *part_;
+
+    CYModule(CYWord *part, CYModule *next = NULL) :
+        CYNext<CYModule>(next),
+        part_(part)
+    {
+    }
+
+    CYString *Replace(CYContext &context, const char *separator) const;
+    void Output(CYOutput &out) const;
+};
+
+struct CYImport :
+    CYStatement
+{
+    CYModule *module_;
+
+    CYImport(CYModule *module) :
+        module_(module)
+    {
+    }
+
+    CYCompact(None)
+
+    virtual CYStatement *Replace(CYContext &context);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYExternal :
+    CYStatement
+{
+    CYString *abi_;
+    CYTypedIdentifier *typed_;
+
+    CYExternal(CYString *abi, CYTypedIdentifier *typed) :
+        abi_(abi),
+        typed_(typed)
+    {
+    }
+
+    CYCompact(None)
+
+    virtual CYStatement *Replace(CYContext &context);
+    virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
 struct CYTypeDefinition :
     CYStatement
 {
@@ -1731,10 +1950,29 @@ struct CYTypeDefinition :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
+struct CYTypeBlockWith :
+    CYTypeModifier
+{
+    CYTypedParameter *parameters_;
+
+    CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
+        CYTypeModifier(next),
+        parameters_(parameters)
+    {
+    }
+
+    CYPrecedence(0)
+
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+};
+
 struct CYTypeFunctionWith :
     CYTypeModifier
 {
@@ -1746,9 +1984,12 @@ struct CYTypeFunctionWith :
     {
     }
 
-    CYPrecedence(2)
+    CYPrecedence(1)
 
-    virtual CYExpression *Replace(CYContext &context);
+    virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+    virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+
+    virtual CYTypeFunctionWith *Function() { return this; }
 };
 
 namespace cy {
@@ -1758,11 +1999,11 @@ struct Catch :
     CYThing
 {
     CYIdentifier *name_;
-    CYBlock code_;
+    CYStatement *code_;
 
-    Catch(CYIdentifier *name, CYStatement *statements) :
+    Catch(CYIdentifier *name, CYStatement *code) :
         name_(name),
-        code_(statements)
+        code_(code)
     {
     }
 
@@ -1773,17 +2014,19 @@ struct Catch :
 struct Try :
     CYStatement
 {
-    CYBlock code_;
+    CYStatement *code_;
     Catch *catch_;
     CYFinally *finally_;
 
-    Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
-        code_(statements),
+    Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
+        code_(code),
         catch_(_catch),
         finally_(finally)
     {
     }
 
+    CYCompact(Short)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1798,6 +2041,8 @@ struct Throw :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1816,6 +2061,8 @@ struct CYWith :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1832,6 +2079,8 @@ struct CYSwitch :
     {
     }
 
+    CYCompact(Long)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1843,6 +2092,8 @@ struct CYDebugger :
     {
     }
 
+    CYCompact(None)
+
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
@@ -1978,7 +2229,7 @@ CYPrefix_(false, "-", Negate)
 CYPrefix_(false, "~", BitwiseNot)
 CYPrefix_(false, "!", LogicalNot)
 
-CYInfix_(false, 5, "*", Multiply)
+CYInfix_(false, 5, "*", Multiply, CYReplace)
 CYInfix_(false, 5, "/", Divide)
 CYInfix_(false, 5, "%", Modulus)
 CYInfix_(false, 6, "+", Add, CYReplace)