# example-vm / tests / test_parser.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73``` ```from minijs.parser import (Block, Stmt, If, While, Assignment, BinOp, Variable, ConstantFloat) class TestParser(object): def test_const_float(self, space): assert space.parse("1;") == Block([Stmt(ConstantFloat(1))]) def test_binary_expressions(self, space): assert space.parse("1 + 1;") == Block([Stmt(BinOp("+", ConstantFloat(1), ConstantFloat(1)))]) assert space.parse("2 - 3;") == Block([Stmt(BinOp("-", ConstantFloat(2), ConstantFloat(3)))]) def test_multi_term_expr(self, space): assert space.parse("1 - 2 * 3;") == Block([Stmt(BinOp("-", ConstantFloat(1), BinOp("*", ConstantFloat(2), ConstantFloat(3))))]) def test_parens(self, space): assert space.parse("(1 - 2) * 3;") == Block([Stmt(BinOp("*", BinOp("-", ConstantFloat(1), ConstantFloat(2)), ConstantFloat(3)))]) def test_comparisons(self, space): assert space.parse("1 > 2;") == Block([Stmt(BinOp(">", ConstantFloat(1), ConstantFloat(2)))]) def test_multiple_statements(self, space): r = space.parse(""" 1 + 1; 2 + 2; 3 + 3; """) assert r == Block([ Stmt(BinOp("+", ConstantFloat(1), ConstantFloat(1))), Stmt(BinOp("+", ConstantFloat(2), ConstantFloat(2))), Stmt(BinOp("+", ConstantFloat(3), ConstantFloat(3))), ]) def test_variables(self, space): r = space.parse(""" a = 3; a + 2; """) assert r == Block([ Stmt(Assignment("a", ConstantFloat(3))), Stmt(BinOp("+", Variable("a"), ConstantFloat(2))), ]) def test_if(self, space): r = space.parse(""" if (2) { a = 4; } """) assert r == Block([ If(ConstantFloat(2), Block([ Stmt(Assignment("a", ConstantFloat(4))), ]) ), ]) def test_while(self, space): r = space.parse(""" i = 0; while (i < 10) { i = i + 1; } """) assert r == Block([ Stmt(Assignment("i", ConstantFloat(0))), While( BinOp("<", Variable("i"), ConstantFloat(10)), Block([ Stmt(Assignment("i", BinOp("+", Variable("i"), ConstantFloat(1)))) ]) ), ]) ```