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_false) { str('false').as(:false) }
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_true) { str('true').as(:true) }
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
# 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('return')| str('int')| str('field')}
end

View File

@ -7,6 +7,9 @@ module Parser
right_parenthesis >> statements_end.as(:body)
end
rule( :keyword_if) do
str("if_") >> alpha.repeat.as(:condition) >> space
end
rule(:if_statement) do
keyword_if >>
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))
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) },
: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))
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
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
rule(:return => simple(:return) , :return_statement => simple(:return_statement))do

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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