Skip to content

Commit

Permalink
list comprehenions are in! resolving my issues (though in ls)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkz committed May 21, 2012
1 parent 2729149 commit 92744c3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion extras/livescript.js

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions src/ast.ls
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ exports.fromJSON = function
node = ^exports[that]::
for key, val of it then node[key] = fromJSON val
return node
if it.length? then (fromJSON v for v in it) else it
if it.length? then [fromJSON v for v in it] else it

#### Modules

Expand Down Expand Up @@ -279,7 +279,7 @@ class exports.Block extends Node
# Compile to a comma-separated list of expressions.
compileExpressions: (o, level) ->
{lines} = this; i = -1
lines.splice i-- 1 if that.comment while lines[++i]
while lines[++i] then lines.splice i-- 1 if that.comment
lines.push Literal \void unless lines.length
lines.0 <<< {@front}; lines[*-1] <<< {@void}
return lines.0.compile o, level unless lines.1
Expand Down Expand Up @@ -621,7 +621,7 @@ class List extends Node
{indent, level} = o
o <<< indent: indent + TAB, level: LEVEL_LIST
code = items[i = 0]compile o
code += ', ' + that.compile o while items[++i]
while items[++i] then code += ', ' + that.compile o
code = "\n#{o.indent}#code\n#indent" if ~code.indexOf \\n
o <<< {indent, level}
code
Expand Down Expand Up @@ -735,7 +735,7 @@ class exports.Arr extends List

isArray: YES

asObj: -> Obj(Prop Literal(i), item for item, i in @items)
asObj: -> Obj([Prop Literal(i), item for item, i in @items])

# `base[x, ...y]` => `[base[x], ...base[y]]`
toSlice: (o, base) ->
Expand Down Expand Up @@ -852,7 +852,7 @@ class exports.Unary extends Node
# `^delete o[p, ...q]` => `[^delete o[p], ...^delete o[q]]`
compileSpread: (o) ->
{it} = this; ops = [this]
ops.push it while it instanceof .., it.=it
while it instanceof .., it.=it then ops.push it
return '' unless it.=expandSlice(o)unwrap! instanceof Arr
and (them = it.items)length
for node, i in them
Expand Down Expand Up @@ -1109,7 +1109,7 @@ class exports.Assign extends Node
[left, reft] = Chain(left)cacheReference o
right = Binary op.slice(0 -1), reft, right
op = \:=
right.=it while right instanceof Parens and not right.keep
while right instanceof Parens and not right.keep then right.=it
right.ripName left.=unwrap!
lvar = left instanceof Var
sign = op.replace \: ''
Expand Down Expand Up @@ -1435,7 +1435,7 @@ class exports.Fun extends Node
names.push name = scope.add vr.value, \arg
p.carp "duplicate parameter \"#name\"" unless dic"#name." = dic"#name." ^^^ 1
if rest
rest.unshift Arr! while splace--
while splace-- then rest.unshift Arr!
assigns.push Assign Arr(rest), Literal \arguments
@body.prepend ...assigns if assigns.length
names.join ', '
Expand Down Expand Up @@ -1828,10 +1828,12 @@ class exports.Case extends Node
compileCase: (o, tab, nobr, bool) ->
tests = for test in @tests
test.=expandSlice(o)unwrap!
if test instanceof Arr then t for t in test.items else test
if test instanceof Arr
for t in test.items then t
else test
tests.length or tests.push Literal \void
if bool
[t] = tests; i = 0; t = Binary \|| t, that while tests[++i]
[t] = tests; i = 0; while tests[++i] then t = Binary \|| t, that
tests = [(@<<<{t, aSource: \t, aTargets: [\body]})anaphorize!invert!]
code = ''
for t in tests then code += tab + "case #{ t.compile o, LEVEL_PAREN }:\n"
Expand Down
2 changes: 1 addition & 1 deletion src/grammar.ls
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ operators =
tokens = do
for name, alts of bnf
for alt in alts
(token for token in alt.0 when token not of bnf)
[token for token in alt.0 when token not of bnf]
.join ' '

bnf.Root = [[[\Body] 'return $$']]
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.ls
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ OPENERS = <[ ( [ { CALL( PARAM( INDENT ]>
CLOSERS = <[ ) ] } )CALL )PARAM DEDENT ]>

# The inverse mappings of {OPEN,CLOS}ERS to look things up from either end.
INVERSES = new -> import (c = CLOSERS[i]): o, (o): c for o, i in OPENERS
INVERSES = new -> for o, i in OPENERS then import (c = CLOSERS[i]): o, (o): c

# Tokens that can start a dot/call chain.
CHAIN = <[ ( { [ ID STRNUM LITERAL LET WITH WORDS ]>
Expand Down
2 changes: 2 additions & 0 deletions test/loop.ls
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ eq multiLiner.length, singleLiner.length
eq 25, multiLiner[*-1]
eq 25, singleLiner[*-1]

comp = ["#x#y" for x in [1 2 3] for y in [\a \b \c]]
eq "#comp", '1a,1b,1c,2a,2b,2c,3a,3b,3c'

# Comprehensions within parentheses.
result = null
Expand Down

1 comment on commit 92744c3

@paulmillr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome. I've always thought postfix loops suck.

Please sign in to comment.