spec/front_end/parser_spec.rb in loxxy-0.0.21 vs spec/front_end/parser_spec.rb in loxxy-0.0.22

- old
+ new

@@ -62,28 +62,30 @@ context 'Parsing literals:' do it 'should parse a false literal' do input = 'false;' ptree = subject.parse(input) - leaf = ptree.root + expect(ptree.root).to be_kind_of(Ast::LoxSeqDecl) + leaf = ptree.root.subnodes[0] 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 + expect(ptree.root).to be_kind_of(Ast::LoxSeqDecl) + leaf = ptree.root.subnodes[0] 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 + leaf = ptree.root.subnodes[0] 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 +96,21 @@ '"";', '"123";' ] inputs.each do |source| ptree = subject.parse(source) - leaf = ptree.root + leaf = ptree.root.subnodes[0] 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 + leaf = ptree.root.subnodes[0] expect(leaf).to be_kind_of(Ast::LoxLiteralExpr) expect(leaf.literal).to be_equal(Datatype::Nil.instance) end end @@ -117,11 +119,11 @@ program = <<-LOX_END // Your first Lox program! print "Hello, world!"; LOX_END ptree = subject.parse(program) - prnt_stmt = ptree.root + prnt_stmt = ptree.root.subnodes[0] expect(prnt_stmt).to be_kind_of(Ast::LoxPrintStmt) expect(prnt_stmt.subnodes[0]).to be_kind_of(Ast::LoxLiteralExpr) expect(prnt_stmt.subnodes[0].literal).to be_kind_of(Loxxy::Datatype::LXString) expect(prnt_stmt.subnodes[0].literal.value).to eq('Hello, world!') end @@ -129,31 +131,31 @@ context 'Parsing arithmetic operations' do it 'should parse the addition of two number literals' do input = '123 + 456;' ptree = subject.parse(input) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) @@ -162,31 +164,31 @@ end it 'should parse the division of two number literals' do input = '8 / 2;' ptree = subject.parse(input) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) @@ -195,11 +197,11 @@ end it 'should parse combination of terms and factors' do input = '5 + 2 / 3;' ptree = subject.parse(input) - expr = ptree.root + expr = ptree.root.subnodes[0] 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(:/) @@ -210,11 +212,11 @@ context 'Parsing string concatenation' do it 'should parse the concatenation of two string literals' do input = '"Lo" + "ve";' ptree = subject.parse(input) - expr = ptree.root + expr = ptree.root.subnodes[0] 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 @@ -223,11 +225,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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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 @@ -237,22 +239,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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) - expr = ptree.root + expr = ptree.root.subnodes[0] 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) @@ -264,11 +266,11 @@ context 'Parsing logical expressions' do it 'should parse the logical operations between two sub-expression' do %w[or and].each do |connector| input = "5 > 2 #{connector} 3 <= 4;" ptree = subject.parse(input) - expr = ptree.root + expr = ptree.root.subnodes[0] expect(expr).to be_kind_of(Ast::LoxLogicalExpr) 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) @@ -281,10 +283,10 @@ end it 'should parse a combinations of logical expressions' do input = '4 > 3 and 1 < 2 or 4 >= 5;' ptree = subject.parse(input) - expr = ptree.root + expr = ptree.root.subnodes[0] expect(expr).to be_kind_of(Ast::LoxLogicalExpr) expect(expr.operator).to eq(:or) # or has lower precedence than and expect(expr.operands[0]).to be_kind_of(Ast::LoxLogicalExpr) expect(expr.operands[0].operator).to eq(:and) conjuncts = expr.operands[0].operands