add bools and nil

This commit is contained in:
Torsten Ruger 2014-06-30 17:51:07 +03:00
parent 040b842333
commit bc8697c733
10 changed files with 85 additions and 4 deletions

View File

@ -18,6 +18,22 @@ module Ast
end
end
class TrueExpression < Expression
def to_s
"true"
end
end
class FalseExpression < Expression
def to_s
"false"
end
end
class NilExpression < Expression
def to_s
"nil"
end
end
class NameExpression < Expression
attr_reader :name
def initialize name

View File

@ -10,7 +10,10 @@
module Ast
class Expression
def attributes
raise "abstract #{self}"
[]
end
def inspect
self.class.name + ".new()"
end
def == other
return false unless other.class == self.class

View File

@ -43,6 +43,7 @@ module Parser
rule(:float) { integer >> dot >> integer >>
(exponent >> sign.maybe >> digit.repeat(1,3)).maybe >> space?}
rule(:basic_type){ integer | name | string | float | instance_variable | module_name }
rule(:basic_type){ integer | name | string | float | instance_variable | module_name |
keyword_true | keyword_false | keyword_nil }
end
end

View File

@ -6,7 +6,10 @@ module Parser
rule(:string => sequence(:chars)) { Ast::StringExpression.new chars.join }
rule(:esc => simple(:esc)) { '\\' + esc }
rule(char: simple(:char)) { char }
rule(:true => simple(:true)) { Ast::TrueExpression.new() }
rule(:false => simple(:false)) { Ast::FalseExpression.new() }
rule(:nil => simple(:nil)) { Ast::NilExpression.new() }
rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
rule(:instance_variable => simple(:instance_variable)) { Ast::VariableExpression.new(instance_variable.name) }

View File

@ -28,6 +28,12 @@ class RootTestCompound < MiniTest::Test
@transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::IntegerExpression.new(33))])])
end
def test_hash2
@string_input = '{ foo => true }'
@parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:true=>"true"}}}]}]}
@transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::TrueExpression.new())])])
end
def test_hash_list
@string_input = "{foo => 33 , bar => 42}"
@parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}, {:hash_pair=>{:hash_key=>{:name=>"bar"}, :hash_value=>{:integer=>"42"}}}]}]}

View File

@ -47,6 +47,11 @@ class RootTestExpressions < MiniTest::Test
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}]}
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st"))])
end
def test_op_variable_true
@string_input = 'a == true'
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"== ", :r=>{:true=>"true"}}]}
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("==", Ast::NameExpression.new(:a),Ast::TrueExpression.new())])
end
def test_two_same_ops
@string_input = '2 + 3 + 4'
@parse_output = {:expression_list=>[{:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}]}

View File

@ -3,7 +3,26 @@ require_relative "../parser_helper"
class TestBasic < MiniTest::Test
# include the magic (setup and parse -> test method translation), see there
include ParserHelper
def test_true
@string_input = 'true '
@parse_output = {:true => 'true'}
@transform_output = Ast::TrueExpression.new()
@parser = @parser.basic_type
end
def test_false
@string_input = 'false '
@parse_output = {:false => 'false'}
@transform_output = Ast::FalseExpression.new()
@parser = @parser.basic_type
end
def test_nil
@string_input = 'nil '
@parse_output = {:nil => 'nil'}
@transform_output = Ast::NilExpression.new()
@parser = @parser.basic_type
end
def test_number
@string_input = '42 '
@parse_output = {:integer => '42'}

View File

@ -18,6 +18,13 @@ class TestCompound < MiniTest::Test
@parser = @parser.array_constant
end
def test_array_list2
@string_input = '[42, nil]'
@parse_output = {:array_constant=>[{:array_element=>{:integer=>"42"}}, {:array_element=>{:nil=>"nil"}}]}
@transform_output = Ast::ArrayExpression.new([Ast::IntegerExpression.new(42), Ast::NilExpression.new()])
@parser = @parser.array_constant
end
def test_array_ops
@string_input = '[ 3 + 4 , foo(22) ]'
@parse_output = {:array_constant=>[{:array_element=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}, {:array_element=>{:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"22"}}]}}]}

View File

@ -38,4 +38,18 @@ HERE
@transform_output = Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::IntegerExpression.new(3),Ast::NameExpression.new("var")), [Ast::CallSiteExpression.new(:initialize, [Ast::IntegerExpression.new(3)] ,Ast::ModuleName.new("Object"))],[Ast::CallSiteExpression.new(:new, [Ast::IntegerExpression.new(33)] ,Ast::NameExpression.new("var"))] )
@parser = @parser.conditional
end
def test_conditional_nil
@string_input = <<HERE
if(3 == nil)
3
else
4
end
HERE
@string_input.chop!
@parse_output = {:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"== ", :r=>{:nil=>"nil"}}, :if_true=>{:expressions=>[{:integer=>"3"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"4"}], :end=>"end"}}
@transform_output = Ast::IfExpression.new(Ast::OperatorExpression.new("==", Ast::IntegerExpression.new(3),Ast::NilExpression.new()), [Ast::IntegerExpression.new(3)],[Ast::IntegerExpression.new(4)] )
@parser = @parser.conditional
end
end

View File

@ -25,4 +25,11 @@ class TestReturn < MiniTest::Test
@parser = @parser.simple_return
end
def test_return_true
@string_input = 'return true'
@parse_output = {:return=>"return", :return_expression=>{:true=>"true"}}
@transform_output = Ast::ReturnExpression.new(Ast::TrueExpression.new() )
@parser = @parser.simple_return
end
end