(JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
}
+static JSValueRef Cycript_compile_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ CYPool pool;
+ std::stringstream value(CYPoolCString(pool, context, arguments[0]));
+ CYUTF8String code(CYPoolCode(pool, value));
+ return CYCastJSValue(context, CYJSString(code));
+} CYCatch(NULL) }
+
static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYGarbageCollect(context);
return CYJSUndefined(context);
JSObjectRef cycript(JSObjectMake(context, NULL, NULL));
CYSetProperty(context, global, CYJSString("Cycript"), cycript);
+ CYSetProperty(context, cycript, CYJSString("compile"), &Cycript_compile_callAsFunction);
CYSetProperty(context, cycript, CYJSString("gc"), &Cycript_gc_callAsFunction);
JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new));
);
}
-CYExpression *CYInstanceLiteral::Replace(CYContext &context) {
+CYTarget *CYInstanceLiteral::Replace(CYContext &context) {
return $N1($V("Instance"), number_);
}
return MessageType(context, type_, next_);
}
-CYExpression *CYBox::Replace(CYContext &context) {
+CYTarget *CYBox::Replace(CYContext &context) {
return $C1($M($V("Instance"), $S("box")), value_);
}
-CYExpression *CYObjCBlock::Replace(CYContext &context) {
+CYTarget *CYObjCBlock::Replace(CYContext &context) {
return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_));
}
);
}
-CYExpression *CYSelector::Replace(CYContext &context) {
+CYTarget *CYSelector::Replace(CYContext &context) {
return $C1($V("sel_registerName"), name_->Replace(context));
}
return $S($pool.strdup(str.str().c_str()));
}
-CYExpression *CYSendDirect::Replace(CYContext &context) {
+CYTarget *CYSendDirect::Replace(CYContext &context) {
std::ostringstream name;
CYArgument **argument(&arguments_);
CYSelectorPart *selector(NULL), *current(NULL);
return $C2($V("objc_msgSend"), self_, selector->Replace(context), arguments_);
}
-CYExpression *CYSendSuper::Replace(CYContext &context) {
+CYTarget *CYSendSuper::Replace(CYContext &context) {
return $ CYSendDirect($V("$cyr"), arguments_);
}
#include "../Syntax.hpp"
struct CYInstanceLiteral :
- CYExpression
+ CYTarget
{
CYNumber *number_;
CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYObjCBlock :
- CYExpression
+ CYTarget
{
CYTypedIdentifier *typed_;
CYTypedParameter *parameters_;
CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYBox :
- CYExpression
+ CYTarget
{
CYExpression *value_;
CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
};
struct CYSend :
- CYExpression
+ CYTarget
{
CYArgument *arguments_;
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
out << "debugger" << ';';
}
-void CYDeclaration::ForIn(CYOutput &out, CYFlags flags) const {
- out << "var" << ' ';
- Output(out, CYRight(flags));
-}
-
void CYDeclaration::Output(CYOutput &out, CYFlags flags) const {
out << *identifier_;
//out.out_ << ':' << identifier_->usage_ << '#' << identifier_->offset_;
}
}
-void CYForDeclarations::Output(CYOutput &out, CYFlags flags) const {
- out << "var" << ' ';
- declarations_->Output(out, CYRight(flags));
-}
-
void CYDeclarations::Output(CYOutput &out) const {
Output(out, CYNoFlags);
}
out.Terminate();
}
+void CYEval::Output(CYOutput &out, CYFlags flags) const {
+ _assert(false);
+}
+
void CYExpress::Output(CYOutput &out, CYFlags flags) const {
expression_->Output(out, flags | CYNoBFC);
out << ';';
}
-void CYExpression::ForIn(CYOutput &out, CYFlags flags) const {
- Output(out, flags | CYNoRightHand);
-}
-
void CYExpression::Output(CYOutput &out) const {
Output(out, CYNoFlags);
}
code_->Single(out, CYRight(flags), CYCompactShort);
}
-void CYForOf::Output(CYOutput &out, CYFlags flags) const {
- out << "for" << ' ' << "each" << ' ' << '(';
- initialiser_->ForIn(out, CYNoIn);
- out << ' ' << "in" << ' ' << *set_ << ')';
- code_->Single(out, CYRight(flags), CYCompactShort);
+void CYForDeclarations::Output(CYOutput &out, CYFlags flags) const {
+ out << "var" << ' ';
+ declarations_->Output(out, CYRight(flags));
}
-void CYForOfComprehension::Output(CYOutput &out) const {
- out << "for" << ' ' << "each" << ' ' << '(';
- declaration_->Output(out, CYNoIn);
- out << ' ' << "in" << ' ' << *set_ << ')' << next_;
+void CYForLexical::Output(CYOutput &out, CYFlags flags) const {
+ out << (constant_ ? "const" : "let") << ' ';
+ declaration_->Output(out, CYRight(flags));
}
void CYForIn::Output(CYOutput &out, CYFlags flags) const {
out << "for" << ' ' << '(';
- if (initialiser_ != NULL)
- initialiser_->ForIn(out, CYNoIn);
+ initialiser_->Output(out, CYNoIn | CYNoRightHand);
out << ' ' << "in" << ' ' << *set_ << ')';
code_->Single(out, CYRight(flags), CYCompactShort);
}
void CYForInComprehension::Output(CYOutput &out) const {
out << "for" << ' ' << '(';
- declaration_->Output(out, CYNoIn);
+ declaration_->Output(out, CYNoIn | CYNoRightHand);
out << ' ' << "in" << ' ' << *set_ << ')';
}
+void CYForOf::Output(CYOutput &out, CYFlags flags) const {
+ out << "for" << ' ' << '(';
+ initialiser_->Output(out, CYNoRightHand);
+ out << ' ' << "of" << ' ' << *set_ << ')';
+ code_->Single(out, CYRight(flags), CYCompactShort);
+}
+
+void CYForOfComprehension::Output(CYOutput &out) const {
+ out << "for" << ' ' << '(';
+ declaration_->Output(out, CYNoRightHand);
+ out << ' ' << "of" << ' ' << *set_ << ')' << next_;
+}
+
+void CYForVariable::Output(CYOutput &out, CYFlags flags) const {
+ out << "var" << ' ';
+ declaration_->Output(out, CYRight(flags));
+}
+
void CYFunction::Output(CYOutput &out) const {
out << '(' << parameters_ << ')' << ' ';
out << '{' << '\n';
}
const char *CYIdentifier::Word() const {
- return replace_ == NULL || replace_ == this ? CYWord::Word() : replace_->Word();
+ return next_ == NULL || next_ == this ? CYWord::Word() : next_->Word();
}
void CYIf::Output(CYOutput &out, CYFlags flags) const {
out << "@import";
}
+void CYIndirect::Output(CYOutput &out, CYFlags flags) const {
+ out << "*";
+ rhs_->Output(out, Precedence(), CYRight(flags));
+}
+
void CYIndirectMember::Output(CYOutput &out, CYFlags flags) const {
object_->Output(out, Precedence(), CYLeft(flags));
if (const char *word = property_->Word())
%union { CYSpan *span_; }
%union { CYStatement *statement_; }
%union { CYString *string_; }
+%union { CYTarget *target_; }
%union { CYThis *this_; }
%union { CYTrue *true_; }
%union { CYWord *word_; }
%token _char_ "char"
%token _constructor_ "constructor"
%token _double_ "double"
+%token _eval_ "eval"
%token _final_ "final"
%token _float_ "float"
%token _from_ "from"
%type <argument_> ArgumentList
%type <argument_> ArgumentListOpt
%type <argument_> Arguments
-%type <expression_> ArrayComprehension
+%type <target_> ArrayComprehension
%type <literal_> ArrayLiteral
%type <expression_> ArrowFunction
%type <functionParameter_> ArrowParameters
%type <statement_> BreakStatement
%type <statement_> BreakableStatement
%type <expression_> CallExpression_
-%type <expression_> CallExpression
+%type <target_> CallExpression
%type <clause_> CaseBlock
%type <clause_> CaseClause
%type <clause_> CaseClausesOpt
%type <catch_> Catch
%type <identifier_> CatchParameter
%type <statement_> ClassDeclaration
-%type <expression_> ClassExpression
+%type <target_> ClassExpression
%type <classTail_> ClassHeritage
%type <classTail_> ClassHeritageOpt
%type <classTail_> ClassTail
-%type <expression_> Comprehension
+%type <target_> Comprehension
%type <comprehension_> ComprehensionFor
%type <comprehension_> ComprehensionIf
%type <comprehension_> ComprehensionTail
%type <statement_> ExpressionStatement
%type <finally_> Finally
%type <declaration_> ForBinding
-%type <declaration_> ForDeclaration
+%type <forin_> ForDeclaration
%type <forin_> ForInStatementInitializer
%type <for_> ForStatementInitializer
%type <declaration_> FormalParameter
%type <functionParameter_> FormalParameters
%type <statement_> FunctionBody
%type <statement_> FunctionDeclaration
-%type <expression_> FunctionExpression
+%type <target_> FunctionExpression
%type <statement_> FunctionStatementList
%type <statement_> GeneratorBody
%type <statement_> GeneratorDeclaration
-%type <expression_> GeneratorExpression
+%type <target_> GeneratorExpression
%type <method_> GeneratorMethod
%type <statement_> HoistableDeclaration
%type <identifier_> Identifier
%type <identifier_> LabelIdentifier
%type <statement_> LabelledItem
%type <statement_> LabelledStatement
-%type <expression_> LeftHandSideExpression
+%type <target_> LeftHandSideExpression
+%type <bool_> LetOrConst
%type <declaration_> LexicalBinding
%type <statement_> LexicalDeclaration
%type <literal_> Literal
%type <expression_> LogicalANDExpression
%type <expression_> LogicalORExpression
%type <access_> MemberAccess
-%type <expression_> MemberExpression
+%type <target_> MemberExpression
%type <method_> MethodDefinition
%type <module_> ModulePath
%type <expression_> MultiplicativeExpression
-%type <expression_> NewExpression
+%type <target_> NewExpression
%type <null_> NullLiteral
%type <literal_> ObjectLiteral
%type <expression_> PostfixExpression
-%type <expression_> PrimaryExpression
+%type <target_> PrimaryExpression
%type <propertyName_> PropertyName
%type <property_> PropertyDefinition
%type <property_> PropertyDefinitionList_
%type <statement_> StatementListOpt
%type <statement_> StatementListItem
%type <functionParameter_> StrictFormalParameters
-%type <expression_> SuperCall
-%type <expression_> SuperProperty
+%type <target_> SuperCall
+%type <target_> SuperProperty
%type <statement_> SwitchStatement
-%type <expression_> TemplateLiteral
+%type <target_> TemplateLiteral
%type <span_> TemplateSpans
%type <statement_> ThrowStatement
%type <statement_> TryStatement
%type <protocol_> ClassProtocols
%type <protocol_> ClassProtocolsOpt
%type <statement_> ImplementationStatement
-%type <expression_> MessageExpression
+%type <target_> MessageExpression
%type <messageParameter_> MessageParameter
%type <messageParameter_> MessageParameters
%type <messageParameter_> MessageParameterList
| "constructor" { $$ = CYNew CYIdentifier("constructor"); }
| "double" { $$ = CYNew CYIdentifier("double"); }
| "each" { $$ = CYNew CYIdentifier("each"); }
+ | "eval" { $$ = CYNew CYIdentifier("eval"); }
| "final" { $$ = CYNew CYIdentifier("final"); }
| "float" { $$ = CYNew CYIdentifier("float"); }
| "from" { $$ = CYNew CYIdentifier("from"); }
;
CallExpression_
- : MemberExpression
- | CallExpression
+ : MemberExpression { $$ = $1; }
+ | CallExpression { $$ = $1; }
;
CallExpression
- : CallExpression_ Arguments { $$ = CYNew CYCall($1, $2); }
+ : CallExpression_ Arguments { if (!$1->Eval()) $$ = CYNew CYCall($1, $2); else $$ = CYNew CYEval($2); }
| SuperCall { $$ = $1; }
| CallExpression { driver.context_ = $1; } MemberAccess { $3->SetLeft($1); $$ = $3; }
;
/* }}} */
/* 13.3 Let and Const Declarations {{{ */
LexicalDeclaration
- : LetOrConst BindingList Terminator { $$ = CYNew CYLet($2); }
+ : LetOrConst BindingList Terminator { $$ = CYNew CYLet($1, $2); }
;
LetOrConst
- : "let"
- | "const"
+ : "let" { $$ = false; }
+ | "const" { $$ = true; }
;
BindingList_
| "while" "(" Expression ")" Statement { $$ = CYNew CYWhile($3, $5); }
| "for" "(" LexPushInOn ForStatementInitializer ";" LexPopIn ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = CYNew CYFor($4, $7, $9, $11); }
| "for" "(" LexPushInOn ForInStatementInitializer "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForIn($4, $7, $9); }
- | "for" "(" LexPushInOn ForInStatementInitializer "of" LexPopIn Expression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); }
+ | "for" "(" LexPushInOn ForInStatementInitializer "of" LexPopIn AssignmentExpression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); }
;
ForStatementInitializer
ForInStatementInitializer
: LeftHandSideExpression { $$ = $1; }
- | LexSetRegExp Var_ ForBinding { $$ = $3; }
+ | LexSetRegExp Var_ ForBinding { $$ = CYNew CYForVariable($3); }
| LexSetRegExp ForDeclaration { $$ = $2; }
;
ForDeclaration
- : LetOrConst ForBinding { $$ = $2; }
+ : LetOrConst ForBinding { $$ = CYNew CYForLexical($1, $2); }
;
ForBinding
/* }}} */
#include <iomanip>
+#include <map>
#include "Replace.hpp"
#include "Syntax.hpp"
return $C0($M(rhs_, $S("$cya")));
}
+CYTarget *CYApply::AddArgument(CYContext &context, CYExpression *value) {
+ CYArgument **argument(&arguments_);
+ while (*argument != NULL)
+ argument = &(*argument)->next_;
+ *argument = $ CYArgument(value);
+ return this;
+}
+
CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
context.Replace(value_);
next_ = next_->Replace(context);
return this;
}
-CYExpression *CYArray::Replace(CYContext &context) {
+CYTarget *CYArray::Replace(CYContext &context) {
if (elements_ != NULL)
elements_->Replace(context);
return this;
}
-CYExpression *CYArrayComprehension::Replace(CYContext &context) {
+CYTarget *CYArrayComprehension::Replace(CYContext &context) {
CYVariable *cyv($V("$cyv"));
return $C0($F(NULL, $P1($L($I("$cyv")), comprehensions_->Parameters(context)), $$->*
}
CYStatement *CYBlock::Replace(CYContext &context) {
+ CYScope scope(true, context);
context.ReplaceAll(code_);
+ scope.Close(context);
+
if (code_ == NULL)
return $ CYEmpty();
return this;
return this;
}
-CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
- CYArgument **argument(&arguments_);
- while (*argument != NULL)
- argument = &(*argument)->next_;
- *argument = $ CYArgument(value);
- return this;
-}
-
-CYExpression *CYCall::Replace(CYContext &context) {
+CYTarget *CYCall::Replace(CYContext &context) {
context.Replace(function_);
arguments_->Replace(context);
return this;
void Catch::Replace(CYContext &context) { $T()
CYScope scope(true, context);
- context.Replace(name_);
- context.scope_->Declare(context, name_, CYIdentifierCatch);
+ name_ = name_->Replace(context, CYIdentifierCatch);
context.ReplaceAll(code_);
- scope.Close(context, code_);
+ scope.Close(context);
}
} }
-CYExpression *CYClassExpression::Replace(CYContext &context) {
+CYTarget *CYClassExpression::Replace(CYContext &context) {
CYBuilder builder;
CYIdentifier *super(context.Unique());
CYContext &context(*this);
if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
- CYIdentifier *cye($I("$cye")->Replace(context));
- CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
+ CYIdentifier *cye($I("$cye")->Replace(context, CYIdentifierGlobal));
+ CYIdentifier *unique(nextlocal_->identifier_->Replace(context, CYIdentifierGlobal));
CYStatement *declare(
$ CYVar($L1($L(unique, $ CYObject()))));
$ CYReturn($M($V(cye), $S("$cyv"))))->*
$ cy::Syntax::Throw($V(cye))));
- // XXX: I don't understand any of this
context.Replace(declare);
rescue->Replace(context);
return this;
}
-CYAssignment *CYDeclaration::Assignment(CYContext &context) {
+CYTarget *CYDeclaration::Target(CYContext &context) {
+ return $V(identifier_);
+}
+
+CYAssignment *CYDeclaration::Replace(CYContext &context, CYIdentifierKind kind) {
+ identifier_ = identifier_->Replace(context, kind);
+
if (initialiser_ == NULL)
return NULL;
- CYAssignment *value($ CYAssign(Variable(context), initialiser_));
+ CYAssignment *value($ CYAssign(Target(context), initialiser_));
initialiser_ = NULL;
return value;
}
-CYVariable *CYDeclaration::Variable(CYContext &context) {
- return $V(identifier_);
-}
+CYExpression *CYDeclarations::Replace(CYContext &context, CYIdentifierKind kind) { $T(NULL)
+ CYAssignment *assignment(declaration_->Replace(context, kind));
+ CYExpression *compound(next_->Replace(context, kind));
-CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
- return $ CYVar($L1($ CYDeclaration(identifier_, value)));
-}
-
-CYExpression *CYDeclaration::Replace(CYContext &context) {
- context.Replace(identifier_);
- context.scope_->Declare(context, identifier_, CYIdentifierVariable);
- return Variable(context);
-}
-
-void CYDeclarations::Replace(CYContext &context) { $T()
- declaration_->Replace(context);
- next_->Replace(context);
+ if (assignment != NULL)
+ if (compound == NULL)
+ compound = assignment;
+ else
+ compound = $ CYCompound(assignment, compound);
+ return compound;
}
CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
}
-CYExpression *CYDeclarations::Expression(CYContext &context) { $T(NULL)
- CYExpression *compound(next_->Expression(context));
- if (CYAssignment *assignment = declaration_->Assignment(context))
- if (compound == NULL)
- compound = assignment;
- else
- compound = $ CYCompound(assignment, compound);
- return compound;
-}
-
-CYExpression *CYDirectMember::Replace(CYContext &context) {
+CYTarget *CYDirectMember::Replace(CYContext &context) {
context.Replace(object_);
context.Replace(property_);
return this;
return NULL;
}
-CYExpression *CYEncodedType::Replace(CYContext &context) {
+CYTarget *CYEncodedType::Replace(CYContext &context) {
return typed_->Replace(context);
}
+CYTarget *CYEval::Replace(CYContext &context) {
+ context.scope_->Damage();
+ if (arguments_ != NULL)
+ arguments_->value_ = $C1($M($V("Cycript"), $S("compile")), arguments_->value_);
+ return $C($V("eval"), arguments_);
+}
+
CYStatement *CYExpress::Return() {
return $ CYReturn(expression_);
}
return this;
}
-CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
+CYTarget *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
return $C1(this, value);
}
-CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
- return $E($ CYAssign(this, value));
-}
-
-CYAssignment *CYExpression::Assignment(CYContext &context) {
- return NULL;
-}
-
CYFunctionParameter *CYExpression::Parameter() const {
return NULL;
}
}
void CYFinally::Replace(CYContext &context) { $T()
+ CYScope scope(true, context);
context.ReplaceAll(code_);
+ scope.Close(context);
}
CYStatement *CYFor::Replace(CYContext &context) {
+ CYScope outer(true, context);
context.Replace(initialiser_);
+
context.Replace(test_);
+
+ {
+ CYScope inner(true, context);
+ context.ReplaceAll(code_);
+ inner.Close(context);
+ }
+
context.Replace(increment_);
- context.ReplaceAll(code_);
+
+ outer.Close(context);
return this;
}
CYExpression *CYForDeclarations::Replace(CYContext &context) {
- declarations_->Replace(context);
- return declarations_->Expression(context);
+ return declarations_->Replace(context, CYIdentifierVariable);
}
-// XXX: this still feels highly suboptimal
-CYStatement *CYForIn::Replace(CYContext &context) {
- if (CYAssignment *assignment = initialiser_->Assignment(context))
- return $ CYBlock($$->*
- $E(assignment)->*
- this
- );
+CYStatement *CYForLexical::Initialize(CYContext &context, CYExpression *value) {
+ if (value == NULL) {
+ if (declaration_->initialiser_ == NULL)
+ return NULL;
+ value = declaration_->initialiser_;
+ }
+
+ return $ CYLet(constant_, $L1($ CYDeclaration(declaration_->identifier_, value)));
+}
+
+CYTarget *CYForLexical::Replace(CYContext &context) {
+ _assert(declaration_->Replace(context, CYIdentifierLexical) == NULL);
+ return declaration_->Target(context);
+}
+CYStatement *CYForIn::Replace(CYContext &context) {
+ CYScope scope(true, context);
context.Replace(initialiser_);
context.Replace(set_);
context.ReplaceAll(code_);
+ scope.Close(context);
return this;
}
}
CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
- return $ CYForIn(declaration_->Variable(context), set_, CYComprehension::Replace(context, statement));
+ return $ CYForIn(declaration_->Target(context), set_, CYComprehension::Replace(context, statement));
}
CYStatement *CYForOf::Replace(CYContext &context) {
- if (CYAssignment *assignment = initialiser_->Assignment(context))
- return $ CYBlock($$->*
- $E(assignment)->*
- this
- );
-
- CYIdentifier *cys(context.Unique()), *cyt(context.Unique());
+ CYIdentifier *item(context.Unique()), *list(context.Unique());
return $ CYBlock($$
- ->* $ CYLet($L2($L(cys, set_), $L(cyt)))
- ->* $ CYForIn($V(cyt), $V(cys), $ CYBlock($$
- ->* initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))
+ ->* initialiser_->Initialize(context, NULL)
+ ->* $ CYLet(false, $L2($L(list, set_), $L(item)))
+ ->* $ CYForIn($V(item), $V(list), $ CYBlock($$
+ ->* initialiser_->Initialize(context, $M($V(list), $V(item)))
->* code_
)));
}
return $E($C0($F(NULL, $P1($L($I("$cys"))), $$->*
$E($ CYAssign($V(cys), set_))->*
- $ CYForIn(declaration_->Variable(context), $V(cys), $ CYBlock($$->*
- $E($ CYAssign(declaration_->Variable(context), $M($V(cys), declaration_->Variable(context))))->*
+ $ CYForIn(declaration_->Target(context), $V(cys), $ CYBlock($$->*
+ $E($ CYAssign(declaration_->Target(context), $M($V(cys), declaration_->Target(context))))->*
CYComprehension::Replace(context, statement)
))
)));
}
+CYStatement *CYForVariable::Initialize(CYContext &context, CYExpression *value) {
+ if (value == NULL) {
+ if (declaration_->initialiser_ == NULL)
+ return NULL;
+ value = declaration_->initialiser_;
+ }
+
+ return $ CYVar($L1($ CYDeclaration(declaration_->identifier_, value)));
+}
+
+CYTarget *CYForVariable::Replace(CYContext &context) {
+ _assert(declaration_->Replace(context, CYIdentifierVariable) == NULL);
+ return declaration_->Target(context);
+}
+
+// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
+#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
+//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
+
void CYFunction::Replace(CYContext &context) {
CYThisScope *_this(context.this_);
context.this_ = &this_;
scope.Close(context, code_);
}
-CYExpression *CYFunctionExpression::Replace(CYContext &context) {
+CYTarget *CYFunctionExpression::Replace(CYContext &context) {
CYScope scope(false, context);
if (name_ != NULL)
- context.scope_->Declare(context, name_, CYIdentifierOther);
+ name_ = name_->Replace(context, CYIdentifierOther);
+
CYFunction::Replace(context);
- scope.Close(context, code_);
+ scope.Close(context);
return this;
}
void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
- CYAssignment *assignment(initialiser_->Assignment(context));
- context.Replace(initialiser_);
+ CYAssignment *assignment(initialiser_->Replace(context, CYIdentifierArgument));
next_->Replace(context, statements);
if (assignment != NULL)
statements = $$->*
- // XXX: this cast is quite incorrect
- $ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
+ $ CYIf($ CYIdentical($ CYTypeOf(initialiser_->Target(context)), $S("undefined")), $$->*
$E(assignment)
)->*
statements;
}
CYStatement *CYFunctionStatement::Replace(CYContext &context) {
- context.scope_->Declare(context, name_, CYIdentifierOther);
+ name_ = name_->Replace(context, CYIdentifierOther);
CYFunction::Replace(context);
return this;
}
-CYIdentifier *CYIdentifier::Replace(CYContext &context) {
- if (replace_ != NULL && replace_ != this)
- return replace_->Replace(context);
- replace_ = context.scope_->Lookup(context, this);
- return replace_;
+CYIdentifier *CYIdentifier::Replace(CYContext &context, CYIdentifierKind kind) {
+ if (next_ == this)
+ return this;
+ if (next_ != NULL)
+ return next_->Replace(context, kind);
+ next_ = context.scope_->Declare(context, this, kind)->identifier_;
+ return next_;
}
CYStatement *CYIf::Return() {
return $ CYVar($L1($L($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
}
-CYExpression *CYIndirect::Replace(CYContext &context) {
+CYTarget *CYIndirect::Replace(CYContext &context) {
return $M(rhs_, $S("$cyi"));
}
-CYExpression *CYIndirectMember::Replace(CYContext &context) {
+CYTarget *CYIndirectMember::Replace(CYContext &context) {
return $M($ CYIndirect(object_), property_);
}
return this;
}
-CYExpression *CYLambda::Replace(CYContext &context) {
+CYTarget *CYLambda::Replace(CYContext &context) {
return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
}
CYStatement *CYLet::Replace(CYContext &context) {
- declarations_->Replace(context);
- if (CYExpression *expression = declarations_->Expression(context))
+ if (CYExpression *expression = declarations_->Replace(context, CYIdentifierLexical))
return $E(expression);
return $ CYEmpty();
}
namespace cy {
namespace Syntax {
-CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
+CYTarget *New::AddArgument(CYContext &context, CYExpression *value) {
CYSetLast(arguments_) = $ CYArgument(value);
return this;
}
-CYExpression *New::Replace(CYContext &context) {
+CYTarget *New::Replace(CYContext &context) {
context.Replace(constructor_);
arguments_->Replace(context);
return this;
return String(context);
}
-CYExpression *CYObject::Replace(CYContext &context) {
+CYTarget *CYObject::Replace(CYContext &context) {
CYBuilder builder;
if (properties_ != NULL)
properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
return this;
}
-CYExpression *CYParenthetical::Replace(CYContext &context) {
- return expression_;
+CYTarget *CYParenthetical::Replace(CYContext &context) {
+ // XXX: return expression_;
+ context.Replace(expression_);
+ return this;
}
CYExpression *CYPostfix::Replace(CYContext &context) {
context.Replace(value_);
}
-// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
-#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
-//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
-
-namespace {
- struct IdentifierUsageLess :
- std::binary_function<CYIdentifier *, CYIdentifier *, bool>
- {
- _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
- if (lhs->usage_ != rhs->usage_)
- return lhs->usage_ > rhs->usage_;
- return lhs < rhs;
- }
- };
-
- typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
-}
-
void CYScript::Replace(CYContext &context) {
- CYScope scope(true, context);
+ CYScope scope(false, context);
+ context.scope_->Damage();
context.nextlocal_ = $ CYNonLocal();
context.ReplaceAll(code_);
scope.Close(context, code_);
- size_t offset(0);
-
- CYCStringSet external;
- for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
- external.insert((*i)->Word());
-
- IdentifierUsages usages;
-
- if (offset < context.rename_.size())
- CYForEach (i, context.rename_[offset].identifier_)
- usages.insert(i);
-
- // XXX: totalling the probable occurrences and sorting by them would improve the result
- for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
- //std::cout << *i << ":" << (*i)->offset_ << std::endl;
+ unsigned offset(0);
+ for (std::vector<CYIdentifier *>::const_iterator i(context.replace_.begin()); i != context.replace_.end(); ++i) {
const char *name;
-
if (context.options_.verbose_)
- name = $pool.strcat("$", $pool.itoa(offset), NULL);
+ name = $pool.strcat("$", $pool.itoa(offset++), NULL);
else {
char id[8];
id[7] = '\0';
id:
- unsigned position(7), local(offset + 1);
+ unsigned position(7), local(offset++ + 1);
do {
unsigned index(local % (sizeof(MappingSet) - 1));
id[--position] = MappingSet[index];
} while (local != 0);
- if (external.find(id + position) != external.end()) {
- ++offset;
+ if (scope.Lookup(context, id + position) != NULL)
goto id;
- }
+ // XXX: at some point, this could become a keyword
name = $pool.strmemdup(id + position, 7 - position);
- // XXX: at some point, this could become a keyword
}
- CYForEach (identifier, i->identifier_)
- identifier->Set(name);
+ CYIdentifier *identifier(*i);
+ _assert(identifier->next_ == identifier);
+ identifier->next_ = $I(name);
}
}
return this;
}
-CYExpression *CYRubyBlock::Replace(CYContext &context) {
+CYTarget *CYRubyBlock::Replace(CYContext &context) {
return call_->AddArgument(context, proc_->Replace(context));
}
-CYExpression *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
+CYTarget *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
return Replace(context)->AddArgument(context, value);
}
-CYExpression *CYRubyProc::Replace(CYContext &context) {
+CYTarget *CYRubyProc::Replace(CYContext &context) {
CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
function = CYNonLocalize(context, function);
function->implicit_ = true;
CYScope::CYScope(bool transparent, CYContext &context) :
transparent_(transparent),
- parent_(context.scope_)
+ parent_(context.scope_),
+ damaged_(false),
+ shadow_(NULL),
+ internal_(NULL)
{
+ _assert(!transparent_ || parent_ != NULL);
context.scope_ = this;
}
-void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
- if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
- internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
- else if (parent_ != NULL)
- parent_->Declare(context, identifier, flags);
+void CYScope::Damage() {
+ damaged_ = true;
+ if (parent_ != NULL)
+ parent_->Damage();
}
-CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
- std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
- return *insert.first;
+CYIdentifierFlags *CYScope::Lookup(CYContext &context, const char *word) {
+ CYForEach (i, internal_)
+ if (strcmp(i->identifier_->Word(), word) == 0)
+ return i;
+ return NULL;
}
-void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
- std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
- if (!insert.second) {
- if ((*insert.first)->offset_ < identifier->offset_)
- (*insert.first)->offset_ = identifier->offset_;
- identifier->replace_ = *insert.first;
- (*insert.first)->usage_ += identifier->usage_ + 1;
- }
+CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
+ return Lookup(context, identifier->Word());
}
-namespace {
- struct IdentifierOffset {
- size_t offset_;
- CYIdentifierFlags flags_;
- size_t usage_;
- CYIdentifier *identifier_;
+CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
+ _assert(identifier->next_ == NULL || identifier->next_ == identifier);
- IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
- offset_(identifier->offset_),
- flags_(flags),
- usage_(identifier->usage_),
- identifier_(identifier)
- {
- }
- };
+ CYIdentifierFlags *existing(Lookup(context, identifier));
+ if (existing == NULL)
+ internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
+ ++internal_->count_;
+ if (existing == NULL)
+ return internal_;
- struct IdentifierOffsetLess :
- std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
- {
- _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
- if (lhs.offset_ != rhs.offset_)
- return lhs.offset_ < rhs.offset_;
- if (lhs.flags_ != rhs.flags_)
- return lhs.flags_ < rhs.flags_;
- /*if (lhs.usage_ != rhs.usage_)
- return lhs.usage_ < rhs.usage_;*/
- return lhs.identifier_ < rhs.identifier_;
- }
- };
+ switch (kind) {
+ case CYIdentifierArgument:
+ case CYIdentifierCatch:
+ case CYIdentifierMagic:
+ _assert(false);
+ default:
+ break;
+ }
+
+ if (existing->kind_ == CYIdentifierGlobal)
+ existing->kind_ = kind;
+ else if (kind == CYIdentifierGlobal)
+ ;
+ else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
+ _assert(false); // XXX: throw new SyntaxError()
- typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
+ return existing;
+}
+
+void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
+ _assert(flags->identifier_->next_ == flags->identifier_);
+ CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
+ flags->identifier_->next_ = existing->identifier_;
+
+ existing->count_ += flags->count_;
+ if (existing->offset_ < flags->offset_)
+ existing->offset_ = flags->offset_;
}
void CYScope::Close(CYContext &context, CYStatement *&statements) {
+ Close(context);
+
+ CYList<CYDeclarations> declarations;
+
+ CYForEach (i, internal_)
+ if (i->kind_ == CYIdentifierVariable)
+ declarations ->* $ CYDeclarations($ CYDeclaration(i->identifier_));
+
+ if (declarations) {
+ CYVar *var($ CYVar(declarations));
+ var->SetNext(statements);
+ statements = var;
+ }
+}
+
+void CYScope::Close(CYContext &context) {
context.scope_ = parent_;
- if (parent_ == NULL)
- return;
+ CYForEach (i, internal_) {
+ _assert(i->identifier_->next_ == i->identifier_);
+ switch (i->kind_) {
+ case CYIdentifierArgument: {
+ _assert(!transparent_);
+ } break;
+
+ case CYIdentifierLexical: {
+ if (!damaged_) {
+ CYIdentifier *replace(context.Unique());
+ replace->next_ = replace;
+ i->identifier_->next_ = replace;
+ i->identifier_ = replace;
+ }
+
+ if (!transparent_)
+ i->kind_ = CYIdentifierVariable;
+ else
+ parent_->Declare(context, i->identifier_, CYIdentifierVariable);
+ } break;
- CYDeclarations *last(NULL), *curr(NULL);
+ case CYIdentifierVariable: {
+ if (transparent_) {
+ parent_->Declare(context, i->identifier_, i->kind_);
+ i->kind_ = CYIdentifierGlobal;
+ }
+ } break;
+ default:; } }
- IdentifierOffsets offsets;
+ if (damaged_)
+ return;
- for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
- if (i->second != CYIdentifierMagic)
- offsets.insert(IdentifierOffset(i->first, i->second));
+ typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
+ CYIdentifierOffsetMap offsets;
- size_t offset(0);
+ CYForEach (i, internal_) {
+ _assert(i->identifier_->next_ == i->identifier_);
+ switch (i->kind_) {
+ case CYIdentifierArgument:
+ case CYIdentifierVariable:
+ offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
+ break;
+ default:; } }
- for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
- if (i->flags_ == CYIdentifierVariable) {
- CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
- if (last == NULL)
- last = next;
- if (curr != NULL)
- curr->SetNext(next);
- curr = next;
- }
+ unsigned offset(0);
- if (offset < i->offset_)
- offset = i->offset_;
- if (context.rename_.size() <= offset)
- context.rename_.resize(offset + 1);
+ for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
+ if (offset < i->first)
+ offset = i->first;
+ CYIdentifier *identifier(i->second);
- CYIdentifierUsage &rename(context.rename_[offset++]);
- i->identifier_->SetNext(rename.identifier_);
- rename.identifier_ = i->identifier_;
- rename.usage_ += i->identifier_->usage_ + 1;
- }
+ if (offset >= context.replace_.size())
+ context.replace_.resize(offset + 1, NULL);
+ CYIdentifier *&replace(context.replace_[offset++]);
- if (last != NULL) {
- CYVar *var($ CYVar(last));
- var->SetNext(statements);
- statements = var;
+ if (replace == NULL)
+ replace = identifier;
+ else {
+ _assert(replace->next_ == replace);
+ identifier->next_ = replace;
+ }
}
- for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
- if (internal_.find(*i) == internal_.end()) {
- //std::cout << *i << '=' << offset << std::endl;
- if ((*i)->offset_ < offset)
- (*i)->offset_ = offset;
- parent_->Merge(context, *i);
- }
+ if (parent_ == NULL)
+ return;
+
+ CYForEach (i, internal_) {
+ switch (i->kind_) {
+ case CYIdentifierGlobal: {
+ if (i->offset_ < offset)
+ i->offset_ = offset;
+ parent_->Merge(context, i);
+ } break;
+ default:; } }
}
CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
return this;
}
-CYExpression *CYSuperAccess::Replace(CYContext &context) {
+CYTarget *CYSuperAccess::Replace(CYContext &context) {
return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
}
-CYExpression *CYSuperCall::Replace(CYContext &context) {
+CYTarget *CYSuperCall::Replace(CYContext &context) {
return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
}
return this;
}
-CYExpression *CYTemplate::Replace(CYContext &context) {
+CYStatement *CYTarget::Initialize(CYContext &context, CYExpression *value) {
+ if (value == NULL)
+ return NULL;
+ return $E($ CYAssign(this, value));
+}
+
+CYTarget *CYTemplate::Replace(CYContext &context) {
return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
}
-CYExpression *CYThis::Replace(CYContext &context) {
+CYTarget *CYThis::Replace(CYContext &context) {
if (context.this_ != NULL)
return $V(context.this_->Identifier(context));
return this;
} }
-CYExpression *CYTrivial::Replace(CYContext &context) {
+CYTarget *CYTrivial::Replace(CYContext &context) {
return this;
}
namespace Syntax {
CYStatement *Try::Replace(CYContext &context) {
+ CYScope scope(true, context);
context.ReplaceAll(code_);
+ scope.Close(context);
+
catch_->Replace(context);
finally_->Replace(context);
return this;
} }
-CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeArrayOf::Replace_(CYContext &context, CYTarget *type) {
return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
}
-CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeBlockWith::Replace_(CYContext &context, CYTarget *type) {
return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
}
-CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeConstant::Replace_(CYContext &context, CYTarget *type) {
return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
}
return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
}
-CYExpression *CYTypeError::Replace(CYContext &context) {
+CYTarget *CYTypeError::Replace(CYContext &context) {
_assert(false);
return NULL;
}
-CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type)
+CYTarget *CYTypeModifier::Replace(CYContext &context, CYTarget *type) { $T(type)
return Replace_(context, type);
}
-CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeFunctionWith::Replace_(CYContext &context, CYTarget *type) {
return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
}
-CYExpression *CYTypeLong::Replace(CYContext &context) {
+CYTarget *CYTypeLong::Replace(CYContext &context) {
return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
}
-CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypePointerTo::Replace_(CYContext &context, CYTarget *type) {
return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
}
-CYExpression *CYTypeShort::Replace(CYContext &context) {
+CYTarget *CYTypeShort::Replace(CYContext &context) {
return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
}
-CYExpression *CYTypeSigned::Replace(CYContext &context) {
+CYTarget *CYTypeSigned::Replace(CYContext &context) {
return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
}
-CYExpression *CYTypeUnsigned::Replace(CYContext &context) {
+CYTarget *CYTypeUnsigned::Replace(CYContext &context) {
return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
}
-CYExpression *CYTypeVariable::Replace(CYContext &context) {
+CYTarget *CYTypeVariable::Replace(CYContext &context) {
return $V(name_);
}
-CYExpression *CYTypeVoid::Replace(CYContext &context) {
+CYTarget *CYTypeVoid::Replace(CYContext &context) {
return $N1($V("Type"), $ CYString("v"));
}
-CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) {
+CYTarget *CYTypeVolatile::Replace_(CYContext &context, CYTarget *type) {
return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
}
-CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
+CYTarget *CYTypedIdentifier::Replace(CYContext &context) {
return modifier_->Replace(context, specifier_->Replace(context));
}
}
CYStatement *CYVar::Replace(CYContext &context) {
- declarations_->Replace(context);
- if (CYExpression *expression = declarations_->Expression(context))
+ if (CYExpression *expression = declarations_->Replace(context, CYIdentifierVariable))
return $E(expression);
return $ CYEmpty();
}
-CYExpression *CYVariable::Replace(CYContext &context) {
- context.Replace(name_);
+CYTarget *CYVariable::Replace(CYContext &context) {
+ name_ = name_->Replace(context, CYIdentifierGlobal);
return this;
}
#define I(type, Type, value, highlight) do { \
yylval->semantic_.type ## _ = A CY ## Type; \
+ yylval->semantic_.type ## _->location_ = *yylloc; \
F(value, highlight); \
} while (false)
"enum" L /*FFF*/ F(tk::_enum_, hi::Meta);
"export" L /*FFK*/ F(tk::_export_, hi::Meta);
"extends" L /*FFK*/ F(tk::_extends_, hi::Meta);
+"eval" L /*III*/ F(tk::_eval_, hi::Special);
"false" L /*LLL*/ F(tk::_false_, hi::Constant);
"final" L /*FII*/ F(tk::_final_, hi::Meta);
"finally" L /*KKK*/ F(tk::_finally_, hi::Control);
#include <streambuf>
#include <string>
#include <vector>
-#include <map>
-#include <set>
#include "List.hpp"
#include "Location.hpp"
{
}
- void Set(const char *value) {
- word_ = value;
- }
-
virtual bool Constructor() const {
return strcmp(word_, "constructor") == 0;
}
return lhs << rhs.Word();
}
+enum CYIdentifierKind {
+ CYIdentifierArgument,
+ CYIdentifierCatch,
+ CYIdentifierGlobal,
+ CYIdentifierLexical,
+ CYIdentifierMagic,
+ CYIdentifierOther,
+ CYIdentifierVariable,
+};
+
struct CYIdentifier :
CYNext<CYIdentifier>,
CYWord
{
- CYIdentifier *replace_;
+ CYLocation location_;
size_t offset_;
size_t usage_;
CYIdentifier(const char *word) :
CYWord(word),
- replace_(NULL),
offset_(0),
usage_(0)
{
}
virtual const char *Word() const;
- CYIdentifier *Replace(CYContext &context);
+ CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
};
struct CYLabel :
}
};
-enum CYIdentifierFlags {
- CYIdentifierArgument,
- CYIdentifierVariable,
- CYIdentifierOther,
- CYIdentifierMagic,
- CYIdentifierCatch,
-};
-
-typedef std::set<const char *, CYCStringLess> CYCStringSet;
-typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
-typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
-
-struct CYIdentifierUsage {
+struct CYIdentifierFlags :
+ CYNext<CYIdentifierFlags>
+{
CYIdentifier *identifier_;
- size_t usage_;
-};
+ CYIdentifierKind kind_;
+ unsigned count_;
+ unsigned offset_;
-typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
+ CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
+ CYNext<CYIdentifierFlags>(next),
+ identifier_(identifier),
+ kind_(kind),
+ count_(0),
+ offset_(0)
+ {
+ }
+};
struct CYScope {
bool transparent_;
CYScope *parent_;
+ bool damaged_;
+ CYIdentifierFlags *shadow_;
- CYIdentifierAddressFlagsMap internal_;
- CYIdentifierValueSet identifiers_;
+ CYIdentifierFlags *internal_;
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);
+ CYIdentifierFlags *Lookup(CYContext &context, const char *word);
+ CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
+
+ CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
+ void Merge(CYContext &context, const CYIdentifierFlags *flags);
+
void Close(CYContext &context, CYStatement *&statements);
+ void Close(CYContext &context);
+ void Damage();
};
struct CYScript :
CYThisScope *this_;
CYIdentifier *super_;
- CYIdentifierUsageVector rename_;
-
CYNonLocal *nonlocal_;
CYNonLocal *nextlocal_;
unsigned unique_;
+ std::vector<CYIdentifier *> replace_;
+
CYContext(CYOptions &options) :
options_(options),
scope_(NULL),
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
-struct CYForInInitializer {
- virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
- virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
+struct CYTarget;
+struct CYVar;
- virtual CYExpression *Replace(CYContext &context) = 0;
- virtual CYAssignment *Assignment(CYContext &context) = 0;
+struct CYForInInitializer {
+ virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
+ virtual CYTarget *Replace(CYContext &context) = 0;
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
};
struct CYExpression :
CYForInitializer,
- CYForInInitializer,
CYThing
{
virtual int Precedence() const = 0;
return true;
}
- virtual void ForIn(CYOutput &out, CYFlags flags) const;
- virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
+ virtual bool Eval() const {
+ return false;
+ }
- virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+ virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
virtual void Output(CYOutput &out) const;
virtual void Output(CYOutput &out, CYFlags flags) const = 0;
void Output(CYOutput &out, int precedence, CYFlags flags) const;
virtual CYExpression *Replace(CYContext &context) = 0;
- virtual CYAssignment *Assignment(CYContext &context);
virtual CYExpression *Primitive(CYContext &context) {
return NULL;
}
};
+struct CYTarget :
+ CYExpression,
+ CYForInInitializer
+{
+ virtual bool RightHand() const {
+ return false;
+ }
+
+ virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
+
+ virtual CYTarget *Replace(CYContext &context) = 0;
+ using CYExpression::Output;
+};
+
#define CYAlphabetic(value) \
virtual bool Alphabetic() const { \
return value; \
return Precedence_; \
}
-#define CYRightHand(value) \
- virtual bool RightHand() const { \
- return value; \
- }
-
struct CYCompound :
CYExpression
{
};
struct CYParenthetical :
- CYExpression
+ CYTarget
{
CYExpression *expression_;
CYPrecedence(0)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
void Output(CYOutput &out, CYFlags flags) const;
};
CYNext<CYFunctionParameter>,
CYThing
{
- CYForInInitializer *initialiser_;
+ CYDeclaration *initialiser_;
- CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
+ CYFunctionParameter(CYDeclaration *initialiser, CYFunctionParameter *next = NULL) :
CYNext<CYFunctionParameter>(next),
initialiser_(initialiser)
{
};
struct CYArrayComprehension :
- CYExpression
+ CYTarget
{
CYExpression *expression_;
CYComprehension *comprehensions_;
CYPrecedence(0)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYLiteral :
- CYExpression
+ CYTarget
{
+ CYLocation location_;
+
CYPrecedence(0)
- CYRightHand(false)
virtual CYExpression *Primitive(CYContext &context) {
return this;
struct CYTrivial :
CYLiteral
{
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
};
struct CYMagic :
- CYExpression
+ CYTarget
{
CYPrecedence(0)
- CYRightHand(false)
};
struct CYRange {
};
struct CYTemplate :
- CYExpression
+ CYTarget
{
CYString *string_;
CYSpan *spans_;
}
CYPrecedence(0)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYThis :
CYMagic
{
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
};
struct CYVariable :
- CYExpression
+ CYTarget
{
CYIdentifier *name_;
}
CYPrecedence(0)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual bool Eval() const {
+ return strcmp(name_->Word(), "eval") == 0;
+ }
+
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
virtual CYFunctionParameter *Parameter() const;
struct CYAssignment :
CYExpression
{
- CYExpression *lhs_;
+ CYTarget *lhs_;
CYExpression *rhs_;
- CYAssignment(CYExpression *lhs, CYExpression *rhs) :
+ CYAssignment(CYTarget *lhs, CYExpression *rhs) :
lhs_(lhs),
rhs_(rhs)
{
}
- void SetLeft(CYExpression *lhs) {
+ void SetLeft(CYTarget *lhs) {
lhs_ = lhs;
}
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
-struct CYDeclaration :
- CYForInInitializer
-{
+struct CYDeclaration {
CYIdentifier *identifier_;
CYExpression *initialiser_;
{
}
- virtual void ForIn(CYOutput &out, CYFlags flags) const;
- virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
+ CYTarget *Target(CYContext &context);
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
+
+struct CYForLexical :
+ CYForInInitializer
+{
+ bool constant_;
+ CYDeclaration *declaration_;
+
+ CYForLexical(bool constant, CYDeclaration *declaration) :
+ constant_(constant),
+ declaration_(declaration)
+ {
+ }
+
+ virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
+
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+};
- virtual CYAssignment *Assignment(CYContext &context);
- CYVariable *Variable(CYContext &context);
+struct CYForVariable :
+ CYForInInitializer
+{
+ CYDeclaration *declaration_;
+ CYForVariable(CYDeclaration *declaration) :
+ declaration_(declaration)
+ {
+ }
+
+ virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
+
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
{
}
- void Replace(CYContext &context);
+ CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
- CYExpression *Expression(CYContext &context);
CYArgument *Argument(CYContext &context);
CYFunctionParameter *Parameter(CYContext &context);
struct CYLet :
CYStatement
{
+ bool constant_;
CYDeclarations *declarations_;
- CYLet(CYDeclarations *declarations) :
+ CYLet(bool constant, CYDeclarations *declarations) :
+ constant_(constant),
declarations_(declarations)
{
}
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
void Output(CYOutput &out, CYFlags flags) const;
};
struct CYMember :
- CYExpression
+ CYTarget
{
CYExpression *object_;
CYExpression *property_;
}
CYPrecedence(1)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
}
CYPrecedence(1)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
namespace Syntax {
struct New :
- CYExpression
+ CYTarget
{
CYExpression *constructor_;
CYArgument *arguments_;
return arguments_ == NULL ? 2 : 1;
}
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
- virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+ virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
};
} }
-struct CYCall :
- CYExpression
+struct CYApply :
+ CYTarget
{
- CYExpression *function_;
CYArgument *arguments_;
- CYCall(CYExpression *function, CYArgument *arguments = NULL) :
- function_(function),
+ CYApply(CYArgument *arguments = NULL) :
arguments_(arguments)
{
}
CYPrecedence(1)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
+};
+
+struct CYCall :
+ CYApply
+{
+ CYExpression *function_;
+
+ CYCall(CYExpression *function, CYArgument *arguments = NULL) :
+ CYApply(arguments),
+ function_(function)
+ {
+ }
+
virtual void Output(CYOutput &out, CYFlags flags) const;
+ virtual CYTarget *Replace(CYContext &context);
+};
+
+struct CYEval :
+ CYApply
+{
+ CYEval(CYArgument *arguments) :
+ CYApply(arguments)
+ {
+ }
- virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
+ virtual CYTarget *Replace(CYContext &context);
};
struct CYRubyProc;
struct CYRubyBlock :
- CYExpression
+ CYTarget
{
CYExpression *call_;
CYRubyProc *proc_;
}
CYPrecedence(1)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
- virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
+ virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
};
struct CYIf :
struct CYFunctionExpression :
CYFunction,
- CYExpression
+ CYTarget
{
CYIdentifier *name_;
}
CYPrecedence(0)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ CYTarget *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
}
CYPrecedence(0)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ CYExpression *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYRubyProc :
CYFunction,
- CYExpression
+ CYTarget
{
CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
CYFunction(parameters, code)
}
CYPrecedence(0)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ CYTarget *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYCompact(None)
- virtual CYStatement *Replace(CYContext &context);
+ CYStatement *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
};
struct CYClassExpression :
- CYExpression
+ CYTarget
{
CYIdentifier *name_;
CYClassTail *tail_;
}
CYPrecedence(0)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ CYTarget *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYCompact(Long)
- virtual CYStatement *Replace(CYContext &context);
+ CYStatement *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYSuperCall :
- CYExpression
+ CYTarget
{
CYArgument *arguments_;
}
CYPrecedence(2)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ CYTarget *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYSuperAccess :
- CYExpression
+ CYTarget
{
CYExpression *property_;
}
CYPrecedence(1)
- CYRightHand(false)
- virtual CYExpression *Replace(CYContext &context);
+ CYTarget *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYCompact(None)
- virtual CYStatement *Replace(CYContext &context);
+ CYStatement *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
virtual CYStatement *Return();
CYCompact(Short)
- virtual CYStatement *Replace(CYContext &context);
+ CYStatement *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYCompact(Short)
- virtual CYStatement *Replace(CYContext &context);
+ CYStatement *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYCompact(None)
- virtual CYStatement *Replace(CYContext &context);
+ CYStatement *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYPrecedence(0)
- virtual CYExpression *Replace(CYContext &context);
+ CYExpression *Replace(CYContext &context) override;
virtual void Output(CYOutput &out, CYFlags flags) const;
};
struct CYTypeSpecifier :
CYThing
{
- virtual CYExpression *Replace(CYContext &context) = 0;
+ virtual CYTarget *Replace(CYContext &context) = 0;
};
struct CYTypeError :
CYTypeError() {
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
CYTypeVoid() {
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
{
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
};
virtual int Precedence() const = 0;
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
- CYExpression *Replace(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
+ CYTarget *Replace(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
CYPrecedence(1)
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
CYPrecedence(0)
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
CYPrecedence(0)
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
CYPrecedence(0)
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
return this;
}
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out) const;
CYTypeFunctionWith *Function();
};
struct CYEncodedType :
- CYExpression
+ CYTarget
{
CYTypedIdentifier *typed_;
CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
};
struct CYLambda :
- CYExpression
+ CYTarget
{
CYTypedIdentifier *typed_;
CYTypedParameter *parameters_;
CYPrecedence(1)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
virtual void Output(CYOutput &out, CYFlags flags) const;
};
CYPrecedence(0)
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
};
CYPrecedence(1)
- virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
+ virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
virtual CYTypeFunctionWith *Function() { return this; }
};
struct CYIndirect :
- CYPrefix
+ CYTarget
{
+ CYExpression *rhs_;
+
CYIndirect(CYExpression *rhs) :
- CYPrefix(rhs)
+ rhs_(rhs)
{
}
- virtual const char *Operator() const {
- return "*";
- }
-
- CYAlphabetic(false)
+ // XXX: this should be checked
+ CYPrecedence(2)
- virtual CYExpression *Replace(CYContext &context);
+ virtual CYTarget *Replace(CYContext &context);
+ virtual void Output(CYOutput &out, CYFlags flags) const;
};
#define CYReplace \
struct CY ## name ## Assign : \
CYAssignment \
{ args \
- CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
+ CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
CYAssignment(lhs, rhs) \
{ \
} \