diff --git a/bridgepoint/oal.py b/bridgepoint/oal.py index b6bfe18..7dce84e 100644 --- a/bridgepoint/oal.py +++ b/bridgepoint/oal.py @@ -868,7 +868,9 @@ class OALParser(object): 'PARAM', 'RCVD_EVT', 'SELF', - 'SELECTED' + 'SELECTED', + 'LOOP', + 'THEN' ) tokens = keywords + ( @@ -1436,26 +1438,47 @@ def p_delete_statement(self, p): p[0] = DeleteNode(variable_name=p[4]) @track_production - def p_for_statement(self, p): + def p_for_statement_1(self, p): + '''statement : FOR EACH variable_name IN variable_name LOOP block END_FOR''' + p[0] = ForEachNode(instance_variable_name=p[3], + set_variable_name=p[5], + block=p[7]) + + @track_production + def p_for_statement_2(self, p): '''statement : FOR EACH variable_name IN variable_name block END_FOR''' p[0] = ForEachNode(instance_variable_name=p[3], set_variable_name=p[5], block=p[6]) @track_production - def p_while_statement(self, p): + def p_while_statement_1(self, p): + '''statement : WHILE expression LOOP block END_WHILE''' + p[0] = WhileNode(expression=p[2], + block=p[4]) + + @track_production + def p_while_statement_2(self, p): '''statement : WHILE expression block END_WHILE''' p[0] = WhileNode(expression=p[2], block=p[3]) @track_production - def p_if_statement(self, p): + def p_if_statement_1(self, p): + '''statement : IF expression THEN block elif_list else_clause END_IF''' + p[0] = IfNode(expression=p[2], + block=p[4], + elif_list=p[5], + else_clause=p[6]) + + @track_production + def p_if_statement_2(self, p): '''statement : IF expression block elif_list else_clause END_IF''' p[0] = IfNode(expression=p[2], block=p[3], elif_list=p[4], else_clause=p[5]) - + @track_production def p_elif_list_1(self, p): '''elif_list : ''' @@ -1468,7 +1491,13 @@ def p_elif_list_2(self, p): p[0].children.insert(0, p[2]) @track_production - def p_elif_clause(self, p): + def p_elif_clause_1(self, p): + '''elif_clause : expression THEN block''' + p[0] = ElIfNode(expression=p[1], + block=p[3]) + + @track_production + def p_elif_clause_2(self, p): '''elif_clause : expression block''' p[0] = ElIfNode(expression=p[1], block=p[2]) diff --git a/tests/test_bridgepoint/test_foreach.py b/tests/test_bridgepoint/test_foreach.py index a4c5153..7269cf2 100644 --- a/tests/test_bridgepoint/test_foreach.py +++ b/tests/test_bridgepoint/test_foreach.py @@ -32,7 +32,7 @@ def setUp(self): relate(pe_pe, o_obj, 8001) @prebuild_docstring - def test_for_each_loop(self): + def test_for_each(self): ''' create object instance of A; create object instance of A; @@ -57,6 +57,33 @@ def test_for_each_loop(self): o_obj = one(act_for).O_OBJ[670]() self.assertEqual(o_obj.Key_Lett, 'A') + + @prebuild_docstring + def test_for_each_loop(self): + ''' + create object instance of A; + create object instance of A; + select many a_set from instances of A; + for each a in a_set loop + end for; + ''' + act_for = self.metamodel.select_one('ACT_FOR') + self.assertTrue(act_for.is_implicit) + + act_smt = one(act_for).ACT_SMT[603]() + self.assertIsNotNone(act_smt) + + act_blk = one(act_for).ACT_BLK[605]() + self.assertIsNotNone(act_blk) + + v_var = one(act_for).V_VAR[614]() + self.assertEqual(v_var.Name, 'a') + + v_var = one(act_for).V_VAR[652]() + self.assertEqual(v_var.Name, 'a_set') + + o_obj = one(act_for).O_OBJ[670]() + self.assertEqual(o_obj.Key_Lett, 'A') if __name__ == "__main__": diff --git a/tests/test_bridgepoint/test_ifs.py b/tests/test_bridgepoint/test_ifs.py index 797e912..3437e19 100644 --- a/tests/test_bridgepoint/test_ifs.py +++ b/tests/test_bridgepoint/test_ifs.py @@ -28,7 +28,7 @@ class TestIfStatements(PrebuildFunctionTestCase): @prebuild_docstring def test_single_if_true(self): ''' - if ( 0 == 0 ) + if ( 0 == 0 ) then return 1; end if; return 0; @@ -53,6 +53,45 @@ def test_single_if_true(self): @prebuild_docstring def test_elif(self): + ''' + assign x = 0; + if (1 == 0) then + assign x = 1; + elif (1 == 1) then + assign x = 2; + end if; + return x; + ''' + act_if = self.metamodel.select_one('ACT_IF') + self.assertIsNotNone(act_if) + + act_smt = one(act_if).ACT_SMT[603]() + self.assertIsNotNone(act_smt) + + v_val = one(act_if).V_VAL[625]() + self.assertIsNotNone(v_val) + + act_blk = one(act_if).ACT_BLK[607]() + self.assertIsNotNone(act_blk) + + act_el = many(act_if).ACT_EL[682]() + self.assertEqual(len(act_el), 1) + + act_el = one(act_if).ACT_EL[682]() + act_smt = one(act_el).ACT_SMT[603]() + self.assertIsNotNone(act_smt) + + v_val = one(act_el).V_VAL[659]() + self.assertIsNotNone(v_val) + + act_blk = one(act_el).ACT_BLK[658]() + self.assertIsNotNone(act_blk) + + act_e = one(act_if).ACT_E[683]() + self.assertIsNone(act_e) + + @prebuild_docstring + def test_elif_no_then(self): ''' assign x = 0; if (1 == 0) @@ -94,9 +133,9 @@ def test_elif(self): def test_elif_else(self): ''' assign x = 0; - if (1 == 0) + if (1 == 0) then assign x = 1; - elif (1 == 1) + elif (1 == 1) then assign x = 2; else assign x = 3; diff --git a/tests/test_bridgepoint/test_while.py b/tests/test_bridgepoint/test_while.py index 0398c67..1f2b523 100644 --- a/tests/test_bridgepoint/test_while.py +++ b/tests/test_bridgepoint/test_while.py @@ -25,7 +25,7 @@ class TestWhileLoop(PrebuildFunctionTestCase): @prebuild_docstring - def test_while_loop(self): + def test_while(self): ''' assign x = 10; while (x > 0) @@ -44,12 +44,32 @@ def test_while_loop(self): act_blk = one(act_whl).ACT_BLK[608]() self.assertIsNotNone(act_blk) + + @prebuild_docstring + def test_while_loop(self): + ''' + assign x = 10; + while (x > 0) loop + assign x = x - 1; + end while; + return x; + ''' + act_whl = self.metamodel.select_one('ACT_WHL') + self.assertIsNotNone(act_whl) + + act_smt = one(act_whl).ACT_SMT[603]() + self.assertIsNotNone(act_smt) + + v_val = one(act_whl).V_VAL[626]() + self.assertIsNotNone(v_val) + + act_blk = one(act_whl).ACT_BLK[608]() @prebuild_docstring def test_while_loop_with_break(self): ''' assign x = 10; - while (x > 0) + while (x > 0) loop if (x == 5) break; end if; @@ -68,7 +88,7 @@ def test_while_loop_with_break(self): def test_while_loop_with_continue(self): ''' assign x = 10; - while (x > 0) + while (x > 0) loop if (x == 5) continue; end if;