start to add while loop
This commit is contained in:
parent
e600911fe8
commit
47f1608685
@ -4,7 +4,10 @@ module Ast
|
||||
def initialize cond, if_true, if_false
|
||||
@cond, @if_true, @if_false = cond, if_true, if_false
|
||||
end
|
||||
|
||||
def inspect
|
||||
self.class.name + ".new(" + cond.inspect + ", "+
|
||||
if_true.inspect + "," + if_false.inspect + " )"
|
||||
end
|
||||
def attributes
|
||||
[:cond, :if_true, :if_false]
|
||||
end
|
||||
|
@ -37,5 +37,6 @@ end
|
||||
|
||||
require_relative "basic_expressions"
|
||||
require_relative "conditional_expression"
|
||||
require_relative "while_expression"
|
||||
require_relative "function_expression"
|
||||
require_relative "operator_expressions"
|
||||
require_relative "operator_expressions"
|
||||
|
16
lib/ast/while_expression.rb
Normal file
16
lib/ast/while_expression.rb
Normal file
@ -0,0 +1,16 @@
|
||||
module Ast
|
||||
class WhileExpression < Expression
|
||||
attr_reader :condition, :body
|
||||
def initialize condition, body
|
||||
@condition , @body = condition , body
|
||||
end
|
||||
def inspect
|
||||
self.class.name + ".new(" + condition.inspect + ", " + body.inspect + " )"
|
||||
end
|
||||
def attributes
|
||||
[:condition, :body]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
@ -6,5 +6,10 @@ module Parser
|
||||
delimited_expressions(keyword_else).as(:if_true) >>
|
||||
delimited_expressions(keyword_end).as(:if_false)
|
||||
}
|
||||
|
||||
rule(:while) {
|
||||
keyword_while >> expression.as(:while_cond) >> keyword_do >> newline >>
|
||||
delimited_expressions(keyword_end).as(:body)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -8,5 +8,8 @@ module Parser
|
||||
rule(:keyword_true) { str('true').as(:true) >> space?}
|
||||
rule(:keyword_false){ str('false').as(:false) >> space?}
|
||||
rule(:keyword_nil) { str('null').as(:nil) >> space?}
|
||||
rule(:keyword_while) { str('while').as(:while) >> space?}
|
||||
rule(:keyword_do) { str('do').as(:do) >> space?}
|
||||
rule(:keyword_begin) { str('begin').as(:begin) >> space?}
|
||||
end
|
||||
end
|
@ -24,6 +24,11 @@ module Parser
|
||||
Ast::ConditionalExpression.new(conditional, if_true, if_false)
|
||||
end
|
||||
|
||||
rule(:while => simple(:while), :while_cond => simple(:while_cond) , :do => simple(:do),
|
||||
:body => {:expressions => sequence(:body)}) do
|
||||
Ast::WhileExpression.new(while_cond, body)
|
||||
end
|
||||
|
||||
rule(:parmeter => simple(:parmeter)) { parmeter }
|
||||
rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list }
|
||||
|
||||
|
@ -20,5 +20,23 @@ HERE
|
||||
|
||||
@parser = @parser.conditional
|
||||
end
|
||||
|
||||
|
||||
def test_while
|
||||
@string_input = <<HERE
|
||||
while 1 do
|
||||
tmp = a
|
||||
a = b
|
||||
end
|
||||
HERE
|
||||
#go in there
|
||||
# b = tmp + b
|
||||
# puts(b)
|
||||
# n = n - 1
|
||||
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:asignee=>{:name=>"tmp"}, :asigned=>{:name=>"a"}}, {:asignee=>{:name=>"a"}, :asigned=>{:name=>"b"}}]}}
|
||||
@transform_output = Ast::WhileExpression.new(
|
||||
Ast::IntegerExpression.new(1),
|
||||
[Ast::AssignmentExpression.new("tmp", Ast::NameExpression.new("a")), Ast::AssignmentExpression.new("a", Ast::NameExpression.new("b"))] )
|
||||
@parser = @parser.while
|
||||
end
|
||||
end
|
@ -34,5 +34,26 @@ HERE
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
|
||||
def test_function_while
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
b = 1
|
||||
while n > 1 do
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
puts b
|
||||
n = n - 1
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = { :function_definition => { :name => "foo" } ,
|
||||
:parmeter_list => [{ :parmeter => { :name => "x" } }],
|
||||
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
||||
}
|
||||
@transform_output = Ast::FunctionExpression.new( "foo", [Ast::NameExpression.new("x")],
|
||||
[Ast::AssignmentExpression.new( "abba", Ast::IntegerExpression.new(5) ) ])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
end
|
13
test/runners/fibo_while.rb
Normal file
13
test/runners/fibo_while.rb
Normal file
@ -0,0 +1,13 @@
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
b = 1
|
||||
while n > 1 do
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
puts b
|
||||
n = n - 1
|
||||
end
|
||||
end
|
||||
|
||||
fibonaccit( 10 )
|
Loading…
Reference in New Issue
Block a user