def add_text_atomic(self, expression): """ :param expression: :return: """ temp = expression.replace("(", " ") temp = temp.replace(")", " ") temp = cleaning.strip_white_space(temp) temp = temp.replace("`", "") args = temp.split(",") return_type = "" func_name = "" # Find the return type for arg in args: if arg in self.sorts.keys(): return_type = arg args.remove(arg) break # Find the function name for arg in args: if arg not in self.sorts.keys(): func_name = arg args.remove(arg) break return self.add_code_atomic(func_name, return_type)
def tokenize_random_dcec(expression, namespace=None): """ This function creates a token representation of a random DCEC statement. It returns the token as well as sorts of new atomics and functions. """ # Default DCEC Functions if namespace is None: namespace = prototypes.Namespace() namespace.add_basic_dcec() else: namespace = namespace # Remove Comments temp = remove_comments(expression) # Check for an empty string if temp == "()": return "", {}, {}, {} # Check for a parentheses mismatch error if not cleaning.check_parens(expression): print("ERROR: parentheses mismatch error.") return False, False, False, False # Make symbols into functions temp = functorize_symbols(temp) # Strip comments temp = cleaning.strip_comments(temp) # Strip whitespace so you can do the rest of the parsing temp = cleaning.strip_white_space(temp) # Tuck the functions inside thier parentheses temp = cleaning.tuck_functions(temp) # Strip whitespace again temp = cleaning.strip_white_space(temp) # Consolidate Parentheses temp = cleaning.consolidate_parens(temp) quantifiers = [] # These are the tokens that should be added to the namespace add_atomics = {} add_functions = {} add_quants = {} return_token = token_tree(temp, namespace, quantifiers, add_quants, add_atomics, add_functions) # check for errors that occur in the lower level if isinstance(return_token, bool) and return_token is False: return False, False, False, False # Add quantifiers to the TokenTree return_token = tokenize_quantifiers(return_token, quantifiers) return return_token, add_quants, add_atomics, add_functions
def add_text_sort(self, expression): """ :param expression: :return: """ temp = expression.replace("(", " ") temp = temp.replace(")", " ") temp = cleaning.strip_white_space(temp) temp = temp.replace("`", "") args = temp.split(",") if len(args) == 2: self.add_code_sort(args[1]) elif len(args) > 2: self.add_code_sort(args[1], args[2:]) else: print("ERROR: Cannot define the sort") return False
def add_text_function(self, expression): """ :param expression: :return: """ temp = expression.replace("(", " ") temp = temp.replace(")", " ") temp = cleaning.strip_white_space(temp) temp = temp.replace("`", "") args = temp.split(",") if args[0].lower() == "typedef": return self.add_text_sort(expression) elif len(args) == 2: return self.add_text_atomic(expression) return_type = "" func_name = "" func_args = [] # Find the return type if args[0] in self.sorts.keys(): return_type = args[0] args.remove(args[0]) # Find the function name for arg in args: if arg not in self.sorts.keys(): func_name = arg args.remove(arg) break # Find the function args for arg in args: if arg in self.sorts.keys(): func_args.append(arg) # Error Checking if return_type == "" or func_name == "" or func_args == []: print("ERROR: The function prototype was not formatted correctly.") return False # Add the function return self.add_code_function(func_name, return_type, func_args)
if __name__ == "__main__": import doctest doctest.testmod() # pylint: disable=invalid-name inputin = input("Enter an expression: ") # Make symbols into functions inputin = remove_comments(inputin) print(inputin) inputin = functorize_symbols(inputin) print(inputin) # Strip comments inputin = cleaning.strip_comments(inputin) print(inputin) # Strip whitespace so you can do the rest of the parsing inputin = cleaning.strip_white_space(inputin) print(inputin) # Tuck the functions inside thier parentheses inputin = cleaning.tuck_functions(inputin) print(inputin) # Consolidate Parentheses inputin = cleaning.consolidate_parens(inputin) print(inputin) test_namespace = prototypes.Namespace() test_namespace.add_basic_dcec() test_namespace.add_basic_numerics() test_namespace.add_basic_logic() test_namespace.add_text_function("ActionType heal Agent") # testNAMESPACE.addTextFunction("Boolean B Agent Moment Boolean Certainty") add_quant = {} add_atomic = {}