added and= and or= to the language.

This commit is contained in:
Jeremy Ashkenas
2010-07-24 22:36:50 -07:00
parent 88847df70b
commit f9dff6ffc4
6 changed files with 36 additions and 54 deletions

View File

@@ -112,7 +112,7 @@ exports.BaseNode = class BaseNode
# `toString` representation of the node, for inspecting the parse tree.
# This is what `coffee --nodes` prints out.
toString: (idt, override) ->
idt ||= ''
idt or= ''
children = (child.toString idt + TAB for child in @collectChildren()).join('')
'\n' + idt + (override or @class) + children
@@ -193,7 +193,7 @@ exports.Expressions = class Expressions extends BaseNode
# An **Expressions** is the only node that can serve as the root.
compile: (o) ->
o ||= {}
o or= {}
if o.scope then super(o) else @compileRoot(o)
compileNode: (o) ->
@@ -354,7 +354,7 @@ exports.ValueNode = class ValueNode extends BaseNode
only = del o, 'onlyFirst'
op = del o, 'operation'
props = if only then @properties[0...@properties.length - 1] else @properties
o.chainRoot ||= this
o.chainRoot or= this
baseline = @base.compile o
baseline = "($baseline)" if @hasProperties() and (@base instanceof ObjectNode or @isNumber())
complete = @last = baseline
@@ -502,7 +502,7 @@ exports.AccessorNode = class AccessorNode extends BaseNode
compileNode: (o) ->
name = @name.compile o
o.chainRoot.wrapped ||= @soakNode
o.chainRoot.wrapped or= @soakNode
namePart = if name.match(IS_STRING) then "[$name]" else ".$name"
@prototype + namePart
@@ -518,7 +518,7 @@ exports.IndexNode = class IndexNode extends BaseNode
@index = index
compileNode: (o) ->
o.chainRoot.wrapped ||= @soakNode
o.chainRoot.wrapped or= @soakNode
idx = @index.compile o
prefix = if @proto then '.prototype' else ''
"$prefix[$idx]"
@@ -570,7 +570,7 @@ exports.RangeNode = class RangeNode extends BaseNode
[from, to] = [parseInt(@fromNum, 10), parseInt(@toNum, 10)]
idx = del o, 'index'
step = del o, 'step'
step &&= "$idx += ${step.compile(o)}"
step and= "$idx += ${step.compile(o)}"
if from <= to
"$idx = $from; $idx <$@equals $to; ${step or "$idx++"}"
else
@@ -721,8 +721,8 @@ exports.ClassNode = class ClassNode extends BaseNode
continue
if func instanceof CodeNode and func.bound
func.bound = false
constScope ||= new Scope(o.scope, constructor.body, constructor)
me ||= constScope.freeVariable()
constScope or= new Scope(o.scope, constructor.body, constructor)
me or= constScope.freeVariable()
pname = pvar.compile(o)
constructor.body.push new ReturnNode literal 'this' if constructor.body.empty()
constructor.body.unshift literal "this.${pname} = function(){ return ${className}.prototype.${pname}.apply($me, arguments); }"
@@ -902,7 +902,7 @@ exports.CodeNode = class CodeNode extends BaseNode
traverseChildren: (crossScope, func) -> super(crossScope, func) if crossScope
toString: (idt) ->
idt ||= ''
idt or= ''
children = (child.toString(idt + TAB) for child in @collectChildren()).join('')
"\n$idt$children"
@@ -1385,7 +1385,7 @@ exports.IfNode = class IfNode extends BaseNode
# The **IfNode** only compiles into a statement if either of its bodies needs
# to be a statement. Otherwise a ternary is safe.
isStatement: ->
@statement ||= !!(@tags.statement or @bodyNode().isStatement() or (@elseBody and @elseBodyNode().isStatement()))
@statement or= !!(@tags.statement or @bodyNode().isStatement() or (@elseBody and @elseBodyNode().isStatement()))
compileCondition: (o) ->
(cond.compile(o) for cond in flatten([@condition])).join(' || ')
@@ -1395,8 +1395,8 @@ exports.IfNode = class IfNode extends BaseNode
makeReturn: ->
if @isStatement()
@body &&= @ensureExpressions(@body.makeReturn())
@elseBody &&= @ensureExpressions(@elseBody.makeReturn())
@body and= @ensureExpressions(@body.makeReturn())
@elseBody and= @ensureExpressions(@elseBody.makeReturn())
this
else
new ReturnNode this