nodes: removed Value.wrap

This commit is contained in:
satyr
2010-10-25 08:13:58 +09:00
parent ab2050dc59
commit 7bfb2e3a78
2 changed files with 25 additions and 28 deletions

View File

@@ -325,13 +325,14 @@
})();
exports.Value = (function() {
Value = (function() {
function Value(_arg, props, tag) {
this.base = _arg;
Value.__super__.constructor.call(this);
this.properties = props || [];
if (tag) {
this.tags[tag] = true;
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) : {};
return this;
};
return Value;
@@ -443,11 +444,8 @@
}
return null;
};
Value.wrap = function(node) {
return node instanceof Value ? node : new Value(node);
};
return Value;
}).call(this);
})();
exports.Comment = (function() {
Comment = (function() {
function Comment(_arg) {
@@ -507,7 +505,7 @@
if (ifn = If.unfoldSoak(o, this, 'variable')) {
return ifn;
}
_ref2 = Value.wrap(this.variable).cacheReference(o), left = _ref2[0], rite = _ref2[1];
_ref2 = new Value(this.variable).cacheReference(o), left = _ref2[0], rite = _ref2[1];
} else {
left = new Literal(this.superReference(o));
rite = new Value(left);
@@ -582,7 +580,7 @@
return "" + (this.superReference(o)) + ".apply(this, " + splatargs + ")";
}
if (!this.isNew) {
base = Value.wrap(this.variable);
base = new Value(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('this');
fun = "(" + ref + " = " + (base.compile(o, LEVEL_LIST)) + ")" + (name.compile(o));
@@ -865,7 +863,7 @@
}
}
if (pvar) {
access = prop.context === 'this' ? pvar.base.properties[0] : new Accessor(pvar, 'prototype');
access = prop.context === 'this' ? pvar.properties[0] : new Accessor(pvar, 'prototype');
val = new Value(variable, [access]);
prop = new Assign(val, func);
}
@@ -953,13 +951,13 @@
_ref2 = obj, (_ref3 = _ref2.variable, idx = _ref3.base, _ref3), obj = _ref2.value;
} else {
if (obj.base instanceof Parens) {
_ref4 = Value.wrap(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1];
_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);
}
}
acc = IDENTIFIER.test(idx.unwrap().value || 0);
value = Value.wrap(value);
value = new Value(value);
value.properties.push(new (acc ? Accessor : Index)(idx));
return new Assign(obj, value).compile(o);
}
@@ -978,7 +976,7 @@
_ref5 = obj, (_ref6 = _ref5.variable, idx = _ref6.base, _ref6), obj = _ref5.value;
} else {
if (obj.base instanceof Parens) {
_ref7 = Value.wrap(obj.unwrapAll()).cacheReference(o), obj = _ref7[0], idx = _ref7[1];
_ref7 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref7[0], idx = _ref7[1];
} else {
idx = obj.tags["this"] ? obj.properties[0].name : obj;
}

View File

@@ -308,10 +308,11 @@ exports.Value = class Value extends Base
children: ['base', 'properties']
# A **Value** has a base and a list of property accesses.
constructor: (@base, props, tag) ->
super()
constructor: (base, props, tag) ->
return base if not props and base instanceof Value
@base = base
@properties = props or []
@tags[tag] = yes if tag
@tags = if tag then {(tag): on} else {}
# Add a property access to the list.
push: (prop) ->
@@ -385,7 +386,7 @@ exports.Value = class Value extends Base
props = @properties
code = @base.compile o, if props.length then LEVEL_ACCESS else null
code = "(#{code})" if props[0] instanceof Accessor and @isSimpleNumber()
(code += prop.compile o) for prop in props
code += prop.compile o for prop in props
code
# Unfold a soak into an `If`: `a?.b` -> `a.b if a?`
@@ -404,8 +405,6 @@ exports.Value = class Value extends Base
return new If new Existence(fst), snd, soak: on
null
@wrap: (node) -> if node instanceof Value then node else new Value node
#### Comment
# CoffeeScript passes through block comments as JavaScript block comments
@@ -459,7 +458,7 @@ exports.Call = class Call extends Base
if @soakNode
if @variable
return ifn if ifn = If.unfoldSoak o, this, 'variable'
[left, rite] = Value.wrap(@variable).cacheReference o
[left, rite] = new Value(@variable).cacheReference o
else
left = new Literal @superReference o
rite = new Value left
@@ -510,7 +509,7 @@ exports.Call = class Call extends Base
splatargs = @compileSplatArguments o
return "#{ @superReference o }.apply(this, #{splatargs})" if @isSuper
unless @isNew
base = Value.wrap @variable
base = new Value @variable
if (name = base.properties.pop()) and base.isComplex()
ref = o.scope.freeVariable 'this'
fun = "(#{ref} = #{ base.compile o, LEVEL_LIST })#{ name.compile o }"
@@ -734,7 +733,7 @@ exports.Class = class Class extends Base
constructor.body.push new Return new Literal 'this' if constructor.body.isEmpty()
constructor.body.unshift new Literal "this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#{me}, arguments); }"
if pvar
access = if prop.context is 'this' then pvar.base.properties[0] else new Accessor(pvar, 'prototype')
access = if prop.context is 'this' then pvar.properties[0] else new Accessor(pvar, 'prototype')
val = new Value variable, [access]
prop = new Assign val, func
props.push prop
@@ -804,14 +803,14 @@ exports.Assign = class Assign extends Base
{variable: {base: idx}, value: obj} = obj
else
if obj.base instanceof Parens
[obj, idx] = Value.wrap(obj.unwrapAll()).cacheReference o
[obj, idx] = new Value(obj.unwrapAll()).cacheReference o
else
idx = if isObject
if obj.tags.this then obj.properties[0].name else obj
else
new Literal 0
acc = IDENTIFIER.test idx.unwrap().value or 0
value = Value.wrap value
value = new Value value
value.properties.push new (if acc then Accessor else Index) idx
return new Assign(obj, value).compile o
valVar = value.compile o, LEVEL_LIST
@@ -830,7 +829,7 @@ exports.Assign = class Assign extends Base
else
# A shorthand `{a, b, @c} = val` pattern-match.
if obj.base instanceof Parens
[obj, idx] = Value.wrap(obj.unwrapAll()).cacheReference o
[obj, idx] = new Value(obj.unwrapAll()).cacheReference o
else
idx = if obj.tags.this then obj.properties[0].name else obj
if not splat and obj instanceof Splat