adding condition to if statement

new syntax is if_true( . .. . )
where any string is allowed in case of true
This commit is contained in:
Torsten Ruger 2015-10-19 15:10:38 +03:00
parent 72ed05254e
commit 7cc7ab5c18
10 changed files with 18 additions and 13 deletions

View File

@ -8,7 +8,6 @@ module Parser
rule(:keyword_end) { str('end').as(:end) >> space? } rule(:keyword_end) { str('end').as(:end) >> space? }
rule(:keyword_false) { str('false').as(:false) } rule(:keyword_false) { str('false').as(:false) }
rule(:keyword_field) { str('field').as(:field) >> space? } rule(:keyword_field) { str('field').as(:field) >> space? }
rule(:keyword_if) { str('if').as(:if_statement) }
rule(:keyword_return) { str('return').as(:return) >> space?} rule(:keyword_return) { str('return').as(:return) >> space?}
rule(:keyword_true) { str('true').as(:true) } rule(:keyword_true) { str('true').as(:true) }
rule(:keyword_nil) { str('nil').as(:nil) } rule(:keyword_nil) { str('nil').as(:nil) }
@ -16,7 +15,7 @@ module Parser
# this rule is just to make sure identifiers can't be keywords. Kind of duplication here, but we need the # this rule is just to make sure identifiers can't be keywords. Kind of duplication here, but we need the
# space in above rules, so just make sure to add any here too. # space in above rules, so just make sure to add any here too.
rule(:keyword){ str('if') | str('else') | str('end') | str('while') | rule(:keyword){ str('if_') | str('else') | str('end') | str('while') |
str('false') | str('true')| str('nil') | str("class") | str('false') | str('true')| str('nil') | str("class") |
str('return')| str('int')| str('field')} str('return')| str('int')| str('field')}
end end

View File

@ -7,6 +7,9 @@ module Parser
right_parenthesis >> statements_end.as(:body) right_parenthesis >> statements_end.as(:body)
end end
rule( :keyword_if) do
str("if_") >> alpha.repeat.as(:condition) >> space
end
rule(:if_statement) do rule(:if_statement) do
keyword_if >> keyword_if >>
left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >> left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >>

View File

@ -69,16 +69,19 @@ module Parser
s(:while_statement , s(:condition , condition), s(:statements , *body)) s(:while_statement , s(:condition , condition), s(:statements , *body))
end end
rule(:if_statement => simple(:if_statement), :conditional => simple(:conditional), rule(:condition => simple(:condition), :conditional => simple(:conditional),
:true_statements => {:statements => sequence(:true_statements) , :else => simple(:else) }, :true_statements => {:statements => sequence(:true_statements) , :else => simple(:else) },
:false_statements => {:statements => sequence(:false_statements) , :end => simple(:e) }) do :false_statements => {:statements => sequence(:false_statements) , :end => simple(:e) }) do
s(:if_statement , s(:condition, conditional), s(:true_statements, *true_statements), s(:if_statement, condition.to_s.to_sym,
s(:condition, conditional),
s(:true_statements, *true_statements),
s(:false_statements , *false_statements)) s(:false_statements , *false_statements))
end end
rule(:if_statement => simple(:if_statement), :conditional => simple(:conditional), rule(:condition => simple(:condition), :conditional => simple(:conditional),
:true_statements => {:statements => sequence(:true_statements) , :end => simple(:e) }) do :true_statements => {:statements => sequence(:true_statements) , :end => simple(:e) }) do
s(:if_statement , s(:condition, conditional), s(:true_statements, *true_statements), s(:false_statements , nil) ) s(:if_statement, condition.to_s.to_sym, s(:condition, conditional),
s(:true_statements, *true_statements), s(:false_statements , nil) )
end end
rule(:return => simple(:return) , :return_statement => simple(:return_statement))do rule(:return => simple(:return) , :return_statement => simple(:return_statement))do

View File

@ -1,6 +1,6 @@
class Ifi class Ifi
int ofthen(int n) int ofthen(int n)
if(0) if_zero(0)
isit = 42 isit = 42
else else
maybenot = 667 maybenot = 667

View File

@ -1,5 +1,5 @@
int ofthen(int n) int ofthen(int n)
if(0) if_plus(0)
isit = 42 isit = 42
else else
maybenot = 667 maybenot = 667

View File

@ -1,5 +1,5 @@
int retvar(int n) int retvar(int n)
if( n > 5) if_positive( n - 5)
return 10 return 10
else else
return 20 return 20

View File

@ -1,4 +1,4 @@
if(0) if_true(0)
fourty = 10 fourty = 10
else else
twenty = 5 twenty = 5

View File

@ -1,4 +1,4 @@
if(3 > var) if_yes(3 > var)
Object.initialize(3) Object.initialize(3)
else else
var.new(33) var.new(33)

View File

@ -1,4 +1,4 @@
if(0) if_zero(0)
four = 42 four = 42
end end
-- -- -- -- -- --

View File

@ -1,4 +1,4 @@
if(3 > var) if_overflow(3 + 100000 )
Object.initialize(3) Object.initialize(3)
end end
-- -- -- -- -- --