satyr commit b2cf91 ... no more @tags

This commit is contained in:
Jeremy Ashkenas
2010-11-01 23:25:28 -04:00
parent b94c15bdcc
commit 4788f842c0
2 changed files with 52 additions and 93 deletions

View File

@@ -22,7 +22,6 @@
exports.Base = (function() {
Base = (function() {
function Base() {
this.tags = {};
return this;
}
return Base;
@@ -170,7 +169,6 @@
exports.Expressions = (function() {
Expressions = (function() {
function Expressions(nodes) {
Expressions.__super__.constructor.call(this);
this.expressions = compact(flatten(nodes || []));
return this;
}
@@ -250,7 +248,7 @@
var code;
node = node.unwrapAll();
node = node.unfoldSoak(o) || node;
node.tags.front = true;
node.front = true;
o.level = LEVEL_TOP;
code = node.compile(o);
return node.isStatement(o) ? code : this.tab + code + ';';
@@ -267,7 +265,6 @@
Literal = (function() {
function Literal(_arg) {
this.value = _arg;
Literal.__super__.constructor.call(this);
return this;
}
return Literal;
@@ -299,7 +296,6 @@
Return = (function() {
function Return(_arg) {
this.expression = _arg;
Return.__super__.constructor.call(this);
return this;
}
return Return;
@@ -323,13 +319,14 @@
exports.Value = (function() {
Value = (function() {
function Value(base, props, tag) {
var _obj;
if (!props && base instanceof Value) {
return base;
}
this.base = base;
this.properties = props || [];
this.tags = tag ? (_obj = {}, _obj[tag] = true, _obj) : {};
if (tag) {
this[tag] = true;
}
return this;
}
return Value;
@@ -404,7 +401,7 @@
};
Value.prototype.compileNode = function(o) {
var _i, _len, code, prop, props;
this.base.tags.front = this.tags.front;
this.base.front = this.front;
props = this.properties;
code = this.base.compile(o, props.length ? LEVEL_ACCESS : null);
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
@@ -447,7 +444,6 @@
Comment = (function() {
function Comment(_arg) {
this.comment = _arg;
Comment.__super__.constructor.call(this);
return this;
}
return Comment;
@@ -466,7 +462,6 @@
function Call(variable, _arg, _arg2) {
this.args = _arg != null ? _arg : [];
this.soak = _arg2;
Call.__super__.constructor.call(this);
this.isNew = false;
this.isSuper = variable === 'super';
this.variable = this.isSuper ? null : variable;
@@ -544,7 +539,7 @@
Call.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _result, arg, args;
if ((_ref2 = this.variable) != null) {
_ref2.tags.front = this.tags.front;
_ref2.front = this.front;
}
_ref3 = this.args;
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
@@ -596,7 +591,6 @@
function Extends(_arg, _arg2) {
this.child = _arg;
this.parent = _arg2;
Extends.__super__.constructor.call(this);
return this;
}
return Extends;
@@ -612,7 +606,6 @@
Accessor = (function() {
function Accessor(_arg, tag) {
this.name = _arg;
Accessor.__super__.constructor.call(this);
this.proto = tag === 'proto' ? '.prototype' : '';
this.soak = tag === 'soak';
return this;
@@ -633,7 +626,6 @@
Index = (function() {
function Index(_arg) {
this.index = _arg;
Index.__super__.constructor.call(this);
return this;
}
return Index;
@@ -651,7 +643,6 @@
exports.Obj = (function() {
Obj = (function() {
function Obj(props) {
Obj.__super__.constructor.call(this);
this.objects = this.properties = props || [];
return this;
}
@@ -689,7 +680,7 @@
prop = _ref3[i];
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : idt;
if (prop instanceof Value && prop.tags["this"]) {
if (prop instanceof Value && prop["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
} else if (!(prop instanceof Assign) && !(prop instanceof Comment)) {
prop = new Assign(prop, prop, 'object');
@@ -703,7 +694,7 @@
if (rest) {
return this.compileDynamic(o, obj, rest);
}
return this.tags.front ? "(" + obj + ")" : obj;
return this.front ? "(" + obj + ")" : obj;
};
Obj.prototype.compileDynamic = function(o, code, props) {
var _len, _ref2, acc, i, key, oref, prop, ref, val;
@@ -747,7 +738,6 @@
exports.Arr = (function() {
Arr = (function() {
function Arr(objs) {
Arr.__super__.constructor.call(this);
this.objects = objs || [];
return this;
}
@@ -790,12 +780,10 @@
})();
exports.Class = (function() {
Class = (function() {
function Class(_arg, _arg2, props) {
function Class(_arg, _arg2, _arg3) {
this.variable = _arg;
this.parent = _arg2;
Class.__super__.constructor.call(this);
this.properties = props || [];
this.returns = false;
this.properties = _arg3 != null ? _arg3 : [];
return this;
}
return Class;
@@ -894,7 +882,6 @@
this.variable = _arg;
this.value = _arg2;
this.context = _arg3;
Assign.__super__.constructor.call(this);
return this;
}
return Assign;
@@ -952,7 +939,7 @@
if (obj.base instanceof Parens) {
_ref4 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1];
} else {
idx = isObject ? obj.tags["this"] ? obj.properties[0].name : obj : new Literal(0);
idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0);
}
}
acc = IDENTIFIER.test(idx.unwrap().value || 0);
@@ -977,7 +964,7 @@
if (obj.base instanceof Parens) {
_ref7 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref7[0], idx = _ref7[1];
} else {
idx = obj.tags["this"] ? obj.properties[0].name : obj;
idx = obj["this"] ? obj.properties[0].name : obj;
}
}
}
@@ -1024,7 +1011,6 @@
function Code(_arg, _arg2, tag) {
this.params = _arg != null ? _arg : [];
this.body = _arg2 != null ? _arg2 : new Expressions;
Code.__super__.constructor.call(this);
this.bound = tag === 'boundfunc';
if (this.bound) {
this.context = 'this';
@@ -1116,7 +1102,7 @@
if (this.bound) {
return "" + (utility('bind')) + "(" + func + ", " + this.context + ")";
}
return this.tags.front ? "(" + func + ")" : func;
return this.front ? "(" + func + ")" : func;
};
Code.prototype.traverseChildren = function(crossScope, func) {
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : void 0;
@@ -1129,7 +1115,6 @@
this.name = _arg;
this.value = _arg2;
this.splat = _arg3;
Param.__super__.constructor.call(this);
return this;
}
return Param;
@@ -1159,7 +1144,6 @@
exports.Splat = (function() {
Splat = (function() {
function Splat(name) {
Splat.__super__.constructor.call(this);
this.name = name.compile ? name : new Literal(name);
return this;
}
@@ -1201,10 +1185,9 @@
}).call(this);
exports.While = (function() {
While = (function() {
function While(condition, opts) {
While.__super__.constructor.call(this);
this.condition = (opts != null ? opts.invert : void 0) ? condition.invert() : condition;
this.guard = opts != null ? opts.guard : void 0;
function While(condition, options) {
this.condition = (options != null ? options.invert : void 0) ? condition.invert() : condition;
this.guard = options != null ? options.guard : void 0;
return this;
}
return While;
@@ -1263,7 +1246,6 @@
first = new Parens(first);
}
}
Op.__super__.constructor.call(this);
this.operator = this.CONVERSIONS[op] || op;
this.first = first;
this.second = second;
@@ -1311,7 +1293,7 @@
if (this.operator === '?') {
return this.compileExistence(o);
}
this.first.tags.front = this.tags.front;
this.first.front = this.front;
return "" + (this.first.compile(o, LEVEL_OP)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
};
Op.prototype.compileChain = function(o) {
@@ -1357,7 +1339,6 @@
function In(_arg, _arg2) {
this.object = _arg;
this.array = _arg2;
In.__super__.constructor.call(this);
return this;
}
return In;
@@ -1409,7 +1390,6 @@
this.error = _arg2;
this.recovery = _arg3;
this.ensure = _arg4;
Try.__super__.constructor.call(this);
return this;
}
return Try;
@@ -1439,7 +1419,6 @@
Throw = (function() {
function Throw(_arg) {
this.expression = _arg;
Throw.__super__.constructor.call(this);
return this;
}
return Throw;
@@ -1457,7 +1436,6 @@
Existence = (function() {
function Existence(_arg) {
this.expression = _arg;
Existence.__super__.constructor.call(this);
return this;
}
return Existence;
@@ -1476,7 +1454,6 @@
Parens = (function() {
function Parens(_arg) {
this.expression = _arg;
Parens.__super__.constructor.call(this);
return this;
}
return Parens;
@@ -1496,7 +1473,7 @@
var bare, code, expr;
expr = this.expression;
if (expr instanceof Value && expr.isAtomic()) {
expr.tags.front = this.tags.front;
expr.front = this.front;
return expr.compile(o);
}
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call);
@@ -1512,7 +1489,6 @@
if (head.index instanceof Value) {
throw SyntaxError('index cannot be a pattern matching expression');
}
For.__super__.constructor.call(this);
extend(this, head);
if (!this.object) {
this.step || (this.step = new Literal(1));
@@ -1637,7 +1613,6 @@
this.subject = _arg;
this.cases = _arg2;
this.otherwise = _arg3;
Switch.__super__.constructor.call(this);
return this;
}
return Switch;
@@ -1698,13 +1673,13 @@
})();
exports.If = (function() {
If = (function() {
function If(condition, _arg, _arg2) {
function If(condition, _arg, options) {
this.body = _arg;
this.tags = _arg2 != null ? _arg2 : {};
this.condition = this.tags.invert ? condition.invert() : condition;
options == null && (options = {});
this.condition = options.invert ? condition.invert() : condition;
this.elseBody = null;
this.isChain = false;
this.soak = this.tags.soak;
this.soak = options.soak;
return this;
}
return If;

View File

@@ -28,8 +28,6 @@ THIS = -> this
# scope, and indentation level.
exports.Base = class Base
constructor: -> @tags = {}
# Common logic for determining whether to wrap this node in a closure before
# compiling it, or to compile directly. We need to wrap if this node is a
# *statement*, and it's not a *pureStatement*, and we're not at
@@ -166,7 +164,6 @@ exports.Expressions = class Expressions extends Base
isStatement: YES
constructor: (nodes) ->
super()
@expressions = compact flatten nodes or []
# Tack an expression on to the end of this expression list.
@@ -234,7 +231,7 @@ exports.Expressions = class Expressions extends Base
compileExpression: (node, o) ->
node = node.unwrapAll()
node = node.unfoldSoak(o) or node
node.tags.front = on
node.front = yes
o.level = LEVEL_TOP
code = node.compile o
if node.isStatement o then code else @tab + code + ';'
@@ -252,7 +249,7 @@ exports.Expressions = class Expressions extends Base
# `true`, `false`, `null`...
exports.Literal = class Literal extends Base
constructor: (@value) -> super()
constructor: (@value) ->
makeReturn: -> if @isStatement() then this else super()
@@ -281,7 +278,7 @@ exports.Return = class Return extends Base
isStatement : YES
isPureStatement: YES
constructor: (@expression) -> super()
constructor: (@expression) ->
makeReturn: THIS
@@ -306,7 +303,7 @@ exports.Value = class Value extends Base
return base if not props and base instanceof Value
@base = base
@properties = props or []
@tags = if tag then {(tag): on} else {}
@[tag] = true if tag
# Add a property access to the list.
push: (prop) ->
@@ -358,7 +355,7 @@ exports.Value = class Value extends Base
# operators `?.` interspersed. Then we have to take care not to accidentally
# evaluate anything twice when building the soak chain.
compileNode: (o) ->
@base.tags.front = @tags.front
@base.front = @front
props = @properties
code = @base.compile o, if props.length then LEVEL_ACCESS else null
code = "(#{code})" if props[0] instanceof Accessor and @isSimpleNumber()
@@ -390,7 +387,7 @@ exports.Comment = class Comment extends Base
isPureStatement: YES
isStatement: YES
constructor: (@comment) -> super()
constructor: (@comment) ->
makeReturn: THIS
@@ -405,7 +402,6 @@ exports.Call = class Call extends Base
children: ['variable', 'args']
constructor: (variable, @args = [], @soak) ->
super()
@isNew = false
@isSuper = variable is 'super'
@variable = if @isSuper then null else variable
@@ -460,7 +456,7 @@ exports.Call = class Call extends Base
# Compile a vanilla function call.
compileNode: (o) ->
@variable?.tags.front = @tags.front
@variable?.front = @front
for arg in @args when arg instanceof Splat
return @compileSplat o
args = (arg.compile o, LEVEL_LIST for arg in @args).join ', '
@@ -508,7 +504,7 @@ exports.Extends = class Extends extends Base
children: ['child', 'parent']
constructor: (@child, @parent) -> super()
constructor: (@child, @parent) ->
# Hooks one constructor into another's prototype chain.
compile: (o) ->
@@ -523,7 +519,6 @@ exports.Accessor = class Accessor extends Base
children: ['name']
constructor: (@name, tag) ->
super()
@proto = if tag is 'proto' then '.prototype' else ''
@soak = tag is 'soak'
@@ -540,7 +535,7 @@ exports.Index = class Index extends Base
children: ['index']
constructor: (@index) -> super()
constructor: (@index) ->
compile: (o) ->
(if @proto then '.prototype' else '') + "[#{ @index.compile o, LEVEL_PAREN }]"
@@ -555,7 +550,6 @@ exports.Obj = class Obj extends Base
children: ['properties']
constructor: (props) ->
super()
@objects = @properties = props or []
compileNode: (o) ->
@@ -573,7 +567,7 @@ exports.Obj = class Obj extends Base
else
',\n'
indent = if prop instanceof Comment then '' else idt
if prop instanceof Value and prop.tags.this
if prop instanceof Value and prop.this
prop = new Assign prop.properties[0].name, prop, 'object'
else if prop not instanceof Assign and prop not instanceof Comment
prop = new Assign prop, prop, 'object'
@@ -581,7 +575,7 @@ exports.Obj = class Obj extends Base
props = props.join ''
obj = "{#{ props and '\n' + props + '\n' + @tab }}"
return @compileDynamic o, obj, rest if rest
if @tags.front then "(#{obj})" else obj
if @front then "(#{obj})" else obj
compileDynamic: (o, code, props) ->
code = "#{ oref = o.scope.freeVariable 'obj' } = #{code}, "
@@ -617,7 +611,6 @@ exports.Arr = class Arr extends Base
children: ['objects']
constructor: (objs) ->
super()
@objects = objs or []
compileNode: (o) ->
@@ -655,10 +648,7 @@ exports.Class = class Class extends Base
# Initialize a **Class** with its name, an optional superclass, and a
# list of prototype property assignments.
constructor: (@variable, @parent, props) ->
super()
@properties = props or []
@returns = false
constructor: (@variable, @parent, @properties = []) ->
makeReturn: ->
@returns = true
@@ -739,7 +729,7 @@ exports.Assign = class Assign extends Base
children: ['variable', 'value']
constructor: (@variable, @value, @context) -> super()
constructor: (@variable, @value, @context) ->
assigns: (name) ->
@[if @context is 'object' then 'value' else 'variable'].assigns name
@@ -786,7 +776,7 @@ exports.Assign = class Assign extends Base
[obj, idx] = new Value(obj.unwrapAll()).cacheReference o
else
idx = if isObject
if obj.tags.this then obj.properties[0].name else obj
if obj.this then obj.properties[0].name else obj
else
new Literal 0
acc = IDENTIFIER.test idx.unwrap().value or 0
@@ -811,7 +801,7 @@ exports.Assign = class Assign extends Base
if obj.base instanceof Parens
[obj, idx] = new Value(obj.unwrapAll()).cacheReference o
else
idx = if obj.tags.this then obj.properties[0].name else obj
idx = if obj.this then obj.properties[0].name else obj
if not splat and obj instanceof Splat
val = "#{olen} <= #{vvar}.length ? #{ utility 'slice' }.call(#{vvar}, #{i}"
if rest = olen - i - 1
@@ -854,7 +844,6 @@ exports.Code = class Code extends Base
children: ['params', 'body']
constructor: (@params = [], @body = new Expressions, tag) ->
super()
@bound = tag is 'boundfunc'
@context = 'this' if @bound
@@ -907,7 +896,7 @@ exports.Code = class Code extends Base
func = "#{open}#{ vars.join ', ' }) {#{code}#{close}"
scope.endLevel()
return "#{ utility 'bind' }(#{func}, #{@context})" if @bound
if @tags.front then "(#{func})" else func
if @front then "(#{func})" else func
# Short-circuit `traverseChildren` method to prevent it from crossing scope boundaries
# unless `crossScope` is `true`.
@@ -923,7 +912,6 @@ exports.Param = class Param extends Base
children: ['name', 'value']
constructor: (@name, @value, @splat) ->
super()
compile: (o) -> @name.compile o, LEVEL_LIST
@@ -947,7 +935,6 @@ exports.Splat = class Splat extends Base
isAssignable: YES
constructor: (name) ->
super()
@name = if name.compile then name else new Literal name
assigns: (name) -> @name.assigns name
@@ -984,10 +971,9 @@ exports.While = class While extends Base
isStatement: YES
constructor: (condition, opts) ->
super()
@condition = if opts?.invert then condition.invert() else condition
@guard = opts?.guard
constructor: (condition, options) ->
@condition = if options?.invert then condition.invert() else condition
@guard = options?.guard
addBody: (body) ->
@body = body
@@ -1043,7 +1029,6 @@ exports.Op = class Op extends Base
if op is 'new'
return first.newInstance() if first instanceof Call
first = new Parens first if first instanceof Code and first.bound
super()
@operator = @CONVERSIONS[op] or op
@first = first
@second = second
@@ -1071,7 +1056,7 @@ exports.Op = class Op extends Base
return @compileUnary o if @isUnary()
return @compileChain o if @isChainable() and @first.isChainable()
return @compileExistence o if @operator is '?'
@first.tags.front = @tags.front
@first.front = @front
"#{ @first.compile o, LEVEL_OP } #{@operator} #{ @second.compile o, LEVEL_OP }"
# Mimic Python's chained comparisons when multiple comparison operators are
@@ -1111,7 +1096,7 @@ exports.In = class In extends Base
children: ['object', 'array']
constructor: (@object, @array) -> super()
constructor: (@object, @array) ->
invert: ->
@negated = not @negated
@@ -1151,7 +1136,7 @@ exports.Try = class Try extends Base
isStatement: YES
constructor: (@attempt, @error, @recovery, @ensure) -> super()
constructor: (@attempt, @error, @recovery, @ensure) ->
makeReturn: ->
@attempt = @attempt .makeReturn() if @attempt
@@ -1182,7 +1167,7 @@ exports.Throw = class Throw extends Base
isStatement: YES
constructor: (@expression) -> super()
constructor: (@expression) ->
# A **Throw** is already a return, of sorts...
makeReturn: THIS
@@ -1198,7 +1183,7 @@ exports.Existence = class Existence extends Base
children: ['expression']
constructor: (@expression) -> super()
constructor: (@expression) ->
compileNode: (o) ->
code = @expression.compile o
@@ -1219,7 +1204,7 @@ exports.Parens = class Parens extends Base
children: ['expression']
constructor: (@expression) -> super()
constructor: (@expression) ->
unwrap : -> @expression
isComplex : -> @expression.isComplex()
@@ -1228,7 +1213,7 @@ exports.Parens = class Parens extends Base
compileNode: (o) ->
expr = @expression
if expr instanceof Value and expr.isAtomic()
expr.tags.front = @tags.front
expr.front = @front
return expr.compile o
bare = o.level < LEVEL_OP and (expr instanceof Op or expr instanceof Call)
code = expr.compile o, LEVEL_PAREN
@@ -1252,7 +1237,6 @@ exports.For = class For extends Base
constructor: (@body, head) ->
if head.index instanceof Value
throw SyntaxError 'index cannot be a pattern matching expression'
super()
extend this, head
@step or= new Literal 1 unless @object
@pattern = @name instanceof Value
@@ -1341,7 +1325,7 @@ exports.Switch = class Switch extends Base
isStatement: YES
constructor: (@subject, @cases, @otherwise) -> super()
constructor: (@subject, @cases, @otherwise) ->
makeReturn: ->
pair[1].makeReturn() for pair in @cases
@@ -1375,11 +1359,11 @@ exports.If = class If extends Base
children: ['condition', 'body', 'elseBody']
constructor: (condition, @body, @tags = {}) ->
@condition = if @tags.invert then condition.invert() else condition
constructor: (condition, @body, options = {}) ->
@condition = if options.invert then condition.invert() else condition
@elseBody = null
@isChain = false
{@soak} = @tags
{@soak} = options
bodyNode: -> @body?.unwrap()
elseBodyNode: -> @elseBody?.unwrap()