Компилируем более сложный IF
Oct. 4th, 2021 02:25 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Теперь интерпретатор может переварить более сложный тест:
Получаем на выходе:
И результат работы:
Заодно перелопатил на использование объектов вместо туплей чтобы избавиться от конструкций типа e[1][0][1] в пользу st.then_block
MODULE Samples; const pass = 12345; fail = 54321; var t1 : integer; procedure test1; begin if t1 = 10 then t1 := 9 elsif t1 < 0 then halt(fail) else halt(fail) end end test1; procedure test2; begin if t1 = 10 then halt(fail) elsif t1 < 0 then halt(fail) else t1 := -1 end end test2; procedure test3; begin if t1 = 10 then halt(fail) elsif t1 < 0 then t1 := pass else halt(fail) end end test3; begin (* initializing test *) t1 := 10; (* do modifications of global var t1 *) test1; test2; test3; (* t1 must be pass *) halt(t1) END Samples.
Получаем на выходе:
{'decls': {'consts': {'fail': 54321, 'pass': 12345}, 'procs': {'test1': {'args': [], 'decls': {'consts': {}, 'procs': {}, 'vars': {}}, 'text': [('LOAD', 't1'), ('CONST', 10), ('RELOP', '='), ('BR_ZERO', 'L0'), ('CONST', 9), ('STOR', 't1'), ('BR', 'L2'), ('LABEL', 'L0'), ('LOAD', 't1'), ('CONST', 0), ('RELOP', '<'), ('BR_ZERO', 'L1'), ('LOAD', 'fail'), ('CALL', 'halt'), ('BR', 'L2'), ('LABEL', 'L1'), ('LOAD', 'fail'), ('CALL', 'halt'), ('LABEL', 'L2')]}, 'test2': {'args': [], 'decls': {'consts': {}, 'procs': {}, 'vars': {}}, 'text': [('LOAD', 't1'), ('CONST', 10), ('RELOP', '='), ('BR_ZERO', 'L3'), ('LOAD', 'fail'), ('CALL', 'halt'), ('BR', 'L5'), ('LABEL', 'L3'), ('LOAD', 't1'), ('CONST', 0), ('RELOP', '<'), ('BR_ZERO', 'L4'), ('LOAD', 'fail'), ('CALL', 'halt'), ('BR', 'L5'), ('LABEL', 'L4'), ('CONST', 1), ('UNARY', '-'), ('STOR', 't1'), ('LABEL', 'L5')]}, 'test3': {'args': [], 'decls': {'consts': {}, 'procs': {}, 'vars': {}}, 'text': [('LOAD', 't1'), ('CONST', 10), ('RELOP', '='), ('BR_ZERO', 'L6'), ('LOAD', 'fail'), ('CALL', 'halt'), ('BR', 'L8'), ('LABEL', 'L6'), ('LOAD', 't1'), ('CONST', 0), ('RELOP', '<'), ('BR_ZERO', 'L7'), ('LOAD', 'pass'), ('STOR', 't1'), ('BR', 'L8'), ('LABEL', 'L7'), ('LOAD', 'fail'), ('CALL', 'halt'), ('LABEL', 'L8')]}}, 'vars': {'t1': (0, 'integer')}}, 'name': 'Samples', 'text': [('CONST', 10), ('STOR', 't1'), ('CALL', 'test1'), ('CALL', 'test2'), ('CALL', 'test3'), ('LOAD', 't1'), ('CALL', 'halt'), ('STOP', '12345')]}
И результат работы:
Program halted with code: 12345 SUCCESS
Заодно перелопатил на использование объектов вместо туплей чтобы избавиться от конструкций типа e[1][0][1] в пользу st.then_block