Skip to content

Commit

Permalink
got postfix if and unless onboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 15, 2009
1 parent 16f80ed commit 5dd295b
Show file tree
Hide file tree
Showing 5 changed files with 646 additions and 587 deletions.
12 changes: 8 additions & 4 deletions code.jaa
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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'.
6 changes: 5 additions & 1 deletion grammar.y
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,7 +18,9 @@ prechigh
left '<=' '<' '>' '>='
right '==' '!=' IS AINT
left '&&' '||' AND OR
left ':'
right '-=' '+=' '/=' '*='
nonassoc IF
preclow

rule
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lexer.rb
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 4 additions & 3 deletions nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,17 @@ def compile(indent, opts={})
# "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
Expand Down
Loading

0 comments on commit 5dd295b

Please sign in to comment.