def parse_child_machines(self, mch_clause, mch_list, env): if mch_clause: for child in mch_clause.children: # avoid double parsing and two (or more) BMachine object for the same Machine if child.idName in env.parsed_bmachines: mch = env.parsed_bmachines[child.idName] mch_list.append(mch) continue # BMachine unknown, continue parsing if isinstance(child, AIdentifierExpression): assert isinstance(mch_clause, AUsesMachineClause) or isinstance(mch_clause, ASeesMachineClause) else: assert isinstance(child, AMachineReference) # TODO: impl search strategy file_path_and_name = env._bmachine_search_dir + child.idName + BFILE_EXTENSION #print file_path_and_name ast_string, error = file_to_AST_str_no_print(file_path_and_name) if error: print error from parsing import str_ast_to_python_ast root = str_ast_to_python_ast(ast_string) # Handle definitions in child-machine root = self.remove_definitions(root, env) # create B machine object mch = BMachine(root, self.remove_definitions) mch.recursive_self_parsing(env) mch_list.append(mch)
def parse_repl_input(input): try: string_to_file("#EXPRESSION "+input, "temp.b") ast_string,error = file_to_AST_str_no_print("temp.b") if ast_string=="\n": raise NameError() # FIXME: refactor this line root = str_ast_to_python_ast(ast_string) except NameError: #no expression string_to_file("#PREDICATE "+input, "temp.b") ast_string, error = file_to_AST_str_no_print("temp.b") if error: result = None return result, error root = str_ast_to_python_ast(ast_string) result = interpret(root, Environment() ) #printing via side effect error = None return result, error
def run_checking_mode(): env = Environment() # 1. create env. file_name_str, solution_file_name_str = read_input_string(argv, 1) # 2. read filenames ast_string, error = file_to_AST_str_no_print(file_name_str) # 3. parse input-file to string if error: print error #env.set_search_dir(file_name_str) root = str_ast_to_python_ast(ast_string) # 4. parse string to python ast TODO: JSON #if solution_file_name_str: # 5. parse solution-file and write to env. # read_solution_file(env, solution_file_name_str) # The concreate solution values are added at # the bmachine object-init time to the respective mch # 6. replace defs and extern-functions inside mch and solution-file (if present) parse_object = remove_defs_and_parse_ast(root, env) # 7. which kind of ast? if not isinstance(parse_object, BMachine): # #PREDICATE or #EXPRESSION result = interpret(parse_object.root, env) # eval predicate or expression print result else: assert isinstance(parse_object, BMachine) # 8. typecheck type_check_root_bmch(root, env, parse_object) # also checks all included, seen, used and extend mch = parse_object #solution_file_read = not solution_file_name_str=="" bstates = set_up_constants(root, env, mch, solution_file_read=False) # also evals properties if not bstates==[]: bool = True for bstate in bstates: env.state_space.add_state(bstate) #if mch.has_properties_mc: # assert interpret(mch.aPropertiesMachineClause, env) init_bstates = exec_initialisation(root, env, mch, not solution_file_name_str=="") for init_bstate in init_bstates: env.state_space.add_state(init_bstate) if mch.has_invariant_mc: w_bool = interpret(mch.aInvariantMachineClause, env) bool = bool and w_bool.bvalue env.state_space.undo() if mch.has_assertions_mc: interpret(mch.aAssertionsMachineClause, env) env.state_space.undo() return bool else: # TODO: dont repeat yourself init_bstates = exec_initialisation(root, env, mch, not solution_file_name_str=="") for bstate in init_bstates: env.state_space.add_state(bstate) if mch.has_invariant_mc: w_bool = interpret(mch.aInvariantMachineClause, env) assert w_bool.bvalue if mch.has_assertions_mc: interpret(mch.aAssertionsMachineClause, env) env.state_space.undo() if not init_bstates==[]: env.state_space.add_state(init_bstates[0]) return eval_Invariant(root, env, mch)
def _process_definition_files(self, clause): for definition in clause.children : if isinstance(definition, AFileDefinitionDefinition): #TODO: implement me if definition.idName in self.used_def_files: # avoid def-file loops continue self.used_def_files.append(definition.idName) # get def-file ast file_path_and_name = self.env._bmachine_search_dir + definition.idName ast_string, error = file_to_AST_str_no_print(file_path_and_name) root = self.str_ast_to_python_ast(ast_string) assert isinstance(root, ADefinitionFileParseUnit) assert isinstance(root.children[0], ADefinitionsMachineClause) # used definitions self._save_definitions(root.children[0])
def startup(argv, offset): env = Environment() # 1. create env. file_name_str, solution_file_name_str = read_input_string(argv, offset) # 2. read filenames ast_string, error = file_to_AST_str_no_print(file_name_str) # 3. parse input-file to string if error: print error #env.set_search_dir(file_name_str) #env.parse_config_parameter(argv) root = str_ast_to_python_ast(ast_string) # 4. parse string to python ast # uncomment for profiling (e.g. performance tests) #import cProfile #cProfile.run('mch = interpret(root, env)','pyB_profile_out.txt') solution_file_present = not solution_file_name_str=="" if solution_file_present: # 5. parse solution-file and write to env. read_solution_file(env, solution_file_name_str) # The concreate solution values are added at # the bmachine object-init time to the respective mch # 6. replace defs and extern-functions inside mch and solution-file (if present) parse_object = remove_defs_and_parse_ast(root, env) # 7. which kind of ast? return root, env, parse_object, solution_file_present
def run_animation_mode(argv): env = Environment() # 1. create env. file_name_str, solution_file_name_str = read_input_string(argv, 1) # 2. read filenames ast_string, error = file_to_AST_str_no_print(file_name_str) # 3. parse input-file to string if error: print error #env.set_search_dir(file_name_str) root = str_ast_to_python_ast(ast_string) # 4. parse string to python ast TODO: JSON # uncomment for profiling (e.g. performance tests) #import cProfile #cProfile.run('mch = interpret(root, env)','pyB_profile_out.txt') solution_file_present = not solution_file_name_str=="" #if solution_file_present: # 5. parse solution-file and write to env. # read_solution_file(env, solution_file_name_str) # The concreate solution values are added at # the bmachine object-init time to the respective mch # 6. replace defs and extern-functions inside mch and solution-file (if present) parse_object = remove_defs_and_parse_ast(root, env) # 7. which kind of ast? if not isinstance(parse_object, BMachine): is_ppu = isinstance(parse_object, PredicateParseUnit) is_epu = isinstance(parse_object, ExpressionParseUnit) assert is_ppu or is_epu result = interpret(parse_object.root, env) # eval predicate or expression print result # TODO: print_values_b_style needs symbolic set impl #print print_values_b_style(result) return 0 assert isinstance(parse_object, BMachine) # 8. typecheck mch = parse_object type_check_root_bmch(root, env, mch) # also checks all included, seen, used and extend # TODO: Check with B spec # 9. animate if ops are present # DO-WHILE Loop while True: next_states = __calc_states_and_print_ui(root, env, mch, solution_file_present) if next_states==[]: # BUG: no enabled ops doesnt mean there are none (deadlock-state) pass undo_possible = not env.state_space.empty() number_of_options = len(next_states) if undo_possible: number_of_options = number_of_options + 1 input_str = "Input (0-"+str(number_of_options)+"):" #number = raw_input(input_str) # XXXX number = number_of_options number = int(number) # quit if number == number_of_options: print "goodbye" break elif undo_possible and number == number_of_options-1: x = env.state_space.get_stack_size() x = x-1 # BUGFIX: empty state on stack if 2==x and env.init_state_on_stack and env.set_up_state_on_stack: env.init_state_on_stack==False env.init_done = False elif 1==x and env.init_state_on_stack and env.set_up_state_on_stack==False: env.init_state_on_stack==False env.init_done = False elif 1==x and env.set_up_state_on_stack: env.set_up_done = False env.set_up_state_on_stack = False env.set_up_bmachines_names = [] env.state_space.undo() elif not env.set_up_done: env.set_up_done = True env.set_up_state_on_stack = True bstate = next_states[number] env.state_space.add_state(bstate) elif not env.init_done: env.init_done = True env.init_state_on_stack = True bstate = next_states[number] env.state_space.add_state(bstate) # init and set_up done. Exec operation: elif len(next_states)>number and number >= 0: # switch state ( bstate = next_states[number] env.state_space.add_state(bstate) else: print "Error! Wrong input:", number return 0