1
0
Fork 0
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:
Jeremy Ashkenas 2009-12-15 08:53:21 -05:00
parent 16f80ed963
commit 5dd295bd08
5 changed files with 643 additions and 584 deletions

View file

@ -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'.

View file

@ -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:

View file

@ -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",

View file

@ -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

1200
parser.rb

File diff suppressed because it is too large Load diff