mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
got postfix if and unless onboard
This commit is contained in:
parent
16f80ed963
commit
5dd295bd08
5 changed files with 643 additions and 584 deletions
12
code.jaa
12
code.jaa
|
@ -46,6 +46,10 @@ else
|
|||
|
||||
eldest: if 25 > 21 then liz else marge.
|
||||
|
||||
decoration: medal_of_honor if war_hero
|
||||
|
||||
go_to_sleep() unless coffee
|
||||
|
||||
# Returning early:
|
||||
race: =>
|
||||
run()
|
||||
|
@ -84,7 +88,7 @@ while supply > demand then buy().
|
|||
!!true
|
||||
|
||||
# For loops.
|
||||
foods: ['toast', 'wine', 'cheese']
|
||||
print(item.capitalize()) for item in foods.
|
||||
|
||||
drink(item) for item in foods if item is 'wine'.
|
||||
# foods: ['toast', 'wine', 'cheese']
|
||||
# print(item.capitalize()) for item in foods.
|
||||
#
|
||||
# drink(item) for item in foods if item is 'wine'.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class Parser
|
||||
|
||||
# Declare tokens produced by the lexer
|
||||
token IF ELSE THEN
|
||||
token IF ELSE THEN UNLESS
|
||||
token NUMBER STRING REGEX
|
||||
token TRUE FALSE NULL
|
||||
token IDENTIFIER PROPERTY_ACCESS
|
||||
|
@ -18,7 +18,9 @@ prechigh
|
|||
left '<=' '<' '>' '>='
|
||||
right '==' '!=' IS AINT
|
||||
left '&&' '||' AND OR
|
||||
left ':'
|
||||
right '-=' '+=' '/=' '*='
|
||||
nonassoc IF
|
||||
preclow
|
||||
|
||||
rule
|
||||
|
@ -223,6 +225,8 @@ rule
|
|||
| IF Expression
|
||||
Then Expressions
|
||||
ELSE Expressions "." { result = IfNode.new(val[1], val[3], val[5]) }
|
||||
| Expression IF Expression { result = IfNode.new(val[2], Nodes.new([val[0]])) }
|
||||
| Expression UNLESS Expression { result = IfNode.new(val[2], Nodes.new([val[0]]), nil, :invert) }
|
||||
;
|
||||
|
||||
Try:
|
||||
|
|
2
lexer.rb
2
lexer.rb
|
@ -1,6 +1,6 @@
|
|||
class Lexer
|
||||
|
||||
KEYWORDS = ["if", "else", "then",
|
||||
KEYWORDS = ["if", "else", "then", "unless",
|
||||
"true", "false", "null",
|
||||
"and", "or", "is", "aint", "not",
|
||||
"new", "return",
|
||||
|
|
7
nodes.rb
7
nodes.rb
|
@ -238,16 +238,17 @@ end
|
|||
# "if-else" control structure. Look at this node if you want to implement other control
|
||||
# structures like while, for, loop, etc.
|
||||
class IfNode < Node
|
||||
FORCE_STATEMENT = [Nodes, ReturnNode]
|
||||
FORCE_STATEMENT = [Nodes, ReturnNode, AssignNode]
|
||||
|
||||
def initialize(condition, body, else_body=nil)
|
||||
def initialize(condition, body, else_body=nil, tag=nil)
|
||||
@condition = condition
|
||||
@body = body && body.flatten
|
||||
@else_body = else_body && else_body.flatten
|
||||
@condition = OpNode.new("!", @condition) if tag == :invert
|
||||
end
|
||||
|
||||
def statement?
|
||||
FORCE_STATEMENT.include?(@body.class) || FORCE_STATEMENT.include?(@else_body.class)
|
||||
@is_statement ||= (FORCE_STATEMENT.include?(@body.class) || FORCE_STATEMENT.include?(@else_body.class))
|
||||
end
|
||||
|
||||
def line_ending
|
||||
|
|
Loading…
Reference in a new issue