Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce TO_<NUM> functions #1338

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions libs/stdlib/iec61131-st/to_num.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
f = open("to_num.st", "w")

types = [
"SINT",
"USINT",
"INT",
"UINT",
"DINT",
"UDINT",
"LINT",
"ULINT",

"REAL",
"LREAL",

# "BIT",
# "BOOL",
# "BYTE",
# "WORD",
# "DWORD",
# "LWORD",

# "STRING",
# "WSTRING",

# "TIME",
# "LTIME",
# "DATE",
# "LDATE",
# "DT",
# "LDT",
# "TOD",
# "LTOD",
]

generic = """(********************
*
* Converts any other numerical value to {0}
*
*********************)
FUNCTION TO_{0}<T: ANY_NUM> : {0}
VAR_INPUT
in : T;
END_VAR
END_FUNCTION
"""

generic_impl = """(********************
*
* Converts {0} to {1}
*
*********************)
FUNCTION TO_{1}__{0} : {1}
VAR_INPUT
in : {0};
END_VAR

TO_{1}__{0} := {0}_TO_{1}(in);
END_FUNCTION
"""

generic_impl_same = """(********************
*
* Converts {0} to {1}
*
*********************)
FUNCTION TO_{1}__{0} : {1}
VAR_INPUT
in : {0};
END_VAR

TO_{1}__{0} := in;
END_FUNCTION
"""

for type_from in types:
f.write(generic.format(type_from))
f.write("\n")

for type_to in types:
if type_from == type_to:
f.write(generic_impl_same.format(type_from, type_to))
else:
f.write(generic_impl.format(type_from, type_to))

f.write("\n")

Loading
Loading