From 42b85dbb4f5f77b1b58eb96f6ef1175a142f02da Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 1 May 2015 18:13:33 +0300 Subject: [PATCH] create some block tests and the expression --- lib/ast/block_expression.rb | 26 ++++++++++++++++++++ test/roots/test_block.rb | 33 +++++++++++++++++++++++++ test/unit/test_block.rb | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 lib/ast/block_expression.rb create mode 100644 test/roots/test_block.rb create mode 100644 test/unit/test_block.rb diff --git a/lib/ast/block_expression.rb b/lib/ast/block_expression.rb new file mode 100644 index 0000000..4491eb5 --- /dev/null +++ b/lib/ast/block_expression.rb @@ -0,0 +1,26 @@ +module Ast + class BlockExpression < Expression + attr_reader :call_exp, :args , :body_exp + + def initialize call_exp, args , body_exp + @call_exp = call_exp.to_sym + @args = args + @body_exp = body_exp + end + + def attributes + [:call_exp, :args , :body_exp] + end + + def inspect + self.class.call_exp + ".new(" + call_exp.inspect + ", ["+ + args.collect{|m| m.inspect }.join( ",") + "] ," + body_exp.inspect + ")" + end + def to_s + "#{call_exp}(" + args.join(",") + ")" + end + def attributes + [:call_exp , :args , :body_exp] + end + end +end diff --git a/test/roots/test_block.rb b/test/roots/test_block.rb new file mode 100644 index 0000000..ae739b1 --- /dev/null +++ b/test/roots/test_block.rb @@ -0,0 +1,33 @@ +require_relative "../parser_helper" + +class RootTestBlock < MiniTest::Test + # include the magic (setup and parse -> test method translation), see there + include ParserHelper + + def pest_block + @string_input = <[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}]} + @transform_output = Ast::ExpressionList.new( [Ast::BlockExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )]) + @parser = @parser + end +end diff --git a/test/unit/test_block.rb b/test/unit/test_block.rb new file mode 100644 index 0000000..ba69c7a --- /dev/null +++ b/test/unit/test_block.rb @@ -0,0 +1,48 @@ +require_relative "../parser_helper" + +class TestBlock < MiniTest::Test + # include the magic (setup and parse -> test method translation), see there + include ParserHelper + + def pest_block + @string_input = <"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}} + @transform_output = Ast::BlockExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self))] ) + @parser = @parser.block + end + + def pest_block_method + @string_input = <"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:receiver=>{:module_name=>"String"}, :call_site=>{:name=>"new"}, :argument_list=>[]}}, {:receiver=>{:name=>"tmp"}, :call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"i"}}]}], :end=>"end"}} + @transform_output = Ast::BlockExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::CallSiteExpression.new(:new, [] ,Ast::ModuleName.new(:String))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:i)] ,Ast::NameExpression.new(:tmp))] ) + @parser = @parser.block + end + + def pest_big_block + @string_input = < 1) do + tmp = a + a = b + b = tmp + b + puts(b) + n = n - 1 +end +HERE + @string_input.chop! + @parse_output = {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}} + @transform_output = Ast::BlockExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] ) + @parser = @parser.block + end +end