fix while and if statements

This commit is contained in:
Torsten Ruger 2015-10-09 17:28:13 +03:00
parent 7f2f64c713
commit 48a6dfabff
8 changed files with 120 additions and 119 deletions

View File

@ -1,24 +1,25 @@
module Parser
module Control
include Parslet
rule(:conditional) do
rule(:while_statement) do
keyword_while >> left_parenthesis >> r_value.as(:condition) >>
right_parenthesis >> statements_end.as(:body)
end
rule(:if_statement) do
keyword_if >>
left_parenthesis >> (operator_expression|r_value).as(:conditional) >> right_parenthesis >>
expressions_else.as(:if_true) >> expressions_end.as(:if_false)
left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >>
statements_else.as(:true_statements) >> statements_end.as(:false_statements)
end
rule(:small_conditional) do
keyword_if >>
left_parenthesis >> (operator_expression|r_value).as(:conditional) >> right_parenthesis >>
expressions_end.as(:if_true)
left_parenthesis >> r_value.as(:conditional) >> right_parenthesis >>
statements_end.as(:true_statements)
end
rule(:while_do) do
keyword_while >> left_parenthesis >> (operator_expression|r_value).as(:while_cond) >>
right_parenthesis >> expressions_end.as(:body)
end
rule(:simple_return) do
keyword_return >> (operator_expression|r_value).as(:return_expression)
rule(:return_statement) do
keyword_return >> r_value.as(:return_statement)
end
end
end

View File

@ -1,14 +1,17 @@
if(0)
42
fourty = 10
else
667
twenty = 5
end
-- -- --
s(:expressions,
s(:if,
s(:condition,
s(:int, 0)),
s(:if_true,
s(:int, 42)),
s(:if_false,
s(:int, 667))))
s(:if_statement,
s(:condition,
s(:int, 0)),
s(:true_statements,
s(:assignment,
s(:name, :fourty),
s(:int, 10))),
s(:false_statements,
s(:assignment,
s(:name, :twenty),
s(:int, 5))))

View File

@ -4,22 +4,22 @@ else
var.new(33)
end
-- -- --
s(:expressions,
s(:if,
s(:condition,
s(:operator, ">",
s(:int, 3),
s(:name, :var))),
s(:if_true,
s(:call,
s(:name, :initialize),
s(:arguments,
s(:int, 3)),
s(:receiver,
s(:module, "Object")))),
s(:if_false, s(:call,
s(:name, :new),
s(:arguments,
s(:int, 33)),
s(:receiver,
s(:name, :var))))))
s(:if_statement,
s(:condition,
s(:operator_value, :>,
s(:int, 3),
s(:name, :var))),
s(:true_statements,
s(:call,
s(:name, :initialize),
s(:arguments,
s(:int, 3)),
s(:receiver,
s(:class_name, :Object)))),
s(:false_statements,
s(:call,
s(:name, :new),
s(:arguments,
s(:int, 33)),
s(:receiver,
s(:name, :var)))))

View File

@ -1,11 +1,12 @@
if(0)
42
four = 42
end
-- -- --
s(:expressions,
s(:if,
s(:condition,
s(:int, 0)),
s(:if_true,
s(:int, 42)),
s(:if_false, nil)))
s(:if_statement,
s(:condition,
s(:int, 0)),
s(:true_statements,
s(:assignment,
s(:name, :four),
s(:int, 42))),
s(:false_statements, nil))

View File

@ -2,17 +2,16 @@ if(3 > var)
Object.initialize(3)
end
-- -- --
s(:expressions,
s(:if,
s(:condition,
s(:operator, ">",
s(:int, 3),
s(:name, :var))),
s(:if_true,
s(:call,
s(:name, :initialize),
s(:arguments,
s(:int, 3)),
s(:receiver,
s(:module, "Object")))),
s(:if_false, nil)))
s(:if_statement,
s(:condition,
s(:operator_value, :>,
s(:int, 3),
s(:name, :var))),
s(:true_statements,
s(:call,
s(:name, :initialize),
s(:arguments,
s(:int, 3)),
s(:receiver,
s(:class_name, :Object)))),
s(:false_statements, nil))

View File

@ -6,30 +6,29 @@ while( n > 1)
n = n - 1
end
-- -- --
s(:expressions,
s(:while,
s(:condition,
s(:operator, ">",
s(:name, :n),
s(:int, 1))),
s(:expressions,
s(:assign,
s(:name, :tmp),
s(:name, :a)),
s(:assign,
s(:name, :a),
s(:name, :b)),
s(:assign,
s(:name, :b),
s(:operator, "+",
s(:name, :tmp),
s(:name, :b))),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :b))),
s(:assign,
s(:name, :n),
s(:operator, "-",
s(:name, :n),
s(:int, 1))))))
s(:while_statement,
s(:condition,
s(:operator_value, :>,
s(:name, :n),
s(:int, 1))),
s(:statements,
s(:assignment,
s(:name, :tmp),
s(:name, :a)),
s(:assignment,
s(:name, :a),
s(:name, :b)),
s(:assignment,
s(:name, :b),
s(:operator_value, :+,
s(:name, :tmp),
s(:name, :b))),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :b))),
s(:assignment,
s(:name, :n),
s(:operator_value, :-,
s(:name, :n),
s(:int, 1)))))

View File

@ -3,15 +3,14 @@ while(1)
puts(b)
end
-- -- --
s(:expressions,
s(:while,
s(:condition,
s(:int, 1)),
s(:expressions,
s(:assign,
s(:name, :tmp),
s(:name, :a)),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :b))))))
s(:while_statement,
s(:condition,
s(:int, 1)),
s(:statements,
s(:assignment,
s(:name, :tmp),
s(:name, :a)),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :b)))))

View File

@ -3,21 +3,20 @@ while(1)
tmp.puts(i)
end
-- -- --
s(:expressions,
s(:while,
s(:condition,
s(:int, 1)),
s(:expressions,
s(:assign,
s(:name, :tmp),
s(:call,
s(:name, :new),
s(:arguments),
s(:receiver,
s(:module, "String")))),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :i)),
s(:receiver,
s(:name, :tmp))))))
s(:while_statement,
s(:condition,
s(:int, 1)),
s(:statements,
s(:assignment,
s(:name, :tmp),
s(:call,
s(:name, :new),
s(:arguments),
s(:receiver,
s(:class_name, :String)))),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :i)),
s(:receiver,
s(:name, :tmp)))))