Skip to content

Instantly share code, notes, and snippets.

@albertklik
Last active April 4, 2022 18:29
Show Gist options
  • Save albertklik/c7e781f2cafdcccb024e16a152446d54 to your computer and use it in GitHub Desktop.
Save albertklik/c7e781f2cafdcccb024e16a152446d54 to your computer and use it in GitHub Desktop.
Delphy-to-kotlin script
import re
reg_list = [
(r'for[ ]+([_A-Z-a-z][_A-Z-a-z0-9]*)[ ]+:=[ ]+(.*)[ ]+downto[ ]+(\d)[ ]+do[ ]*', r'for (\1 in \2 downto \3)'),
(r'for[ ]+([_A-Z-a-z][_A-Z-a-z0-9]*)[ ]+:=[ ]+(\d)[ ]+to[ ]+(.*)[ ]+do[ ]*', r'for (\1 in \2..\3)'),
(r'if[ ]+(.*)[ ]+then', r'if (\1)'),
(r'\bvar\b', r''),
(r'\bbegin\b', r'{'),
(r'\bend\b', r'}'),
(r' = ', r' == '),
(r'inc\((.*)\)', r'\1++'),
(r'dec\((.*)\)', r'\1--'),
(r'TList<(.*)>.Create', r'mutableListOf<\1>()'),
(r'\'(..+)\'', r'"\1"'),
(r'SqRt\(', r'sqrt('),
(r'\.Count := (.*);', r'.resize(\1, 0.0)'),
(r'\.Count ', r'.size'),
(r'Length\((.*?)\)', r'\1.size'),
(r'\.Add\(', r'.add('),
(r'\.Sort\(', r'.sort('),
(r'\.Clear\b', r'.clear()'),
(r'TList<(.*)>', r'List<\1>'),
(r'\bInteger\b', r'Int'),
(r'\bInt64\b', r'Long'),
(r'\bor\b', r'||'),
(r'\band\b', r'&&'),
(r'<>', r'!='),
(r'.*\.Free\n*', r''),
(r'\bfunction\b', r'fun'),
(r'\bprocedure\b', r'fun'),
(r'\b\.ToString\b', r'.toString()'),
(r':=', r'='),
(r'\[(.*?)\]', r'listOf(\1)'),
(r'\bFalse\b', r'false'),
(r'\bTrue\b', r'true'),
(r'\bnot\b', r'!'),
(r'\bnil\b', r'null'),
(r'([_A-Z-a-z][_A-Z-a-z0-9]*)[ ]*:[ ]*(.*);', r'var \1: \2'),
(r';', r''),
(r'\bT(.*?)\b', r'\1')
]
text = """
m_ArTimeLine.Count := nPosTime + 1;
"""
for reg_text, result in reg_list:
reg = re.compile(reg_text, re.MULTILINE | re.IGNORECASE)
text = re.sub(reg, result, text)
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment