spec/front_end/parser_spec.rb in loxxy-0.0.12 vs spec/front_end/parser_spec.rb in loxxy-0.0.13
- old
+ new
@@ -62,28 +62,28 @@
context 'Parsing literals:' do
it 'should parse a false literal' do
input = 'false;'
ptree = subject.parse(input)
- leaf = ptree.root.subnodes[0]
+ leaf = ptree.root
expect(leaf).to be_kind_of(Ast::LoxLiteralExpr)
expect(leaf.literal).to be_equal(Datatype::False.instance)
end
it 'should parse a true literal' do
input = 'true;'
ptree = subject.parse(input)
- leaf = ptree.root.subnodes[0]
+ leaf = ptree.root
expect(leaf).to be_kind_of(Ast::LoxLiteralExpr)
expect(leaf.literal).to be_equal(Datatype::True.instance)
end
it 'should parse number literals' do
inputs = %w[1234; 12.34;]
inputs.each do |source|
ptree = subject.parse(source)
- leaf = ptree.root.subnodes[0]
+ leaf = ptree.root
expect(leaf).to be_kind_of(Ast::LoxLiteralExpr)
expect(leaf.literal).to be_kind_of(Datatype::Number)
expect(leaf.literal.value).to eq(source.to_f)
end
end
@@ -94,21 +94,21 @@
'"";',
'"123";'
]
inputs.each do |source|
ptree = subject.parse(source)
- leaf = ptree.root.subnodes[0]
+ leaf = ptree.root
expect(leaf).to be_kind_of(Ast::LoxLiteralExpr)
expect(leaf.literal).to be_kind_of(Datatype::LXString)
expect(leaf.literal.value).to eq(source.gsub(/(^")|(";$)/, ''))
end
end
it 'should parse a nil literal' do
input = 'nil;'
ptree = subject.parse(input)
- leaf = ptree.root.subnodes[0]
+ leaf = ptree.root
expect(leaf).to be_kind_of(Ast::LoxLiteralExpr)
expect(leaf.literal).to be_equal(Datatype::Nil.instance)
end
end
@@ -129,40 +129,31 @@
context 'Parsing arithmetic operations' do
it 'should parse the addition of two number literals' do
input = '123 + 456;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:+)
expect(expr.operands[0].literal.value).to eq(123)
expect(expr.operands[1].literal.value).to eq(456)
end
it 'should parse the subtraction of two number literals' do
input = '4 - 3;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:-)
expect(expr.operands[0].literal.value).to eq(4)
expect(expr.operands[1].literal.value).to eq(3)
end
it 'should parse multiple additive operations' do
input = '5 + 2 - 3;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:-)
expect(expr.operands[0]).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operands[0].operator).to eq(:+)
expect(expr.operands[0].operands[0].literal.value).to eq(5)
@@ -171,40 +162,31 @@
end
it 'should parse the division of two number literals' do
input = '8 / 2;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:/)
expect(expr.operands[0].literal.value).to eq(8)
expect(expr.operands[1].literal.value).to eq(2)
end
it 'should parse the product of two number literals' do
input = '12.34 * 0.3;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:*)
expect(expr.operands[0].literal.value).to eq(12.34)
expect(expr.operands[1].literal.value).to eq(0.3)
end
it 'should parse multiple multiplicative operations' do
input = '5 * 2 / 3;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:/)
expect(expr.operands[0]).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operands[0].operator).to eq(:*)
expect(expr.operands[0].operands[0].literal.value).to eq(5)
@@ -213,14 +195,11 @@
end
it 'should parse combination of terms and factors' do
input = '5 + 2 / 3;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:+)
expect(expr.operands[0].literal.value).to eq(5)
expect(expr.operands[1]).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operands[1].operator).to eq(:/)
@@ -231,14 +210,11 @@
context 'Parsing string concatenation' do
it 'should parse the concatenation of two string literals' do
input = '"Lo" + "ve";'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:+)
expect(expr.operands[0].literal.value).to eq('Lo')
expect(expr.operands[1].literal.value).to eq('ve')
end
@@ -247,14 +223,11 @@
context 'Parsing comparison expressions' do
it 'should parse the comparison of two number literals' do
%w[> >= < <=].each do |predicate|
input = "3 #{predicate} 2;"
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(predicate.to_sym)
expect(expr.operands[0].literal.value).to eq(3)
expect(expr.operands[1].literal.value).to eq(2)
end
@@ -264,28 +237,22 @@
context 'Parsing equality expressions' do
it 'should parse the equality of two number literals' do
%w[!= ==].each do |predicate|
input = "3 #{predicate} 2;"
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(predicate.to_sym)
expect(expr.operands[0].literal.value).to eq(3)
expect(expr.operands[1].literal.value).to eq(2)
end
end
it 'should parse combination of equality expressions' do
input = '5 != 2 == false; // A bit contrived example'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:==)
expect(expr.operands[0]).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operands[0].operator).to eq(:!=)
expect(expr.operands[0].operands[0].literal.value).to eq(5)
@@ -297,14 +264,11 @@
context 'Parsing logical expressions' do
it 'should parse the logical operations betweentwo sub-expression' do
%w[or and].each do |connector|
input = "5 > 2 #{connector} 3 <= 4;"
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(connector.to_sym)
expect(expr.operands[0]).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operands[0].operator).to eq(:>)
expect(expr.operands[0].operands[0].literal.value).to eq(5)
@@ -317,13 +281,10 @@
end
it 'should parse a combinations of logical expressions' do
input = '4 > 3 and 1 < 2 or 4 >= 5;'
ptree = subject.parse(input)
- parent = ptree.root
- expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
- expect(parent.symbol.name).to eq('exprStmt')
- expr = parent.subnodes[0]
+ expr = ptree.root
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operator).to eq(:or) # or has lower precedence than and
expect(expr.operands[0]).to be_kind_of(Ast::LoxBinaryExpr)
expect(expr.operands[0].operator).to eq(:and)
conjuncts = expr.operands[0].operands