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.
|
eldest: if 25 > 21 then liz else marge.
|
||||||
|
|
||||||
|
decoration: medal_of_honor if war_hero
|
||||||
|
|
||||||
|
go_to_sleep() unless coffee
|
||||||
|
|
||||||
# Returning early:
|
# Returning early:
|
||||||
race: =>
|
race: =>
|
||||||
run()
|
run()
|
||||||
|
@ -84,7 +88,7 @@ while supply > demand then buy().
|
||||||
!!true
|
!!true
|
||||||
|
|
||||||
# For loops.
|
# For loops.
|
||||||
foods: ['toast', 'wine', 'cheese']
|
# foods: ['toast', 'wine', 'cheese']
|
||||||
print(item.capitalize()) for item in foods.
|
# print(item.capitalize()) for item in foods.
|
||||||
|
#
|
||||||
drink(item) for item in foods if item is 'wine'.
|
# drink(item) for item in foods if item is 'wine'.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class Parser
|
class Parser
|
||||||
|
|
||||||
# Declare tokens produced by the lexer
|
# Declare tokens produced by the lexer
|
||||||
token IF ELSE THEN
|
token IF ELSE THEN UNLESS
|
||||||
token NUMBER STRING REGEX
|
token NUMBER STRING REGEX
|
||||||
token TRUE FALSE NULL
|
token TRUE FALSE NULL
|
||||||
token IDENTIFIER PROPERTY_ACCESS
|
token IDENTIFIER PROPERTY_ACCESS
|
||||||
|
@ -18,7 +18,9 @@ prechigh
|
||||||
left '<=' '<' '>' '>='
|
left '<=' '<' '>' '>='
|
||||||
right '==' '!=' IS AINT
|
right '==' '!=' IS AINT
|
||||||
left '&&' '||' AND OR
|
left '&&' '||' AND OR
|
||||||
|
left ':'
|
||||||
right '-=' '+=' '/=' '*='
|
right '-=' '+=' '/=' '*='
|
||||||
|
nonassoc IF
|
||||||
preclow
|
preclow
|
||||||
|
|
||||||
rule
|
rule
|
||||||
|
@ -223,6 +225,8 @@ rule
|
||||||
| IF Expression
|
| IF Expression
|
||||||
Then Expressions
|
Then Expressions
|
||||||
ELSE Expressions "." { result = IfNode.new(val[1], val[3], val[5]) }
|
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:
|
Try:
|
||||||
|
|
2
lexer.rb
2
lexer.rb
|
@ -1,6 +1,6 @@
|
||||||
class Lexer
|
class Lexer
|
||||||
|
|
||||||
KEYWORDS = ["if", "else", "then",
|
KEYWORDS = ["if", "else", "then", "unless",
|
||||||
"true", "false", "null",
|
"true", "false", "null",
|
||||||
"and", "or", "is", "aint", "not",
|
"and", "or", "is", "aint", "not",
|
||||||
"new", "return",
|
"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
|
# "if-else" control structure. Look at this node if you want to implement other control
|
||||||
# structures like while, for, loop, etc.
|
# structures like while, for, loop, etc.
|
||||||
class IfNode < Node
|
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
|
@condition = condition
|
||||||
@body = body && body.flatten
|
@body = body && body.flatten
|
||||||
@else_body = else_body && else_body.flatten
|
@else_body = else_body && else_body.flatten
|
||||||
|
@condition = OpNode.new("!", @condition) if tag == :invert
|
||||||
end
|
end
|
||||||
|
|
||||||
def statement?
|
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
|
end
|
||||||
|
|
||||||
def line_ending
|
def line_ending
|
||||||
|
|
Loading…
Add table
Reference in a new issue