self-compiling ranges and expressions

This commit is contained in:
Jeremy Ashkenas 2010-02-11 20:20:59 -05:00
parent 38d1381c02
commit 950d1199c2
2 changed files with 9 additions and 5 deletions

View File

@ -311,7 +311,7 @@
// JavaScript without translation, eg.: strings, numbers, true, false, null...
LiteralNode = (exports.LiteralNode = inherit(Node, {
constructor: function constructor(value) {
this.children = [(this.value = value)];
this.value = value;
return this;
},
// Break and continue must be treated as statements -- they lose their meaning
@ -563,12 +563,14 @@
return this.from_var + ' = ' + this.from.compile(o) + '; ' + this.to_var + ' = ' + this.to.compile(o) + ";\n" + this.idt();
},
compile_node: function compile_node(o) {
var compare, equals, idx, incr, intro, step;
var compare, equals, idx, incr, intro, step, vars;
if (!(o.index)) {
return this.compile_array(o);
}
idx = del(o, 'index');
step = del(o, 'step');
vars = idx + '=' + this.from_var;
step = step ? step.compile(o) : '1';
equals = this.exclusive ? '' : '=';
intro = '(' + this.from_var + ' <= ' + this.to_var + ' ? ' + idx;
compare = intro + ' <' + equals + ' ' + this.to_var + ' : ' + idx + ' >' + equals + ' ' + this.to_var + ')';
@ -583,7 +585,7 @@
body = Expressions.wrap([new LiteralNode('i')]);
arr = Expressions.wrap([new ForNode(body, {
source: (new ValueNode(this))
}, 'i')
}, new LiteralNode('i'))
]);
return (new ParentheticalNode(new CallNode(new CodeNode([], arr)))).compile(o);
}

View File

@ -195,7 +195,7 @@ statement Expressions
LiteralNode: exports.LiteralNode: inherit Node, {
constructor: (value) ->
@children: [@value: value]
@value: value
this
# Break and continue must be treated as statements -- they lose their meaning
@ -442,6 +442,8 @@ RangeNode: exports.RangeNode: inherit Node, {
return @compile_array(o) unless o.index
idx: del o, 'index'
step: del o, 'step'
vars: idx + '=' + @from_var
step: if step then step.compile(o) else '1'
equals: if @exclusive then '' else '='
intro: '(' + @from_var + ' <= ' + @to_var + ' ? ' + idx
compare: intro + ' <' + equals + ' ' + @to_var + ' : ' + idx + ' >' + equals + ' ' + @to_var + ')'
@ -453,7 +455,7 @@ RangeNode: exports.RangeNode: inherit Node, {
# TODO: This generates pretty ugly code ... shrink it.
compile_array: (o) ->
body: Expressions.wrap([new LiteralNode('i')])
arr: Expressions.wrap([new ForNode(body, {source: (new ValueNode(this))}, 'i')])
arr: Expressions.wrap([new ForNode(body, {source: (new ValueNode(this))}, new LiteralNode('i'))])
(new ParentheticalNode(new CallNode(new CodeNode([], arr)))).compile(o)
}