mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
test_functions.coffee compiles and runs successfully.
This commit is contained in:
parent
04f07f4c15
commit
12685aa54a
4 changed files with 26 additions and 18 deletions
|
@ -234,7 +234,7 @@
|
||||||
],
|
],
|
||||||
// A Parameter (or ParamSplat) in a function definition.
|
// A Parameter (or ParamSplat) in a function definition.
|
||||||
Param: [o("PARAM", function() {
|
Param: [o("PARAM", function() {
|
||||||
return yytext;
|
return new LiteralNode(yytext);
|
||||||
}), o("Param . . .", function() {
|
}), o("Param . . .", function() {
|
||||||
return new SplatNode($1);
|
return new SplatNode($1);
|
||||||
})
|
})
|
||||||
|
|
|
@ -239,10 +239,10 @@
|
||||||
},
|
},
|
||||||
// Is the node last in this block of expressions?
|
// Is the node last in this block of expressions?
|
||||||
is_last: function is_last(node) {
|
is_last: function is_last(node) {
|
||||||
var l;
|
var l, last_index;
|
||||||
l = this.expressions.length;
|
l = this.expressions.length;
|
||||||
this.last_index = this.last_index || this.expressions[l - 1] instanceof CommentNode ? 2 : 1;
|
last_index = this.expressions[l - 1] instanceof CommentNode ? 2 : 1;
|
||||||
return node === this.expressions[l - this.last_index];
|
return node === this.expressions[l - last_index];
|
||||||
},
|
},
|
||||||
compile: function compile(o) {
|
compile: function compile(o) {
|
||||||
o = o || {
|
o = o || {
|
||||||
|
@ -437,7 +437,7 @@
|
||||||
},
|
},
|
||||||
compile_node: function compile_node(o) {
|
compile_node: function compile_node(o) {
|
||||||
var delimiter;
|
var delimiter;
|
||||||
delimiter = "\n" + this.idt() + '//';
|
delimiter = this.idt() + '//';
|
||||||
return delimiter + this.lines.join(delimiter);
|
return delimiter + this.lines.join(delimiter);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -642,7 +642,7 @@
|
||||||
// AssignNodes get interleaved correctly, with no trailing commas or
|
// AssignNodes get interleaved correctly, with no trailing commas or
|
||||||
// commas affixed to comments. TODO: Extract this and add it to ArrayNode.
|
// commas affixed to comments. TODO: Extract this and add it to ArrayNode.
|
||||||
compile_node: function compile_node(o) {
|
compile_node: function compile_node(o) {
|
||||||
var __a, __b, __c, __d, __e, i, indent, join, last_noncom, non_comments, prop, props;
|
var __a, __b, __c, __d, __e, i, indent, inner, join, last_noncom, non_comments, prop, props;
|
||||||
o.indent = this.idt(1);
|
o.indent = this.idt(1);
|
||||||
non_comments = (function() {
|
non_comments = (function() {
|
||||||
__a = []; __b = this.properties;
|
__a = []; __b = this.properties;
|
||||||
|
@ -673,7 +673,9 @@
|
||||||
}
|
}
|
||||||
return __d;
|
return __d;
|
||||||
}).call(this);
|
}).call(this);
|
||||||
return '{\n' + props.join('') + '\n' + this.idt() + '}';
|
props = props.join('');
|
||||||
|
inner = props ? '\n' + props + '\n' + this.idt() : '';
|
||||||
|
return '{' + inner + '}';
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
// An array literal.
|
// An array literal.
|
||||||
|
@ -885,11 +887,14 @@
|
||||||
SplatNode = (exports.SplatNode = inherit(Node, {
|
SplatNode = (exports.SplatNode = inherit(Node, {
|
||||||
type: 'Splat',
|
type: 'Splat',
|
||||||
constructor: function constructor(name) {
|
constructor: function constructor(name) {
|
||||||
|
if (!(name.compile)) {
|
||||||
|
name = new LiteralNode(name);
|
||||||
|
}
|
||||||
this.children = [(this.name = name)];
|
this.children = [(this.name = name)];
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
compile_node: function compile_node(o) {
|
compile_node: function compile_node(o) {
|
||||||
return this.index ? this.compile_param(o) : this.name.compile(o);
|
return (typeof this.index !== "undefined" && this.index !== null) ? this.compile_param(o) : this.name.compile(o);
|
||||||
},
|
},
|
||||||
compile_param: function compile_param(o) {
|
compile_param: function compile_param(o) {
|
||||||
var name;
|
var name;
|
||||||
|
@ -1261,7 +1266,7 @@
|
||||||
compile_condition: function compile_condition(o) {
|
compile_condition: function compile_condition(o) {
|
||||||
var __a, __b, __c, cond;
|
var __a, __b, __c, cond;
|
||||||
return ((function() {
|
return ((function() {
|
||||||
__a = []; __b = flatten(this.condition);
|
__a = []; __b = flatten([this.condition]);
|
||||||
for (__c = 0; __c < __b.length; __c++) {
|
for (__c = 0; __c < __b.length; __c++) {
|
||||||
cond = __b[__c];
|
cond = __b[__c];
|
||||||
__a.push(cond.compile(o));
|
__a.push(cond.compile(o));
|
||||||
|
@ -1284,7 +1289,7 @@
|
||||||
if_dent = child ? '' : this.idt();
|
if_dent = child ? '' : this.idt();
|
||||||
com_dent = child ? this.idt() : '';
|
com_dent = child ? this.idt() : '';
|
||||||
prefix = this.comment ? this.comment.compile(cond_o) + '\n' + com_dent : '';
|
prefix = this.comment ? this.comment.compile(cond_o) + '\n' + com_dent : '';
|
||||||
body = Expressions.wrap([body]).compile(o);
|
body = Expressions.wrap([this.body]).compile(o);
|
||||||
if_part = prefix + if_dent + 'if (' + this.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)) {
|
if (!(this.else_body)) {
|
||||||
return if_part;
|
return if_part;
|
||||||
|
|
|
@ -217,7 +217,7 @@ grammar: {
|
||||||
|
|
||||||
# A Parameter (or ParamSplat) in a function definition.
|
# A Parameter (or ParamSplat) in a function definition.
|
||||||
Param: [
|
Param: [
|
||||||
o "PARAM", -> yytext
|
o "PARAM", -> new LiteralNode(yytext)
|
||||||
o "Param . . .", -> new SplatNode($1)
|
o "Param . . .", -> new SplatNode($1)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -144,8 +144,8 @@ Expressions: exports.Expressions: inherit Node, {
|
||||||
# Is the node last in this block of expressions?
|
# Is the node last in this block of expressions?
|
||||||
is_last: (node) ->
|
is_last: (node) ->
|
||||||
l: @expressions.length
|
l: @expressions.length
|
||||||
@last_index ||= if @expressions[l - 1] instanceof CommentNode then 2 else 1
|
last_index: if @expressions[l - 1] instanceof CommentNode then 2 else 1
|
||||||
node is @expressions[l - @last_index]
|
node is @expressions[l - last_index]
|
||||||
|
|
||||||
compile: (o) ->
|
compile: (o) ->
|
||||||
o ||= {}
|
o ||= {}
|
||||||
|
@ -310,7 +310,7 @@ CommentNode: exports.CommentNode: inherit Node, {
|
||||||
this
|
this
|
||||||
|
|
||||||
compile_node: (o) ->
|
compile_node: (o) ->
|
||||||
delimiter: "\n" + @idt() + '//'
|
delimiter: @idt() + '//'
|
||||||
delimiter + @lines.join(delimiter)
|
delimiter + @lines.join(delimiter)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -515,7 +515,9 @@ ObjectNode: exports.ObjectNode: inherit Node, {
|
||||||
join: '' if i is non_comments.length - 1
|
join: '' if i is non_comments.length - 1
|
||||||
indent: if prop instanceof CommentNode then '' else @idt(1)
|
indent: if prop instanceof CommentNode then '' else @idt(1)
|
||||||
indent + prop.compile(o) + join
|
indent + prop.compile(o) + join
|
||||||
'{\n' + props.join('') + '\n' + @idt() + '}'
|
props: props.join('')
|
||||||
|
inner: if props then '\n' + props + '\n' + @idt() else ''
|
||||||
|
'{' + inner + '}'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -682,11 +684,12 @@ SplatNode: exports.SplatNode: inherit Node, {
|
||||||
type: 'Splat'
|
type: 'Splat'
|
||||||
|
|
||||||
constructor: (name) ->
|
constructor: (name) ->
|
||||||
|
name: new LiteralNode(name) unless name.compile
|
||||||
@children: [@name: name]
|
@children: [@name: name]
|
||||||
this
|
this
|
||||||
|
|
||||||
compile_node: (o) ->
|
compile_node: (o) ->
|
||||||
if @index then @compile_param(o) else @name.compile(o)
|
if @index? then @compile_param(o) else @name.compile(o)
|
||||||
|
|
||||||
compile_param: (o) ->
|
compile_param: (o) ->
|
||||||
name: @name.compile(o)
|
name: @name.compile(o)
|
||||||
|
@ -988,7 +991,7 @@ IfNode: exports.IfNode: inherit Node, {
|
||||||
@statement ||= !!(@comment or @tags.statement or @body.is_statement() or (@else_body and @else_body.is_statement()))
|
@statement ||= !!(@comment or @tags.statement or @body.is_statement() or (@else_body and @else_body.is_statement()))
|
||||||
|
|
||||||
compile_condition: (o) ->
|
compile_condition: (o) ->
|
||||||
(cond.compile(o) for cond in flatten(@condition)).join(' || ')
|
(cond.compile(o) for cond in flatten([@condition])).join(' || ')
|
||||||
|
|
||||||
compile_node: (o) ->
|
compile_node: (o) ->
|
||||||
if @is_statement() then @compile_statement(o) else @compile_ternary(o)
|
if @is_statement() then @compile_statement(o) else @compile_ternary(o)
|
||||||
|
@ -1004,7 +1007,7 @@ IfNode: exports.IfNode: inherit Node, {
|
||||||
if_dent: if child then '' else @idt()
|
if_dent: if child then '' else @idt()
|
||||||
com_dent: if child then @idt() else ''
|
com_dent: if child then @idt() else ''
|
||||||
prefix: if @comment then @comment.compile(cond_o) + '\n' + com_dent else ''
|
prefix: if @comment then @comment.compile(cond_o) + '\n' + com_dent else ''
|
||||||
body: Expressions.wrap([body]).compile(o)
|
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
|
return if_part unless @else_body
|
||||||
else_part: if @is_chain()
|
else_part: if @is_chain()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue