Beispiel #1
0
def var_name(name: str) -> str:
    """Transform a variable name into a valid one for Rust"""
    candidate = snake_case(name)
    if candidate in EXTENDED_KEYWORDS:
        return candidate + "_"
    if candidate.startswith("read_struct_") or candidate.endswith("_elem"):
        return candidate + "_"
    return candidate
Beispiel #2
0
def var_name(name: str) -> str:
    """Transform a variable name into a valid one for Python"""
    candidate = snake_case(name)
    return (
        candidate + "_"
        if iskeyword(candidate) or candidate == "dataclass"
        else candidate
    )
Beispiel #3
0
def method_name(name: str) -> str:
    """Transform a method name into a valid procedure name for Prolog"""
    candidate = snake_case(name)
    if candidate in KEYWORDS or candidate in USED_PROCEDURES:
        return candidate + "_"
    if candidate.startswith("read_assoc_"):
        return candidate + "_"
    return candidate
Beispiel #4
0
def var_name(var: Variable) -> str:
    """Transform a variable name into a valid one for Perl"""
    name = snake_case(var.name)
    if name in ("a", "b"):  # $a and $b are used by Perl
        name += "_"
    if var.type.main == TypeEnum.LIST:
        return "@" + name
    if var.type.main == TypeEnum.STRUCT:
        return "%" + name
    return "$" + name
Beispiel #5
0
def var_name(name: str) -> str:
    """Transform a variable name into a valid one for PHP"""
    if not name:
        return ""
    snake = snake_case(name)
    try:
        int(snake)
        return snake
    except ValueError:
        return "$" + snake
Beispiel #6
0
 def declare_read_struct(self) -> str:
     """Declare a procedure reading a struct into a assoc"""
     output = ""
     for struct in self.input.structs:
         name = "read_assoc_{}(X) :-".format(snake_case(struct.name))
         keys = ", ".join('"{}"'.format(i.name) for i in struct.fields)
         if Type(TypeEnum.STRUCT, struct_name=struct.name).fits_in_one_line(
                 self.input.structs):
             if all(i.type.main == TypeEnum.INT for i in struct.fields):
                 self.read.add("List[int]")
                 output += ("{} read_int_list(L), pairs_keys_values(P, "
                            "[{}], L), list_to_assoc(P, X).\n").format(
                                name, keys)
             elif all(i.type.main == TypeEnum.CHAR for i in struct.fields):
                 self.read.add("List[char]")
                 output += ("{} read_line(S), atomic_list_concat(L, ' ',"
                            "S), pairs_keys_values(P, [{}], L), "
                            "list_to_assoc(P, X).\n").format(name, keys)
             else:
                 output += "{}\n".format(name)
                 output += INDENTATION + (
                     "read_line_to_codes(user_input, C), "
                     'split_string(C, " ", "", L), empty_assoc(A0),\n')
                 for i, field in enumerate(struct.fields):
                     output += INDENTATION + (
                         "nth0({1}, L, L{1}), {3}, "
                         'put_assoc("{0}", A{1}, C{1}, A{2}),\n').format(
                             field.name,
                             i,
                             i + 1,
                             ("number_string(C{0}, L{0})"
                              if field.type.main == TypeEnum.INT else
                              "sub_atom(L{0}, 0, 1, _, C{0})").format(i),
                         )
                 output += INDENTATION + "X = A{}.\n".format(
                     len(struct.fields))
         else:
             output += "{}\n".format(name)
             names = [var_name(f.name) for f in struct.fields]
             var_x = "X"
             var_p = "P"
             while var_x in names:
                 var_x += "X"
             while var_p in names:
                 var_p += "P"
             for field in struct.fields:
                 output += (INDENTATION + call_goal(
                     self.read_lines(field.type), var_name(field.name)) +
                            "\n")
             output += INDENTATION + (
                 "pairs_keys_values({0}, [{1}], [{2}]), "
                 "list_to_assoc({0}, {3}).\n").format(
                     var_p, keys, ", ".join(names), var_x)
     return output
Beispiel #7
0
def size_name(name: str) -> str:
    """Transform a variable name or an integer into a valid Perl identifier"""
    if not name:
        return ""
    snake = snake_case(name)
    try:
        int(snake)
        return snake
    except ValueError:
        if snake in ("a", "b"):  # $a and $b are used by Perl
            snake += "_"
        return "$" + snake
Beispiel #8
0
 def read_lines(self,
                type_: Type,
                style: FormatStyle = FormatStyle.DEFAULT) -> str:
     """Read one or several lines and parse them"""
     if type_.fits_in_one_line(self.input.structs, style):
         return self.read_line(type_)
     if type_.main == TypeEnum.STRUCT:
         return "read_assoc_{}".format(snake_case(type_.struct_name))
     assert type_.main == TypeEnum.LIST
     assert type_.encapsulated is not None
     self.read.add("List")
     replicate = self.read_lines(type_.encapsulated)
     return "read_list({}, {})".format(replicate, var_name(type_.size))
Beispiel #9
0
 def read_line(self, type_: Type) -> str:
     """Read an entire line and parse it"""
     assert type_.fits_in_one_line(self.input.structs)
     if type_.main == TypeEnum.INT:
         self.read.add("int")
         return "read_int"
     if type_.main == TypeEnum.CHAR:
         self.read.add("char")
         return "read_char"
     if type_.main == TypeEnum.STR:
         self.read.add("str")
         return "read_line"
     if type_.main == TypeEnum.LIST:
         assert type_.encapsulated is not None
         if type_.encapsulated.main == TypeEnum.INT:
             self.read.add("List[int]")
             return "read_int_list"
         assert type_.encapsulated.main == TypeEnum.CHAR
         self.read.add("List[char]")
         return "read_char_list"
     assert type_.main == TypeEnum.STRUCT
     return "read_assoc_{}".format(snake_case(type_.struct_name))
Beispiel #10
0
def sub_name(name: str) -> str:
    """Transform a subroutine name into a valid one for Perl"""
    candidate = snake_case(name)
    return candidate + "_" if candidate in KEYWORDS else candidate
Beispiel #11
0
def var_name(name: str) -> str:
    """Transform a variable name into a valid one for Julia"""
    candidate = snake_case(name)
    return candidate + "_" if candidate in RESERVED else candidate
Beispiel #12
0
def struct_name(name: str) -> str:
    """Transform a struct name into a valid one for C"""
    candidate = snake_case(name)  # We make want an other style
    if candidate in KEYWORDS:
        return candidate + "_"
    return candidate
Beispiel #13
0
def var_name(name: str) -> str:
    """Transform a variable name into a valid one for C"""
    candidate = snake_case(name)
    if candidate in KEYWORDS or candidate == "main":
        return candidate + "_"
    return candidate
Beispiel #14
0
def var_name(name: str) -> str:
    """Transform a variable name into a valid one for Lua"""
    candidate = snake_case(name)
    return candidate + "_" if candidate in KEYWORDS else candidate
Beispiel #15
0
def record_name(name: str) -> str:
    """Transform a record name into a valid one for OCaml"""
    candidate = snake_case(name)
    if candidate in ("int", "char", "string", "list"):
        return candidate + "_"
    return candidate + "_" if candidate in KEYWORDS else candidate
Beispiel #16
0
def function_name(name: str) -> str:
    """Transform a function name into a valid one for PHP"""
    candidate = snake_case(name)
    return candidate + "_" if candidate in KEYWORDS else candidate