still trying that while loop. Check as everything else works

This commit is contained in:
Torsten Ruger 2014-05-12 12:14:04 +03:00
parent 4a27314533
commit 7940efc64d
7 changed files with 56 additions and 38 deletions

View File

@ -27,7 +27,7 @@ module Parser
rule(:exponent) { (str('e')| str('E')) } rule(:exponent) { (str('e')| str('E')) }
# identifier must start with lower case # identifier must start with lower case
rule(:name) { (match['a-z'] >> match['a-zA-Z0-9'].repeat).as(:name) >> space? } rule(:name) { keyword.absent? >> (match['a-z'] >> match['a-zA-Z0-9'].repeat).as(:name) >> space? }
rule(:escape) { str('\\') >> any.as(:esc) } rule(:escape) { str('\\') >> any.as(:esc) }
rule(:string) { quote >> ( rule(:string) { quote >> (

View File

@ -1,15 +1,13 @@
module Parser module Parser
module Conditional module Conditional
include Parslet include Parslet
rule(:conditional) { rule(:conditional) do
keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >> newline >> keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >> newline >>
delimited_expressions(keyword_else).as(:if_true) >> expressions_else.as(:if_true) >> expressions_end.as(:if_false)
delimited_expressions(keyword_end).as(:if_false) end
}
rule(:while) { rule(:while_do) do
keyword_while >> expression.as(:while_cond) >> keyword_do >> newline >> keyword_while >> expressions_do.as(:while_cond) >> expressions_end.as(:body)
delimited_expressions(keyword_end).as(:body) end
}
end end
end end

View File

@ -3,12 +3,13 @@ module Parser
include Parslet include Parslet
rule(:simple_expression) { function_call | integer | string | name } rule(:simple_expression) { function_call | integer | string | name }
rule(:expression) { (conditional | simple_expression ) >> newline.maybe } rule(:expression) { (while_do | conditional | simple_expression ) >> newline.maybe }
def delimited_expressions( delimit ) def delimited_expressions( delimit )
( (delimit.absent? >> (operator_expression | expression)).repeat(1)).as(:expressions) >> delimit >> newline.maybe ( (delimit.absent? >> (operator_expression | expression)).repeat(1)).as(:expressions) >> delimit >> newline.maybe
end end
rule(:expressions_do) { delimited_expressions(keyword_do) }
rule(:expressions_else) { delimited_expressions(keyword_else) } rule(:expressions_else) { delimited_expressions(keyword_else) }
rule(:expressions_end) { delimited_expressions(keyword_end) } rule(:expressions_end) { delimited_expressions(keyword_end) }

View File

@ -1,6 +1,7 @@
module Parser module Parser
module Keywords module Keywords
include Parslet include Parslet
rule(:keyword_begin) { str('begin').as(:begin) >> space?} rule(:keyword_begin) { str('begin').as(:begin) >> space?}
rule(:keyword_def) { str('def') >> space? } rule(:keyword_def) { str('def') >> space? }
rule(:keyword_do) { str('do').as(:do) >> space?} rule(:keyword_do) { str('do').as(:do) >> space?}
@ -14,5 +15,11 @@ module Parser
rule(:keyword_unless) { str('unless').as(:unless) >> space?} rule(:keyword_unless) { str('unless').as(:unless) >> space?}
rule(:keyword_until) { str('until').as(:until) >> space?} rule(:keyword_until) { str('until').as(:until) >> space?}
rule(:keyword_while) { str('while').as(:while) >> space?} rule(:keyword_while) { str('while').as(:while) >> space?}
# 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('begin') | str('def') | str('do') | str('else') | str('end') |
str('false')| str('if')| str('rescue')| str('true')| str('nil') |
str('unless')| str('until')| str('while')}
end end
end end

View File

@ -24,7 +24,8 @@ module Parser
Ast::ConditionalExpression.new(conditional, if_true, if_false) Ast::ConditionalExpression.new(conditional, if_true, if_false)
end end
rule(:while => simple(:while), :while_cond => simple(:while_cond) , :do => simple(:do), rule(:while => simple(:while),
:while_cond => { :expressions => sequence(:while_cond) , :do => simple(:do)} ,
:body => {:expressions => sequence(:body) , :end => simple(:e) }) do :body => {:expressions => sequence(:body) , :end => simple(:e) }) do
Ast::WhileExpression.new(while_cond, body) Ast::WhileExpression.new(while_cond, body)
end end

View File

@ -25,23 +25,19 @@ HERE
@string_input = <<HERE @string_input = <<HERE
while 1 do while 1 do
tmp = a tmp = a
a = b puts(b)
end end
HERE HERE
#go in there
# b = tmp + b
# puts(b)
# n = n - 1
@parse_output = {:while=>"while", @parse_output = {:while=>"while",
:while_cond=>{:integer=>"1"}, :while_cond=>{:expressions=>[{:integer=>"1"}],
:do=>"do", :do=>"do"},
:body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}], :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}},
:end=>"end"}} {:function_call=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}
@transform_output = Ast::WhileExpression.new( @transform_output = Ast::WhileExpression.new(
Ast::IntegerExpression.new(1), [Ast::IntegerExpression.new(1)],
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")),
Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))] ) Ast::FuncallExpression.new("puts", [Ast::NameExpression.new("b")] )] )
@parser = @parser.while @parser = @parser.while_do
end end
end end

View File

@ -31,24 +31,39 @@ HERE
@parser = @parser.function_definition @parser = @parser.function_definition
end end
def ttest_function_while def test_function_if
@string_input = <<HERE @string_input = <<HERE
def fibonaccit(n) def ofthen(n)
a = 0 if(0)
b = 1 42
while n > 1 do else
tmp = a 667
a = b
b = tmp + b
puts b
n = n - 1
end end
end end
HERE HERE
@parse_output = { :function_definition => { :name => "foo" } , @parse_output = {:function_definition=>{:name=>"ofthen"},
:parmeter_list => [{ :parmeter => { :name => "x" } }], :parmeter_list=>[{:parmeter=>{:name=>"n"}}],
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ] :expressions=>[
} {:if=>"if", :conditional=>{:integer=>"0"},
:if_true=>{:expressions=>[{:integer=>"42"}],
:else=>"else"},
:if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}], :end=>"end"}
@transform_output = Ast::FunctionExpression.new(:ofthen,
[Ast::NameExpression.new("n")] ,
[Ast::ConditionalExpression.new(Ast::IntegerExpression.new(0),
[Ast::IntegerExpression.new(42)],[Ast::IntegerExpression.new(667)] )] )
@parser = @parser.function_definition
end
def test_function_while
@string_input = <<HERE
def fibonaccit(n)
a = 0
while n do
1
end
HERE
@parse_output = nil
@transform_output = nil @transform_output = nil
@parser = @parser.function_definition @parser = @parser.function_definition
end end