1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

make the compiler use 'obj = or default' for default values.

This commit is contained in:
Jeremy Ashkenas 2010-07-26 23:58:06 -04:00
parent 9be1453886
commit b4ef4a9e28
6 changed files with 20 additions and 20 deletions

View file

@ -30,7 +30,7 @@ lexer = new Lexer
# Compile a string of CoffeeScript code to JavaScript, using the Coffee/Jison
# compiler.
exports.compile = compile = (code, options) ->
options or= {}
options = or {}
try
(parser.parse lexer.tokenize code).compile options
catch err

View file

@ -156,7 +156,7 @@ printTokens = (tokens) ->
parseOptions = ->
optionParser = new optparse.OptionParser SWITCHES, BANNER
o = options = optionParser.parse(process.argv[2...process.argv.length])
options.compile or= !!o.output
options.compile = or !!o.output
options.run = not (o.compile or o.print or o.lint)
options.print = !! (o.print or (o.eval or o.stdio and o.compile))
sources = options.arguments

View file

@ -259,7 +259,7 @@ exports.Lexer = class Lexer
value = match and match[1]
space = match and match[2]
@tagParameters() if value and value.match CODE
value or= @chunk.substr 0, 1
value = or @chunk.substr 0, 1
@i += value.length
prevSpaced = @prev() and @prev().spaced
tag = value
@ -347,7 +347,7 @@ exports.Lexer = class Lexer
# contents of the string. This method allows us to have strings within
# interpolations within strings, ad infinitum.
balancedString: (str, delimited, options) ->
options or= {}
options = or {}
slash = delimited[0][0] is '/'
levels = []
i = 0
@ -384,7 +384,7 @@ exports.Lexer = class Lexer
# new Lexer, tokenize the interpolated contents, and merge them into the
# token stream.
interpolateString: (str, options) ->
options or= {}
options = or {}
if str.length < 3 or not starts str, '"'
@token 'STRING', str
else

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 or= ''
idt = or ''
children = (child.toString idt + TAB for child in @collectChildren()).join('')
'\n' + idt + (override or @class) + children
@ -187,7 +187,7 @@ exports.Expressions = class Expressions extends BaseNode
# An **Expressions** is the only node that can serve as the root.
compile: (o) ->
o or= {}
o = or {}
if o.scope then super(o) else @compileRoot(o)
compileNode: (o) ->
@ -348,7 +348,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 or= this
o.chainRoot = or this
baseline = @base.compile o
baseline = "(#baseline)" if @hasProperties() and (@base instanceof ObjectNode or @isNumber())
complete = @last = baseline
@ -496,7 +496,7 @@ exports.AccessorNode = class AccessorNode extends BaseNode
compileNode: (o) ->
name = @name.compile o
o.chainRoot.wrapped or= @soakNode
o.chainRoot.wrapped = or @soakNode
namePart = if name.match(IS_STRING) then "[#name]" else ".#name"
@prototype + namePart
@ -512,7 +512,7 @@ exports.IndexNode = class IndexNode extends BaseNode
@index = index
compileNode: (o) ->
o.chainRoot.wrapped or= @soakNode
o.chainRoot.wrapped = or @soakNode
idx = @index.compile o
prefix = if @proto then '.prototype' else ''
"#prefix[#idx]"
@ -564,7 +564,7 @@ exports.RangeNode = class RangeNode extends BaseNode
[from, to] = [parseInt(@fromNum, 10), parseInt(@toNum, 10)]
idx = del o, 'index'
step = del o, 'step'
step and= "#idx += #{step.compile(o)}"
step = and "#idx += #{step.compile(o)}"
if from <= to
"#idx = #from; #idx <#@equals #to; #{step or "#idx++"}"
else
@ -715,8 +715,8 @@ exports.ClassNode = class ClassNode extends BaseNode
continue
if func instanceof CodeNode and func.bound
func.bound = false
constScope or= new Scope(o.scope, constructor.body, constructor)
me or= 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); }"
@ -897,7 +897,7 @@ exports.CodeNode = class CodeNode extends BaseNode
traverseChildren: (crossScope, func) -> super(crossScope, func) if crossScope
toString: (idt) ->
idt or= ''
idt = or ''
children = (child.toString(idt + TAB) for child in @collectChildren()).join('')
"\n#idt#children"
@ -1382,7 +1382,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 or= !!(@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(' || ')
@ -1392,8 +1392,8 @@ exports.IfNode = class IfNode extends BaseNode
makeReturn: ->
if @isStatement()
@body and= @ensureExpressions(@body.makeReturn())
@elseBody and= @ensureExpressions(@elseBody.makeReturn())
@body = and @ensureExpressions(@body.makeReturn())
@elseBody = and @ensureExpressions(@elseBody.makeReturn())
this
else
new ReturnNode this

View file

@ -237,7 +237,7 @@ exports.Rewriter = class Rewriter
@scanTokens (prev, token, post, i) =>
for pair in pairs
[open, close] = pair
levels[open] or= 0
levels[open] = or 0
if token[0] is open
openLine[open] = token[2] if levels[open] == 0
levels[open] += 1

View file

@ -69,13 +69,13 @@ ok x*+y is -50
# Half-operators.
one = two = null
one = or 1
two or= 2
two = or 2
ok one is 1
ok two is 2
one = and 'one'
two and= 'two'
two = and 'two'
ok one is 'one'
ok two is 'two'