Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita122b37

Browse files
basicermichaelficarra
authored andcommitted
Fixes#284 - Loops/comprehensions over decreasing ranges don't work (#365)
*Fixes#284 - Loops/comprehensions over decreasing ranges don't work* - Fix code formatting- Add more unit tests for decreasing numeric ranges
1 parentad91003 commita122b37

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

‎lib/compiler.js‎

Lines changed: 35 additions & 4 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎src/compiler.coffee‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@ generateSoak = do ->
316316
[tests,e]=fn node
317317
newCS.Conditional (foldl1 tests, (memo,t)->newCS.LogicalAndOp memo, t), e
318318

319+
extractNumber= (what)->
320+
returnwhat.dataifwhat.instanceofCS.Int
321+
returnfalseunlesswhat.instanceofCS.UnaryNegateOp
322+
returnfalseunlesswhat.expression.instanceofCS.Int
323+
return0-what.expression.data
324+
325+
extractStaticRange= (range)->
326+
returnundefinedunlessrange.instanceofCS.Range
327+
left=extractNumberrange.left
328+
right=extractNumberrange.right
329+
330+
returnundefinedif left==false
331+
returnundefinedif right==false
332+
333+
return [left, right]
319334

320335
helperNames= {}
321336
helpers=
@@ -392,7 +407,6 @@ for own h, fn of inlineHelpers
392407
helpers[h]= fn
393408

394409

395-
396410
classexports.Compiler
397411

398412
@compile==> (newthis).compilearguments...
@@ -449,17 +463,17 @@ class exports.Compiler
449463
block=forceBlock body
450464
block.body.pushstmthelpers.undef()unlessblock.body.length
451465

466+
numericRange=extractStaticRange(@target)
452467
increment=
453468
if@step?andnot ((@step.instanceofCS.Int)and@step.datais1)
454469
(x)->newJS.AssignmentExpression'+=', x, step
470+
elseif numericRange?and numericRange[1]< numericRange[0]
471+
(x)->newJS.UpdateExpression'--',yes, x
455472
else
456473
(x)->newJS.UpdateExpression'++',yes, x
457474

458475
# optimise loops over static, integral ranges
459-
if (@target.instanceofCS.Range)and
460-
# TODO: extract this test to some "static, integral range" helper
461-
((@target.left.instanceofCS.Int)or ((@target.left.instanceofCS.UnaryNegateOp)and@target.left.expression.instanceofCS.Int))and
462-
((@target.right.instanceofCS.Int)or ((@target.right.instanceofCS.UnaryNegateOp)and@target.right.expression.instanceofCS.Int))
476+
if numericRange?
463477
varDeclaration=newJS.VariableDeclaration'var', [newJS.VariableDeclarator i,compile@target.left]
464478
update=increment i
465479
if@filter?
@@ -471,7 +485,10 @@ class exports.Compiler
471485
block.body.unshiftstmtnewJS.AssignmentExpression'=', keyAssignee, k
472486
if valAssignee?
473487
block.body.unshiftstmtnewJS.AssignmentExpression'=', valAssignee, i
474-
op=if@target.isInclusivethen'<='else'<'
488+
if numericRange[1]> numericRange[0]
489+
op=if@target.isInclusivethen'<='else'<'
490+
else
491+
op=if@target.isInclusivethen'>='else'>'
475492
returnnewJS.ForStatement varDeclaration, (newJS.BinaryExpression op, i,compile@target.right), update, block
476493

477494
e=ifneedsCaching@targetthengenSym'cache'else target

‎test/comprehensions.coffee‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ suite 'Comprehensions', ->
4343
arrayEq [0,3,6], (kfor v, kin [1..7]by3)
4444
arrayEq [0,0,0], (0forin listby3)
4545
arrayEq [0,0,0], (0forin [1..7]by3)
46+
47+
test'#284: loops/comprehensions over decreasing ranges don\'t work',->
48+
a=2
49+
b=-2
50+
arrayEq [5,4,3,2,1], (nfor nin [5..1])
51+
arrayEq [5,4,3,2,1,0,-1,-2,-3,-4,-5], (nfor nin [5..-5])
52+
arrayEq [2,1,0,-1,-2], (nfor nin [a..b])
53+
arrayEq [2,1,0,-1,-2], (nfor nin [a..-2])
54+
arrayEq [2,1,0,-1,-2], (nfor nin [2..b])
55+
56+
arrayEq [5,4,3,2], (nfor nin [5...1])
57+
arrayEq [2,1,0,-1], (nfor nin [a...b])
58+
arrayEq [2,1,0,-1], (nfor nin [a...-2])
59+
arrayEq [2,1,0,-1], (nfor nin [2...b])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp