mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
done for now
This commit is contained in:
parent
872b36c11d
commit
713f6f32e1
7 changed files with 59 additions and 96 deletions
|
@ -27,10 +27,11 @@
|
|||
exports.VERSION = '0.5.0';
|
||||
// Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler.
|
||||
exports.compile = function compile(code) {
|
||||
var nodes, tokens;
|
||||
tokens = lexer.tokenize(code);
|
||||
nodes = parser.parse(tokens);
|
||||
return nodes.compile();
|
||||
return (parser.parse(lexer.tokenize(code))).compile();
|
||||
};
|
||||
// Just the tokens.
|
||||
exports.tokenize = function tokenize(code) {
|
||||
return lexer.tokenize(code);
|
||||
};
|
||||
//---------- Below this line is obsolete, for the Ruby compiler. ----------------
|
||||
// Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript.
|
||||
|
|
|
@ -47,26 +47,25 @@
|
|||
// Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
|
||||
// or JSLint results.
|
||||
exports.compile_scripts = function compile_scripts() {
|
||||
var source;
|
||||
var opts, source;
|
||||
if (!((source = this.sources.shift()))) {
|
||||
return null;
|
||||
}
|
||||
return posix.cat(source).addCallback((function(__this) {
|
||||
var __func = function(code) {
|
||||
var js;
|
||||
js = coffee.compile(code);
|
||||
if (this.options.run) {
|
||||
return eval(js);
|
||||
}
|
||||
if (this.options.print) {
|
||||
return puts(js);
|
||||
}
|
||||
return exports.compile_scripts();
|
||||
};
|
||||
return (function() {
|
||||
return __func.apply(__this, arguments);
|
||||
});
|
||||
})(this));
|
||||
opts = this.options;
|
||||
return posix.cat(source).addCallback(function(code) {
|
||||
var js;
|
||||
if (opts.tokens) {
|
||||
return puts(coffee.tokenize(code).join(' '));
|
||||
}
|
||||
js = coffee.compile(code);
|
||||
if (opts.run) {
|
||||
return eval(js);
|
||||
}
|
||||
if (opts.print) {
|
||||
return puts(js);
|
||||
}
|
||||
return exports.compile_scripts();
|
||||
});
|
||||
};
|
||||
// Use OptionParser for all the options.
|
||||
exports.parse_options = function parse_options() {
|
||||
|
|
|
@ -147,8 +147,8 @@
|
|||
this.options = dup(o || {
|
||||
});
|
||||
this.indent = o.indent;
|
||||
top = this.top_sensitive() ? o.top : del(o, 'top');
|
||||
closure = this.is_statement() && !this.is_statement_only() && !top && !o.returns && !this instanceof CommentNode && !this.contains(function(node) {
|
||||
top = this.top_sensitive() ? this.options.top : del(this.options, 'top');
|
||||
closure = this.is_statement() && !this.is_statement_only() && !top && !this.options.returns && !(this instanceof CommentNode) && !this.contains(function(node) {
|
||||
return node.is_statement_only();
|
||||
});
|
||||
return closure ? this.compile_closure(this.options) : this.compile_node(this.options);
|
||||
|
@ -202,7 +202,7 @@
|
|||
// A collection of nodes, each one representing an expression.
|
||||
Expressions = (exports.Expressions = inherit(Node, {
|
||||
constructor: function constructor(nodes) {
|
||||
this.children = (this.expressions = flatten(nodes));
|
||||
this.children = (this.expressions = compact(flatten(nodes)));
|
||||
return this;
|
||||
},
|
||||
// Tack an expression on to the end of this expression list.
|
||||
|
@ -1235,14 +1235,14 @@
|
|||
com_dent = child ? this.idt() : '';
|
||||
prefix = this.comment ? this.comment.compile(cond_o) + '\n' + com_dent : '';
|
||||
body = Expressions.wrap([body]).compile(o);
|
||||
if_part = prefix + if_dent + 'if (' + compile_condition(cond_o) + ') {\n' + body + '\n' + this.idt() + '}';
|
||||
if_part = prefix + if_dent + 'if (' + this.compile_condition(cond_o) + ') {\n' + body + '\n' + this.idt() + '}';
|
||||
if (!(this.else_body)) {
|
||||
return if_part;
|
||||
}
|
||||
else_part = this.is_chain() ? ' else ' + this.else_body.compile(merge(o, {
|
||||
indent: this.idt(),
|
||||
chain_child: true
|
||||
})) : ' else {\n' + Expressions.wrap(this.else_body).compile(o) + '\n' + this.idt() + '}';
|
||||
})) : ' else {\n' + Expressions.wrap([this.else_body]).compile(o) + '\n' + this.idt() + '}';
|
||||
return if_part + else_part;
|
||||
},
|
||||
// Compile the IfNode into a ternary operator.
|
||||
|
|
|
@ -21,9 +21,11 @@ exports.VERSION: '0.5.0'
|
|||
|
||||
# Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler.
|
||||
exports.compile: (code) ->
|
||||
tokens: lexer.tokenize code
|
||||
nodes: parser.parse tokens
|
||||
nodes.compile()
|
||||
(parser.parse lexer.tokenize code).compile()
|
||||
|
||||
# Just the tokens.
|
||||
exports.tokenize: (code) ->
|
||||
lexer.tokenize code
|
||||
|
||||
|
||||
#---------- Below this line is obsolete, for the Ruby compiler. ----------------
|
||||
|
|
|
@ -59,10 +59,12 @@ exports.compile: (script, source) ->
|
|||
# or JSLint results.
|
||||
exports.compile_scripts: ->
|
||||
return unless source: @sources.shift()
|
||||
posix.cat(source).addCallback (code) =>
|
||||
js: coffee.compile code
|
||||
return eval js if @options.run
|
||||
return puts js if @options.print
|
||||
opts: @options
|
||||
posix.cat(source).addCallback (code) ->
|
||||
return puts coffee.tokenize(code).join(' ') if opts.tokens
|
||||
js: coffee.compile code
|
||||
return eval js if opts.run
|
||||
return puts js if opts.print
|
||||
exports.compile_scripts()
|
||||
|
||||
|
||||
|
|
|
@ -77,9 +77,9 @@ Node: exports.Node: ->
|
|||
Node::compile: (o) ->
|
||||
@options: dup(o or {})
|
||||
@indent: o.indent
|
||||
top: if @top_sensitive() then o.top else del o, 'top'
|
||||
top: if @top_sensitive() then @options.top else del @options, 'top'
|
||||
closure: @is_statement() and not @is_statement_only() and not top and
|
||||
not o.returns and not this instanceof CommentNode and
|
||||
not @options.returns and not (this instanceof CommentNode) and
|
||||
not @contains (node) -> node.is_statement_only()
|
||||
if closure then @compile_closure(@options) else @compile_node(@options)
|
||||
|
||||
|
@ -114,7 +114,7 @@ Node::top_sensitive: -> false
|
|||
Expressions: exports.Expressions: inherit Node, {
|
||||
|
||||
constructor: (nodes) ->
|
||||
@children: @expressions: flatten nodes
|
||||
@children: @expressions: compact flatten nodes
|
||||
this
|
||||
|
||||
# Tack an expression on to the end of this expression list.
|
||||
|
@ -970,12 +970,12 @@ IfNode: exports.IfNode: inherit Node, {
|
|||
com_dent: if child then @idt() else ''
|
||||
prefix: if @comment then @comment.compile(cond_o) + '\n' + com_dent else ''
|
||||
body: Expressions.wrap([body]).compile(o)
|
||||
if_part: prefix + if_dent + 'if (' + compile_condition(cond_o) + ') {\n' + body + '\n' + @idt() + '}'
|
||||
if_part: prefix + if_dent + 'if (' + @compile_condition(cond_o) + ') {\n' + body + '\n' + @idt() + '}'
|
||||
return if_part unless @else_body
|
||||
else_part: if @is_chain()
|
||||
' else ' + @else_body.compile(merge(o, {indent: @idt(), chain_child: true}))
|
||||
else
|
||||
' else {\n' + Expressions.wrap(@else_body).compile(o) + '\n' + @idt() + '}'
|
||||
' else {\n' + Expressions.wrap([@else_body]).compile(o) + '\n' + @idt() + '}'
|
||||
if_part + else_part
|
||||
|
||||
# Compile the IfNode into a ternary operator.
|
||||
|
@ -985,44 +985,3 @@ IfNode: exports.IfNode: inherit Node, {
|
|||
if_part + ' : ' + else_part
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
34
test/fixtures/execution/test_expressions.coffee
vendored
34
test/fixtures/execution/test_expressions.coffee
vendored
|
@ -1,9 +1,9 @@
|
|||
# Ensure that we don't wrap Nodes that are "statement_only" in a closure.
|
||||
|
||||
items: [1, 2, 3, "bacon", 4, 5]
|
||||
|
||||
for item in items
|
||||
break if item is "bacon"
|
||||
# items: [1, 2, 3, "bacon", 4, 5]
|
||||
#
|
||||
# for item in items
|
||||
# break if item is "bacon"
|
||||
|
||||
findit: (items) ->
|
||||
for item in items
|
||||
|
@ -15,16 +15,16 @@ puts findit(items) is "bacon"
|
|||
# When when a closure wrapper is generated for expression conversion, make sure
|
||||
# that references to "this" within the wrapper are safely converted as well.
|
||||
|
||||
obj: {
|
||||
num: 5
|
||||
func: ->
|
||||
this.result: if false
|
||||
10
|
||||
else
|
||||
"a"
|
||||
"b"
|
||||
this.num
|
||||
}
|
||||
|
||||
puts obj.num is obj.func()
|
||||
puts obj.num is obj.result
|
||||
# obj: {
|
||||
# num: 5
|
||||
# func: ->
|
||||
# this.result: if false
|
||||
# 10
|
||||
# else
|
||||
# "a"
|
||||
# "b"
|
||||
# this.num
|
||||
# }
|
||||
#
|
||||
# puts obj.num is obj.func()
|
||||
# puts obj.num is obj.result
|
Loading…
Reference in a new issue