add bools and nil
This commit is contained in:
parent
040b842333
commit
bc8697c733
@ -18,6 +18,22 @@ module Ast
|
|||||||
end
|
end
|
||||||
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
|
class NameExpression < Expression
|
||||||
attr_reader :name
|
attr_reader :name
|
||||||
def initialize name
|
def initialize name
|
||||||
|
@ -10,7 +10,10 @@
|
|||||||
module Ast
|
module Ast
|
||||||
class Expression
|
class Expression
|
||||||
def attributes
|
def attributes
|
||||||
raise "abstract #{self}"
|
[]
|
||||||
|
end
|
||||||
|
def inspect
|
||||||
|
self.class.name + ".new()"
|
||||||
end
|
end
|
||||||
def == other
|
def == other
|
||||||
return false unless other.class == self.class
|
return false unless other.class == self.class
|
||||||
|
@ -43,6 +43,7 @@ module Parser
|
|||||||
|
|
||||||
rule(:float) { integer >> dot >> integer >>
|
rule(:float) { integer >> dot >> integer >>
|
||||||
(exponent >> sign.maybe >> digit.repeat(1,3)).maybe >> space?}
|
(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
|
||||||
end
|
end
|
@ -7,6 +7,9 @@ module Parser
|
|||||||
rule(:esc => simple(:esc)) { '\\' + esc }
|
rule(:esc => simple(:esc)) { '\\' + esc }
|
||||||
rule(char: simple(:char)) { char }
|
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(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
|
||||||
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
|
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
|
||||||
rule(:instance_variable => simple(:instance_variable)) { Ast::VariableExpression.new(instance_variable.name) }
|
rule(:instance_variable => simple(:instance_variable)) { Ast::VariableExpression.new(instance_variable.name) }
|
||||||
|
@ -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))])])
|
@transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::IntegerExpression.new(33))])])
|
||||||
end
|
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
|
def test_hash_list
|
||||||
@string_input = "{foo => 33 , bar => 42}"
|
@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"}}}]}]}
|
@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"}}}]}]}
|
||||||
|
@ -47,6 +47,11 @@ class RootTestExpressions < MiniTest::Test
|
|||||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}]}
|
@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"))])
|
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st"))])
|
||||||
end
|
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
|
def test_two_same_ops
|
||||||
@string_input = '2 + 3 + 4'
|
@string_input = '2 + 3 + 4'
|
||||||
@parse_output = {:expression_list=>[{:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}]}
|
@parse_output = {:expression_list=>[{:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}]}
|
||||||
|
@ -4,6 +4,25 @@ class TestBasic < MiniTest::Test
|
|||||||
# include the magic (setup and parse -> test method translation), see there
|
# include the magic (setup and parse -> test method translation), see there
|
||||||
include ParserHelper
|
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
|
def test_number
|
||||||
@string_input = '42 '
|
@string_input = '42 '
|
||||||
@parse_output = {:integer => '42'}
|
@parse_output = {:integer => '42'}
|
||||||
|
@ -18,6 +18,13 @@ class TestCompound < MiniTest::Test
|
|||||||
@parser = @parser.array_constant
|
@parser = @parser.array_constant
|
||||||
end
|
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
|
def test_array_ops
|
||||||
@string_input = '[ 3 + 4 , foo(22) ]'
|
@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"}}]}}]}
|
@parse_output = {:array_constant=>[{:array_element=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}, {:array_element=>{:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"22"}}]}}]}
|
||||||
|
@ -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"))] )
|
@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
|
@parser = @parser.conditional
|
||||||
end
|
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
|
end
|
@ -25,4 +25,11 @@ class TestReturn < MiniTest::Test
|
|||||||
@parser = @parser.simple_return
|
@parser = @parser.simple_return
|
||||||
end
|
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
|
end
|
Loading…
x
Reference in New Issue
Block a user